]> Cypherpunks.ru repositories - goircd.git/blobdiff - daemon.go
Increase maximum nickname length for convenience
[goircd.git] / daemon.go
index 9b0d77f500c0614cafe33121e5b19c81bf9dae07..9145ba08dd950e345b2df1c7843b93817de17d24 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -1,6 +1,6 @@
 /*
 goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -15,6 +15,7 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
+
 package main
 
 import (
@@ -29,13 +30,16 @@ import (
 )
 
 const (
-       PingTimeout    = time.Second * 180 // Max time deadline for client's unresponsiveness
-       PingThreshold  = time.Second * 90  // Max idle client's time before PING are sent
-       AlivenessCheck = time.Second * 10  // Client's aliveness check period
+       // Max time deadline for client's unresponsiveness
+       PingTimeout = time.Second * 180
+       // Max idle client's time before PING are sent
+       PingThreshold = time.Second * 90
+       // Client's aliveness check period
+       AlivenessCheck = time.Second * 10
 )
 
 var (
-       RENickname = regexp.MustCompile("^[a-zA-Z0-9-]{1,9}$")
+       RENickname = regexp.MustCompile("^[a-zA-Z0-9-]{1,24}$")
 )
 
 type Daemon struct {
@@ -44,7 +48,7 @@ type Daemon struct {
        hostname           *string
        motd               *string
        passwords          *string
-       clients            map[*Client]bool
+       clients            map[*Client]struct{}
        clientAliveness    map[*Client]*ClientAlivenessState
        rooms              map[string]*Room
        roomSinks          map[*Room]chan ClientEvent
@@ -54,8 +58,13 @@ type Daemon struct {
 }
 
 func NewDaemon(version string, hostname, motd, passwords *string, logSink chan<- LogEvent, stateSink chan<- StateEvent) *Daemon {
-       daemon := Daemon{version: version, hostname: hostname, motd: motd, passwords: passwords}
-       daemon.clients = make(map[*Client]bool)
+       daemon := Daemon{
+               version:   version,
+               hostname:  hostname,
+               motd:      motd,
+               passwords: passwords,
+       }
+       daemon.clients = make(map[*Client]struct{})
        daemon.clientAliveness = make(map[*Client]*ClientAlivenessState)
        daemon.rooms = make(map[string]*Room)
        daemon.roomSinks = make(map[*Room]chan ClientEvent)
@@ -111,6 +120,9 @@ func (daemon *Daemon) SendWhois(client *Client, nicknames []string) {
                        }
                        client.ReplyNicknamed("311", c.nickname, c.username, h, "*", c.realname)
                        client.ReplyNicknamed("312", c.nickname, *daemon.hostname, *daemon.hostname)
+                       if c.away != nil {
+                               client.ReplyNicknamed("301", c.nickname, *c.away)
+                       }
                        subscriptions := []string{}
                        for _, room := range daemon.rooms {
                                for subscriber := range room.members {
@@ -168,6 +180,8 @@ func (daemon *Daemon) ClientRegister(client *Client, command string, cols []stri
                        return
                }
                nickname := cols[1]
+               // Compatibility with some clients prepending colons to nickname
+               nickname = strings.TrimPrefix(nickname, ":")
                for existingClient := range daemon.clients {
                        if existingClient.nickname == nickname {
                                client.ReplyParts("433", "*", nickname, "Nickname is already in use")
@@ -289,8 +303,9 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
 }
 
 func (daemon *Daemon) Processor(events <-chan ClientEvent) {
+       var now time.Time
        for event := range events {
-               now := time.Now()
+               now = time.Now()
                client := event.client
 
                // Check for clients aliveness
@@ -320,8 +335,11 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
 
                switch event.eventType {
                case EventNew:
-                       daemon.clients[client] = true
-                       daemon.clientAliveness[client] = &ClientAlivenessState{pingSent: false, timestamp: now}
+                       daemon.clients[client] = struct{}{}
+                       daemon.clientAliveness[client] = &ClientAlivenessState{
+                               pingSent:  false,
+                               timestamp: now,
+                       }
                case EventDel:
                        delete(daemon.clients, client)
                        delete(daemon.clientAliveness, client)
@@ -347,7 +365,14 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                        }
                        switch command {
                        case "AWAY":
-                               continue
+                               if len(cols) == 1 {
+                                       client.away = nil
+                                       client.ReplyNicknamed("305", "You are no longer marked as being away")
+                                       continue
+                               }
+                               msg := strings.TrimLeft(cols[1], ":")
+                               client.away = &msg
+                               client.ReplyNicknamed("306", "You have been marked as being away")
                        case "JOIN":
                                if len(cols) == 1 || len(cols[1]) < 1 {
                                        client.ReplyNotEnoughParameters("JOIN")
@@ -390,7 +415,8 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                        client.ReplyNotEnoughParameters("PART")
                                        continue
                                }
-                               for _, room := range strings.Split(cols[1], ",") {
+                               rooms := strings.Split(cols[1], " ")[0]
+                               for _, room := range strings.Split(rooms, ",") {
                                        r, found := daemon.rooms[room]
                                        if !found {
                                                client.ReplyNoChannel(room)
@@ -422,6 +448,9 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                        if c.nickname == target {
                                                msg = fmt.Sprintf(":%s %s %s %s", client, command, c.nickname, cols[1])
                                                c.Msg(msg)
+                                               if c.away != nil {
+                                                       client.ReplyNicknamed("301", c.nickname, *c.away)
+                                               }
                                                break
                                        }
                                }
@@ -431,8 +460,13 @@ func (daemon *Daemon) Processor(events <-chan ClientEvent) {
                                r, found := daemon.rooms[target]
                                if !found {
                                        client.ReplyNoNickChan(target)
+                                       continue
+                               }
+                               daemon.roomSinks[r] <- ClientEvent{
+                                       client,
+                                       EventMsg,
+                                       command + " " + strings.TrimLeft(cols[1], ":"),
                                }
-                               daemon.roomSinks[r] <- ClientEvent{client, EventMsg, command + " " + strings.TrimLeft(cols[1], ":")}
                        case "TOPIC":
                                if len(cols) == 1 {
                                        client.ReplyNotEnoughParameters("TOPIC")