]> Cypherpunks.ru repositories - goircd.git/blob - client.go
Add AWAY command support
[goircd.git] / client.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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 package main
19
20 import (
21         "bytes"
22         "log"
23         "net"
24         "strings"
25         "time"
26 )
27
28 const (
29         CRLF    = "\x0d\x0a"
30         BufSize = 1380
31 )
32
33 type Client struct {
34         hostname   *string
35         conn       net.Conn
36         registered bool
37         nickname   string
38         username   string
39         realname   string
40         password   string
41         away       *string
42 }
43
44 type ClientAlivenessState struct {
45         pingSent  bool
46         timestamp time.Time
47 }
48
49 func (client Client) String() string {
50         return client.nickname + "!" + client.username + "@" + client.conn.RemoteAddr().String()
51 }
52
53 func NewClient(hostname *string, conn net.Conn) *Client {
54         return &Client{hostname: hostname, conn: conn, nickname: "*", password: ""}
55 }
56
57 // Client processor blockingly reads everything remote client sends,
58 // splits messages by CRLF and send them to Daemon gorouting for processing
59 // it futher. Also it can signalize that client is unavailable (disconnected).
60 func (client *Client) Processor(sink chan<- ClientEvent) {
61         var bufNet []byte
62         buf := make([]byte, 0)
63         log.Println(client, "New client")
64         sink <- ClientEvent{client, EventNew, ""}
65         for {
66                 bufNet = make([]byte, BufSize)
67                 _, err := client.conn.Read(bufNet)
68                 if err != nil {
69                         sink <- ClientEvent{client, EventDel, ""}
70                         break
71                 }
72                 bufNet = bytes.TrimRight(bufNet, "\x00")
73                 buf = append(buf, bufNet...)
74                 if !bytes.HasSuffix(buf, []byte(CRLF)) {
75                         continue
76                 }
77                 for _, msg := range bytes.Split(buf[:len(buf)-2], []byte(CRLF)) {
78                         if len(msg) > 0 {
79                                 sink <- ClientEvent{client, EventMsg, string(msg)}
80                         }
81                 }
82                 buf = []byte{}
83         }
84 }
85
86 // Send message as is with CRLF appended.
87 func (client *Client) Msg(text string) {
88         client.conn.Write([]byte(text + CRLF))
89 }
90
91 // Send message from server. It has ": servername" prefix.
92 func (client *Client) Reply(text string) {
93         client.Msg(":" + *client.hostname + " " + text)
94 }
95
96 // Send server message, concatenating all provided text parts and
97 // prefix the last one with ":".
98 func (client *Client) ReplyParts(code string, text ...string) {
99         parts := []string{code}
100         for _, t := range text {
101                 parts = append(parts, t)
102         }
103         parts[len(parts)-1] = ":" + parts[len(parts)-1]
104         client.Reply(strings.Join(parts, " "))
105 }
106
107 // Send nicknamed server message. After servername it always has target
108 // client's nickname. The last part is prefixed with ":".
109 func (client *Client) ReplyNicknamed(code string, text ...string) {
110         client.ReplyParts(code, append([]string{client.nickname}, text...)...)
111 }
112
113 // Reply "461 not enough parameters" error for given command.
114 func (client *Client) ReplyNotEnoughParameters(command string) {
115         client.ReplyNicknamed("461", command, "Not enough parameters")
116 }
117
118 // Reply "403 no such channel" error for specified channel.
119 func (client *Client) ReplyNoChannel(channel string) {
120         client.ReplyNicknamed("403", channel, "No such channel")
121 }
122
123 func (client *Client) ReplyNoNickChan(channel string) {
124         client.ReplyNicknamed("401", channel, "No such nick/channel")
125 }