2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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/>.
33 PING_TIMEOUT = time.Second * 180 // Max time deadline for client's unresponsiveness
34 PING_THRESHOLD = time.Second * 90 // Max idle client's time before PING are sent
35 ALIVENESS_CHECK = time.Second * 10 // Client's aliveness check period
39 RE_NICKNAME = regexp.MustCompile("^[a-zA-Z0-9-]{1,9}$")
46 clients map[*Client]bool
47 rooms map[string]*Room
48 room_sinks map[*Room]chan ClientEvent
49 last_aliveness_check time.Time
50 log_sink chan<- LogEvent
51 state_sink chan<- StateEvent
54 func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon {
55 daemon := Daemon{hostname: hostname, motd: motd}
56 daemon.clients = make(map[*Client]bool)
57 daemon.rooms = make(map[string]*Room)
58 daemon.room_sinks = make(map[*Room]chan ClientEvent)
59 daemon.log_sink = log_sink
60 daemon.state_sink = state_sink
64 func (daemon *Daemon) SendLusers(client *Client) {
66 for client := range daemon.clients {
67 if client.registered {
71 client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers))
74 func (daemon *Daemon) SendMotd(client *Client) {
75 if daemon.motd != "" {
76 fd, err := os.Open(daemon.motd)
82 buf := make([]byte, 1024)
84 motd = append(motd, bytes.TrimRight(buf, "\x00")...)
87 client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
88 for _, s := range bytes.Split(bytes.TrimRight(motd, "\n"), []byte("\n")) {
89 client.ReplyNicknamed("372", "- "+string(s))
91 client.ReplyNicknamed("376", "End of /MOTD command")
94 log.Println("Can not open motd file", daemon.motd, err)
97 client.ReplyNicknamed("422", "MOTD File is missing")
100 func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
101 for _, nickname := range nicknames {
102 nickname = strings.ToLower(nickname)
104 for c := range daemon.clients {
105 if strings.ToLower(c.nickname) != nickname {
109 client.ReplyNicknamed("311", c.nickname, c.username, c.conn.RemoteAddr().String(), "*", c.realname)
110 client.ReplyNicknamed("312", c.nickname, daemon.hostname, daemon.hostname)
111 subscriptions := []string{}
112 for _, room := range daemon.rooms {
113 for subscriber := range room.members {
114 if subscriber.nickname == nickname {
115 subscriptions = append(subscriptions, room.name)
119 sort.Strings(subscriptions)
120 client.ReplyNicknamed("319", c.nickname, strings.Join(subscriptions, " "))
121 client.ReplyNicknamed("318", c.nickname, "End of /WHOIS list")
124 client.ReplyNoNickChan(nickname)
129 func (daemon *Daemon) SendList(client *Client, cols []string) {
131 if (len(cols) > 1) && (cols[1] != "") {
132 rooms = strings.Split(strings.Split(cols[1], " ")[0], ",")
135 for room := range daemon.rooms {
136 rooms = append(rooms, room)
140 for _, room := range rooms {
141 r, found := daemon.rooms[room]
143 client.ReplyNicknamed("322", room, fmt.Sprintf("%d", len(r.members)), r.topic)
146 client.ReplyNicknamed("323", "End of /LIST")
149 // Unregistered client workflow processor. Unregistered client:
151 // * only QUIT, NICK and USER commands are processed
152 // * other commands are quietly ignored
153 // When client finishes NICK/USER workflow, then MOTD and LUSERS are send to him.
154 func (daemon *Daemon) ClientRegister(client *Client, command string, cols []string) {
157 if len(cols) == 1 || len(cols[1]) < 1 {
158 client.ReplyParts("431", "No nickname given")
162 for client := range daemon.clients {
163 if client.nickname == nickname {
164 client.ReplyParts("433", "*", nickname, "Nickname is already in use")
168 if !RE_NICKNAME.MatchString(nickname) {
169 client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
172 client.nickname = nickname
175 client.ReplyNotEnoughParameters("USER")
178 args := strings.SplitN(cols[1], " ", 4)
180 client.ReplyNotEnoughParameters("USER")
183 client.username = args[0]
184 client.realname = strings.TrimLeft(args[3], ":")
186 if client.nickname != "*" && client.username != "" {
187 client.registered = true
188 client.ReplyNicknamed("001", "Hi, welcome to IRC")
189 client.ReplyNicknamed("002", "Your host is "+daemon.hostname+", running goircd")
190 client.ReplyNicknamed("003", "This server was created sometime")
191 client.ReplyNicknamed("004", daemon.hostname+" goircd o o")
192 daemon.SendLusers(client)
193 daemon.SendMotd(client)
197 // Register new room in Daemon. Create an object, events sink, save pointers
198 // to corresponding daemon's places and start room's processor goroutine.
199 func (daemon *Daemon) RoomRegister(name string) (*Room, chan<- ClientEvent) {
200 room_new := NewRoom(daemon.hostname, name, daemon.log_sink, daemon.state_sink)
201 room_new.Verbose = daemon.Verbose
202 room_sink := make(chan ClientEvent)
203 daemon.rooms[name] = room_new
204 daemon.room_sinks[room_new] = room_sink
205 go room_new.Processor(room_sink)
206 return room_new, room_sink
209 func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
210 args := strings.Split(cmd, " ")
211 rooms := strings.Split(args[0], ",")
214 keys = strings.Split(args[1], ",")
218 for n, room := range rooms {
219 if !RoomNameValid(room) {
220 client.ReplyNoChannel(room)
224 if (n < len(keys)) && (keys[n] != "") {
231 for room_existing, room_sink := range daemon.room_sinks {
232 if room == room_existing.name {
233 if (room_existing.key != "") && (room_existing.key != key) {
236 room_sink <- ClientEvent{client, EVENT_NEW, ""}
243 client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
245 if denied || joined {
248 room_new, room_sink := daemon.RoomRegister(room)
253 room_sink <- ClientEvent{client, EVENT_NEW, ""}
257 func (daemon *Daemon) Processor(events <-chan ClientEvent) {
258 for event := range events {
260 // Check for clients aliveness
262 if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) {
263 for c := range daemon.clients {
264 if c.timestamp.Add(PING_TIMEOUT).Before(now) {
265 log.Println(c, "ping timeout")
269 if !c.ping_sent && c.timestamp.Add(PING_THRESHOLD).Before(now) {
271 c.Msg("PING :" + daemon.hostname)
274 log.Println(c, "ping timeout")
279 daemon.last_aliveness_check = now
282 client := event.client
283 switch event.event_type {
285 daemon.clients[client] = true
287 delete(daemon.clients, client)
288 for _, room_sink := range daemon.room_sinks {
292 cols := strings.SplitN(event.text, " ", 2)
293 command := strings.ToUpper(cols[0])
295 log.Println(client, "command", command)
297 if command == "QUIT" {
298 delete(daemon.clients, client)
302 if !client.registered {
303 go daemon.ClientRegister(client, command, cols)
310 if len(cols) == 1 || len(cols[1]) < 1 {
311 client.ReplyNotEnoughParameters("JOIN")
314 go daemon.HandlerJoin(client, cols[1])
316 daemon.SendList(client, cols)
318 go daemon.SendLusers(client)
320 if len(cols) == 1 || len(cols[1]) < 1 {
321 client.ReplyNotEnoughParameters("MODE")
324 cols = strings.SplitN(cols[1], " ", 2)
325 if cols[0] == client.username {
327 client.Msg("221 " + client.nickname + " +")
329 client.ReplyNicknamed("501", "Unknown MODE flag")
334 r, found := daemon.rooms[room]
336 client.ReplyNoChannel(room)
340 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, ""}
342 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, cols[1]}
345 go daemon.SendMotd(client)
347 if len(cols) == 1 || len(cols[1]) < 1 {
348 client.ReplyNotEnoughParameters("PART")
351 for _, room := range strings.Split(cols[1], ",") {
352 r, found := daemon.rooms[room]
354 client.ReplyNoChannel(room)
357 daemon.room_sinks[r] <- ClientEvent{client, EVENT_DEL, ""}
361 client.ReplyNicknamed("409", "No origin specified")
364 client.Reply(fmt.Sprintf("PONG %s :%s", daemon.hostname, cols[1]))
367 case "NOTICE", "PRIVMSG":
369 client.ReplyNicknamed("411", "No recipient given ("+command+")")
372 cols = strings.SplitN(cols[1], " ", 2)
374 client.ReplyNicknamed("412", "No text to send")
378 target := strings.ToLower(cols[0])
379 for c := range daemon.clients {
380 if c.nickname == target {
381 msg = fmt.Sprintf(":%s %s %s :%s", client, command, c.nickname, cols[1])
389 r, found := daemon.rooms[target]
391 client.ReplyNoNickChan(target)
393 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MSG, command + " " + strings.TrimLeft(cols[1], ":")}
396 client.ReplyNotEnoughParameters("TOPIC")
399 cols = strings.SplitN(cols[1], " ", 2)
400 r, found := daemon.rooms[cols[0]]
402 client.ReplyNoChannel(cols[0])
411 daemon.room_sinks[r] <- ClientEvent{client, EVENT_TOPIC, change}
413 if len(cols) == 1 || len(cols[1]) < 1 {
414 client.ReplyNotEnoughParameters("WHO")
417 room := strings.Split(cols[1], " ")[0]
418 r, found := daemon.rooms[room]
420 client.ReplyNoChannel(room)
423 daemon.room_sinks[r] <- ClientEvent{client, EVENT_WHO, ""}
425 if len(cols) == 1 || len(cols[1]) < 1 {
426 client.ReplyNotEnoughParameters("WHOIS")
429 cols := strings.Split(cols[1], " ")
430 nicknames := strings.Split(cols[len(cols)-1], ",")
431 go daemon.SendWhois(client, nicknames)
433 client.ReplyNicknamed("421", command, "Unknown command")