]> Cypherpunks.ru repositories - goircd.git/blob - room.go
Unnecessary message broadcasting goroutines
[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         name       string
40         topic      string
41         key        string
42         members    map[*Client]bool
43         hostname   string
44         log_sink   chan LogEvent
45         state_sink chan StateEvent
46 }
47
48 func NewRoom(hostname, name string, log_sink chan LogEvent, state_sink chan StateEvent) *Room {
49         room := Room{name: name}
50         room.members = make(map[*Client]bool)
51         room.topic = ""
52         room.key = ""
53         room.hostname = hostname
54         room.log_sink = log_sink
55         room.state_sink = state_sink
56         return &room
57 }
58
59 func (room *Room) SendTopic(client *Client) {
60         if room.topic == "" {
61                 client.ReplyNicknamed("331", room.name, "No topic is set")
62         } else {
63                 client.ReplyNicknamed("332", room.name, room.topic)
64         }
65 }
66
67 // Send message to all room's subscribers, possibly excluding someone
68 func (room *Room) Broadcast(msg string, client_to_ignore ...*Client) {
69         for member := range room.members {
70                 if (len(client_to_ignore) > 0) && member == client_to_ignore[0] {
71                         continue
72                 }
73                 member.Msg(msg)
74         }
75 }
76
77 func (room *Room) StateSave() {
78         room.state_sink <- StateEvent{room.name, room.topic, room.key}
79 }
80
81 func (room *Room) Processor(events chan ClientEvent) {
82         var client *Client
83         for event := range events {
84                 client = event.client
85                 switch event.event_type {
86                 case EVENT_NEW:
87                         room.members[client] = true
88                         log.Println(client, "joined", room.name)
89                         room.SendTopic(client)
90                         room.Broadcast(fmt.Sprintf(":%s JOIN %s", client, room.name))
91                         room.log_sink <- LogEvent{room.name, client.nickname, "joined", true}
92                         nicknames := []string{}
93                         for member := range room.members {
94                                 nicknames = append(nicknames, member.nickname)
95                         }
96                         sort.Strings(nicknames)
97                         client.ReplyNicknamed("353", "=", room.name, strings.Join(nicknames, " "))
98                         client.ReplyNicknamed("366", room.name, "End of NAMES list")
99                 case EVENT_DEL:
100                         if _, subscribed := room.members[client]; !subscribed {
101                                 client.ReplyNicknamed("442", room.name, "You are not on that channel")
102                                 continue
103                         }
104                         delete(room.members, client)
105                         msg := fmt.Sprintf(":%s PART %s :%s", client, room.name, client.nickname)
106                         go room.Broadcast(msg)
107                         room.log_sink <- LogEvent{room.name, client.nickname, "left", true}
108                 case EVENT_TOPIC:
109                         if _, subscribed := room.members[client]; !subscribed {
110                                 client.ReplyParts("442", room.name, "You are not on that channel")
111                                 continue
112                         }
113                         if event.text == "" {
114                                 go room.SendTopic(client)
115                                 continue
116                         }
117                         room.topic = strings.TrimLeft(event.text, ":")
118                         msg := fmt.Sprintf(":%s TOPIC %s :%s", client, room.name, room.topic)
119                         go room.Broadcast(msg)
120                         room.log_sink <- LogEvent{room.name, client.nickname, "set topic to " + room.topic, true}
121                         room.StateSave()
122                 case EVENT_WHO:
123                         for m := range room.members {
124                                 client.ReplyNicknamed("352", room.name, m.username, m.conn.RemoteAddr().String(), room.hostname, m.nickname, "H", "0 "+m.realname)
125                         }
126                         client.ReplyNicknamed("315", room.name, "End of /WHO list")
127                 case EVENT_MODE:
128                         if event.text == "" {
129                                 mode := "+"
130                                 if room.key != "" {
131                                         mode = mode + "k"
132                                 }
133                                 client.Msg(fmt.Sprintf("324 %s %s %s", client.nickname, room.name, mode))
134                                 continue
135                         }
136                         if strings.HasPrefix(event.text, "-k") || strings.HasPrefix(event.text, "+k") {
137                                 if _, subscribed := room.members[client]; !subscribed {
138                                         client.ReplyParts("442", room.name, "You are not on that channel")
139                                         continue
140                                 }
141                         } else {
142                                 client.ReplyNicknamed("472", event.text, "Unknown MODE flag")
143                                 continue
144                         }
145                         var msg string
146                         var msg_log string
147                         if strings.HasPrefix(event.text, "+k") {
148                                 cols := strings.Split(event.text, " ")
149                                 if len(cols) == 1 {
150                                         client.ReplyNotEnoughParameters("MODE")
151                                         continue
152                                 }
153                                 room.key = cols[1]
154                                 msg = fmt.Sprintf(":%s MODE %s +k %s", client, room.name, room.key)
155                                 msg_log = "set channel key to " + room.key
156                         } else if strings.HasPrefix(event.text, "-k") {
157                                 room.key = ""
158                                 msg = fmt.Sprintf(":%s MODE %s -k", client, room.name)
159                                 msg_log = "removed channel key"
160                         }
161                         go room.Broadcast(msg)
162                         room.log_sink <- LogEvent{room.name, client.nickname, msg_log, true}
163                         room.StateSave()
164                 case EVENT_MSG:
165                         sep := strings.Index(event.text, " ")
166                         room.Broadcast(fmt.Sprintf(":%s %s %s :%s", client, event.text[:sep], room.name, event.text[sep+1:]), client)
167                         room.log_sink <- LogEvent{room.name, client.nickname, event.text[sep+1:], false}
168                 }
169         }
170 }