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":
|
2020-05-24 09:09:59 +00:00
|
|
|
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 != "" {
|
2020-05-24 09:09:59 +00:00
|
|
|
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Version)
|
2020-05-20 02:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "PING":
|
2020-05-24 09:09:59 +00:00
|
|
|
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 != "" {
|
2020-05-24 09:09:59 +00:00
|
|
|
c.ReplyCTCP(msg.Sender, ctcp.Command, c.Config.Source)
|
2020-05-20 02:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "TIME":
|
2020-05-24 09:09:59 +00:00
|
|
|
c.ReplyCTCP(msg.Sender, ctcp.Command, time.Now().UTC().Format(time.RFC3339))
|
2020-05-20 02:19:40 +00:00
|
|
|
|
|
|
|
case "USERINFO":
|
2020-05-24 09:09:59 +00:00
|
|
|
c.ReplyCTCP(msg.Sender, ctcp.Command, fmt.Sprintf("%s (%s)", c.GetNick(), c.Config.Realname))
|
2020-05-20 02:19:40 +00:00
|
|
|
}
|
|
|
|
}
|