3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 07:29:31 +01:00
ergo/irc/client.go

183 lines
3.2 KiB
Go
Raw Normal View History

2012-04-07 20:44:59 +02:00
package irc
import (
2012-04-18 07:11:35 +02:00
"fmt"
"log"
2012-04-07 20:44:59 +02:00
"net"
2012-12-12 08:12:35 +01:00
"time"
2012-04-07 20:44:59 +02:00
)
type Client struct {
2014-02-05 04:28:24 +01:00
away bool
channels ChannelSet
conn net.Conn
2012-12-10 05:24:53 +01:00
hostname string
invisible bool
2012-04-08 08:32:08 +02:00
nick string
2014-02-09 19:07:40 +01:00
operator bool
2014-02-05 04:28:24 +01:00
realname string
registered bool
replies chan<- Reply
2014-02-05 04:28:24 +01:00
server *Server
serverPass bool
username string
2014-02-09 21:13:09 +01:00
idleTimer *time.Timer
quitTimer *time.Timer
}
2012-12-09 21:51:50 +01:00
func NewClient(server *Server, conn net.Conn) *Client {
2012-12-13 08:27:17 +01:00
read := StringReadChan(conn)
write := StringWriteChan(conn)
2013-06-03 01:53:06 +02:00
replies := make(chan Reply)
2012-12-13 08:27:17 +01:00
2012-12-09 21:51:50 +01:00
client := &Client{
2014-02-09 17:48:11 +01:00
channels: make(ChannelSet),
conn: conn,
hostname: LookupHostname(conn.RemoteAddr()),
replies: replies,
server: server,
2012-12-09 21:51:50 +01:00
}
2012-12-13 08:27:17 +01:00
go client.readConn(read)
go client.writeConn(write, replies)
2012-12-13 08:27:17 +01:00
2014-02-09 21:13:09 +01:00
client.Touch()
2012-04-08 08:32:08 +02:00
return client
2012-04-07 20:44:59 +02:00
}
2014-02-09 21:13:09 +01:00
func (client *Client) Touch() {
if client.quitTimer != nil {
client.quitTimer.Stop()
}
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.Quit)
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
client.Reply(RplPing(client.server, client))
}
func (client *Client) Quit() {
msg := &QuitCommand{
message: "connection timeout",
}
msg.SetClient(client)
client.server.commands <- msg
}
2012-12-13 08:27:17 +01:00
func (c *Client) readConn(recv <-chan string) {
for str := range recv {
m, err := ParseCommand(str)
2012-12-09 21:51:50 +01:00
if err != nil {
2013-05-11 22:55:01 +02:00
if err == NotEnoughArgsError {
2014-02-09 17:48:11 +01:00
c.Reply(ErrNeedMoreParams(c.server, str))
2013-05-11 22:55:01 +02:00
} else {
2014-02-09 17:48:11 +01:00
c.Reply(ErrUnknownCommand(c.server, str))
2013-05-11 22:55:01 +02:00
}
2012-12-13 08:27:17 +01:00
continue
2012-04-08 08:32:08 +02:00
}
2012-12-13 08:27:17 +01:00
2014-02-09 08:51:51 +01:00
m.SetClient(c)
c.server.commands <- m
2012-12-13 08:27:17 +01:00
}
}
func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
for reply := range replies {
if DEBUG_CLIENT {
2013-05-26 22:28:22 +02:00
log.Printf("%s ← %s : %s", c, reply.Source(), reply)
}
2013-06-03 07:07:50 +02:00
reply.Format(c, write)
2012-04-08 08:32:08 +02:00
}
2012-04-07 20:44:59 +02:00
}
2012-04-09 16:57:55 +02:00
2014-02-09 17:48:11 +01:00
func (client *Client) Destroy() error {
if client.replies == nil {
return ErrAlreadyDestroyed
}
2014-02-10 04:06:30 +01:00
2014-02-09 08:15:05 +01:00
close(client.replies)
2014-02-09 17:48:11 +01:00
client.replies = nil
client.conn.Close()
2014-02-10 04:06:30 +01:00
if client.idleTimer != nil {
client.idleTimer.Stop()
}
if client.quitTimer != nil {
client.quitTimer.Stop()
}
2014-02-09 17:48:11 +01:00
return nil
}
2014-02-09 17:48:11 +01:00
func (client *Client) Reply(reply Reply) error {
if client.replies == nil {
return ErrAlreadyDestroyed
}
client.replies <- reply
return nil
}
func (client *Client) HasNick() bool {
return client.nick != ""
}
func (client *Client) HasUsername() bool {
return client.username != ""
}
2014-02-09 02:43:59 +01:00
func (client *Client) InterestedClients() ClientSet {
clients := make(ClientSet)
2014-02-09 02:43:59 +01:00
for channel := range client.channels {
for member := range channel.members {
clients[member] = true
}
2012-04-09 16:57:55 +02:00
}
return clients
2012-04-09 16:57:55 +02:00
}
2012-04-18 05:24:26 +02:00
2014-02-09 17:53:06 +01:00
// <mode>
func (c *Client) ModeString() (str string) {
if c.invisible {
2014-02-09 17:53:06 +01:00
str += Invisible.String()
}
2014-02-09 19:07:40 +01:00
if c.operator {
str += Operator.String()
}
2014-02-09 17:53:06 +01:00
if len(str) > 0 {
str = "+" + str
}
return
2012-04-18 05:24:26 +02:00
}
2012-04-18 06:13:12 +02:00
func (c *Client) UserHost() string {
nick := c.nick
if nick == "" {
nick = "*"
}
username := c.username
if username == "" {
username = "*"
}
return fmt.Sprintf("%s!%s@%s", nick, username, c.hostname)
}
func (c *Client) Nick() string {
return c.nick
2012-04-18 06:13:12 +02:00
}
2012-04-18 07:11:35 +02:00
func (c *Client) Id() string {
return c.UserHost()
2012-04-18 07:11:35 +02:00
}
func (c *Client) String() string {
2014-02-08 22:18:11 +01:00
return c.UserHost()
2013-05-11 22:55:01 +02:00
}