From d201e74fff4e6d82d639d858826bdb4190936c07 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 08:52:23 +0800 Subject: Server and self awareness --- msg.go | 53 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) (limited to 'msg.go') diff --git a/msg.go b/msg.go index 1743116..87ed1ab 100644 --- a/msg.go +++ b/msg.go @@ -5,10 +5,10 @@ import ( ) type RMsg struct { - RawSource string - Command string - Tags map[string]string - Params []string + RawSource string + Command string + Tags map[string]string + Params []string } type Sourceable interface { @@ -17,36 +17,65 @@ type Sourceable interface { } type SMsg struct { - Source *Sourceable - Command string - Tags map[string]string - Params []string + 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{ + 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.Source.ClientSource() + " " } final += msg.Command + " " if len(msg.Params) > 0 { - for i := 0; i < len(msg.Params) - 1; i++ { + for i := 0; i < len(msg.Params)-1; i++ { final += msg.Params[i] + " " } - final += ":" + msg.Params[len(msg.Params) - 1] + final += ":" + msg.Params[len(msg.Params)-1] } final += "\n" return } +func (msg *SMsg) ServerSerialize() (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.ServerSource() + " " + } + 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 +} + +func MakeMsg(source Sourceable, command string, params ...string) (msg SMsg) { + // TODO: Add tags + return SMsg{Source: source, Command: command, Params: params} +} + // Partially adapted from https://github.com/ergochat/irc-go.git func parseIRCMsg(line string) (msg RMsg, err error) { msg = RMsg{} -- cgit v1.2.3