2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 RERoom = regexp.MustCompile("^#[^\x00\x07\x0a\x0d ,:/]{1,200}$")
34 // Sanitize room's name. It can consist of 1 to 50 ASCII symbols
35 // with some exclusions. All room names will have "#" prefix.
36 func RoomNameValid(name string) bool {
37 return RERoom.MatchString(name)
44 members map[*Client]struct{}
48 func (room *Room) String() (name string) {
55 func NewRoom(name string) *Room {
62 members: make(map[*Client]struct{}),
66 func (room *Room) SendTopic(client *Client) {
68 if *room.topic == "" {
69 client.ReplyNicknamed("331", room.String(), "No topic is set")
71 client.ReplyNicknamed("332", room.String(), *room.topic)
76 // Send message to all room's subscribers, possibly excluding someone.
77 func (room *Room) Broadcast(msg string, clientToIgnore ...*Client) {
79 for member := range room.members {
80 if (len(clientToIgnore) > 0) && member == clientToIgnore[0] {
88 func (room *Room) StateSave() {
90 stateSink <- StateEvent{room.String(), *room.topic, *room.key}
94 func (room *Room) Processor(events <-chan ClientEvent) {
96 for event := range events {
98 switch event.eventType {
104 room.members[client] = struct{}{}
106 log.Println(client, "joined", room.name)
109 room.SendTopic(client)
110 room.Broadcast(fmt.Sprintf(":%s JOIN %s", client, room.String()))
111 logSink <- LogEvent{room.String(), *client.nickname, "joined", true}
112 nicknames := make([]string, 0)
114 for member := range room.members {
115 nicknames = append(nicknames, *member.nickname)
118 sort.Strings(nicknames)
119 client.ReplyNicknamed("353", "=", room.String(), strings.Join(nicknames, " "))
120 client.ReplyNicknamed("366", room.String(), "End of NAMES list")
123 if _, subscribed := room.members[client]; !subscribed {
124 client.ReplyNicknamed("442", room.String(), "You are not on that channel")
130 delete(room.members, client)
133 msg := fmt.Sprintf(":%s PART %s :%s", client, room.String(), *client.nickname)
135 logSink <- LogEvent{room.String(), *client.nickname, "left", true}
139 if _, subscribed := room.members[client]; !subscribed {
140 client.ReplyParts("442", room.String(), "You are not on that channel")
144 if event.text == "" {
145 room.SendTopic(client)
150 topic := strings.TrimLeft(event.text, ":")
155 msg := fmt.Sprintf(":%s TOPIC %s :%s", client, room.String(), *room.topic)
160 "set topic to " + *room.topic,
167 for m := range room.members {
168 client.ReplyNicknamed(
179 client.ReplyNicknamed("315", room.String(), "End of /WHO list")
183 if event.text == "" {
188 client.Msg(fmt.Sprintf("324 %s %s %s", *client.nickname, room.String(), mode))
192 if strings.HasPrefix(event.text, "b") {
193 client.ReplyNicknamed("368", room.String(), "End of channel ban list")
197 if strings.HasPrefix(event.text, "-k") || strings.HasPrefix(event.text, "+k") {
198 if _, subscribed := room.members[client]; !subscribed {
199 client.ReplyParts("442", room.String(), "You are not on that channel")
204 client.ReplyNicknamed("472", event.text, "Unknown MODE flag")
211 if strings.HasPrefix(event.text, "+k") {
212 cols := strings.Split(event.text, " ")
214 client.ReplyNotEnoughParameters("MODE")
219 msg = fmt.Sprintf(":%s MODE %s +k %s", client, *room.name, *room.key)
220 msgLog = "set channel key to " + *room.key
226 msg = fmt.Sprintf(":%s MODE %s -k", client, *room.name)
228 msgLog = "removed channel key"
231 logSink <- LogEvent{room.String(), *client.nickname, msgLog, true}
234 sep := strings.Index(event.text, " ")
235 room.Broadcast(fmt.Sprintf(