2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 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/>.
30 RERoom = regexp.MustCompile("^#[^\x00\x07\x0a\x0d ,:/]{1,200}$")
32 rooms map[string]*Room = make(map[string]*Room)
34 roomSinks map[*Room]chan ClientEvent = make(map[*Room]chan ClientEvent)
37 // Sanitize room's name. It can consist of 1 to 50 ASCII symbols
38 // with some exclusions. All room names will have "#" prefix.
39 func RoomNameValid(name string) bool {
40 return RERoom.MatchString(name)
47 members map[*Client]struct{}
50 func (room Room) String() string {
54 func NewRoom(name string) *Room {
61 members: make(map[*Client]struct{}),
65 func (room *Room) SendTopic(client *Client) {
66 if *room.topic == "" {
67 client.ReplyNicknamed("331", *room.name, "No topic is set")
69 client.ReplyNicknamed("332", *room.name, *room.topic)
73 // Send message to all room's subscribers, possibly excluding someone.
74 func (room *Room) Broadcast(msg string, clientToIgnore ...*Client) {
75 for member := range room.members {
76 if (len(clientToIgnore) > 0) && member == clientToIgnore[0] {
83 func (room *Room) StateSave() {
84 stateSink <- StateEvent{*room.name, *room.topic, *room.key}
87 func (room *Room) Processor(events <-chan ClientEvent) {
89 for event := range events {
91 switch event.eventType {
96 room.members[client] = struct{}{}
98 log.Println(client, "joined", room.name)
100 room.SendTopic(client)
101 room.Broadcast(fmt.Sprintf(":%s JOIN %s", client, *room.name))
102 logSink <- LogEvent{*room.name, *client.nickname, "joined", true}
103 nicknames := make([]string, 0)
104 for member := range room.members {
105 nicknames = append(nicknames, *member.nickname)
107 sort.Strings(nicknames)
108 client.ReplyNicknamed("353", "=", *room.name, strings.Join(nicknames, " "))
109 client.ReplyNicknamed("366", *room.name, "End of NAMES list")
111 if _, subscribed := room.members[client]; !subscribed {
112 client.ReplyNicknamed("442", *room.name, "You are not on that channel")
115 delete(room.members, client)
116 msg := fmt.Sprintf(":%s PART %s :%s", client, *room.name, *client.nickname)
118 logSink <- LogEvent{*room.name, *client.nickname, "left", true}
120 if _, subscribed := room.members[client]; !subscribed {
121 client.ReplyParts("442", *room.name, "You are not on that channel")
124 if event.text == "" {
125 room.SendTopic(client)
128 topic := strings.TrimLeft(event.text, ":")
130 msg := fmt.Sprintf(":%s TOPIC %s :%s", client, *room.name, *room.topic)
135 "set topic to " + *room.topic,
140 for m := range room.members {
141 client.ReplyNicknamed(
152 client.ReplyNicknamed("315", *room.name, "End of /WHO list")
154 if event.text == "" {
159 client.Msg(fmt.Sprintf("324 %s %s %s", *client.nickname, *room.name, mode))
162 if strings.HasPrefix(event.text, "b") {
163 client.ReplyNicknamed("368", *room.name, "End of channel ban list")
166 if strings.HasPrefix(event.text, "-k") || strings.HasPrefix(event.text, "+k") {
167 if _, subscribed := room.members[client]; !subscribed {
168 client.ReplyParts("442", *room.name, "You are not on that channel")
172 client.ReplyNicknamed("472", event.text, "Unknown MODE flag")
177 if strings.HasPrefix(event.text, "+k") {
178 cols := strings.Split(event.text, " ")
180 client.ReplyNotEnoughParameters("MODE")
184 msg = fmt.Sprintf(":%s MODE %s +k %s", client, *room.name, *room.key)
185 msgLog = "set channel key to " + *room.key
189 msg = fmt.Sprintf(":%s MODE %s -k", client, *room.name)
190 msgLog = "removed channel key"
193 logSink <- LogEvent{*room.name, *client.nickname, msgLog, true}
196 sep := strings.Index(event.text, " ")
197 room.Broadcast(fmt.Sprintf(