Beispiel #1
0
// Reply to the current message. Depending on the origin it will either be send in a
// query or to a channel
func (self *ModuleApi) Reply(srvMsg *irc.PrivateMessage, text string) irc.ClientMessage {
	msg := &irc.PrivateMessage{Text: text}

	if strings.HasPrefix(srvMsg.Target, "#") || strings.HasPrefix(srvMsg.Target, "&") {
		msg.Target = srvMsg.Target
	} else {
		msg.Target = strings.Split(srvMsg.From(), "!")[0]
	}
	return msg
}
Beispiel #2
0
func (self *DefaultModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	switch cmd {
	case "version":
		c <- self.Reply(srvMsg, config.Version)
	case "contributors":
		c <- self.Reply(srvMsg, "Contributors: AlphaBernd, Rosenmann")
	case "whoami":
		c <- self.Reply(srvMsg, srvMsg.From())
	case "help":
		prefix, err := self.GetConfigStringValue("CmdPrefix")
		if err != nil {
			return
		}
		if len(params) == 0 {
			msg := fmt.Sprintf("Type %vhelp [COMMAND] for more - Command list:", prefix)

			for i := range self.comex {
				cmd := self.comex[i].GetCommands()
				for key := range cmd {
					msg += fmt.Sprintf(" %v%v", prefix, key)
				}
			}

			cmsg := self.ReplyMultiLine(srvMsg, msg)
			for _, v := range cmsg {
				c <- v
			}
		} else {
			for i := range self.comex {
				cmd := self.comex[i].GetCommands()

				for key := range cmd {
					if params[0] == key {
						c <- self.Reply(srvMsg, fmt.Sprintf("%v%v %v", prefix, key, cmd[key]))
					}
				}
			}
		}
	}
}
Beispiel #3
0
func (self *RSSModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	feeds, err := self.GetConfigMapValue("feeds")
	if err == nil {
		if url, ok := feeds[cmd]; ok {
			cmd = "rss"
			params = []string{url}
		}
	}

	switch cmd {

	case "news":
		if len(params) == 0 {
			url, err := self.GetConfigStringValue("news.url")
			if err != nil {
				return
			}
			self.ExecuteCommand("rss", []string{url}, srvMsg, c)
		} else {
			msg := strings.Join(params, " ")
			author := strings.Split(srvMsg.From(), "!")[0]

			path, err := self.GetConfigStringValue("news.file")
			if err != nil {
				return
			}

			feed, err2 := rss.ParseFromFile(path)

			if err2 != nil {
				feed = rss.New("News", "", "")
			}
			feed.AddItem(msg, "", "", author, "")

			if err := feed.WriteToFile(path); err != nil {
				log.Printf("%v", err)
			}
			c <- self.Reply(srvMsg, "success")
		}
	case "news.setFile":
		if !self.IsIdentified(srvMsg.From()) {
			return
		}

		if len(params) == 0 {
			return
		}

		if err := self.SetConfigValue("news.file", params[0]); err != nil {
			return
		}

		c <- self.Reply(srvMsg, "success")
	case "news.setUrl":
		if !self.IsIdentified(srvMsg.From()) {
			return
		}

		if len(params) == 0 {
			return
		}

		if err := self.SetConfigValue("news.url", params[0]); err != nil {
			return
		}
		c <- self.Reply(srvMsg, "success")
	case "rss.add":
		if !self.IsIdentified(srvMsg.From()) {
			return
		}

		if len(params) < 2 {
			return
		}

		if !strings.HasPrefix(params[1], "http://") {
			return
		}

		if params[0] == "rss" || params[0] == "rss.add" || params[0] == "rss.del" || params[0] == "rss.list" || params[0] == "news" || params[0] == "news.setFile" || params[0] == "news.setUrl" {
			return
		}

		feeds, err := self.GetConfigMapValue("feeds")
		if err != nil {
			return
		}
		feeds[params[0]] = params[1]

		if err := self.SetConfigValue("feeds", feeds); err != nil {
			return
		}
		c <- self.Reply(srvMsg, "success")
	case "rss.del":
		if !self.IsIdentified(srvMsg.From()) {
			return
		}

		if len(params) < 1 {
			return
		}

		if err := self.DeleteConfigValue(params[0]); err != nil {
			log.Printf("%v", err)
		}

		c <- self.Reply(srvMsg, "success")
	case "rss.list":
		keys := self.GetConfigKeys()

		for i := range keys {
			if url, err := self.GetConfigStringValue(keys[i]); err == nil {
				c <- self.Reply(srvMsg, fmt.Sprintf("%v - %v", keys[i], url))
			}
		}

	case "rss":
		if len(params) > 0 {

			rss, err := rss.ParseFromUrl(params[0])
			if err != nil {
				log.Printf("%v", err)
				return
			}

			for i := range rss.Channel {
				c <- self.Reply(srvMsg, rss.Channel[i].Title)
				for j := range rss.Channel[i].Item {
					if j >= 5 {
						return
					}
					if rss.Channel[i].Item[j].Author == "" {
						c <- self.Reply(srvMsg, fmt.Sprintf("Item[%v]: %v", j, rss.Channel[i].Item[j].Title))
					} else {
						c <- self.Reply(srvMsg, fmt.Sprintf("Item[%v]: %v - %v", j, rss.Channel[i].Item[j].Title, rss.Channel[i].Item[j].Author))
					}
				}
			}
		}
	}
}
Beispiel #4
0
func (p *PushModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	if !p.IsIdentified(srvMsg.From()) {
		return
	}
	switch cmd {
	case "push":
		if len(params) == 0 {
			return
		}

		msg := strings.Join(params, " ")
		conf, err := p.GetConfigMapValue("pushover")
		if err != nil {
			log.Printf("PushModule: %v", err)
			return
		}

		user, ok := conf["user"]
		if !ok {
			log.Println("PushModule: Pushover user not set")
			return
		}

		token, ok := conf["token"]
		if !ok {
			log.Println("PushModule: Token user not set")
			return
		}

		pmsg := &push.PushoverMessage{Token: token, User: user, Message: msg}

		err = push.Pushover(pmsg)
		if err != nil {
			log.Printf("PushModuler: %v", err)
		}
	case "blog":
		if !p.IsIdentified(srvMsg.From()) {
			return
		}

		if len(params) == 0 {
			return
		}

		msg := strings.Join(params, " ")
		conf, err := p.GetConfigMapValue("blog")
		if err != nil {
			log.Printf("PushModule: %v", err)
			return
		}

		u, ok := conf["url"]
		if !ok {
			log.Println("PushModule: Blog url not set")
			return
		}

		pw, ok := conf["password"]
		if !ok {
			log.Println("PushModule: Blog password not set")
			return
		}

		title, ok := conf["title"]
		if !ok {
			log.Println("PushModule: Blog title not set")
			return
		}

		res, err := http.PostForm(u, url.Values{"a": {pw}, "method": {"add"}, "title": {title}, "text": {msg}})
		if err != nil {
			return
		}
		defer res.Body.Close()

		if res.StatusCode == 200 {
			c <- p.Reply(srvMsg, "Success")
		}
	}
}
Beispiel #5
0
func (self *AdminModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	switch cmd {
	case "identify":
		if self.IsIdentified(srvMsg.From()) {
			c <- self.Privmsg(srvMsg.From(), "Already identified")
		}

		if len(params) >= 1 {
			pw, _ := self.GetConfigStringValue("password")
			if params[0] == pw {
				self.AddIdentified(srvMsg.From())
				c <- self.Privmsg(srvMsg.From(), "success")
			}
		}
	case "nick":
		if self.IsIdentified(srvMsg.From()) {
			if len(params) >= 1 {
				c <- self.Nick(params[0])
			}
		}
	case "join":
		if self.IsIdentified(srvMsg.From()) {
			if len(params) >= 1 {
				for _, v := range params {
					if strings.HasPrefix(v, "&") || strings.HasPrefix(v, "#") {
						c <- self.Join(v)
					}
				}
			}
		}
	case "part":
		if self.IsIdentified(srvMsg.From()) {
			if len(params) >= 1 {
				for _, v := range params {
					if strings.HasPrefix(v, "&") || strings.HasPrefix(v, "#") {
						c <- self.Part(v)
					}
				}
			}
		}
	}
}
Beispiel #6
0
func (self *GameModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	switch cmd {
	case "morse":
		if len(params) == 0 {
			return
		}

		c <- self.Reply(srvMsg, crypto.Morse(strings.Join(params, " ")))
	case "dice":
		c <- self.Reply(srvMsg, strconv.Itoa(rand.Intn(6)+1))
	case "rot13":
		if len(params) == 0 {
			return
		}
		msg := strings.Join(params, " ")
		c <- self.Reply(srvMsg, crypto.Rot13(msg))
	case "roulette":
		ch := srvMsg.Target

		r, ok := self.rdb[ch]
		if !ok {
			r = &roulette{pos: rand.Intn(6), num: 0}
			self.rdb[ch] = r
		}

		if r.num == r.pos {
			c <- self.Reply(srvMsg, "You are dead. As agreed on in the TOS all your money will be transfered to the server owner")
			c <- self.Ban(srvMsg.Target, strings.Split(srvMsg.From(), "!")[0])
			c <- self.Kick(srvMsg.Target, strings.Split(srvMsg.From(), "!")[0], "Bye Bye")

			go func() {
				time.Sleep(5 * time.Minute)
				c <- self.Unban(srvMsg.Target, strings.Split(srvMsg.From(), "!")[0])
			}()

			r.num = 0
			r.pos = rand.Intn(6)
		} else {
			c <- self.Reply(srvMsg, "Lucky Bastard")
			r.num++
		}
	case "yn":
		answer := []string{"Yes", "No"}

		if len(params) == 0 {
			return
		}

		c <- self.Reply(srvMsg, answer[rand.Intn(2)])
	case "choice":
		if len(params) == 0 {
			return
		}
		c <- self.Reply(srvMsg, params[rand.Intn(len(params))])
	case "8ball":
		if len(params) == 0 {
			return
		}

		answers := []string{
			"Signs point to yes",
			"Yes",
			"Without a doubt",
			"As I see it, yes",
			"Most likely",
			"You may rely on it",
			"Yes definitely",
			"It is decidedly so",
			"Outlook good",
			"It is certain",
			"My sources say no",
			"Very doubtful",
			"Don't count on it",
			"Outlook not so good",
			"My reply is no",
			"Reply hazy, try again",
			"Concentrate and ask again",
			"Better not tell you now",
			"Cannot predict now",
			"Ask again later"}

		c <- self.Reply(srvMsg, answers[rand.Intn(len(answers))])
	}
}
Beispiel #7
0
func (self *BrainfuckModule) ExecuteCommand(cmd string, params []string, srvMsg *irc.PrivateMessage, c chan irc.ClientMessage) {
	user := strings.Split(srvMsg.From(), "!")[0]

	switch cmd {
	case "bf":
		if len(params) == 0 {
			return
		}

		source := params[0]
		input := ""
		vars, _ := self.GetConfigMapValue("var")

		if len(params) > 1 {
			input = strings.Join(params[1:], " ")
		}

		bf := brainfuck.NewBrainfuckInterpreter(self.replaceVariables(source, vars), input)
		output, err := bf.Start()

		pos, mem := bf.DumpMemory()
		c <- self.Reply(srvMsg, fmt.Sprintf("Pointer: %v; Dump: %v", pos, mem))

		if err != nil {
			c <- self.Reply(srvMsg, fmt.Sprintf("Error: %v", err.Error()))
			return
		}

		if len(output) > 0 {
			msg := ""
			for i := range output {
				if output[i] == 0 || output[i] == 10 || output[i] == 13 {
					msg += fmt.Sprintf("%q", output[i])
				} else {
					msg += fmt.Sprintf("%c", output[i])
				}
			}
			c <- self.Reply(srvMsg, msg)
		}
	case "bf.add":
		if len(params) == 0 {
			return
		}
		cache, _ := self.GetConfigMapValue("cache")
		defer self.SetConfigValue("cache", cache)

		cache[user] = cache[user] + params[0]
		c <- self.Reply(srvMsg, "success")
	case "bf.prt":
		cache, _ := self.GetConfigMapValue("cache")

		c <- self.Reply(srvMsg, cache[user])
	case "bf.rst":
		cache, _ := self.GetConfigMapValue("cache")
		defer self.SetConfigValue("cache", cache)

		cache[user] = ""
		c <- self.Reply(srvMsg, "success")
	case "bf.exec":
		cache, _ := self.GetConfigMapValue("cache")
		vars, _ := self.GetConfigMapValue("var")
		input := ""
		if len(params) > 0 {
			input = strings.Join(params, " ")
		}

		bf := brainfuck.NewBrainfuckInterpreter(self.replaceVariables(cache[user], vars), input)
		output, err := bf.Start()

		pos, mem := bf.DumpMemory()
		c <- self.Reply(srvMsg, fmt.Sprintf("Pointer: %v; Dump: %v", pos, mem))

		if err != nil {
			c <- self.Reply(srvMsg, fmt.Sprintf("Error: %v", err.Error()))
			return
		}

		if len(output) > 0 {
			msg := ""
			for i := range output {
				if output[i] == 0 || output[i] == 10 || output[i] == 13 {
					msg += fmt.Sprintf("%q", output[i])
				} else {
					msg += fmt.Sprintf("%c", output[i])
				}
			}
			c <- self.Reply(srvMsg, msg)
		}
	case "bf.set":
		if len(params) < 2 {
			return
		}
		vars, _ := self.GetConfigMapValue("var")
		defer self.SetConfigValue("var", vars)

		vars[params[0]] = params[1]
		c <- self.Reply(srvMsg, "success")
	case "bf.del":
		if len(params) < 1 {
			return
		}
		vars, _ := self.GetConfigMapValue("var")
		defer self.SetConfigValue("var", vars)

		if _, ok := vars[params[0]]; ok {
			delete(vars, params[0])
		}
		c <- self.Reply(srvMsg, "success")
	case "bf.list":
		vars, _ := self.GetConfigMapValue("var")

		for k, v := range vars {
			c <- self.Reply(srvMsg, fmt.Sprintf("Name: %v; %v", k, v))
		}
	}
}