// handleMessage gets messages received on the IRC network and parses them to recognize commands. func (a *Amigo) handleMessage(msg *irc.Message) { // Handle panics defer func() { if r := recover(); r != nil { log.Println("!!! I'm panicking !!! ", r) } }() // Log message log.Println(msg.String()) // Handle PING if msg.Command == "PING" { a.Send("PONG :" + msg.Trailing) } // Handle message if msg.Command == "PRIVMSG" { // Are you talking to me? if strings.HasPrefix(msg.Trailing, a.Nick) { a.handleCommand(msg) } else { // Free talk ('say when', 'cmd when') a.handleConversation(msg) } } }
func (server *Server) NewMessage(msg *irc.Message, serverAddr string) (string, error) { message := &Message{ Message: msg.String(), Server: serverAddr, } serialized, err := json.Marshal(message) return string(serialized), err }
func MsgHandler(s ircx.Sender, m *irc.Message) { message := strings.Split(m.String(), " ") user := strings.Split(message[0], "!")[0][1:] err := db.Update(func(tx *bolt.Tx) error { words := make(map[string]uint64) message[3] = message[3][1:] for _, element := range message[3:] { words[element]++ } b := tx.Bucket([]byte(*channel)) bu := b.Bucket([]byte(users)) bm := b.Bucket([]byte(messages)) v := bu.Get([]byte(user)) var count uint64 = 0 if v != nil { count = ByteToUint64(v) } count = count + uint64(len(message[3:])) err := bu.Put([]byte(user), Uint64ToByte(count)) if err != nil { return err } for word, count := range words { v := bm.Get([]byte(word)) var sum uint64 = 0 if v != nil { sum = ByteToUint64(v) } sum = sum + count err := bm.Put([]byte(word), Uint64ToByte(sum)) if err != nil { return err } } return nil }) if err != nil { log.Fatal(err) } else { log.Println(user + " said " + strings.Join(message[3:], " ")) } }
func (logger *Logger) logRaw(msg *irc.Message, server string) { timestamp := time.Now().Format(time.RFC3339) entry := &rawLog{ Server: server, TimeStamp: timestamp, Message: msg.String(), } _, err := rdb.DB("bot").Table("raw_logs").Insert(entry).RunWrite(logger.DbSession) if err != nil { fmt.Println(err) return } }
func (sender ServerSender) Send(msg *irc.Message) error { fmt.Printf(">> %s\n", msg.String()) return sender.writer.Encode(msg) }
// SendStruct sends a message. func (b *Bot) SendStruct(msg *irc.Message) { b.SendString(msg.String()) }
func (net *netImpl) rawHandler(evt *msg.Message) { // libmauirc adds the trailing text as a param, remove it. evt.Params = evt.Params[:len(evt.Params)-1] net.Owner.SendMessage(messages.Container{Type: messages.MsgRaw, Object: messages.RawMessage{Network: net.GetName(), Message: evt.String()}}) }
func (server *Server) sendMessage(msg *irc.Message) error { if msg.Command != irc.PRIVMSG { fmt.Printf(">> %s\n", msg.String()) } return server.Writer.Encode(msg) }
func (channel *IrcChannel) handlePrivMsg(msg *irc.Message) { //fmt.Println(msg) fmt_msg := new(TwitchChat) fmt_msg.raw = msg.String() // Parse the tags out of the PRIVMSG for use in the front end // Parse usertype reUserType, err := regexp.Compile(`user-type\=(.*?)(\;|\s)`) if err != nil { log.Print("Could not parse UserType\n") } fmt_msg.usertype = reUserType.FindStringSubmatch(fmt_msg.raw) // Parse subscriber reSub, err := regexp.Compile(`subscriber\=(.*?)(\;|\s)`) if err != nil { log.Print("Could not parse Subscriber\n") } fmt_msg.sub = reSub.FindStringSubmatch(fmt_msg.raw) // Parse turbo reTurbo, err := regexp.Compile(`turbo\=(.*?)(\;|\s)`) if err != nil { log.Print("Could not parse Turbo\n") } fmt_msg.turbo = reTurbo.FindStringSubmatch(fmt_msg.raw) // Parse display name reDisp, err := regexp.Compile(`display-name\=(.*?)(\;|\s)`) if err != nil { log.Print("Could not parse DisplayName\n") } fmt_msg.disp_name = reDisp.FindStringSubmatch(fmt_msg.raw) // Parse color tag reColor, err := regexp.Compile(`#[[:xdigit:]]{6}`) if err != nil { log.Print("Could not parse Color\n") } fmt_msg.color = reColor.FindStringSubmatch(fmt_msg.raw) if len(fmt_msg.color) == 1 && len(fmt_msg.disp_name) >= 1 && len(fmt_msg.sub) >= 1 && len(fmt_msg.turbo) >= 1 && len(fmt_msg.usertype) >= 1 { // User has all fields (mod or staff) // <a href='https://www.twitch.tv/" + msg.Prefix.Name + "/profile' target='_blank'><strong>" + fmt_msg.disp_name[1] + "</strong></a> channel.ReadFromChannel <- []byte("<span data-usertype='" + fmt_msg.usertype[1] + "' data-sub='" + fmt_msg.sub[1] + "' data-turbo='" + fmt_msg.turbo[1] + "' style='color:" + fmt_msg.color[0] + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else if len(fmt_msg.color) == 1 && len(fmt_msg.disp_name) >= 1 && len(fmt_msg.sub) >= 1 && len(fmt_msg.turbo) >= 1 { // User is missing user-type tag (non-mod) channel.ReadFromChannel <- []byte("<span data-sub='" + fmt_msg.sub[1] + "' data-turbo='" + fmt_msg.turbo[1] + "' style='color:" + fmt_msg.color[0] + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else if len(fmt_msg.color) == 1 && len(fmt_msg.disp_name) >= 1 { // User is missing user-type, subscriber, and turbo tags (rare) channel.ReadFromChannel <- []byte("<span data-sub='0' data-turbo='0' style='color:" + fmt_msg.color[0] + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else if len(fmt_msg.color) == 1 { // User is bot (or not authenticated) channel.ReadFromChannel <- []byte("<span style='color:" + fmt_msg.color[0] + "' id='username'><strong>" + msg.Prefix.Name + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else { // Randomize colors if the user has never set them before rand.Seed(time.Now().UTC().UnixNano()) colors := []string{ "#FF0000", "#0000FF", "#008000", "#B22222", "#FF7F50", "#9ACD32", "#FF4500", "#2E8B57", "#DAA520", "#D2691E", "#5F9EA0", "#1E90FF", "#FF69B4", "#8A2BE2", "#00FF7F", } /* Map colors to the name, broken for some reason color, ok := fmt_msg.colorMap[msg.Prefix.Name] if !ok { color = colors[rand.Intn(len(colors))] fmt_msg.colorMap[msg.Prefix.Name] = color }*/ color := colors[rand.Intn(len(colors))] if len(fmt_msg.disp_name) >= 1 && len(fmt_msg.sub) >= 1 && len(fmt_msg.turbo) >= 1 && len(fmt_msg.usertype) >= 1 { // User has all fields (mod or staff) channel.ReadFromChannel <- []byte("<span data-usertype='" + fmt_msg.usertype[1] + "' data-sub='" + fmt_msg.sub[1] + "' data-turbo='" + fmt_msg.turbo[1] + "' style='color:" + color + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else if len(fmt_msg.disp_name) >= 1 && len(fmt_msg.sub) >= 1 && len(fmt_msg.turbo) >= 1 { // User is missing user-type tag (non-mod) channel.ReadFromChannel <- []byte("<span data-sub='" + fmt_msg.sub[1] + "' data-turbo='" + fmt_msg.turbo[1] + "' style='color:" + color + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } else if len(fmt_msg.disp_name) >= 1 { // User is missing user-type, subscriber, and turbo tags (rare) channel.ReadFromChannel <- []byte("<span data-sub='0' data-turbo='0' style='color:" + color + "' id='username'><strong>" + fmt_msg.disp_name[1] + "</strong></span><span id='text'>: " + html.EscapeString(msg.Trailing) + " </span>") } } }
func (sender ServerSender) Send(msg *irc.Message, server string) { fmt.Printf(">> %s\n", msg.String()) sender.r.Publish(server, msg.String()) }