Browse Source

Merge pull request #1056 from jesopo/services-ctcp

handle CTCP VERSION, PING and TIME for services pseudo-users
tags/v2.2.0-rc1
Shivaram Lingamneni 4 years ago
parent
commit
8430fec333
No account linked to committer's email address
1 changed files with 29 additions and 0 deletions
  1. 29
    0
      irc/services.go

+ 29
- 0
irc/services.go View File

@@ -9,6 +9,7 @@ import (
9 9
 	"log"
10 10
 	"sort"
11 11
 	"strings"
12
+	"time"
12 13
 
13 14
 	"github.com/goshuirc/irc-go/ircfmt"
14 15
 	"github.com/goshuirc/irc-go/ircmsg"
@@ -130,6 +131,11 @@ func serviceCmdHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb
130 131
 
131 132
 // generic handler for service PRIVMSG, like `/msg NickServ INFO`
132 133
 func servicePrivmsgHandler(service *ircService, server *Server, client *Client, message string, rb *ResponseBuffer) {
134
+	if strings.HasPrefix(message, "\x01") {
135
+		serviceCTCPHandler(service, client, message)
136
+		return
137
+	}
138
+
133 139
 	params := strings.Fields(message)
134 140
 	if len(params) == 0 {
135 141
 		return
@@ -147,6 +153,29 @@ func servicePrivmsgHandler(service *ircService, server *Server, client *Client,
147 153
 	serviceRunCommand(service, server, client, cmd, commandName, params, rb)
148 154
 }
149 155
 
156
+func serviceCTCPHandler(service *ircService, client *Client, message string) {
157
+	ctcp := strings.TrimSuffix(message[1:], "\x01")
158
+
159
+	ctcpSplit := utils.FieldsN(ctcp, 2)
160
+	ctcpCmd := strings.ToUpper(ctcpSplit[0])
161
+	ctcpOut := ""
162
+
163
+	switch ctcpCmd {
164
+	case "VERSION":
165
+		ctcpOut = fmt.Sprintf("%s (%s)", service.Name, Ver)
166
+	case "PING":
167
+		if len(ctcpSplit) > 1 {
168
+			ctcpOut = ctcpSplit[1]
169
+		}
170
+	case "TIME":
171
+		ctcpOut = time.Now().UTC().Format(time.RFC1123)
172
+	}
173
+
174
+	if ctcpOut != "" {
175
+		client.Send(nil, service.prefix, "NOTICE", client.Nick(), fmt.Sprintf("\x01%s %s\x01", ctcpCmd, ctcpOut))
176
+	}
177
+}
178
+
150 179
 // actually execute a service command
151 180
 func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
152 181
 	nick := rb.target.Nick()

Loading…
Cancel
Save