]> Cypherpunks.ru repositories - goircd.git/blob - room.go
Fix several races
[goircd.git] / room.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18 package main
19
20 import (
21         "fmt"
22         "log"
23         "regexp"
24         "sort"
25         "strings"
26 )
27
28 var (
29         RE_ROOM = regexp.MustCompile("^#[^\x00\x07\x0a\x0d ,:/]{1,200}$")
30 )
31
32 // Sanitize room's name. It can consist of 1 to 50 ASCII symbols
33 // with some exclusions. All room names will have "#" prefix.
34 func RoomNameValid(name string) bool {
35         return RE_ROOM.MatchString(name)
36 }
37
38 type Room struct {
39         Verbose    bool
40         name       string
41         topic      string
42         key        string
43         members    map[*Client]bool
44         hostname   string
45         log_sink   chan<- LogEvent
46         state_sink chan<- StateEvent
47 }
48
49 func NewRoom(hostname, name string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Room {
50         room := Room{name: name}
51         room.members = make(map[*Client]bool)
52         room.topic = ""
53         room.key = ""
54         room.hostname = hostname
55         room.log_sink = log_sink
56         room.state_sink = state_sink
57         return &room
58 }
59
60 func (room *Room) SendTopic(client *Client) {
61         if room.topic == "" {
62                 client.ReplyNicknamed("331", room.name, "No topic is set")
63         } else {
64                 client.ReplyNicknamed("332", room.name, room.topic)
65         }
66 }
67
68 // Send message to all room's subscribers, possibly excluding someone
69 func (room *Room) Broadcast(msg string, client_to_ignore ...*Client) {
70         for member := range room.members {
71                 if (len(client_to_ignore) > 0) && member == client_to_ignore[0] {
72                         continue
73                 }
74                 member.Msg(msg)
75         }
76 }
77
78 func (room *Room) StateSave() {
79         room.state_sink <- StateEvent{room.name, room.topic, room.key}
80 }
81
82 func (room *Room) Processor(events <-chan ClientEvent) {
83         var client *Client
84         for event := range events {
85                 client = event.client
86                 switch event.event_type {
87                 case EVENT_NEW:
88                         room.members[client] = true
89                         if room.Verbose {
90                                 log.Println(client, "joined", room.name)
91                         }
92                         room.SendTopic(client)
93                         room.Broadcast(fmt.Sprintf(":%s JOIN %s", client, room.name))
94                         room.log_sink <- LogEvent{room.name, client.nickname, "joined", true}
95                         nicknames := []string{}
96                         for member := range room.members {
97                                 nicknames = append(nicknames, member.nickname)
98                         }
99                         sort.Strings(nicknames)
100                         client.ReplyNicknamed("353", "=", room.name, strings.Join(nicknames, " "))
101                         client.ReplyNicknamed("366", room.name, "End of NAMES list")
102                 case EVENT_DEL:
103                         if _, subscribed := room.members[client]; !subscribed {
104                                 client.ReplyNicknamed("442", room.name, "You are not on that channel")
105                                 continue
106                         }
107                         delete(room.members, client)
108                         msg := fmt.Sprintf(":%s PART %s :%s", client, room.name, client.nickname)
109                         room.Broadcast(msg)
110                         room.log_sink <- LogEvent{room.name, client.nickname, "left", true}
111                 case EVENT_TOPIC:
112                         if _, subscribed := room.members[client]; !subscribed {
113                                 client.ReplyParts("442", room.name, "You are not on that channel")
114                                 continue
115                         }
116                         if event.text == "" {
117                                 go room.SendTopic(client)
118                                 continue
119                         }
120                         room.topic = strings.TrimLeft(event.text, ":")
121                         msg := fmt.Sprintf(":%s TOPIC %s :%s", client, room.name, room.topic)
122                         go room.Broadcast(msg)
123                         room.log_sink <- LogEvent{room.name, client.nickname, "set topic to " + room.topic, true}
124                         room.StateSave()
125                 case EVENT_WHO:
126                         for m := range room.members {
127                                 client.ReplyNicknamed("352", room.name, m.username, m.conn.RemoteAddr().String(), room.hostname, m.nickname, "H", "0 "+m.realname)
128                         }
129                         client.ReplyNicknamed("315", room.name, "End of /WHO list")
130                 case EVENT_MODE:
131                         if event.text == "" {
132                                 mode := "+"
133                                 if room.key != "" {
134                                         mode = mode + "k"
135                                 }
136                                 client.Msg(fmt.Sprintf("324 %s %s %s", client.nickname, room.name, mode))
137                                 continue
138                         }
139                         if strings.HasPrefix(event.text, "-k") || strings.HasPrefix(event.text, "+k") {
140                                 if _, subscribed := room.members[client]; !subscribed {
141                                         client.ReplyParts("442", room.name, "You are not on that channel")
142                                         continue
143                                 }
144                         } else {
145                                 client.ReplyNicknamed("472", event.text, "Unknown MODE flag")
146                                 continue
147                         }
148                         var msg string
149                         var msg_log string
150                         if strings.HasPrefix(event.text, "+k") {
151                                 cols := strings.Split(event.text, " ")
152                                 if len(cols) == 1 {
153                                         client.ReplyNotEnoughParameters("MODE")
154                                         continue
155                                 }
156                                 room.key = cols[1]
157                                 msg = fmt.Sprintf(":%s MODE %s +k %s", client, room.name, room.key)
158                                 msg_log = "set channel key to " + room.key
159                         } else if strings.HasPrefix(event.text, "-k") {
160                                 room.key = ""
161                                 msg = fmt.Sprintf(":%s MODE %s -k", client, room.name)
162                                 msg_log = "removed channel key"
163                         }
164                         go room.Broadcast(msg)
165                         room.log_sink <- LogEvent{room.name, client.nickname, msg_log, true}
166                         room.StateSave()
167                 case EVENT_MSG:
168                         sep := strings.Index(event.text, " ")
169                         room.Broadcast(fmt.Sprintf(":%s %s %s :%s", client, event.text[:sep], room.name, event.text[sep+1:]), client)
170                         room.log_sink <- LogEvent{room.name, client.nickname, event.text[sep+1:], false}
171                 }
172         }
173 }