Пример #1
0
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)
	}
}
Пример #2
0
func PingHandler(s ircx.Sender, m *irc.Message) {
	s.Send(&irc.Message{
		Command:  irc.PONG,
		Params:   m.Params,
		Trailing: m.Trailing,
	})
}
Пример #3
0
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},
		})
	}
}
Пример #4
0
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,
	})
}
Пример #5
0
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},
	})
}
Пример #6
0
func RegisterConnect(s ircx.Sender, m *irc.Message) {
	log.Println(*user + " joining channel " + *channel)

	s.Send(&irc.Message{
		Command: irc.JOIN,
		Params:  []string{*channel},
	})
}
Пример #7
0
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)
}
Пример #8
0
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)
	}
}
Пример #9
0
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)
	}
}
Пример #10
0
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",
		})
	}
}
Пример #11
0
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)
	}
}
Пример #12
0
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)
	}
}
Пример #13
0
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)
}
Пример #14
0
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])
	}
}
Пример #15
0
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)
		}

	}
}
Пример #16
0
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)
	}
}
Пример #17
0
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
							}

						}
					}
				}
			}
		}
	}
}
Пример #18
0
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)},
			})
		}
	}
}
Пример #19
0
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
			}
		}
	}
}
Пример #20
0
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")
		}
	}()
}
Пример #21
0
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)
		}
	}
}
Пример #22
0
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)
			}
		}
	}
}
Пример #23
0
func RegisterConnect(s ircx.Sender, m *irc.Message) {
	s.Send(&irc.Message{
		Command: irc.JOIN,
		Params:  []string{*channels},
	})
}
Пример #24
0
func (client IRCClient) onConnected(s ircx.Sender, m *irc.Message) {
	s.Send(&irc.Message{
		Command: irc.JOIN,
		Params:  []string{channels},
	})
}