Esempio n. 1
0
func (c *Command) processArgs(ib *irc.Connection, to string) {
	cnf := configuration.Config
	if len(c.args) == 0 {
		c.Start()
		return
	}
	switch c.args[0] {
	case "save":
		if err := c.save(); err != nil {
			ib.Privmsg(to, "Error while saving configuration. Consult the logs.")
		} else {
			ib.Privmsg(to, "Configuration saved")
		}
		c.Start()
		return
	case "admins":
		if len(c.args) == 1 {
			ib.Privmsgf(to, "Admins : %v", cnf.Admins)
		} else {
			if c.admins() {
				ib.Privmsg(to, "Admins configuration changed")
			}
		}
		c.Start()
		return
	case "reset":
		// Can be done in a smarter way, we should copy the slices from configuration
		// and stop/start only those different from the default config
		plugins.Stop()
		configuration.Load()
		plugins.Start()
		ib.Privmsg(to, "Configuration was reseted")
		return
	case "plugins":
		if len(c.args) == 1 {
			list := list()
			ib.Privmsgf(to, "Commands     : %s", list[0])
			ib.Privmsgf(to, "Middlewares : %s", list[1])
			c.Start()
			return
		}
		if c.plugins() {
			ib.Privmsg(to, "Commands configuration changed")
		}
	}
	c.Start()
}
Esempio n. 2
0
func main() {
	var err error

	cnf := configuration.Config

	// Argument parsing
	configuration.ConfPath = flag.String("c", "conf.yml", "Local path to configuration file.")
	flag.Parse()

	// Load the configuration of the bot
	configuration.Load()

	// Storage initialization
	if err := database.BotStorage.Open(); err != nil {
		log.Fatalf("something went wrong with the databse : %v", err)
	}

	plugins.Start()

	// Defer all Close/Stop methods
	defer database.BotStorage.Close()
	defer plugins.Stop()

	// Bot initialization
	ib := irc.IRC(cnf.BotName, cnf.BotName)
	if cnf.TLS {
		ib.UseTLS = true
		if cnf.InsecureTLS {
			ib.TLSConfig = &tls.Config{InsecureSkipVerify: true}
		}
	}
	if err = ib.Connect(cnf.Server); err != nil {
		log.Fatal(err)
	}

	// Callback on 'Connected' event
	ib.AddCallback("001", func(e *irc.Event) {
		ib.Join(cnf.Channel)
	})

	// Callback on 'Message' event
	ib.AddCallback("PRIVMSG", func(e *irc.Event) {
		from := e.Nick
		to := e.Arguments[0]
		m := e.Message()

		for _, c := range plugins.Middlewares {
			c.Get(ib, from, to, m)
		}

		if strings.HasPrefix(m, cnf.CommandCharacter) {
			if len(m) > 1 {
				splitted := strings.Fields(m[1:])
				command := splitted[0]
				args := splitted[1:]
				if p, ok := plugins.Commands[command]; ok {
					p.Get(ib, from, to, args)
				}
			}
		}
	})
	ib.Loop()
}