3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-02-20 15:40:51 +01:00
ergo/irc/client.go

241 lines
4.3 KiB
Go
Raw Normal View History

2012-04-07 11:44:59 -07:00
package irc
import (
2012-04-17 22:11:35 -07:00
"fmt"
"log"
2012-04-07 11:44:59 -07:00
"net"
2012-12-11 23:12:35 -08:00
"time"
2012-04-07 11:44:59 -07:00
)
type Client struct {
atime time.Time
2014-02-11 15:44:58 -08:00
away bool
awayMessage string
channels ChannelSet
2014-02-13 18:59:45 -08:00
ctime time.Time
friends map[*Client]uint
2014-02-11 15:44:58 -08:00
hostname string
idleTimer *time.Timer
invisible bool
loginTimer *time.Timer
2014-02-11 15:44:58 -08:00
nick string
operator bool
2014-02-14 18:28:36 -08:00
phase Phase
2014-02-11 15:44:58 -08:00
quitTimer *time.Timer
realname string
replies chan Reply
2014-02-11 15:44:58 -08:00
server *Server
2014-02-13 19:37:16 -08:00
socket *Socket
2014-02-11 15:44:58 -08:00
username string
}
2012-12-09 12:51:50 -08:00
func NewClient(server *Server, conn net.Conn) *Client {
2014-02-13 18:59:45 -08:00
now := time.Now()
2012-12-09 12:51:50 -08:00
client := &Client{
2014-02-13 18:59:45 -08:00
atime: now,
2014-02-09 08:48:11 -08:00
channels: make(ChannelSet),
2014-02-13 18:59:45 -08:00
ctime: now,
friends: make(map[*Client]uint),
2014-02-13 18:11:32 -08:00
hostname: AddrLookupHostname(conn.RemoteAddr()),
2014-02-14 18:28:36 -08:00
phase: server.InitPhase(),
replies: make(chan Reply),
2014-02-09 08:48:11 -08:00
server: server,
2014-02-13 19:37:16 -08:00
socket: NewSocket(conn),
2012-12-09 12:51:50 -08:00
}
2012-12-12 23:27:17 -08:00
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.ConnectionClosed)
2014-02-13 19:37:16 -08:00
go client.readCommands()
go client.writeReplies()
2012-12-12 23:27:17 -08:00
2012-04-07 23:32:08 -07:00
return client
2012-04-07 11:44:59 -07:00
}
func (client *Client) readCommands() {
for line := range client.socket.Read() {
msg, err := ParseCommand(line)
if err != nil {
switch err {
case NotEnoughArgsError:
client.Reply(ErrNeedMoreParams(client.server, line))
default:
client.Reply(ErrUnknownCommand(client.server, line))
}
continue
}
msg.SetClient(client)
client.server.commands <- msg
}
}
func (client *Client) writeReplies() {
for reply := range client.replies {
if DEBUG_CLIENT {
log.Printf("%s ← %s", client, reply)
}
client.socket.Write(reply.Format(client))
}
}
2014-02-09 12:13:09 -08:00
func (client *Client) Touch() {
client.atime = time.Now()
2014-02-09 12:13:09 -08:00
if client.quitTimer != nil {
client.quitTimer.Stop()
}
2014-02-09 12:13:09 -08:00
if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
} else {
client.idleTimer.Reset(IDLE_TIMEOUT)
}
}
func (client *Client) Idle() {
if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.ConnectionTimeout)
2014-02-09 12:13:09 -08:00
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
2014-02-09 12:13:09 -08:00
client.Reply(RplPing(client.server, client))
}
func (client *Client) ConnectionTimeout() {
2014-02-09 12:13:09 -08:00
msg := &QuitCommand{
message: "connection timeout",
}
msg.SetClient(client)
client.server.commands <- msg
2014-02-09 12:13:09 -08:00
}
func (client *Client) ConnectionClosed() {
msg := &QuitCommand{
message: "connection closed",
}
msg.SetClient(client)
client.server.commands <- msg
2012-12-12 23:27:17 -08:00
}
func (client *Client) Destroy() {
if DEBUG_CLIENT {
log.Printf("%s destroy", client)
2012-04-07 23:32:08 -07:00
}
2012-04-09 07:57:55 -07:00
client.socket.Close()
2014-02-12 21:04:50 -08:00
2014-02-16 18:16:13 -08:00
client.loginTimer.Stop()
2014-02-09 19:06:30 -08:00
if client.idleTimer != nil {
client.idleTimer.Stop()
}
2014-02-12 18:33:08 -08:00
2014-02-09 19:06:30 -08:00
if client.quitTimer != nil {
client.quitTimer.Stop()
}
2014-02-12 18:33:08 -08:00
client.server.clients.Remove(client)
2014-02-13 23:16:07 -08:00
if DEBUG_CLIENT {
log.Printf("%s destroyed", client)
2014-02-13 20:42:06 -08:00
}
}
2014-02-14 19:35:25 -08:00
func (client *Client) Reply(reply Reply) {
if client.replies == nil {
if DEBUG_CLIENT {
log.Printf("%s dropped %s", client, reply)
}
2014-02-14 19:35:25 -08:00
return
2014-02-11 15:33:02 -08:00
}
2014-02-14 19:35:25 -08:00
client.replies <- reply
}
func (client *Client) HasNick() bool {
return client.nick != ""
}
func (client *Client) HasUsername() bool {
return client.username != ""
}
2014-02-09 08:53:06 -08:00
// <mode>
func (c *Client) ModeString() (str string) {
if c.invisible {
2014-02-09 08:53:06 -08:00
str += Invisible.String()
}
2014-02-09 10:07:40 -08:00
if c.operator {
str += Operator.String()
}
2014-02-09 08:53:06 -08:00
if len(str) > 0 {
str = "+" + str
}
return
2012-04-17 20:24:26 -07:00
}
2012-04-17 21:13:12 -07:00
func (c *Client) UserHost() string {
username := "*"
if c.HasUsername() {
username = c.username
}
return fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname)
}
func (c *Client) Nick() string {
if c.HasNick() {
return c.nick
}
return "*"
2012-04-17 21:13:12 -07:00
}
2012-04-17 22:11:35 -07:00
func (c *Client) Id() string {
return c.UserHost()
2012-04-17 22:11:35 -07:00
}
func (c *Client) String() string {
2014-02-08 13:18:11 -08:00
return c.UserHost()
2013-05-11 13:55:01 -07:00
}
func (client *Client) AddFriend(friend *Client) {
client.friends[friend] += 1
}
func (client *Client) RemoveFriend(friend *Client) {
client.friends[friend] -= 1
if client.friends[friend] <= 0 {
delete(client.friends, friend)
}
}
func (client *Client) ChangeNickname(nickname string) {
// Make reply before changing nick.
reply := RplNick(client, nickname)
client.nick = nickname
for friend := range client.friends {
friend.Reply(reply)
}
}
func (client *Client) Quit(message string) {
if len(client.friends) > 0 {
reply := RplQuit(client, message)
for friend := range client.friends {
if friend == client {
continue
}
friend.Reply(reply)
}
}
for channel := range client.channels {
channel.Quit(client)
}
client.Destroy()
}