commit
0567c0ee53
@ -20,10 +20,11 @@ import (
|
|||||||
|
|
||||||
// ServerConnection is a connection to a single server.
|
// ServerConnection is a connection to a single server.
|
||||||
type ServerConnection struct {
|
type ServerConnection struct {
|
||||||
Name string
|
Name string
|
||||||
Connected bool
|
Connected bool
|
||||||
Registered bool
|
Registered bool
|
||||||
Casemapping ircmap.MappingType
|
Casemapping ircmap.MappingType
|
||||||
|
CommandPrefixes []string
|
||||||
|
|
||||||
// internal stuff
|
// internal stuff
|
||||||
RawConnection net.Conn
|
RawConnection net.Conn
|
||||||
@ -176,6 +177,10 @@ func (sc *ServerConnection) ProcessIncomingLine(line string) {
|
|||||||
|
|
||||||
// IRC commands are case-insensitive
|
// IRC commands are case-insensitive
|
||||||
sc.dispatchIn(strings.ToUpper(cmd), info)
|
sc.dispatchIn(strings.ToUpper(cmd), info)
|
||||||
|
if strings.ToUpper(cmd) == "PRIVMSG" {
|
||||||
|
sc.dispatchCommand(info)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect closes the IRC socket.
|
// Disconnect closes the IRC socket.
|
||||||
@ -222,6 +227,11 @@ func (sc *ServerConnection) RegisterEvent(direction string, name string, handler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterCommand registers a command to be called via the configured prefix or the client's nickname (e.g !help, "GoshuBot: help")
|
||||||
|
func (sc *ServerConnection) RegisterCommand(name string, handler eventmgr.HandlerFn, priority int) {
|
||||||
|
sc.eventsIn.Attach("cmd_"+name, handler, priority)
|
||||||
|
}
|
||||||
|
|
||||||
// Shutdown closes the connection to the server.
|
// Shutdown closes the connection to the server.
|
||||||
func (sc *ServerConnection) Shutdown(message string) {
|
func (sc *ServerConnection) Shutdown(message string) {
|
||||||
sc.Send(nil, "", "QUIT", message)
|
sc.Send(nil, "", "QUIT", message)
|
||||||
@ -273,6 +283,22 @@ func (sc *ServerConnection) Send(tags map[string]string, prefix string, command
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dispatchCommand dispatches an event based on simple commands (e.g !help)
|
||||||
|
func (sc *ServerConnection) dispatchCommand(info eventmgr.InfoMap) {
|
||||||
|
params := strings.Fields(info["params"].([]string)[1])
|
||||||
|
|
||||||
|
for _, p := range sc.CommandPrefixes {
|
||||||
|
if strings.HasPrefix(params[0], p) {
|
||||||
|
sc.eventsIn.Dispatch("cmd_"+params[0][1:], info)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params[0] == sc.Nick || params[0] == sc.Nick+":") && len(params) > 1 {
|
||||||
|
sc.eventsIn.Dispatch("cmd_"+params[1], info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// dispatchRawIn dispatches raw inbound messages.
|
// dispatchRawIn dispatches raw inbound messages.
|
||||||
func (sc *ServerConnection) dispatchRawIn(info eventmgr.InfoMap) {
|
func (sc *ServerConnection) dispatchRawIn(info eventmgr.InfoMap) {
|
||||||
sc.eventsIn.Dispatch("raw", info)
|
sc.eventsIn.Dispatch("raw", info)
|
||||||
|
Loading…
Reference in New Issue
Block a user