commit
0567c0ee53
@ -24,6 +24,7 @@ type ServerConnection struct {
|
||||
Connected bool
|
||||
Registered bool
|
||||
Casemapping ircmap.MappingType
|
||||
CommandPrefixes []string
|
||||
|
||||
// internal stuff
|
||||
RawConnection net.Conn
|
||||
@ -176,6 +177,10 @@ func (sc *ServerConnection) ProcessIncomingLine(line string) {
|
||||
|
||||
// IRC commands are case-insensitive
|
||||
sc.dispatchIn(strings.ToUpper(cmd), info)
|
||||
if strings.ToUpper(cmd) == "PRIVMSG" {
|
||||
sc.dispatchCommand(info)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (sc *ServerConnection) Shutdown(message string) {
|
||||
sc.Send(nil, "", "QUIT", message)
|
||||
@ -273,6 +283,22 @@ func (sc *ServerConnection) Send(tags map[string]string, prefix string, command
|
||||
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.
|
||||
func (sc *ServerConnection) dispatchRawIn(info eventmgr.InfoMap) {
|
||||
sc.eventsIn.Dispatch("raw", info)
|
||||
|
Loading…
Reference in New Issue
Block a user