dispatch/pkg/irc/ctcp.go

70 lines
1.4 KiB
Go
Raw Normal View History

2020-05-20 02:19:40 +00:00
package irc
import (
"fmt"
"strings"
"time"
)
// ClientInfo is the CTCP messages this client implements
const ClientInfo = "ACTION CLIENTINFO DCC FINGER PING SOURCE TIME VERSION USERINFO"
type CTCP struct {
Command string
Params string
}
func DecodeCTCP(str string) *CTCP {
if len(str) > 1 && str[0] == 0x01 {
parts := strings.SplitN(strings.Trim(str, "\x01"), " ", 2)
ctcp := CTCP{}
if parts[0] != "" {
ctcp.Command = parts[0]
} else {
return nil
}
if len(parts) == 2 {
ctcp.Params = parts[1]
}
return &ctcp
}
return nil
}
func EncodeCTCP(ctcp *CTCP) string {
if ctcp == nil || ctcp.Command == "" {
return ""
}
return fmt.Sprintf("\x01%s %s\x01", ctcp.Command, ctcp.Params)
}
func (c *Client) handleCTCP(ctcp *CTCP, msg *Message) {
switch ctcp.Command {
case "CLIENTINFO":
c.ReplyCTCP(msg.Sender, ctcp.Command, ClientInfo)
2020-05-20 02:19:40 +00:00
case "FINGER", "VERSION":
2020-05-23 07:42:20 +00:00
if c.Config.Version != "" {
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Version)
2020-05-20 02:19:40 +00:00
}
case "PING":
c.ReplyCTCP(msg.Sender, ctcp.Command, ctcp.Params)
2020-05-20 02:19:40 +00:00
case "SOURCE":
2020-05-23 07:42:20 +00:00
if c.Config.Source != "" {
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Source)
2020-05-20 02:19:40 +00:00
}
case "TIME":
c.ReplyCTCP(msg.Sender, ctcp.Command, time.Now().UTC().Format(time.RFC3339))
2020-05-20 02:19:40 +00:00
case "USERINFO":
c.ReplyCTCP(msg.Sender, ctcp.Command, fmt.Sprintf("%s (%s)", c.GetNick(), c.Config.Realname))
2020-05-20 02:19:40 +00:00
}
}