2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 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/>.
34 // Max deadline time of client's unresponsiveness
35 PingTimeout = time.Second * 180
36 // Max idle client's time before PING are sent
37 PingThreshold = time.Second * 90
41 RENickname = regexp.MustCompile("^[a-zA-Z0-9-]{1,24}$")
43 roomsGroup sync.WaitGroup
45 clients map[*Client]struct{} = make(map[*Client]struct{})
48 func SendLusers(client *Client) {
50 for client := range clients {
51 if client.registered {
55 client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers))
58 func SendMotd(client *Client) {
60 client.ReplyNicknamed("422", "MOTD File is missing")
63 motdText, err := ioutil.ReadFile(*motd)
65 log.Printf("Can not read motd file %s: %v", *motd, err)
66 client.ReplyNicknamed("422", "Error reading MOTD File")
69 client.ReplyNicknamed("375", "- "+*hostname+" Message of the day -")
70 for _, s := range strings.Split(strings.TrimSuffix(string(motdText), "\n"), "\n") {
71 client.ReplyNicknamed("372", "- "+s)
73 client.ReplyNicknamed("376", "End of /MOTD command")
76 func SendWhois(client *Client, nicknames []string) {
80 var subscriptions []string
82 var subscriber *Client
83 for _, nickname := range nicknames {
84 nickname = strings.ToLower(nickname)
85 for c = range clients {
86 if strings.ToLower(*c.nickname) == nickname {
90 client.ReplyNoNickChan(nickname)
93 hostPort, _, err = net.SplitHostPort(c.conn.RemoteAddr().String())
95 log.Printf("Can't parse RemoteAddr %q: %v", hostPort, err)
98 client.ReplyNicknamed("311", *c.nickname, *c.username, hostPort, "*", *c.realname)
99 client.ReplyNicknamed("312", *c.nickname, *hostname, *hostname)
101 client.ReplyNicknamed("301", *c.nickname, *c.away)
103 subscriptions = make([]string, 0)
104 for _, room = range rooms {
105 for subscriber = range room.members {
106 if *subscriber.nickname == nickname {
107 subscriptions = append(subscriptions, *room.name)
111 sort.Strings(subscriptions)
112 client.ReplyNicknamed("319", *c.nickname, strings.Join(subscriptions, " "))
113 client.ReplyNicknamed("318", *c.nickname, "End of /WHOIS list")
117 func SendList(client *Client, cols []string) {
120 if (len(cols) > 1) && (cols[1] != "") {
121 rs = strings.Split(strings.Split(cols[1], " ")[0], ",")
123 rs = make([]string, 0)
124 for r = range rooms {
131 for _, r = range rs {
132 if room, found = rooms[r]; found {
133 client.ReplyNicknamed(
136 fmt.Sprintf("%d", len(room.members)),
141 client.ReplyNicknamed("323", "End of /LIST")
144 // Unregistered client workflow processor. Unregistered client:
146 // * only QUIT, NICK and USER commands are processed
147 // * other commands are quietly ignored
148 // When client finishes NICK/USER workflow, then MOTD and LUSERS are send to him.
149 func ClientRegister(client *Client, cmd string, cols []string) {
152 if len(cols) == 1 || len(cols[1]) < 1 {
153 client.ReplyNotEnoughParameters("PASS")
156 client.password = &cols[1]
158 if len(cols) == 1 || len(cols[1]) < 1 {
159 client.ReplyParts("431", "No nickname given")
163 // Compatibility with some clients prepending colons to nickname
164 nickname = strings.TrimPrefix(nickname, ":")
165 for existingClient := range clients {
166 if *existingClient.nickname == nickname {
167 client.ReplyParts("433", "*", nickname, "Nickname is already in use")
171 if !RENickname.MatchString(nickname) {
172 client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
175 client.nickname = &nickname
178 client.ReplyNotEnoughParameters("USER")
181 args := strings.SplitN(cols[1], " ", 4)
183 client.ReplyNotEnoughParameters("USER")
186 client.username = &args[0]
187 realname := strings.TrimLeft(args[3], ":")
188 client.realname = &realname
190 if *client.nickname != "*" && *client.username != "" {
191 if passwords != nil && *passwords != "" {
192 if client.password == nil {
193 client.ReplyParts("462", "You may not register")
197 contents, err := ioutil.ReadFile(*passwords)
199 log.Fatalf("Can no read passwords file %s: %s", *passwords, err)
202 for _, entry := range strings.Split(string(contents), "\n") {
206 if lp := strings.Split(entry, ":"); lp[0] == *client.nickname && lp[1] != *client.password {
207 client.ReplyParts("462", "You may not register")
213 client.registered = true
214 client.ReplyNicknamed("001", "Hi, welcome to IRC")
215 client.ReplyNicknamed("002", "Your host is "+*hostname+", running goircd "+version)
216 client.ReplyNicknamed("003", "This server was created sometime")
217 client.ReplyNicknamed("004", *hostname+" goircd o o")
220 log.Println(client, "logged in")
224 // Register new room in Daemon. Create an object, events sink, save pointers
225 // to corresponding daemon's places and start room's processor goroutine.
226 func RoomRegister(name string) (*Room, chan ClientEvent) {
227 roomNew := NewRoom(name)
228 roomSink := make(chan ClientEvent)
229 rooms[name] = roomNew
230 roomSinks[roomNew] = roomSink
231 go roomNew.Processor(roomSink)
233 return roomNew, roomSink
236 func HandlerJoin(client *Client, cmd string) {
237 args := strings.Split(cmd, " ")
238 rs := strings.Split(args[0], ",")
241 keys = strings.Split(args[1], ",")
243 keys = make([]string, 0)
245 var roomExisting *Room
246 var roomSink chan ClientEvent
248 for n, room := range rs {
249 if !RoomNameValid(room) {
250 client.ReplyNoChannel(room)
254 if (n < len(keys)) && (keys[n] != "") {
259 for roomExisting, roomSink = range roomSinks {
260 if room == *roomExisting.name {
261 if (*roomExisting.key != "") && (*roomExisting.key != key) {
264 roomSink <- ClientEvent{client, EventNew, ""}
268 roomNew, roomSink = RoomRegister(room)
269 log.Println("Room", roomNew, "created")
274 roomSink <- ClientEvent{client, EventNew, ""}
277 client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
282 func Processor(events chan ClientEvent, finished chan struct{}) {
286 time.Sleep(10 * time.Second)
287 events <- ClientEvent{eventType: EventTick}
290 for event := range events {
292 client := event.client
293 switch event.eventType {
295 for c := range clients {
296 if c.recvTimestamp.Add(PingTimeout).Before(now) {
297 log.Println(c, "ping timeout")
301 if c.sendTimestamp.Add(PingThreshold).Before(now) {
303 c.Msg("PING :" + *hostname)
304 c.sendTimestamp = time.Now()
306 log.Println(c, "ping timeout")
312 for _, sink := range roomSinks {
313 sink <- ClientEvent{eventType: EventTerm}
319 clients[client] = struct{}{}
321 delete(clients, client)
322 for _, roomSink := range roomSinks {
326 cols := strings.SplitN(event.text, " ", 2)
327 cmd := strings.ToUpper(cols[0])
329 log.Println(client, "command", cmd)
332 log.Println(client, "quit")
336 if !client.registered {
337 ClientRegister(client, cmd, cols)
344 client.ReplyNicknamed("305", "You are no longer marked as being away")
347 msg := strings.TrimLeft(cols[1], ":")
349 client.ReplyNicknamed("306", "You have been marked as being away")
351 if len(cols) == 1 || len(cols[1]) < 1 {
352 client.ReplyNotEnoughParameters("JOIN")
355 HandlerJoin(client, cols[1])
357 SendList(client, cols)
361 if len(cols) == 1 || len(cols[1]) < 1 {
362 client.ReplyNotEnoughParameters("MODE")
365 cols = strings.SplitN(cols[1], " ", 2)
366 if cols[0] == *client.username {
368 client.Msg("221 " + *client.nickname + " +")
370 client.ReplyNicknamed("501", "Unknown MODE flag")
375 r, found := rooms[room]
377 client.ReplyNoChannel(room)
381 roomSinks[r] <- ClientEvent{client, EventMode, ""}
383 roomSinks[r] <- ClientEvent{client, EventMode, cols[1]}
388 if len(cols) == 1 || len(cols[1]) < 1 {
389 client.ReplyNotEnoughParameters("PART")
392 rs := strings.Split(cols[1], " ")[0]
393 for _, room := range strings.Split(rs, ",") {
394 if r, found := rooms[room]; found {
395 roomSinks[r] <- ClientEvent{client, EventDel, ""}
397 client.ReplyNoChannel(room)
403 client.ReplyNicknamed("409", "No origin specified")
406 client.Reply(fmt.Sprintf("PONG %s :%s", *hostname, cols[1]))
409 case "NOTICE", "PRIVMSG":
411 client.ReplyNicknamed("411", "No recipient given ("+cmd+")")
414 cols = strings.SplitN(cols[1], " ", 2)
416 client.ReplyNicknamed("412", "No text to send")
420 target := strings.ToLower(cols[0])
421 for c := range clients {
422 if *c.nickname == target {
423 msg = fmt.Sprintf(":%s %s %s %s", client, cmd, *c.nickname, cols[1])
426 client.ReplyNicknamed("301", *c.nickname, *c.away)
434 if r, found := rooms[target]; found {
435 roomSinks[r] <- ClientEvent{
438 cmd + " " + strings.TrimLeft(cols[1], ":"),
441 client.ReplyNoNickChan(target)
445 client.ReplyNotEnoughParameters("TOPIC")
448 cols = strings.SplitN(cols[1], " ", 2)
449 r, found := rooms[cols[0]]
451 client.ReplyNoChannel(cols[0])
460 roomSinks[r] <- ClientEvent{client, EventTopic, change}
462 if len(cols) == 1 || len(cols[1]) < 1 {
463 client.ReplyNotEnoughParameters("WHO")
466 room := strings.Split(cols[1], " ")[0]
467 if r, found := rooms[room]; found {
468 roomSinks[r] <- ClientEvent{client, EventWho, ""}
470 client.ReplyNoChannel(room)
473 if len(cols) == 1 || len(cols[1]) < 1 {
474 client.ReplyNotEnoughParameters("WHOIS")
477 cols := strings.Split(cols[1], " ")
478 nicknames := strings.Split(cols[len(cols)-1], ",")
479 SendWhois(client, nicknames)
487 client.ReplyNicknamed("351", fmt.Sprintf("%s.%s %s :", version, debug, *hostname))
489 client.ReplyNicknamed("421", cmd, "Unknown command")
493 client.recvTimestamp = now