]> Cypherpunks.ru repositories - goircd.git/blob - client.go
Fixed unkeyed room mode getting
[goircd.git] / client.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "bytes"
23         "log"
24         "net"
25         "strings"
26         "sync"
27         "time"
28 )
29
30 const (
31         BufSize   = 1500
32         MaxOutBuf = 1 << 12
33 )
34
35 var (
36         CRLF []byte = []byte{'\x0d', '\x0a'}
37 )
38
39 type Client struct {
40         conn          net.Conn
41         registered    bool
42         nickname      *string
43         username      *string
44         realname      *string
45         password      *string
46         away          *string
47         recvTimestamp time.Time
48         sendTimestamp time.Time
49         outBuf        chan string
50         alive         bool
51         sync.Mutex
52 }
53
54 func (c Client) String() string {
55         return *c.nickname + "!" + *c.username + "@" + c.conn.RemoteAddr().String()
56 }
57
58 func NewClient(conn net.Conn) *Client {
59         nickname := "*"
60         username := ""
61         c := Client{
62                 conn:          conn,
63                 nickname:      &nickname,
64                 username:      &username,
65                 recvTimestamp: time.Now(),
66                 sendTimestamp: time.Now(),
67                 alive:         true,
68                 outBuf:        make(chan string, MaxOutBuf),
69         }
70         go c.MsgSender()
71         return &c
72 }
73
74 func (c *Client) SetDead() {
75         close(c.outBuf)
76         c.alive = false
77 }
78
79 func (c *Client) Close() {
80         c.Lock()
81         c.conn.Close()
82         if c.alive {
83                 c.SetDead()
84         }
85         c.Unlock()
86 }
87
88 // Client processor blockingly reads everything remote client sends,
89 // splits messages by CRLF and send them to Daemon gorouting for processing
90 // it futher. Also it can signalize that client is unavailable (disconnected).
91 func (c *Client) Processor(sink chan ClientEvent) {
92         sink <- ClientEvent{c, EventNew, ""}
93         log.Println(c, "New client")
94         buf := make([]byte, BufSize*2)
95         var n int
96         var prev int
97         var i int
98         var err error
99         for {
100                 if prev == BufSize {
101                         log.Println(c, "input buffer size exceeded, kicking him")
102                         break
103                 }
104                 n, err = c.conn.Read(buf[prev:])
105                 if err != nil {
106                         break
107                 }
108                 prev += n
109         CheckMore:
110                 i = bytes.Index(buf[:prev], CRLF)
111                 if i == -1 {
112                         continue
113                 }
114                 sink <- ClientEvent{c, EventMsg, string(buf[:i])}
115                 copy(buf, buf[i+2:prev])
116                 prev -= (i + 2)
117                 goto CheckMore
118         }
119         c.Close()
120         sink <- ClientEvent{c, EventDel, ""}
121 }
122
123 func (c *Client) MsgSender() {
124         for msg := range c.outBuf {
125                 c.conn.Write(append([]byte(msg), CRLF...))
126         }
127 }
128
129 // Send message as is with CRLF appended.
130 func (c *Client) Msg(text string) {
131         c.Lock()
132         defer c.Unlock()
133         if !c.alive {
134                 return
135         }
136         if len(c.outBuf) == MaxOutBuf {
137                 log.Println(c, "output buffer size exceeded, kicking him")
138                 go c.Close()
139                 c.SetDead()
140                 return
141         }
142         c.outBuf <- text
143 }
144
145 // Send message from server. It has ": servername" prefix.
146 func (c *Client) Reply(text string) {
147         c.Msg(":" + *hostname + " " + text)
148 }
149
150 // Send server message, concatenating all provided text parts and
151 // prefix the last one with ":".
152 func (c *Client) ReplyParts(code string, text ...string) {
153         parts := []string{code}
154         for _, t := range text {
155                 parts = append(parts, t)
156         }
157         parts[len(parts)-1] = ":" + parts[len(parts)-1]
158         c.Reply(strings.Join(parts, " "))
159 }
160
161 // Send nicknamed server message. After servername it always has target
162 // client's nickname. The last part is prefixed with ":".
163 func (c *Client) ReplyNicknamed(code string, text ...string) {
164         c.ReplyParts(code, append([]string{*c.nickname}, text...)...)
165 }
166
167 // Reply "461 not enough parameters" error for given command.
168 func (c *Client) ReplyNotEnoughParameters(command string) {
169         c.ReplyNicknamed("461", command, "Not enough parameters")
170 }
171
172 // Reply "403 no such channel" error for specified channel.
173 func (c *Client) ReplyNoChannel(channel string) {
174         c.ReplyNicknamed("403", channel, "No such channel")
175 }
176
177 func (c *Client) ReplyNoNickChan(channel string) {
178         c.ReplyNicknamed("401", channel, "No such nick/channel")
179 }