2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 CRLF []byte = []byte{'\x0d', '\x0a'}
46 recvTimestamp time.Time
47 sendTimestamp time.Time
53 func (c *Client) Host() string {
54 addr := c.conn.RemoteAddr().String()
55 if host, _, err := net.SplitHostPort(addr); err == nil {
58 if domains, err := net.LookupAddr(addr); err == nil {
59 addr = strings.TrimSuffix(domains[0], ".")
64 func (c *Client) String() string {
65 return *c.nickname + "!" + *c.username + "@" + c.Host()
68 func NewClient(conn net.Conn) *Client {
75 recvTimestamp: time.Now(),
76 sendTimestamp: time.Now(),
78 outBuf: make(chan *string, MaxOutBuf),
84 func (c *Client) SetDead() {
89 func (c *Client) Close() {
97 // Client processor blockingly reads everything remote client sends,
98 // splits messages by CRLF and send them to Daemon gorouting for processing
99 // it futher. Also it can signalize that client is unavailable (disconnected).
100 func (c *Client) Processor(sink chan ClientEvent) {
101 sink <- ClientEvent{c, EventNew, ""}
102 log.Println(c, "New client")
103 buf := make([]byte, BufSize*2)
110 log.Println(c, "input buffer size exceeded, kicking him")
113 n, err = c.conn.Read(buf[prev:])
119 i = bytes.Index(buf[:prev], CRLF)
123 sink <- ClientEvent{c, EventMsg, string(buf[:i])}
124 copy(buf, buf[i+2:prev])
129 sink <- ClientEvent{c, EventDel, ""}
132 func (c *Client) MsgSender() {
133 for msg := range c.outBuf {
138 c.conn.Write(append([]byte(*msg), CRLF...))
142 // Send message as is with CRLF appended.
143 func (c *Client) Msg(text string) {
149 if len(c.outBuf) == MaxOutBuf {
150 log.Println(c, "output buffer size exceeded, kicking him")
159 // Send message from server. It has ": servername" prefix.
160 func (c *Client) Reply(text string) {
161 c.Msg(":" + *hostname + " " + text)
164 // Send server message, concatenating all provided text parts and
165 // prefix the last one with ":".
166 func (c *Client) ReplyParts(code string, text ...string) {
167 parts := []string{code}
168 for _, t := range text {
169 parts = append(parts, t)
171 parts[len(parts)-1] = ":" + parts[len(parts)-1]
172 c.Reply(strings.Join(parts, " "))
175 // Send nicknamed server message. After servername it always has target
176 // client's nickname. The last part is prefixed with ":".
177 func (c *Client) ReplyNicknamed(code string, text ...string) {
178 c.ReplyParts(code, append([]string{*c.nickname}, text...)...)
181 // Reply "461 not enough parameters" error for given command.
182 func (c *Client) ReplyNotEnoughParameters(command string) {
183 c.ReplyNicknamed("461", command, "Not enough parameters")
186 // Reply "403 no such channel" error for specified channel.
187 func (c *Client) ReplyNoChannel(channel string) {
188 c.ReplyNicknamed("403", channel, "No such channel")
191 func (c *Client) ReplyNoNickChan(channel string) {
192 c.ReplyNicknamed("401", channel, "No such nick/channel")