func (c *Client) commandSender(sender ircx.Sender, to string, enc bool) func(s string, a ...interface{}) { return func(s string, a ...interface{}) { if s == "" { return } msg := s if len(a) > 0 { msg = fmt.Sprintf(msg, a...) } if enc { if key, ok := c.state.Encryption().Check(to); ok { data, err := dh1080.Enc([]byte(msg), []byte(key)) if err == nil { msg = "+OK *" + base64.StdEncoding.EncodeToString(data) } } } newMsg := &irc.Message{ Command: irc.PRIVMSG, Params: []string{to}, Trailing: msg, } sender.Send(newMsg) } }
func (c *Client) encryptionStartHandler(s ircx.Sender, m *irc.Message) { data := strings.Split(m.Trailing, " ") if len(data) < 2 { return } if data[0] == "DH1080_INIT" { enc := dh1080.New() err := enc.Unpack(m.Trailing) data, err := enc.Pack() if err != nil { return } msg := &irc.Message{ Command: irc.NOTICE, Params: []string{m.Name}, Trailing: data + " CBC", } s.Send(msg) secret, err := enc.GetSecret() if err != nil { msg := &irc.Message{ Command: irc.PRIVMSG, Params: []string{m.Name}, Trailing: "I couldn't create the key for our chat, I won't be understanding your encrypted messages", } s.Send(msg) } else { c.state.Encryption().New(m.Name, secret) time.Sleep(1 * time.Second) // wait for their client to get situated out := fmt.Sprintf("I've stared an encrypted chat, just message %s to remove our group key", encOff) encryptedMessage, err := dh1080.Enc([]byte(out), []byte(secret)) if err != nil { return } encryptedBaseMessage := base64.StdEncoding.EncodeToString(encryptedMessage) infoMessage := &irc.Message{ Command: irc.PRIVMSG, Params: []string{m.Name}, Trailing: "+OK *" + encryptedBaseMessage, } s.Send(infoMessage) } } }