// RemoveModule will first remove all commands and listeners of a module from the handler. It will // then remove its RPC client. func (h *Handler) RemoveModule(name ModuleName) { h.mu.Lock() defer h.mu.Unlock() // Let's make sure we haven't already removed the module, as removal can be initiated by // multiple sources. if !h.ModuleExists(string(name)) { // Our job here is/was done return } // Remove all commands for cmd, modname := range h.Commands { if modname == name { delete(h.Commands, cmd) } } // Remove all triggers for event, modnames := range h.Triggers { for i, modname := range modnames { if modname == name { rlog.Debug("here", "here") h.Triggers[event] = append(h.Triggers[event][:i], h.Triggers[event][i+1:]...) } } } // And finally remove from handler delete(h.Modules, name) }
// EnableModules goes through every module list found in the config and sets them up appropriately. func (b *Bot) EnableModules() { rlog.Info("Bot", "Enabling Modules...") b.Handler.AddRemoveCallback(func(name ModuleName) { b.Modules[strings.ToLower(string(name))].Registered = false }) for modtype, modules := range b.Config.Module.Modules { for name, value := range modules { route, optionsArray := b.Parser.ParseModuleValue(value) optionsMap := make(map[string]bool) for _, opt := range optionsArray { optionsMap[opt] = true } if route == "." { route = DefaultModulesRoute + modtype } b.Modules[strings.ToLower(name)] = NewModule(name, route, optionsMap, modtype) rlog.Debug("Bot", "Module "+name+" Created") } } b.loadModules() }
// Register registers a module with the bot. With the given port number in the Ticket, the bot // creates a new rpc provider client connection to the module. The module is kept in the handler // for event dispatching and module management. func (b BotAPI) Register(t Ticket, result *string) error { rlog.Debug("Bot", "Starting registration for "+t.ModuleName+" [Module Client]") client, err := RpcCodecClientWithPort(t.Port) if err != nil { rlog.Error("Bot", "Could not establish an RPC client: "+err.Error()) return err } module := rpc.NewClientWithCodec(client) if module == nil { rlog.Warn("Bot", "Could not register:"+t.ModuleName) return errors.New("Failed to regsiter module") } b.bot.Handler.AddModule(ModuleName(strings.ToLower(t.ModuleName)), module) b.bot.Modules[strings.ToLower(t.ModuleName)].Registered = true rlog.Debug("Bot", "Registered "+t.ModuleName+" on port "+t.Port) return nil }
// RegisterCommand registers a command from a module with the handler. The command request // holds the command's name and the module it belongs to (used to signal the module to fire the // command). func (b BotAPI) RegisterCommand(cr CommandRequest, result *string) error { b.bot.Handler.AddCommand(CommandName(cr.CommandName), ModuleName(cr.ModuleName)) rlog.Debug("Bot", "Added: "+cr.CommandName+" for module: "+cr.ModuleName) return nil }