func main() { name := os.Getenv("GOCHATBOT_NAME") if name == "" { name = "gochatbot" } provider := providers.Detect(os.Getenv) if err := provider.Error(); err != nil { log.SetOutput(os.Stderr) log.Fatalln("error in message provider:", err) } memory := brain.Detect(os.Getenv) if err := memory.Error(); err != nil { log.SetOutput(os.Stderr) log.Fatalln("error in brain memory:", err) } wd, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { log.Fatalln("error detecting working directory:", err) } options := []bot.Option{ bot.MessageProvider(provider), bot.RegisterRuleset(regex.New(regexRules)), bot.RegisterRuleset(cron.New(cronRules)), bot.RegisterRuleset(plugins.New(wd)), } rpcHostAddr := os.Getenv("GOCHATBOT_RPC_BIND") if rpcHostAddr != "" { l, err := net.Listen("tcp4", rpcHostAddr) if err != nil { log.Fatalf("rpc: cannot bind. err: %v", err) } options = append( options, bot.RegisterRuleset(rpc.New(l)), ) } bot.New( name, memory, options..., ).Process() }
// Boot runs preparatory steps for ruleset execution func (r *pluginRuleset) Boot(self *bot.Self) { for _, pluginBin := range r.pluginBins { l, err := net.Listen("tcp4", "0.0.0.0:0") if err != nil { log.Println("could not start plugin %s. error while setting listener: %v", pluginBin, err) continue } log.Printf("plugin: starting %s", pluginBin) rs := rpc.New(l) rs.Boot(self) cmd := exec.Command(pluginBin) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, fmt.Sprintf("GOCHATBOT_RPC_BIND=%s", l.Addr())) cmd.Env = append(cmd.Env, fmt.Sprintf("GOCHATBOT_NAME=%s", self.Name())) cmd.Stderr = os.Stderr if err := cmd.Start(); err != nil { log.Printf("plugin: %s - error: %v", pluginBin, err) log.Printf("plugin: %s closing listener.", pluginBin) l.Close() continue } r.plugins = append(r.plugins, rs) } }
func main() { var wg sync.WaitGroup var botCount int wd, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { log.Fatalln("plugins: error detecting working directory:", err) } for { e := &envGet{botCount} if e.getenv("GOCHATBOT_NAME") == "" { break } wg.Add(1) go func(e *envGet) { name := e.getenv("GOCHATBOT_NAME") if name == "" { name = "gochatbot" } provider := providers.Detect(e.getenv) if err := provider.Error(); err != nil { log.SetOutput(os.Stderr) log.Fatalln("error in message provider:", err) } memory := brain.Detect(e.getenv) if err := memory.Error(); err != nil { log.SetOutput(os.Stderr) log.Fatalln("error in brain memory:", err) } options := []bot.Option{ bot.MessageProvider(provider), bot.RegisterRuleset(regex.New(regexRules)), bot.RegisterRuleset(cron.New(cronRules)), bot.RegisterRuleset(plugins.New(wd)), } rpcHostAddr := e.getenv("GOCHATBOT_RPC_BIND") if rpcHostAddr != "" { l, err := net.Listen("tcp4", rpcHostAddr) if err != nil { log.Fatalf("rpc: cannot bind. err: %v", err) } options = append( options, bot.RegisterRuleset(rpc.New(l)), ) } bot.New( name, memory, options..., ).Process() wg.Done() }(e) botCount++ } wg.Wait() }