示例#1
0
文件: plugin.go 项目: rskang/ircb
func (p *Plugin) Load(c *proto.Client) (err error) {
	err = p.Base.Load(c)
	if err != nil {
		return
	}

	w := new(cmd.Command)
	w.Name = "define"
	w.Description = "Fetch the definition for the given term"
	w.Restricted = false
	w.Params = []cmd.Param{
		{Name: "term", Description: "Word to find definition for", Pattern: cmd.RegAny},
	}
	w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		dict, err := Dial("tcp", "dict.org:2628")
		if err != nil {
			log.Printf("[dict] %s", err)
			c.PrivMsg(m.Receiver, "%s, No definition found for '%s'",
				m.SenderName, cmd.Params[0].Value)
			return
		}

		def, err := dict.Define("wn", cmd.Params[0].Value)
		if err != nil {
			log.Printf("[dict] %s", err)
			c.PrivMsg(m.Receiver, "%s, No definition found for '%s'",
				m.SenderName, cmd.Params[0].Value)
			return
		}

		if len(def) == 0 {
			c.PrivMsg(m.Receiver, "%s, No definition found for '%s'",
				m.SenderName, cmd.Params[0].Value)
			return
		}

		space := []byte{' '}
		mspace := []byte{' ', ' '}
		line := bytes.Replace(def[0].Text, []byte{'\n'}, space, -1)

		// Strip all multi-space indents.
		for bytes.Index(line, mspace) > -1 {
			line = bytes.Replace(line, mspace, space, -1)
		}

		c.PrivMsg(m.Receiver, "%s: %s", m.SenderName, line)
	}

	cmd.Register(w)

	return
}
示例#2
0
func (p *Plugin) Load(c *proto.Client) (err error) {
	err = p.Base.Load(c)
	if err != nil {
		return
	}

	comm := new(cmd.Command)
	comm.Name = "quit"
	comm.Description = "Unconditionally quit the bot program"
	comm.Restricted = true
	comm.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		c.Quit("")
	}
	cmd.Register(comm)

	comm = new(cmd.Command)
	comm.Name = "join"
	comm.Description = "Join the given channel"
	comm.Restricted = true
	comm.Params = []cmd.Param{
		{Name: "channel", Optional: false, Pattern: cmd.RegChannel},
		{Name: "key", Optional: true, Pattern: cmd.RegAny},
		{Name: "chanservpass", Optional: true, Pattern: cmd.RegAny},
	}
	comm.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		var ch irc.Channel
		ch.Name = cmd.Params[0].Value

		if len(cmd.Params) > 1 {
			ch.Key = cmd.Params[1].Value
		}

		if len(cmd.Params) > 2 {
			ch.ChanservPassword = cmd.Params[2].Value
		}

		c.Join(&ch)
	}
	cmd.Register(comm)

	comm = new(cmd.Command)
	comm.Name = "leave"
	comm.Description = "Leave the given channel"
	comm.Restricted = true
	comm.Params = []cmd.Param{
		{Name: "channel", Optional: true, Pattern: cmd.RegChannel},
	}
	comm.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		var ch irc.Channel

		if len(cmd.Params) > 0 {
			ch.Name = cmd.Params[0].Value
		} else {
			if !m.FromChannel() {
				return
			}
			ch.Name = m.Receiver
		}

		c.Part(&ch)
	}
	cmd.Register(comm)

	return
}
示例#3
0
func (p *Plugin) Load(c *proto.Client) (err error) {
	err = p.Base.Load(c)
	if err != nil {
		return
	}

	ini := p.LoadConfig()
	if ini == nil {
		log.Fatalf("[ipintel] No configuration found.")
		return
	}

	key := ini.Section("api").S("key", "")
	if len(key) == 0 {
		log.Fatalf("[ipintel] No API key found.")
		return
	}

	shared := ini.Section("api").S("shared", "")
	if len(shared) == 0 {
		log.Fatalf("[ipintel] No API shared secret found.")
		return
	}

	drift := ini.Section("api").I64("drift", 0)
	if len(shared) == 0 {
		log.Fatalf("[ipintel] No API shared secret found.")
		return
	}

	w := new(cmd.Command)
	w.Name = "loc"
	w.Description = "Fetch geo-location data for the given IP address."
	w.Restricted = false
	w.Params = []cmd.Param{
		{Name: "ip", Description: "IPv4 address to look up", Pattern: cmd.RegIPv4},
	}
	w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		hash := md5.New()
		stamp := fmt.Sprintf("%d", time.Now().UTC().Unix()+drift)
		io.WriteString(hash, key+shared+stamp)

		sig := fmt.Sprintf("%x", hash.Sum(nil))
		target := fmt.Sprintf(url, cmd.Params[0].Value, key, sig)

		resp, err := http.Get(target)
		if err != nil {
			log.Printf("[ipintel]: %v", err)
			return
		}

		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()

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

		var data Response
		err = json.Unmarshal(body, &data)
		if err != nil {
			log.Printf("[ipintel]: %v", err)
			return
		}

		inf := data.IPInfo
		mapsURL := fmt.Sprintf("https://maps.google.com/maps?q=%f,%f",
			inf.Location.Latitude, inf.Location.Longitude)

		c.PrivMsg(m.Receiver,
			"%s: %s (%s), Network org.: %s, Carrier: %s, TLD: %s, SLD: %s. "+
				"Location: %s/%s/%s/%s (%f, %f). Postalcode: %s, Timezone: %d, %s",
			m.SenderName,

			inf.IPAddress, inf.IPType,
			inf.Network.Organization,
			inf.Network.Carrier,
			inf.Network.Domain.TLD,
			inf.Network.Domain.SLD,

			inf.Location.Continent,
			inf.Location.CountryData.Name,
			inf.Location.StateData.Name,
			inf.Location.CityData.Name,
			inf.Location.Latitude,
			inf.Location.Longitude,
			inf.Location.CityData.PostalCode,
			inf.Location.CityData.TimeZone,

			mapsURL,
		)
	}

	cmd.Register(w)

	w = new(cmd.Command)
	w.Name = "mibbit"
	w.Description = "Resolve a mibbit address to a real IP address."
	w.Restricted = false
	w.Params = []cmd.Param{
		{Name: "hex", Description: "Mibbit hex string", Pattern: regMibbit},
	}
	w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		hex := cmd.Params[0].Value

		var ip [4]uint64
		var err error

		ip[0], err = strconv.ParseUint(hex[:2], 16, 8)
		if err != nil {
			c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
			return
		}

		ip[1], err = strconv.ParseUint(hex[2:4], 16, 8)
		if err != nil {
			c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
			return
		}

		ip[2], err = strconv.ParseUint(hex[4:6], 16, 8)
		if err != nil {
			c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
			return
		}

		ip[3], err = strconv.ParseUint(hex[6:], 16, 8)
		if err != nil {
			c.PrivMsg(m.Receiver, "%s: invalid mibbit address.", m.SenderName)
			return
		}

		address := fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
		names, err := net.LookupAddr(address)

		if err != nil || len(names) == 0 {
			c.PrivMsg(m.Receiver, "%s: %s is %s", m.SenderName, hex, address)
		} else {
			c.PrivMsg(m.Receiver, "%s: %s is %s / %s",
				m.SenderName, hex, address, names[0])
		}
	}

	cmd.Register(w)

	return
}
示例#4
0
func (p *Plugin) Load(c *proto.Client) (err error) {
	err = p.Base.Load(c)
	if err != nil {
		return
	}

	ini := p.LoadConfig()
	if ini == nil {
		log.Fatalf("[weather] No API key found.")
		return
	}

	key := ini.Section("api").S("key", "")
	if len(key) == 0 {
		log.Fatalf("[weather] No API key found.")
		return
	}

	w := new(cmd.Command)
	w.Name = "weather"
	w.Description = "Fetch the current weather for a given location"
	w.Restricted = false
	w.Params = []cmd.Param{
		{Name: "location", Description: "Name of the city/town for the forecast", Pattern: cmd.RegAny},
	}

	w.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		location := url.QueryEscape(cmd.Params[0].Value)
		resp, err := http.Get(fmt.Sprintf(_url, location, key))
		if err != nil {
			return
		}

		data, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()

		if err != nil {
			return
		}

		var wd WeatherData
		err = json.Unmarshal(data, &wd)
		if err != nil {
			return
		}

		if len(wd.Data.Request) == 0 || len(wd.Data.Conditions) == 0 {
			c.PrivMsg(m.Receiver, "%s: No weather data for %q",
				m.SenderName, cmd.Params[0].Value)
			return
		}

		wr := wd.Data.Request[0]
		wc := wd.Data.Conditions[0]

		c.PrivMsg(m.Receiver,
			"%s, weather in %s: %s°C/%s°F/%.2f°K, %s, cloud cover: %s%%, humidity: %s%%, wind: %skph/%smph from %s, pressure: %s mb, visibility: %s km",
			m.SenderName, wr.Query,
			wc.TempC, wc.TempF, wd.TempK(), codeName(wc.WeatherCode),
			wc.CloudCover, wc.Humidity, wc.WindSpeedKmph, wc.WindSpeedMiles,
			wc.WindDir16Point, wc.Pressure, wc.Visibility,
		)
	}

	cmd.Register(w)

	return
}