ergo/src/irc/client.go

130 lines
2.1 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
)
const (
DEBUG_CLIENT = true
)
2012-04-07 20:44:59 +02:00
type Client struct {
conn net.Conn
2012-04-08 08:32:08 +02:00
username string
realname string
2012-12-10 05:24:53 +01:00
hostname string
2012-04-08 08:32:08 +02:00
nick string
2012-12-10 05:24:53 +01:00
serverPass bool
registered bool
away bool
server *Server
2012-12-12 08:12:35 +01:00
atime time.Time
user *User
replies chan<- Reply
}
type ClientSet map[*Client]bool
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-05-26 22:28:22 +02:00
replies := make(chan Reply, 1)
2012-12-13 08:27:17 +01:00
2012-12-09 21:51:50 +01:00
client := &Client{
conn: conn,
hostname: conn.RemoteAddr().String(),
2012-12-09 21:51:50 +01:00
server: server,
replies: replies,
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
2012-04-08 08:32:08 +02:00
return client
2012-04-07 20:44:59 +02:00
}
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 {
c.Replies() <- ErrNeedMoreParams(c.server, str)
} else {
c.Replies() <- ErrUnknownCommand(c.server, str)
}
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
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)
}
write <- reply.Format(c)
2012-04-08 08:32:08 +02:00
}
2012-04-07 20:44:59 +02:00
}
2012-04-09 16:57:55 +02:00
func (c *Client) Replies() chan<- Reply {
return c.replies
}
func (c *Client) Server() *Server {
return c.server
}
func (c *Client) Nick() string {
if c.user != nil {
return c.user.Nick()
}
2012-04-09 16:57:55 +02:00
if c.nick != "" {
return c.nick
}
return "guest"
2012-04-09 16:57:55 +02:00
}
2012-04-18 05:24:26 +02:00
func (c *Client) UModeString() string {
return ""
2012-04-18 05:24:26 +02:00
}
2012-04-18 06:13:12 +02:00
func (c *Client) HasNick() bool {
return c.Nick() != ""
2012-04-18 06:13:12 +02:00
}
func (c *Client) HasUsername() bool {
2012-04-18 06:13:12 +02:00
return c.username != ""
}
func (c *Client) Username() string {
if c.HasUsername() {
return c.username
}
return "guest"
}
func (c *Client) UserHost() string {
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Username(), c.hostname)
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 {
return c.hostname
2013-05-11 22:55:01 +02:00
}
func (c *Client) PublicId() string {
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
}