ircd/channel.go

159 lines
3.3 KiB
Go
Raw Normal View History

2014-11-22 13:21:30 +00:00
package ircd
import (
"time"
)
type ChangedByTS struct {
By string
Time int64
}
type ChannelModes string
type Channel struct {
Name string
server *Server
topic string
topicChanged ChangedByTS
flags string
keys map[int]string
args map[int]string
bans map[int]string
receive chan *Message
destroy chan bool
invites map[string]bool
clients map[string]ChannelModes
}
func NewChannel(srv *Server, name string) *Channel {
ch := &Channel{Name: name, server: srv, topic: "", flags: ""}
ch.receive = make(chan *Message, 1024)
ch.destroy = make(chan bool)
ch.clients = make(map[string]ChannelModes)
go ch.loop()
return ch
}
func (ch *Channel) Receive(msg *Message) {
ch.receive <- msg
}
func (ch *Channel) loop() {
for {
time.Sleep(1e6)
select {
case msg := <-ch.receive:
cmd := msg.Cmd
hook, exists := chCommandHooks[cmd]
if !exists {
ch.server.sendCommand(msg.Src, ERR_UNKNOWNCOMMAND, cmd,
"Unknown command.")
return
}
argc := len(msg.Args)
if argc < hook.MinArgs {
ch.server.sendCommand(msg.Src, ERR_NEEDMOREPARAMS, cmd,
"Not enough parameters.")
return
}
hook.HookFn(ch, msg)
case <-ch.destroy:
break
default:
continue
}
}
}
func (ch *Channel) recvMsg(msg *Message) {
}
func (ch *Channel) sendMsg(msg *Message) {
ch.server.sendMsg(msg)
}
func (ch *Channel) bcMsg(msg *Message, localEcho bool) {
msg.Ctx = ch.Name
for client, _ := range ch.clients {
if client != msg.Src || localEcho {
msg.Dst = client
ch.server.sendMsgToClient(msg)
}
}
}
func (ch *Channel) AddMode(mode string) {
//
}
type ChCommandHook struct {
HookFn func(ch *Channel, msg *Message)
MinArgs int
NeedOper bool
NeedAuth bool
}
var chCommandHooks = map[string]ChCommandHook{
CMD_QUIT: {chHandleCmdQuit, 0, false, false},
CMD_JOIN: {chHandleCmdJoin, 0, false, false},
CMD_PART: {chHandleCmdPart, 0, false, false},
CMD_MODE: {chHandleCmdMode, 0, false, false},
CMD_TOPIC: {chHandleCmdTopic, 0, false, false},
CMD_NAMES: {chHandleCmdNames, 0, false, false},
CMD_LIST: {chHandleCmdList, 0, false, false},
CMD_INVITE: {chHandleCmdInvite, 0, false, false},
CMD_KICK: {chHandleCmdKick, 0, false, false},
CMD_PRIVMSG: {chHandleCmdPrivmsg, 0, false, false},
CMD_NOTICE: {chHandleCmdNotice, 0, false, false},
CMD_USERS: {chHandleCmdUsers, 0, false, false},
}
func chHandleCmdQuit(ch *Channel, msg *Message) {
ch.bcMsg(M(msg.Src, "", msg.Ctx, "QUIT", "", ""), true)
delete(ch.clients, msg.Src)
}
func chHandleCmdJoin(ch *Channel, msg *Message) {
ch.clients[msg.Src] = ""
ch.bcMsg(M(msg.Src, "", msg.Ctx, "JOIN", "", ""), true)
}
func chHandleCmdPart(ch *Channel, msg *Message) {
ch.bcMsg(M(msg.Src, "", msg.Ctx, "PART", "", ""), true)
delete(ch.clients, msg.Src)
}
func chHandleCmdMode(ch *Channel, msg *Message) {
}
func chHandleCmdTopic(ch *Channel, msg *Message) {
}
func chHandleCmdNames(ch *Channel, msg *Message) {
}
func chHandleCmdList(ch *Channel, msg *Message) {
}
func chHandleCmdInvite(ch *Channel, msg *Message) {
}
func chHandleCmdKick(ch *Channel, msg *Message) {
}
func chHandleCmdPrivmsg(ch *Channel, msg *Message) {
ch.bcMsg(msg, false)
}
func chHandleCmdNotice(ch *Channel, msg *Message) {
}
func chHandleCmdUsers(ch *Channel, msg *Message) {
}
// vi:ts=4:sw=4:et