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/>.
36 CRLF []byte = []byte{'\x0d', '\x0a'}
47 recvTimestamp time.Time
48 sendTimestamp time.Time
54 func (c Client) String() string {
55 return *c.nickname + "!" + *c.username + "@" + c.conn.RemoteAddr().String()
58 func NewClient(conn net.Conn) *Client {
65 recvTimestamp: time.Now(),
66 sendTimestamp: time.Now(),
68 outBuf: make(chan string, MaxOutBuf),
74 func (c *Client) SetDead() {
79 func (c *Client) Close() {
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)
101 log.Println(c, "input buffer size exceeded, kicking him")
104 n, err = c.conn.Read(buf[prev:])
110 i = bytes.Index(buf[:prev], CRLF)
114 sink <- ClientEvent{c, EventMsg, string(buf[:i])}
115 copy(buf, buf[i+2:prev])
120 sink <- ClientEvent{c, EventDel, ""}
123 func (c *Client) MsgSender() {
124 for msg := range c.outBuf {
125 c.conn.Write(append([]byte(msg), CRLF...))
129 // Send message as is with CRLF appended.
130 func (c *Client) Msg(text string) {
136 if len(c.outBuf) == MaxOutBuf {
137 log.Println(c, "output buffer size exceeded, kicking him")
145 // Send message from server. It has ": servername" prefix.
146 func (c *Client) Reply(text string) {
147 c.Msg(":" + *hostname + " " + text)
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)
157 parts[len(parts)-1] = ":" + parts[len(parts)-1]
158 c.Reply(strings.Join(parts, " "))
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...)...)
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")
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")
177 func (c *Client) ReplyNoNickChan(channel string) {
178 c.ReplyNicknamed("401", channel, "No such nick/channel")