mirror of
https://github.com/ergochat/ergo.git
synced 2025-04-14 03:48:01 +02:00
implement join/quit and channel messages properly
This commit is contained in:
parent
3f9495cda0
commit
06648393a1
@ -13,6 +13,7 @@ I wanted to learn Go.
|
|||||||
|
|
||||||
## Helpful Documentation
|
## Helpful Documentation
|
||||||
|
|
||||||
|
- [RFC 1459: Internet Relay Chat Protocol](http://tools.ietf.org/html/rfc1459)
|
||||||
- [RFC 2811: IRC Channel Management](http://tools.ietf.org/html/rfc2811)
|
- [RFC 2811: IRC Channel Management](http://tools.ietf.org/html/rfc2811)
|
||||||
- [RFC 2812: IRC Client Protocol](http://tools.ietf.org/html/rfc2812)
|
- [RFC 2812: IRC Client Protocol](http://tools.ietf.org/html/rfc2812)
|
||||||
- [RFC 2813: IRC Server Protocol](http://tools.ietf.org/html/rfc2813)
|
- [RFC 2813: IRC Server Protocol](http://tools.ietf.org/html/rfc2813)
|
||||||
|
@ -55,7 +55,10 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
|
|||||||
log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
|
log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
|
||||||
}
|
}
|
||||||
for client := range channel.members {
|
for client := range channel.members {
|
||||||
client.replies <- reply
|
var dest Identifier = client
|
||||||
|
if reply.Source() != dest {
|
||||||
|
client.replies <- reply
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,10 +102,6 @@ func (channel *Channel) Nick() string {
|
|||||||
return channel.name
|
return channel.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (channel *Channel) PublicId() string {
|
|
||||||
return channel.name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (channel *Channel) String() string {
|
func (channel *Channel) String() string {
|
||||||
return channel.Id()
|
return channel.Id()
|
||||||
}
|
}
|
||||||
@ -176,5 +175,5 @@ func (m *TopicCommand) HandleChannel(channel *Channel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
|
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
|
||||||
channel.replies <- RplPrivMsgChannel(channel, m.Client(), m.message)
|
channel.replies <- RplPrivMsg(m.Client(), channel, m.message)
|
||||||
}
|
}
|
||||||
|
@ -71,20 +71,33 @@ func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) Destroy() *Client {
|
||||||
|
client.conn.Close()
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Replies() chan<- Reply {
|
func (c *Client) Replies() chan<- Reply {
|
||||||
return c.replies
|
return c.replies
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Server() *Server {
|
func (client *Client) HasNick() bool {
|
||||||
return c.server
|
return client.nick != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Nick() string {
|
func (client *Client) HasUsername() bool {
|
||||||
if c.HasNick() {
|
return client.username != ""
|
||||||
return c.nick
|
}
|
||||||
|
|
||||||
|
func (fromClient *Client) InterestedClients() ClientSet {
|
||||||
|
clients := make(ClientSet)
|
||||||
|
clients[fromClient] = true
|
||||||
|
for channel := range fromClient.channels {
|
||||||
|
for client := range channel.members {
|
||||||
|
clients[client] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "guest"
|
return clients
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UModeString() string {
|
func (c *Client) UModeString() string {
|
||||||
@ -94,23 +107,20 @@ func (c *Client) UModeString() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) HasNick() bool {
|
|
||||||
return c.nick != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) HasUsername() bool {
|
|
||||||
return c.username != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Username() string {
|
|
||||||
if c.HasUsername() {
|
|
||||||
return c.username
|
|
||||||
}
|
|
||||||
return "guest"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) UserHost() string {
|
func (c *Client) UserHost() string {
|
||||||
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Username(), c.hostname)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Id() string {
|
func (c *Client) Id() string {
|
||||||
@ -120,7 +130,3 @@ func (c *Client) Id() string {
|
|||||||
func (c *Client) String() string {
|
func (c *Client) String() string {
|
||||||
return c.UserHost()
|
return c.UserHost()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PublicId() string {
|
|
||||||
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "irc-1"
|
VERSION = "ergonomadic-1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -150,6 +150,7 @@ const (
|
|||||||
ERR_UMODEUNKNOWNFLAG = 501
|
ERR_UMODEUNKNOWNFLAG = 501
|
||||||
ERR_USERSDONTMATCH = 502
|
ERR_USERSDONTMATCH = 502
|
||||||
// message codes
|
// message codes
|
||||||
|
RPL_ERROR = "ERROR"
|
||||||
RPL_INVITE = "INVITE"
|
RPL_INVITE = "INVITE"
|
||||||
RPL_JOIN = "JOIN"
|
RPL_JOIN = "JOIN"
|
||||||
RPL_NICK = "NICK"
|
RPL_NICK = "NICK"
|
||||||
|
@ -20,9 +20,12 @@ func StringReadChan(conn net.Conn) <-chan string {
|
|||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer conn.Close()
|
||||||
|
defer close(ch)
|
||||||
for {
|
for {
|
||||||
line, err := readTrimmedLine(reader)
|
line, err := readTrimmedLine(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Print("net: ", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if DEBUG_NET {
|
if DEBUG_NET {
|
||||||
@ -30,7 +33,6 @@ func StringReadChan(conn net.Conn) <-chan string {
|
|||||||
}
|
}
|
||||||
ch <- line
|
ch <- line
|
||||||
}
|
}
|
||||||
close(ch)
|
|
||||||
}()
|
}()
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
@ -39,18 +41,19 @@ func StringWriteChan(conn net.Conn) chan<- string {
|
|||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
writer := bufio.NewWriter(conn)
|
writer := bufio.NewWriter(conn)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer conn.Close()
|
||||||
|
defer close(ch)
|
||||||
for str := range ch {
|
for str := range ch {
|
||||||
if DEBUG_NET {
|
if DEBUG_NET {
|
||||||
log.Printf("%s ← %s : %s", conn.RemoteAddr(), conn.LocalAddr(), str)
|
log.Printf("%s ← %s : %s", conn.RemoteAddr(), conn.LocalAddr(), str)
|
||||||
}
|
}
|
||||||
if _, err := writer.WriteString(str + "\r\n"); err != nil {
|
if _, err := writer.WriteString(str + "\r\n"); err != nil {
|
||||||
|
log.Print("net: ", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
}
|
}
|
||||||
close(ch)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
irc/reply.go
15
irc/reply.go
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
type Identifier interface {
|
type Identifier interface {
|
||||||
Id() string
|
Id() string
|
||||||
PublicId() string
|
|
||||||
Nick() string
|
Nick() string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,10 +145,6 @@ func RplNick(source Identifier, newNick string) Reply {
|
|||||||
return NewStringReply(source, RPL_NICK, newNick)
|
return NewStringReply(source, RPL_NICK, newNick)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RplPrivMsgChannel(channel *Channel, source Identifier, message string) Reply {
|
|
||||||
return NewStringReply(source, RPL_PRIVMSG, "%s :%s", channel.name, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RplJoin(channel *Channel, client *Client) Reply {
|
func RplJoin(channel *Channel, client *Client) Reply {
|
||||||
return NewStringReply(client, RPL_JOIN, channel.name)
|
return NewStringReply(client, RPL_JOIN, channel.name)
|
||||||
}
|
}
|
||||||
@ -158,14 +153,18 @@ func RplPart(channel *Channel, client *Client, message string) Reply {
|
|||||||
return NewStringReply(client, RPL_PART, "%s :%s", channel.name, message)
|
return NewStringReply(client, RPL_PART, "%s :%s", channel.name, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RplPong(server *Server) Reply {
|
func RplPong(server *Server, client *Client) Reply {
|
||||||
return NewStringReply(server, RPL_PONG, server.Id())
|
return NewStringReply(server, RPL_PONG, client.Nick())
|
||||||
}
|
}
|
||||||
|
|
||||||
func RplQuit(client *Client, message string) Reply {
|
func RplQuit(client *Client, message string) Reply {
|
||||||
return NewStringReply(client, RPL_QUIT, ":%s", message)
|
return NewStringReply(client, RPL_QUIT, ":%s", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RplError(server *Server, target Identifier) Reply {
|
||||||
|
return NewStringReply(server, RPL_ERROR, target.Nick())
|
||||||
|
}
|
||||||
|
|
||||||
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
|
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
|
||||||
return NewStringReply(inviter, RPL_INVITE, channel.name)
|
return NewStringReply(inviter, RPL_INVITE, channel.name)
|
||||||
}
|
}
|
||||||
@ -189,7 +188,7 @@ func RplCreated(server *Server) Reply {
|
|||||||
|
|
||||||
func RplMyInfo(server *Server) Reply {
|
func RplMyInfo(server *Server) Reply {
|
||||||
return NewNumericReply(server, RPL_MYINFO,
|
return NewNumericReply(server, RPL_MYINFO,
|
||||||
"%s %s a kn", server.name, VERSION)
|
"%s %s aiwroOs kn", server.name, VERSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RplUModeIs(server *Server, client *Client) Reply {
|
func RplUModeIs(server *Server, client *Client) Reply {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
@ -49,18 +52,16 @@ func (s *Server) Listen(addr string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.hostname = LookupHostname(listener.Addr())
|
s.hostname = LookupHostname(listener.Addr())
|
||||||
if DEBUG_SERVER {
|
log.Print("Server.Listen: listening on ", addr)
|
||||||
log.Print("Server.Listen: listening on ", addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Server.Listen: ", err)
|
log.Print("Server.Accept: ", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if DEBUG_SERVER {
|
if DEBUG_SERVER {
|
||||||
log.Print("Server.Listen: accepted ", conn.RemoteAddr())
|
log.Print("Server.Accept: ", conn.RemoteAddr())
|
||||||
}
|
}
|
||||||
NewClient(s, conn)
|
NewClient(s, conn)
|
||||||
}
|
}
|
||||||
@ -77,17 +78,22 @@ func (s *Server) GetOrMakeChannel(name string) *Channel {
|
|||||||
return channel
|
return channel
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a message to clients of channels fromClient is a member.
|
func (s *Server) GenerateGuestNick() string {
|
||||||
func (s *Server) interestedClients(fromClient *Client) ClientSet {
|
bytes := make([]byte, 8)
|
||||||
clients := make(ClientSet)
|
for {
|
||||||
clients[fromClient] = true
|
_, err := rand.Read(bytes)
|
||||||
for channel := range fromClient.channels {
|
if err != nil {
|
||||||
for client := range channel.members {
|
panic(err)
|
||||||
clients[client] = true
|
}
|
||||||
|
randInt, n := binary.Uvarint(bytes)
|
||||||
|
if n <= 0 {
|
||||||
|
continue // TODO handle error
|
||||||
|
}
|
||||||
|
nick := fmt.Sprintf("guest%d", randInt)
|
||||||
|
if s.clients[nick] == nil {
|
||||||
|
return nick
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return clients
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// server functionality
|
// server functionality
|
||||||
@ -112,15 +118,11 @@ func (s *Server) Id() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) String() string {
|
func (s *Server) String() string {
|
||||||
return s.Id()
|
return s.name
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) PublicId() string {
|
|
||||||
return s.Id()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Nick() string {
|
func (s *Server) Nick() string {
|
||||||
return s.name
|
return s.Id()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) DeleteChannel(channel *Channel) {
|
func (s *Server) DeleteChannel(channel *Channel) {
|
||||||
@ -136,7 +138,7 @@ func (m *UnknownCommand) HandleServer(s *Server) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *PingCommand) HandleServer(s *Server) {
|
func (m *PingCommand) HandleServer(s *Server) {
|
||||||
m.Client().Replies() <- RplPong(s)
|
m.Client().Replies() <- RplPong(s, m.Client())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *PongCommand) HandleServer(s *Server) {
|
func (m *PongCommand) HandleServer(s *Server) {
|
||||||
@ -163,13 +165,11 @@ func (m *NickCommand) HandleServer(s *Server) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reply := RplNick(c, m.nickname)
|
reply := RplNick(c, m.nickname)
|
||||||
for iclient := range s.interestedClients(c) {
|
for iclient := range c.InterestedClients() {
|
||||||
iclient.replies <- reply
|
iclient.replies <- reply
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.HasNick() {
|
delete(s.clients, c.nick)
|
||||||
delete(s.clients, c.nick)
|
|
||||||
}
|
|
||||||
s.clients[m.nickname] = c
|
s.clients[m.nickname] = c
|
||||||
c.nick = m.nickname
|
c.nick = m.nickname
|
||||||
|
|
||||||
@ -190,18 +190,19 @@ func (m *UserMsgCommand) HandleServer(s *Server) {
|
|||||||
func (m *QuitCommand) HandleServer(s *Server) {
|
func (m *QuitCommand) HandleServer(s *Server) {
|
||||||
c := m.Client()
|
c := m.Client()
|
||||||
|
|
||||||
reply := RplQuit(c, m.message)
|
|
||||||
for client := range s.interestedClients(c) {
|
|
||||||
client.replies <- reply
|
|
||||||
}
|
|
||||||
cmd := &PartCommand{
|
|
||||||
BaseCommand: BaseCommand{c},
|
|
||||||
}
|
|
||||||
for channel := range c.channels {
|
|
||||||
channel.commands <- cmd
|
|
||||||
}
|
|
||||||
c.conn.Close()
|
|
||||||
delete(s.clients, c.nick)
|
delete(s.clients, c.nick)
|
||||||
|
for channel := range c.channels {
|
||||||
|
delete(channel.members, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.replies <- RplError(s, c)
|
||||||
|
c.Destroy()
|
||||||
|
|
||||||
|
reply := RplQuit(c, m.message)
|
||||||
|
for client := range c.InterestedClients() {
|
||||||
|
client.replies <- reply
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *JoinCommand) HandleServer(s *Server) {
|
func (m *JoinCommand) HandleServer(s *Server) {
|
||||||
@ -266,10 +267,16 @@ func (m *PrivMsgCommand) HandleServer(s *Server) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *ModeCommand) HandleServer(s *Server) {
|
func (m *ModeCommand) HandleServer(s *Server) {
|
||||||
for _, change := range m.changes {
|
client := m.Client()
|
||||||
if change.mode == Invisible {
|
if client.Nick() == m.nickname {
|
||||||
m.Client().invisible = change.add
|
for _, change := range m.changes {
|
||||||
|
if change.mode == Invisible {
|
||||||
|
client.invisible = change.add
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
client.replies <- RplUModeIs(s, client)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
m.Client().replies <- RplUModeIs(s, m.Client())
|
|
||||||
|
client.replies <- ErrUsersDontMatch(client)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user