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/>.
32 PING_TIMEOUT = time.Second * 180 // Max time deadline for client's unresponsiveness
33 PING_THRESHOLD = time.Second * 90 // Max idle client's time before PING are sent
34 ALIVENESS_CHECK = time.Second * 10 // Client's aliveness check period
38 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 len(daemon.motd) == 0 {
75 client.ReplyNicknamed("422", "MOTD File is missing")
79 motd, err := ioutil.ReadFile(daemon.motd)
81 log.Printf("Can not read motd file %s: %v", daemon.motd, err)
82 client.ReplyNicknamed("422", "Error reading MOTD File")
86 client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -")
87 for _, s := range strings.Split(strings.Trim(string(motd), "\n"), "\n") {
88 client.ReplyNicknamed("372", "- "+string(s))
90 client.ReplyNicknamed("376", "End of /MOTD command")
93 func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
94 for _, nickname := range nicknames {
95 nickname = strings.ToLower(nickname)
97 for c := range daemon.clients {
98 if strings.ToLower(c.nickname) != nickname {
102 h := c.conn.RemoteAddr().String()
103 h, _, err := net.SplitHostPort(h)
105 log.Printf("Can't parse RemoteAddr %q: %v", h, err)
108 client.ReplyNicknamed("311", c.nickname, c.username, h, "*", 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_new.Verbose = daemon.Verbose
201 room_sink := make(chan ClientEvent)
202 daemon.rooms[name] = room_new
203 daemon.room_sinks[room_new] = room_sink
204 go room_new.Processor(room_sink)
205 return room_new, room_sink
208 func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
209 args := strings.Split(cmd, " ")
210 rooms := strings.Split(args[0], ",")
213 keys = strings.Split(args[1], ",")
217 for n, room := range rooms {
218 if !RoomNameValid(room) {
219 client.ReplyNoChannel(room)
223 if (n < len(keys)) && (keys[n] != "") {
230 for room_existing, room_sink := range daemon.room_sinks {
231 if room == room_existing.name {
232 if (room_existing.key != "") && (room_existing.key != key) {
235 room_sink <- ClientEvent{client, EVENT_NEW, ""}
242 client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
244 if denied || joined {
247 room_new, room_sink := daemon.RoomRegister(room)
252 room_sink <- ClientEvent{client, EVENT_NEW, ""}
256 func (daemon *Daemon) Processor(events <-chan ClientEvent) {
257 for event := range events {
259 // Check for clients aliveness
261 if daemon.last_aliveness_check.Add(ALIVENESS_CHECK).Before(now) {
262 for c := range daemon.clients {
263 if c.timestamp.Add(PING_TIMEOUT).Before(now) {
264 log.Println(c, "ping timeout")
268 if !c.ping_sent && c.timestamp.Add(PING_THRESHOLD).Before(now) {
270 c.Msg("PING :" + daemon.hostname)
273 log.Println(c, "ping timeout")
278 daemon.last_aliveness_check = now
281 client := event.client
282 switch event.event_type {
284 daemon.clients[client] = true
286 delete(daemon.clients, client)
287 for _, room_sink := range daemon.room_sinks {
291 cols := strings.SplitN(event.text, " ", 2)
292 command := strings.ToUpper(cols[0])
294 log.Println(client, "command", command)
296 if command == "QUIT" {
297 delete(daemon.clients, client)
301 if !client.registered {
302 go daemon.ClientRegister(client, command, cols)
309 if len(cols) == 1 || len(cols[1]) < 1 {
310 client.ReplyNotEnoughParameters("JOIN")
313 go daemon.HandlerJoin(client, cols[1])
315 daemon.SendList(client, cols)
317 go daemon.SendLusers(client)
319 if len(cols) == 1 || len(cols[1]) < 1 {
320 client.ReplyNotEnoughParameters("MODE")
323 cols = strings.SplitN(cols[1], " ", 2)
324 if cols[0] == client.username {
326 client.Msg("221 " + client.nickname + " +")
328 client.ReplyNicknamed("501", "Unknown MODE flag")
333 r, found := daemon.rooms[room]
335 client.ReplyNoChannel(room)
339 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, ""}
341 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MODE, cols[1]}
344 go daemon.SendMotd(client)
346 if len(cols) == 1 || len(cols[1]) < 1 {
347 client.ReplyNotEnoughParameters("PART")
350 for _, room := range strings.Split(cols[1], ",") {
351 r, found := daemon.rooms[room]
353 client.ReplyNoChannel(room)
356 daemon.room_sinks[r] <- ClientEvent{client, EVENT_DEL, ""}
360 client.ReplyNicknamed("409", "No origin specified")
363 client.Reply(fmt.Sprintf("PONG %s :%s", daemon.hostname, cols[1]))
366 case "NOTICE", "PRIVMSG":
368 client.ReplyNicknamed("411", "No recipient given ("+command+")")
371 cols = strings.SplitN(cols[1], " ", 2)
373 client.ReplyNicknamed("412", "No text to send")
377 target := strings.ToLower(cols[0])
378 for c := range daemon.clients {
379 if c.nickname == target {
380 msg = fmt.Sprintf(":%s %s %s :%s", client, command, c.nickname, cols[1])
388 r, found := daemon.rooms[target]
390 client.ReplyNoNickChan(target)
392 daemon.room_sinks[r] <- ClientEvent{client, EVENT_MSG, command + " " + strings.TrimLeft(cols[1], ":")}
395 client.ReplyNotEnoughParameters("TOPIC")
398 cols = strings.SplitN(cols[1], " ", 2)
399 r, found := daemon.rooms[cols[0]]
401 client.ReplyNoChannel(cols[0])
410 daemon.room_sinks[r] <- ClientEvent{client, EVENT_TOPIC, change}
412 if len(cols) == 1 || len(cols[1]) < 1 {
413 client.ReplyNotEnoughParameters("WHO")
416 room := strings.Split(cols[1], " ")[0]
417 r, found := daemon.rooms[room]
419 client.ReplyNoChannel(room)
422 daemon.room_sinks[r] <- ClientEvent{client, EVENT_WHO, ""}
424 if len(cols) == 1 || len(cols[1]) < 1 {
425 client.ReplyNotEnoughParameters("WHOIS")
428 cols := strings.Split(cols[1], " ")
429 nicknames := strings.Split(cols[len(cols)-1], ",")
430 go daemon.SendWhois(client, nicknames)
432 client.ReplyNicknamed("421", command, "Unknown command")