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}$")
45 clients map[*Client]bool
46 rooms map[string]*Room
47 room_sinks map[*Room]chan ClientEvent
48 last_aliveness_check time.Time
49 log_sink chan<- LogEvent
50 state_sink chan<- StateEvent
53 func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon {
54 daemon := Daemon{hostname: hostname, motd: motd}
55 daemon.clients = make(map[*Client]bool)
56 daemon.rooms = make(map[string]*Room)
57 daemon.room_sinks = make(map[*Room]chan ClientEvent)
58 daemon.log_sink = log_sink
59 daemon.state_sink = state_sink
63 func (daemon *Daemon) SendLusers(client *Client) {
65 for client := range daemon.clients {
66 if client.registered {
70 client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers))
73 func (daemon *Daemon) SendMotd(client *Client) {
74 if daemon.motd != "" {
75 fd, err := os.Open(daemon.motd)
81 buf := make([]byte, 1024)
83 motd = append(motd, bytes.TrimRight(buf, "\x00")...)
86 client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
87 for _, s := range bytes.Split(bytes.TrimRight(motd, "\n"), []byte("\n")) {
88 client.ReplyNicknamed("372", "- "+string(s))
90 client.ReplyNicknamed("376", "End of /MOTD command")
93 log.Println("Can not open motd file", daemon.motd, err)
96 client.ReplyNicknamed("422", "MOTD File is missing")
99 func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
100 for _, nickname := range nicknames {
101 nickname = strings.ToLower(nickname)
103 for c := range daemon.clients {
104 if strings.ToLower(c.nickname) != nickname {
108 client.ReplyNicknamed("311", c.nickname, c.username, c.conn.RemoteAddr().String(), "*", c.realname)
109 client.ReplyNicknamed("312", c.nickname, daemon.hostname, daemon.hostname)
110 subscriptions := []string{}
111 for _, room := range daemon.rooms {
112 for subscriber := range room.members {
113 if subscriber.nickname == nickname {
114 subscriptions = append(subscriptions, room.name)
118 sort.Strings(subscriptions)
119 client.ReplyNicknamed("319", c.nickname, strings.Join(subscriptions, " "))
120 client.ReplyNicknamed("318", c.nickname, "End of /WHOIS list")
123 client.ReplyNoNickChan(nickname)
128 func (daemon *Daemon) SendList(client *Client, cols []string) {
130 if (len(cols) > 1) && (cols[1] != "") {
131 rooms = strings.Split(strings.Split(cols[1], " ")[0], ",")
134 for room := range daemon.rooms {
135 rooms = append(rooms, room)
139 for _, room := range rooms {
140 r, found := daemon.rooms[room]
142 client.ReplyNicknamed("322", room, fmt.Sprintf("%d", len(r.members)), r.topic)
145 client.ReplyNicknamed("323", "End of /LIST")
148 // Unregistered client workflow processor. Unregistered client:
150 // * only QUIT, NICK and USER commands are processed
151 // * other commands are quietly ignored
152 // When client finishes NICK/USER workflow, then MOTD and LUSERS are send to him.
153 func (daemon *Daemon) ClientRegister(client *Client, command string, cols []string) {
156 if len(cols) == 1 || len(cols[1]) < 1 {
157 client.ReplyParts("431", "No nickname given")
161 for client := range daemon.clients {
162 if client.nickname == nickname {
163 client.ReplyParts("433", "*", nickname, "Nickname is already in use")
167 if !RE_NICKNAME.MatchString(nickname) {
168 client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
171 client.nickname = nickname
174 client.ReplyNotEnoughParameters("USER")
177 args := strings.SplitN(cols[1], " ", 4)
179 client.ReplyNotEnoughParameters("USER")
182 client.username = args[0]
183 client.realname = strings.TrimLeft(args[3], ":")
185 if client.nickname != "*" && client.username != "" {
186 client.registered = true
187 client.ReplyNicknamed("001", "Hi, welcome to IRC")
188 client.ReplyNicknamed("002", "Your host is "+daemon.hostname+", running goircd")
189 client.ReplyNicknamed("003", "This server was created sometime")
190 client.ReplyNicknamed("004", daemon.hostname+" goircd o o")
191 daemon.SendLusers(client)
192 daemon.SendMotd(client)
196 // Register new room in Daemon. Create an object, events sink, save pointers
197 // to corresponding daemon's places and start room's processor goroutine.
198 func (daemon *Daemon) RoomRegister(name string) (*Room, chan<- ClientEvent) {
199 room_new := NewRoom(daemon.hostname, name, daemon.log_sink, daemon.state_sink)
200 room_sink := make(chan ClientEvent)
201 daemon.rooms[name] = room_new
202 daemon.room_sinks[room_new] = room_sink
203 go room_new.Processor(room_sink)
204 return room_new, room_sink
207 func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
208 args := strings.Split(cmd, " ")
209 rooms := strings.Split(args[0], ",")
212 keys = strings.Split(args[1], ",")
216 for n, room := range rooms {
217 if !RoomNameValid(room) {
218 client.ReplyNoChannel(room)
222 if (n < len(keys)) && (keys[n] != "") {
229 for room_existing, room_sink := range daemon.room_sinks {
230 if room == room_existing.name {
231 if (room_existing.key != "") && (room_existing.key != key) {
234 room_sink <- ClientEvent{client, EVENT_NEW, ""}
241 client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
243 if denied || joined {
246 room_new, room_sink := daemon.RoomRegister(room)
251 room_sink <- ClientEvent{client, EVENT_NEW, ""}
255 func (daemon *Daemon) Processor(events <-chan ClientEvent) {
256 for event := range events {
258 // Check for clients aliveness
260 if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) {
261 for c := range daemon.clients {
262 if c.timestamp.Add(PING_TIMEOUT).Before(now) {
263 log.Println(c, "ping timeout")
267 if !c.ping_sent && c.timestamp.Add(PING_THRESHOLD).Before(now) {
269 c.Msg("PING :" + daemon.hostname)
272 log.Println(c, "ping timeout")
277 daemon.last_aliveness_check = now
280 client := event.client
281 switch event.event_type {
283 daemon.clients[client] = true
285 delete(daemon.clients, client)
286 for _, room_sink := range daemon.room_sinks {
290 cols := strings.SplitN(event.text, " ", 2)
291 command := strings.ToUpper(cols[0])
292 log.Println(client, "command", command)
293 if command == "QUIT" {
294 delete(daemon.clients, client)
298 if !client.registered {
299 go daemon.ClientRegister(client, command, cols)
306 if len(cols) == 1 || len(cols[1]) < 1 {
307 client.ReplyNotEnoughParameters("JOIN")
310 go daemon.HandlerJoin(client, cols[1])
312 daemon.SendList(client, cols)
314 go daemon.SendLusers(client)
316 if len(cols) == 1 || len(cols[1]) < 1 {
317 client.ReplyNotEnoughParameters("MODE")
320 cols = strings.SplitN(cols[1], " ", 2)
321 if cols[0] == client.username {
323 client.Msg("221 " + client.nickname + " +")
325 client.ReplyNicknamed("501", "Unknown MODE flag")
330 r, found := daemon.rooms[room]
332 client.ReplyNoChannel(room)
336 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, ""}
338 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, cols[1]}
341 go daemon.SendMotd(client)
343 if len(cols) == 1 || len(cols[1]) < 1 {
344 client.ReplyNotEnoughParameters("PART")
347 for _, room := range strings.Split(cols[1], ",") {
348 r, found := daemon.rooms[room]
350 client.ReplyNoChannel(room)
353 daemon.room_sinks[r] <- ClientEvent{client, EVENT_DEL, ""}
357 client.ReplyNicknamed("409", "No origin specified")
360 client.Reply(fmt.Sprintf("PONG %s :%s", daemon.hostname, cols[1]))
363 case "NOTICE", "PRIVMSG":
365 client.ReplyNicknamed("411", "No recipient given ("+command+")")
368 cols = strings.SplitN(cols[1], " ", 2)
370 client.ReplyNicknamed("412", "No text to send")
374 target := strings.ToLower(cols[0])
375 for c := range daemon.clients {
376 if c.nickname == target {
377 msg = fmt.Sprintf(":%s %s %s :%s", client, command, c.nickname, cols[1])
385 r, found := daemon.rooms[target]
387 client.ReplyNoNickChan(target)
389 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MSG, command + " " + strings.TrimLeft(cols[1], ":")}
392 client.ReplyNotEnoughParameters("TOPIC")
395 cols = strings.SplitN(cols[1], " ", 2)
396 r, found := daemon.rooms[cols[0]]
398 client.ReplyNoChannel(cols[0])
407 daemon.room_sinks[r] <- ClientEvent{client, EVENT_TOPIC, change}
409 if len(cols) == 1 || len(cols[1]) < 1 {
410 client.ReplyNotEnoughParameters("WHO")
413 room := strings.Split(cols[1], " ")[0]
414 r, found := daemon.rooms[room]
416 client.ReplyNoChannel(room)
419 daemon.room_sinks[r] <- ClientEvent{client, EVENT_WHO, ""}
421 if len(cols) == 1 || len(cols[1]) < 1 {
422 client.ReplyNotEnoughParameters("WHOIS")
425 cols := strings.Split(cols[1], " ")
426 nicknames := strings.Split(cols[len(cols)-1], ",")
427 go daemon.SendWhois(client, nicknames)
429 client.ReplyNicknamed("421", command, "Unknown command")