mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-22 20:09:41 +01:00
type cleanup
This commit is contained in:
parent
20257ec624
commit
a3b7441939
@ -17,20 +17,6 @@ type Channel struct {
|
|||||||
topic string
|
topic string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChannelSet map[*Channel]bool
|
|
||||||
|
|
||||||
func (channels ChannelSet) First() *Channel {
|
|
||||||
for channel := range channels {
|
|
||||||
return channel
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChannelCommand interface {
|
|
||||||
Command
|
|
||||||
HandleChannel(channel *Channel)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsChannel(target string) bool {
|
func IsChannel(target string) bool {
|
||||||
if target == "" {
|
if target == "" {
|
||||||
return false
|
return false
|
||||||
@ -132,8 +118,8 @@ func (channel *Channel) ModeString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (channel *Channel) Join(client *Client) {
|
func (channel *Channel) Join(client *Client) {
|
||||||
channel.members[client] = true
|
channel.members.Add(client)
|
||||||
client.channels[channel] = true
|
client.channels.Add(channel)
|
||||||
reply := RplJoin(client, channel)
|
reply := RplJoin(client, channel)
|
||||||
client.replies <- reply
|
client.replies <- reply
|
||||||
channel.replies <- reply
|
channel.replies <- reply
|
||||||
@ -141,10 +127,6 @@ func (channel *Channel) Join(client *Client) {
|
|||||||
channel.GetUsers(client)
|
channel.GetUsers(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (channel *Channel) HasMember(client *Client) bool {
|
|
||||||
return channel.members[client]
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// commands
|
// commands
|
||||||
//
|
//
|
||||||
@ -162,7 +144,7 @@ func (m *JoinCommand) HandleChannel(channel *Channel) {
|
|||||||
func (m *PartCommand) HandleChannel(channel *Channel) {
|
func (m *PartCommand) HandleChannel(channel *Channel) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
|
|
||||||
if !channel.HasMember(client) {
|
if !channel.members.Has(client) {
|
||||||
client.replies <- ErrNotOnChannel(channel)
|
client.replies <- ErrNotOnChannel(channel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -171,19 +153,19 @@ func (m *PartCommand) HandleChannel(channel *Channel) {
|
|||||||
client.replies <- reply
|
client.replies <- reply
|
||||||
channel.replies <- reply
|
channel.replies <- reply
|
||||||
|
|
||||||
delete(channel.members, client)
|
channel.members.Remove(client)
|
||||||
delete(client.channels, channel)
|
client.channels.Remove(channel)
|
||||||
|
|
||||||
// TODO persistent channels
|
// TODO persistent channels
|
||||||
if channel.IsEmpty() {
|
if channel.IsEmpty() {
|
||||||
channel.server.DeleteChannel(channel)
|
channel.server.channels.Remove(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TopicCommand) HandleChannel(channel *Channel) {
|
func (m *TopicCommand) HandleChannel(channel *Channel) {
|
||||||
client := m.Client()
|
client := m.Client()
|
||||||
|
|
||||||
if !channel.HasMember(client) {
|
if !channel.members.Has(client) {
|
||||||
client.replies <- ErrNotOnChannel(channel)
|
client.replies <- ErrNotOnChannel(channel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,6 @@ type Client struct {
|
|||||||
username string
|
username string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientSet map[*Client]bool
|
|
||||||
|
|
||||||
func NewClient(server *Server, conn net.Conn) *Client {
|
func NewClient(server *Server, conn net.Conn) *Client {
|
||||||
read := StringReadChan(conn)
|
read := StringReadChan(conn)
|
||||||
write := StringWriteChan(conn)
|
write := StringWriteChan(conn)
|
||||||
@ -73,6 +71,7 @@ func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
|
|||||||
|
|
||||||
func (client *Client) Destroy() *Client {
|
func (client *Client) Destroy() *Client {
|
||||||
client.conn.Close()
|
client.conn.Close()
|
||||||
|
close(client.replies)
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ var (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "ergonomadic-1"
|
VERSION = "ergonomadic-1"
|
||||||
|
CRLF = "\r\n"
|
||||||
|
|
||||||
// numeric codes
|
// numeric codes
|
||||||
RPL_WELCOME = 1
|
RPL_WELCOME = 1
|
||||||
@ -173,7 +174,7 @@ const (
|
|||||||
Anonymous ChannelMode = 'a'
|
Anonymous ChannelMode = 'a'
|
||||||
BanMask ChannelMode = 'b' // arg
|
BanMask ChannelMode = 'b' // arg
|
||||||
ExceptionMask ChannelMode = 'e' // arg
|
ExceptionMask ChannelMode = 'e' // arg
|
||||||
InviteMask ChannelMode = 'i' // arg
|
InviteMask ChannelMode = 'I' // arg
|
||||||
InviteOnly ChannelMode = 'i'
|
InviteOnly ChannelMode = 'i'
|
||||||
Key ChannelMode = 'k' // arg
|
Key ChannelMode = 'k' // arg
|
||||||
Moderated ChannelMode = 'm'
|
Moderated ChannelMode = 'm'
|
||||||
|
@ -32,10 +32,6 @@ func StringReadChan(conn net.Conn) <-chan string {
|
|||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
CRLF = "\r\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
func maybeLogWriteError(conn net.Conn, err error) bool {
|
func maybeLogWriteError(conn net.Conn, err error) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
|
@ -188,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 aiwroOs kn", server.name, VERSION)
|
"%s %s aiOorsw abeIikmntpqrsl", server.name, VERSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RplUModeIs(server *Server, client *Client) Reply {
|
func RplUModeIs(server *Server, client *Client) Reply {
|
||||||
|
@ -9,9 +9,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChannelNameMap map[string]*Channel
|
|
||||||
type ClientNameMap map[string]*Client
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
channels ChannelNameMap
|
channels ChannelNameMap
|
||||||
commands chan<- Command
|
commands chan<- Command
|
||||||
@ -125,20 +122,16 @@ func (s *Server) Nick() string {
|
|||||||
return s.Id()
|
return s.Id()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) DeleteChannel(channel *Channel) {
|
|
||||||
delete(s.channels, channel.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// commands
|
// commands
|
||||||
//
|
//
|
||||||
|
|
||||||
func (m *UnknownCommand) HandleServer(s *Server) {
|
func (m *UnknownCommand) HandleServer(s *Server) {
|
||||||
m.Client().Replies() <- ErrUnknownCommand(s, m.command)
|
m.Client().replies <- ErrUnknownCommand(s, m.command)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *PingCommand) HandleServer(s *Server) {
|
func (m *PingCommand) HandleServer(s *Server) {
|
||||||
m.Client().Replies() <- RplPong(s, m.Client())
|
m.Client().replies <- RplPong(s, m.Client())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *PongCommand) HandleServer(s *Server) {
|
func (m *PongCommand) HandleServer(s *Server) {
|
||||||
@ -147,8 +140,8 @@ func (m *PongCommand) HandleServer(s *Server) {
|
|||||||
|
|
||||||
func (m *PassCommand) HandleServer(s *Server) {
|
func (m *PassCommand) HandleServer(s *Server) {
|
||||||
if s.password != m.password {
|
if s.password != m.password {
|
||||||
m.Client().Replies() <- ErrPasswdMismatch(s)
|
m.Client().replies <- ErrPasswdMismatch(s)
|
||||||
// TODO disconnect
|
m.Client().Destroy()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,9 +163,9 @@ func (m *NickCommand) HandleServer(s *Server) {
|
|||||||
iclient.replies <- reply
|
iclient.replies <- reply
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(s.clients, c.nick)
|
s.clients.Remove(c)
|
||||||
s.clients[m.nickname] = c
|
|
||||||
c.nick = m.nickname
|
c.nick = m.nickname
|
||||||
|
s.clients.Add(c)
|
||||||
|
|
||||||
s.tryRegister(c)
|
s.tryRegister(c)
|
||||||
}
|
}
|
||||||
@ -191,9 +184,9 @@ func (m *UserMsgCommand) HandleServer(s *Server) {
|
|||||||
func (m *QuitCommand) HandleServer(s *Server) {
|
func (m *QuitCommand) HandleServer(s *Server) {
|
||||||
c := m.Client()
|
c := m.Client()
|
||||||
|
|
||||||
delete(s.clients, c.nick)
|
s.clients.Remove(c)
|
||||||
for channel := range c.channels {
|
for channel := range c.channels {
|
||||||
delete(channel.members, c)
|
channel.members.Remove(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.replies <- RplError(s, c)
|
c.replies <- RplError(s, c)
|
||||||
|
72
irc/types.go
72
irc/types.go
@ -23,6 +23,73 @@ type ChannelMode rune
|
|||||||
// user-channel mode flags
|
// user-channel mode flags
|
||||||
type UserChannelMode rune
|
type UserChannelMode rune
|
||||||
|
|
||||||
|
type ChannelNameMap map[string]*Channel
|
||||||
|
|
||||||
|
func (channels ChannelNameMap) Add(channel *Channel) error {
|
||||||
|
if channels[channel.name] != nil {
|
||||||
|
return fmt.Errorf("%s: already set", channel.name)
|
||||||
|
}
|
||||||
|
channels[channel.name] = channel
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channels ChannelNameMap) Remove(channel *Channel) error {
|
||||||
|
if channel != channels[channel.name] {
|
||||||
|
return fmt.Errorf("%s: mismatch", channel.name)
|
||||||
|
}
|
||||||
|
delete(channels, channel.name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientNameMap map[string]*Client
|
||||||
|
|
||||||
|
func (clients ClientNameMap) Add(client *Client) error {
|
||||||
|
if clients[client.nick] != nil {
|
||||||
|
return fmt.Errorf("%s: already set", client.nick)
|
||||||
|
}
|
||||||
|
clients[client.nick] = client
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (clients ClientNameMap) Remove(client *Client) error {
|
||||||
|
if clients[client.nick] != client {
|
||||||
|
return fmt.Errorf("%s: mismatch", client.nick)
|
||||||
|
}
|
||||||
|
delete(clients, client.nick)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientSet map[*Client]bool
|
||||||
|
|
||||||
|
func (clients ClientSet) Add(client *Client) {
|
||||||
|
clients[client] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (clients ClientSet) Remove(client *Client) {
|
||||||
|
delete(clients, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (clients ClientSet) Has(client *Client) bool {
|
||||||
|
return clients[client]
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChannelSet map[*Channel]bool
|
||||||
|
|
||||||
|
func (channels ChannelSet) Add(channel *Channel) {
|
||||||
|
channels[channel] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channels ChannelSet) Remove(channel *Channel) {
|
||||||
|
delete(channels, channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channels ChannelSet) First() *Channel {
|
||||||
|
for channel := range channels {
|
||||||
|
return channel
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// interfaces
|
// interfaces
|
||||||
//
|
//
|
||||||
@ -36,6 +103,11 @@ type Command interface {
|
|||||||
HandleServer(*Server)
|
HandleServer(*Server)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChannelCommand interface {
|
||||||
|
Command
|
||||||
|
HandleChannel(channel *Channel)
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// structs
|
// structs
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user