Merge pull request #1056 from jesopo/services-ctcp

handle CTCP VERSION, PING and TIME for services pseudo-users
This commit is contained in:
Shivaram Lingamneni 2020-05-31 21:26:32 -07:00 committed by GitHub
commit 8430fec333
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ import (
"log"
"sort"
"strings"
"time"
"github.com/goshuirc/irc-go/ircfmt"
"github.com/goshuirc/irc-go/ircmsg"
@ -130,6 +131,11 @@ func serviceCmdHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb
// generic handler for service PRIVMSG, like `/msg NickServ INFO`
func servicePrivmsgHandler(service *ircService, server *Server, client *Client, message string, rb *ResponseBuffer) {
if strings.HasPrefix(message, "\x01") {
serviceCTCPHandler(service, client, message)
return
}
params := strings.Fields(message)
if len(params) == 0 {
return
@ -147,6 +153,29 @@ func servicePrivmsgHandler(service *ircService, server *Server, client *Client,
serviceRunCommand(service, server, client, cmd, commandName, params, rb)
}
func serviceCTCPHandler(service *ircService, client *Client, message string) {
ctcp := strings.TrimSuffix(message[1:], "\x01")
ctcpSplit := utils.FieldsN(ctcp, 2)
ctcpCmd := strings.ToUpper(ctcpSplit[0])
ctcpOut := ""
switch ctcpCmd {
case "VERSION":
ctcpOut = fmt.Sprintf("%s (%s)", service.Name, Ver)
case "PING":
if len(ctcpSplit) > 1 {
ctcpOut = ctcpSplit[1]
}
case "TIME":
ctcpOut = time.Now().UTC().Format(time.RFC1123)
}
if ctcpOut != "" {
client.Send(nil, service.prefix, "NOTICE", client.Nick(), fmt.Sprintf("\x01%s %s\x01", ctcpCmd, ctcpOut))
}
}
// actually execute a service command
func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
nick := rb.target.Nick()