-
Notifications
You must be signed in to change notification settings - Fork 0
/
chanserv.go
127 lines (105 loc) · 3.77 KB
/
chanserv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"fmt"
"strings"
"time"
"github.com/goshuirc/irc-go/ircfmt"
"github.com/goshuirc/irc-go/ircmsg"
"github.com/oragono/oragono/irc/sno"
"github.com/tidwall/buntdb"
)
// csHandler handles the /CS and /CHANSERV commands
func csHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
server.chanservReceivePrivmsg(client, strings.Join(msg.Params, " "))
return false
}
func (server *Server) chanservReceiveNotice(client *Client, message string) {
// do nothing
}
// ChanServNotice sends the client a notice from ChanServ.
func (client *Client) ChanServNotice(text string) {
client.Send(nil, fmt.Sprintf("ChanServ!services@%s", client.server.name), "NOTICE", client.nick, text)
}
func (server *Server) chanservReceivePrivmsg(client *Client, message string) {
var params []string
for _, p := range strings.Split(message, " ") {
if len(p) > 0 {
params = append(params, p)
}
}
if len(params) < 1 {
client.ChanServNotice("You need to run a command")
//TODO(dan): dump CS help here
return
}
command := strings.ToLower(params[0])
server.logger.Debug("chanserv", fmt.Sprintf("Client %s ran command %s", client.nick, command))
if command == "register" {
if len(params) < 2 {
client.ChanServNotice("Syntax: REGISTER <channel>")
return
}
if !server.channelRegistrationEnabled {
client.ChanServNotice("Channel registration is not enabled")
return
}
server.registeredChannelsMutex.Lock()
defer server.registeredChannelsMutex.Unlock()
channelName := params[1]
channelKey, err := CasefoldChannel(channelName)
if err != nil {
client.ChanServNotice("Channel name is not valid")
return
}
channelInfo := server.channels.Get(channelKey)
if channelInfo == nil {
client.ChanServNotice("You must be an oper on the channel to register it")
return
}
if !channelInfo.ClientIsAtLeast(client, ChannelOperator) {
client.ChanServNotice("You must be an oper on the channel to register it")
return
}
server.store.Update(func(tx *buntdb.Tx) error {
currentChan := server.loadChannelNoMutex(tx, channelKey)
if currentChan != nil {
client.ChanServNotice("Channel is already registered")
return nil
}
account := client.account
if account == &NoAccount {
client.ChanServNotice("You must be logged in to register a channel")
return nil
}
chanRegInfo := RegisteredChannel{
Name: channelName,
RegisteredAt: time.Now(),
Founder: account.Name,
Topic: channelInfo.topic,
TopicSetBy: channelInfo.topicSetBy,
TopicSetTime: channelInfo.topicSetTime,
}
server.saveChannelNoMutex(tx, channelKey, chanRegInfo)
client.ChanServNotice(fmt.Sprintf("Channel %s successfully registered", channelName))
server.logger.Info("chanserv", fmt.Sprintf("Client %s registered channel %s", client.nick, channelName))
server.snomasks.Send(sno.LocalChannels, fmt.Sprintf(ircfmt.Unescape("Channel registered $c[grey][$r%s$c[grey]] by $c[grey][$r%s$c[grey]]"), channelName, client.nickMaskString))
channelInfo.membersMutex.Lock()
defer channelInfo.membersMutex.Unlock()
// give them founder privs
change := channelInfo.applyModeMemberNoMutex(client, ChannelFounder, Add, client.nickCasefolded)
if change != nil {
//TODO(dan): we should change the name of String and make it return a slice here
//TODO(dan): unify this code with code in modes.go
args := append([]string{channelName}, strings.Split(change.String(), " ")...)
for member := range channelInfo.members {
member.Send(nil, fmt.Sprintf("ChanServ!services@%s", client.server.name), "MODE", args...)
}
}
return nil
})
} else {
client.ChanServNotice("Sorry, I don't know that command")
}
}