summaryrefslogtreecommitdiff
path: root/weather.go
blob: 312b330b535de066b8da7d11c56c38a58b6cfb61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"fmt"
	"strings"

	"dim13.org/weather"
	irc "github.com/fluffle/goirc/client"
)

type Weather struct {
	Command
	lastCity map[string]string
}

func (_ Weather) WithArgs(_ int) bool { return true }

func (w *Weather) Handle(conn *irc.Conn, line *irc.Line) {
	var city string
	q := strings.SplitN(line.Text(), " ", 2)
	if len(q) == 2 {
		city = q[1]
	} else if l, ok := w.lastCity[line.Nick]; ok {
		city = l
	} else {
		conn.Notice(line.Target(), "set your location first")
		return
	}
	if c, err := weather.ByCityName(city); err != nil {
		conn.Notice(line.Target(), err.Error())
	} else {
		conn.Notice(line.Target(), fmt.Sprint(c))
		w.lastCity[line.Nick] = city
	}
}

func (_ Weather) Help() string {
	return "Fetch current weather"
}

func init() {
	Register("weather", &Weather{lastCity: make(map[string]string)})
}