예제 #1
0
파일: ircbot.go 프로젝트: aimless/g0
func (b *Bot) Run() {
	log.Println("Bot is underway")
	for _, i := range b.Connections {
		i.Connection = qairc.QAIrc(b.Nickname, b.Realname)
		i.Connection.UseTLS = useCrypto
		i.Connection.TLSCfg = &tls.Config{InsecureSkipVerify: true}
		i.Connection.Address = i.Address
		err := i.Connection.Run()
		if err != nil {
			log.Println(err.Error())
		}

		go func() {
			for {
				select {
				case msg := <-i.Connection.Out:
					switch msg.Type {
					case "005":
						for _, j := range strings.Fields(msg.Raw) {
							if len(j) > 7 && j[:7] == "NETWORK" {
								i.Network = j[8:]
								log.Println("Network name is: " + i.Network)
							}
						}
					case "001":
						for _, j := range i.Channels {
							log.Println("Joining: " + j + " on " + i.Address)
							i.Connection.Join(j)
						}
					case "PRIVMSG":
						parseIrcMsg(msg, b, &i)
					}
				case resp := <-b.SendChannel:
					for _, c := range b.Connections {
						if c.Network == resp.Network {
							for _, ch := range c.Channels {
								if ch == resp.Channel {
									c.Connection.Privmsg(resp.Channel, resp.Text)
								}
							}
						}
					}
				}
			}
		}()
	}
}
예제 #2
0
파일: holla.go 프로젝트: aimless/Holla
func worker(kind string, address string) {
	irc := qairc.QAIrc("holla", "holla")
	store := make([]qairc.Message, 0)
	posmap := map[string]int{}
	position := 0
	exit := make(chan interface{}, 1)
	rqch := make(chan ReqRep, 5)

	resetkey := func(name string) {
		if len(name) > 0 {
			posmap[name] = 0
		} else {
			position = 0
		}
	}

	retrievekey := func(name string) (r qairc.Message) {
		if len(name) > 0 {
			if _, ok := posmap[name]; !ok {
				posmap[name] = 0
			}
			r = store[posmap[name]]
			posmap[name]++
		} else {
			r = store[position]
			position++
		}
		return
	}

	lenkey := func(name string) bool {
		if len(name) > 0 {
			if _, ok := posmap[name]; !ok {
				posmap[name] = 0
			}
			return posmap[name] < len(store)-1
		} else {
			return position < len(store)-1
		}
	}

	cmds := map[string]func(data string) string{
		"CONN": func(data string) string {
			irc.Address = data
			err := irc.Run()
			if err != nil {
				return "!ERROR: " + err.Error()
			} else {
				return "OK"
			}
		},
		"RETR": func(data string) string {
			if !lenkey(data) {
				return "!ERROR: END REACHED"
			}
			rmsg := retrievekey(data).Raw
			return rmsg
		},
		"RETJ": func(data string) string {
			if !lenkey(data) {
				return "!ERROR: END REACHED"
			}
			rmsg, err := json.Marshal(retrievekey(data))
			if err != nil {
				return "!ERROR: " + err.Error()
			}
			return string(rmsg)
		},
		"SEND": func(data string) string {
			irc.SendRaw(data)
			return "OK"
		},
		"RSET": func(data string) string {
			resetkey(data)
			return "OK"
		},
		"DISC": func(data string) string {
			irc.Stop()
			return "OK"
		},
		"EXIT": func(data string) string {
			exit <- true
			return "OK"
		},
	}

	l, _ := net.Listen(kind, address)
	go lazyserv.Serve(l, func(s string) string {
		replych := make(chan string, 1)
		rqch <- ReqRep{s, replych}
		return <-replych
	})

	for {
		select {
		case <-exit:
			fmt.Println("Exiting...")
			return
		case m := <-irc.Out:
			store = append(store, m)
		case s := <-rqch:
			cmd := s.Command[0:4]
			if _, ok := cmds[cmd]; ok {
				reply := cmds[cmd](s.Command[4:len(s.Command)-1]) + "\n"
				s.ReplyCh <- reply
			} else {
				s.ReplyCh <- "UNRECOGNIZED\n"
			}
		}
	}
}