diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-08 08:45:13 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-08 01:20:26 +0800 |
commit | d810d12355873d3eaf61ed3f7f0ae52db06c2c96 (patch) | |
tree | 5cfa6d11c31377b459b06f4e3399bcc0542c57d8 /msg.go | |
parent | Slight refactor (diff) | |
download | meseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.tar.gz meseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.tar.zst meseircd-d810d12355873d3eaf61ed3f7f0ae52db06c2c96.zip |
Add basic command handling
Diffstat (limited to 'msg.go')
-rw-r--r-- | msg.go | 45 |
1 files changed, 39 insertions, 6 deletions
@@ -4,19 +4,52 @@ import ( "strings" ) -type Msg struct { - RawMessage string +type RMsg struct { RawSource string Command string Tags map[string]string Params []string } -// Partially adapted from https://github.com/ergochat/irc-go.git -func parseIRCMsg(line string) (msg Msg, err error) { - msg = Msg{ - RawMessage: line, +type Sourceable interface { + ClientSource() string + ServerSource() string +} + +type SMsg struct { + Source *Sourceable + Command string + Tags map[string]string + Params []string +} + +func (msg *SMsg) ClientSerialize() (final string) { + if msg.Tags != nil && len(msg.Tags) != 0 { + final = "@" + for k, v := range msg.Tags{ + // TODO: Tag values must be escaped + final += k + "=" + v + ";" + } + final += " " } + if msg.Source != nil { + final += ":" + (*msg.Source).ClientSource() + " " + } + final += msg.Command + " " + + if len(msg.Params) > 0 { + for i := 0; i < len(msg.Params) - 1; i++ { + final += msg.Params[i] + " " + } + final += ":" + msg.Params[len(msg.Params) - 1] + } + final += "\n" + return +} + +// Partially adapted from https://github.com/ergochat/irc-go.git +func parseIRCMsg(line string) (msg RMsg, err error) { + msg = RMsg{} line = strings.TrimSuffix(line, "\n") line = strings.TrimSuffix(line, "\r") |