func (c *Client) commandSender(sender ircx.Sender, to string, enc bool) func(s string, a ...interface{}) { return func(s string, a ...interface{}) { if s == "" { return } msg := s if len(a) > 0 { msg = fmt.Sprintf(msg, a...) } if enc { if key, ok := c.state.Encryption().Check(to); ok { data, err := dh1080.Enc([]byte(msg), []byte(key)) if err == nil { msg = "+OK *" + base64.StdEncoding.EncodeToString(data) } } } newMsg := &irc.Message{ Command: irc.PRIVMSG, Params: []string{to}, Trailing: msg, } sender.Send(newMsg) } }
func PingHandler(s ircx.Sender, m *irc.Message) { s.Send(&irc.Message{ Command: irc.PONG, Params: m.Params, Trailing: m.Trailing, }) }
func RegisterConnect(s ircx.Sender, m *irc.Message) { for _, irc_chan := range config.General.Channels { s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{irc_chan.Name, irc_chan.Pass}, }) } }
func pingHandler(s ircx.Sender, m *irc.Message) { // special case, don't use message queue s.Send(&irc.Message{ Command: irc.PONG, Params: m.Params, Trailing: m.Trailing, }) }
func RegisterConnect(s ircx.Sender, m *irc.Message) { channel := fmt.Sprintf("#%s", *user) fmt.Println("Connected, joining", channel, "...") s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{channel}, }) }
func RegisterConnect(s ircx.Sender, m *irc.Message) { log.Println(*user + " joining channel " + *channel) s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{*channel}, }) }
func (c *Client) nickTakenHandler(s ircx.Sender, m *irc.Message) { newName := fmt.Sprintf("%s|", c.state.Name()) msg := &irc.Message{ Command: irc.NICK, Params: []string{newName}, } c.state.SetName(newName) s.Send(msg) }
func onPing(s ircx.Sender, m *irc.Message) { err := s.Send(&irc.Message{ Command: irc.PONG, Params: m.Params, Trailing: m.Trailing, }) if err != nil { log.Printf("Could not reply to PING: %v", err) } }
func onWelcome(s ircx.Sender, m *irc.Message) { log.Printf("Connected.") err := s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{strings.Join(config.Chans, ",")}, }) if err != nil { log.Printf("Could not send JOIN: %v", err) } }
func JoinHandler(s ircx.Sender, m *irc.Message) { if m.Name != name { userAdd(m.Name, m.Trailing) } else { s.Send(&irc.Message{ Command: irc.WHO, Params: []string{m.Params[0]}, Trailing: "%na", }) } }
func pingHandler(s ircx.Sender, m *irc.Message) { err := s.Send(&irc.Message{ Command: irc.PONG, Params: m.Params, Trailing: m.Trailing, }) if err != nil { log.Error(err) } }
func (c *Client) encryptionMsgHandler(s ircx.Sender, m *irc.Message) { if dm, ok := c.decodedMessage(m); ok && dm.Trailing == ".encoff" { c.state.Encryption().Stop(m.Name) out := "I've removed the key for this conversation" msg := &irc.Message{ Command: irc.PRIVMSG, Params: []string{m.Name}, Trailing: out, } s.Send(msg) } }
func (c *Client) inviteHandler(s ircx.Sender, m *irc.Message) { var channel string if len(m.Params) > 1 { channel = m.Params[1] } else { channel = m.Trailing } msg := &irc.Message{ Command: irc.JOIN, Params: []string{channel}, } s.Send(msg) }
func (c *Client) kickHandler(s ircx.Sender, m *irc.Message) { if m.Params[1] == c.state.Name() { c.state.RemoveChannel(m.Params[0]) if c.state.Rejoin() { msg := &irc.Message{ Command: irc.JOIN, Params: []string{m.Params[0]}, } s.Send(msg) } } else { c.state.RemoveUser(m.Params[0], m.Params[1]) } }
func registerHandler(s ircx.Sender, m *irc.Message) { log.Debug("Registered") for _, channel := range strings.Split(*channels, " ") { err := s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{channel}, }) if err != nil { log.WithFields(log.Fields{ "channel": *channels, "host": *serverName, }).Error(err) } } }
func inviteHandler(s ircx.Sender, m *irc.Message) { log.WithFields(log.Fields{ "params": m.Params, "trailing": m.Trailing, }).Info("Just got invite") err := s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{m.Params[1]}, }) if err != nil { log.WithFields(log.Fields{ "params": m.Params, "trailing": m.Trailing, }).Error(err) } }
func getURL(s ircx.Sender, message *irc.Message, word string) { if url, err := url.Parse(word); err == nil && strings.HasPrefix(url.Scheme, "http") { resp, err := http.Head(url.String()) if err == nil { resp.Body.Close() if resp.StatusCode == http.StatusOK && strings.Contains(resp.Header.Get("content-type"), "text/html") && resp.ContentLength < 1024*1024 { resp2, err2 := http.Get(url.String()) if err2 == nil { defer resp2.Body.Close() if resp.StatusCode == http.StatusOK && strings.Contains(resp.Header.Get("content-type"), "text/html") { z := html.NewTokenizer(resp2.Body) for { tt := z.Next() if tt == html.ErrorToken { return } if tt == html.StartTagToken && z.Token().DataAtom == atom.Title { tt = z.Next() if tt == html.TextToken { p := message.Params if p[0] == config.General.Name { p = []string{message.Prefix.Name} } title := fmt.Sprintf("Title: %s", strings.TrimSpace(strings.Replace(z.Token().Data, "\n", "", -1))) log.Println("Sending HTML", title) s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: p, Trailing: title, }) } return } } } } } } } }
func send(robot types.Robot, s ircx.Sender, message, target, user string) { q := types.Query{ Statement: message, Context: types.QueryContext{ Username: user, Group: getGroupForUser(robot, user), }, } if found, c := robot.Query(q); found { for answer := range c { fmt.Println(answer) s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: []string{target, ":" + string(answer)}, }) } } }
func MessageHandler(s ircx.Sender, m *irc.Message) { msg := m.Trailing var command string if m.Params[0] == name { m.Params = []string{m.Name} } if strings.HasPrefix(strings.ToLower(msg), strings.ToLower(name)) { pieces := strings.Split(msg, " ") if len(pieces) >= 2 { command = pieces[1] runCommand(command, pieces, msg, s, m) return } } else if strings.HasPrefix(msg, prefix) { pieces := strings.Split(msg, " ") if len(pieces) >= 1 { command = pieces[0][1:] runCommand(command, pieces, msg, s, m) return } } else if strings.HasPrefix(msg, "\x01VERSION") { log.Println(ctcp.VersionReply()) s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: m.Params, Trailing: ctcp.VersionReply(), }) return } else { for k := range replies { if ok := k.FindAllString(msg, 1); ok != nil { s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: m.Params, Trailing: replies[k], }) return } } } }
func (z *Zombie) messageHandler(s ircx.Sender, m *irc.Message) { go func() { for { log.Info("Waiting for message") msg := <-z.Messages log.WithFields(log.Fields{ "message": msg.Message, "channel_key": msg.Channel, }).Info("Received message going to send to IRC") channel, err := ParseChannelKey(msg.Channel) if err != nil { log.WithFields(log.Fields{ "error": err, "channel_key": msg.Channel, }).Error("Couldnt get channel") return } log.WithFields(log.Fields{ "channel": channel, }).Info("Succesfully parsed channels") err = s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: []string{channel}, Trailing: msg.Message, }) if err != nil { log.WithFields(log.Fields{ "error": err, }).Warn("Couldn't send message") } log.Info("Message sent") } }() }
func (c *Client) encryptionStartHandler(s ircx.Sender, m *irc.Message) { data := strings.Split(m.Trailing, " ") if len(data) < 2 { return } if data[0] == "DH1080_INIT" { enc := dh1080.New() err := enc.Unpack(m.Trailing) data, err := enc.Pack() if err != nil { return } msg := &irc.Message{ Command: irc.NOTICE, Params: []string{m.Name}, Trailing: data + " CBC", } s.Send(msg) secret, err := enc.GetSecret() if err != nil { msg := &irc.Message{ Command: irc.PRIVMSG, Params: []string{m.Name}, Trailing: "I couldn't create the key for our chat, I won't be understanding your encrypted messages", } s.Send(msg) } else { c.state.Encryption().New(m.Name, secret) time.Sleep(1 * time.Second) // wait for their client to get situated out := fmt.Sprintf("I've stared an encrypted chat, just message %s to remove our group key", encOff) encryptedMessage, err := dh1080.Enc([]byte(out), []byte(secret)) if err != nil { return } encryptedBaseMessage := base64.StdEncoding.EncodeToString(encryptedMessage) infoMessage := &irc.Message{ Command: irc.PRIVMSG, Params: []string{m.Name}, Trailing: "+OK *" + encryptedBaseMessage, } s.Send(infoMessage) } } }
func GetWeather(s ircx.Sender, message *irc.Message) { if causZip.MatchString(message.Trailing) { var p []string var prefix string if message.Params[0] == config.General.Name { p = []string{message.Prefix.Name} } else { p = message.Params prefix = fmt.Sprint(message.Prefix.Name, ": ") } m := &irc.Message{ Command: irc.PRIVMSG, Params: p, } zl := cache.Get(message.Trailing) if zl != nil { z := zl.(*ZipInfo) if z.Places != nil { resp, err := http.Get(fmt.Sprintf("https://api.forecast.io/forecast/%s/%.4f,%.4f?exclude=flags", config.Forecast.Key, z.Places[0].Latitude, z.Places[0].Longitude)) if err != nil { // handle error return } defer resp.Body.Close() dec := json.NewDecoder(resp.Body) var w WeatherReport err = dec.Decode(&w) l, _ := time.LoadLocation(w.Timezone) log.Println("Sending weather for", message.Trailing) m.Trailing = fmt.Sprintf("%s%s, %s - %.2f°F (feels like %.2f°F) - %s - Sunrise: %s Sunset: %s", prefix, z.Places[0].PlaceName, z.Places[0].StateAbbr, w.Currently.Temperature, w.Currently.ApparentTemperature, w.Currently.Summary, time.Unix(w.Daily.Data[0].SunriseTime, 0).In(l).Format(time.Kitchen), time.Unix(w.Daily.Data[0].SunsetTime, 0).In(l).Format(time.Kitchen)) s.Send(m) m.Trailing = fmt.Sprintf("%s%d%% Humidity - Wind from %d° at %.2fmph - Visibility %.2fmi - Cloud Cover %d%% - Precipitation Probability %d%%", prefix, int(w.Currently.Humidity*100), w.Currently.WindBearing, w.Currently.WindSpeed, w.Currently.Visibility, int(w.Currently.CloudCover*100), int(w.Currently.PrecipProbability*100)) s.Send(m) m.Trailing = fmt.Sprintf("%s%s %s %s", prefix, w.Minutely.Summary, w.Hourly.Summary, w.Daily.Summary) s.Send(m) } else { log.Println("No data returned for zip:", message.Trailing) } } } }
func RegisterConnect(s ircx.Sender, m *irc.Message) { s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{*channels}, }) }
func (client IRCClient) onConnected(s ircx.Sender, m *irc.Message) { s.Send(&irc.Message{ Command: irc.JOIN, Params: []string{channels}, }) }