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
 	"log"
9
 	"log"
10
 	"sort"
10
 	"sort"
11
 	"strings"
11
 	"strings"
12
+	"time"
12
 
13
 
13
 	"github.com/goshuirc/irc-go/ircfmt"
14
 	"github.com/goshuirc/irc-go/ircfmt"
14
 	"github.com/goshuirc/irc-go/ircmsg"
15
 	"github.com/goshuirc/irc-go/ircmsg"
130
 
131
 
131
 // generic handler for service PRIVMSG, like `/msg NickServ INFO`
132
 // generic handler for service PRIVMSG, like `/msg NickServ INFO`
132
 func servicePrivmsgHandler(service *ircService, server *Server, client *Client, message string, rb *ResponseBuffer) {
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
 	params := strings.Fields(message)
139
 	params := strings.Fields(message)
134
 	if len(params) == 0 {
140
 	if len(params) == 0 {
135
 		return
141
 		return
147
 	serviceRunCommand(service, server, client, cmd, commandName, params, rb)
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
 // actually execute a service command
179
 // actually execute a service command
151
 func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
180
 func serviceRunCommand(service *ircService, server *Server, client *Client, cmd *serviceCommand, commandName string, params []string, rb *ResponseBuffer) {
152
 	nick := rb.target.Nick()
181
 	nick := rb.target.Nick()

Loading…
Cancel
Save