Send message

This commit is contained in:
cyp0633 2022-11-22 10:42:14 +08:00
parent 02b222e085
commit 8912acef5a
Signed by: cyp0633
GPG Key ID: 8ACC79012E3DDAA0
3 changed files with 36 additions and 3 deletions

8
internal/message.go Normal file
View File

@ -0,0 +1,8 @@
package internal
type Message struct {
From string
To string
Time string
Content string
}

View File

@ -1,8 +1,12 @@
package internal
import "regexp"
import (
"regexp"
"strings"
)
var Helo = regexp.MustCompile(`^HELO .+`)
// HELO 信息格式HELO
var Helo = regexp.MustCompile(`^HELO$`)
// ProcessHelo 返回在线客户端列表
func ProcessHelo(msg string) (clients string) {
@ -12,3 +16,24 @@ func ProcessHelo(msg string) (clients string) {
}
return
}
// SEND 信息格式SEND 127.0.0.1 MSG This is Message
var Send = regexp.MustCompile(`/^SEND [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} MSG .+`)
// 提取消息使用
var sendMessage = regexp.MustCompile(`MSG .+`)
// ProcessSend 发送消息
func ProcessSend(msg string, from string) (reply string) {
temp := strings.Split(msg, "")
addr := temp[1]
content := sendMessage.FindString(msg)[4:]
message := Message{From: from, To: addr, Content: content}
for _, client := range Clients {
if client.Addr == addr {
client.Messages <- message
reply = "OK"
}
}
return
}

View File

@ -25,7 +25,7 @@ func TCPHandler(conn net.Conn) {
case Helo.MatchString(msg):
reply = ProcessHelo(msg)
case Send.MatchString(msg):
reply = ProcessSend(msg)
reply = ProcessSend(msg, client.Addr)
}
_, err = conn.Write([]byte(reply))
if err != nil {