summaryrefslogtreecommitdiff
path: root/main.go
blob: b2b49242e1b7e541c0bb065e2b4f5bb2c3920902 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main

import (
	"container/ring"
	"flag"
	"fmt"
	"log"
	"sort"
	"strings"
	"time"

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

var (
	server = flag.String("server", "irc.rusnet.org.ru:6660", "IRC Server")
	room   = flag.String("room", "#lor", "IRC Channel")
	name   = flag.String("name", "Hoff9000", "Bots Name")
)

type Commander interface {
	irc.Handler
	fmt.Stringer
}

type Command struct {
	Help string
	Arg  string
}

type (
	Last struct{ Command }
	RSS  struct{ Command }
	Theo struct{ Command }
	Help struct{ Command }
	Top  struct{ Command }
)

var (
	commands = make(map[string]Commander)
	buffer   = ring.New(10)
)

func Register(cmd string, f Commander) {
	commands[cmd] = f
}

func (v Command) String() string { return v.Help }

func (_ Last) Handle(conn *irc.Conn, line *irc.Line) {
	buffer.Do(func(v interface{}) {
		if v != nil {
			l := v.(*irc.Line)
			s := fmt.Sprintf("%v <%v> %v",
				l.Time.Format(time.Kitchen),
				l.Nick, l.Text())
			conn.Privmsg(line.Nick, s)
		}
	})
}

func (_ Theo) Handle(conn *irc.Conn, line *irc.Line) {
	conn.Privmsg(line.Target(), theo.Theo())
}

func (v RSS) Handle(conn *irc.Conn, line *irc.Line) {
	news, err := rss.Fetch(v.Arg)
	if err != nil {
		conn.Privmsg(line.Target(), err.Error())
		return
	}
	if line.Public() && len(news.Channel.Items) > 3 {
		news.Channel.Items = news.Channel.Items[:3]
	}
	for n, i := range news.Channel.Items {
		s := fmt.Sprintf("%2d. %v - %v", n+1, i.Title, i.Link)
		conn.Privmsg(line.Target(), s)
	}
}

func (_ Help) Handle(conn *irc.Conn, line *irc.Line) {
	var msg []string
	for k, v := range commands {
		msg = append(msg, fmt.Sprintf("%-8s%v", k, v))
	}
	sort.Sort(sort.StringSlice(msg))
	for _, s := range msg {
		conn.Privmsg(line.Target(), s)
	}
}

func (_ Top) Handle(conn *irc.Conn, line *irc.Line) {
	s := fmt.Sprint(NewScores())
	conn.Privmsg(line.Target(), s)
}

func privmsg(conn *irc.Conn, line *irc.Line) {
	f := strings.Fields(line.Text())

	if line.Public() && line.Nick != conn.Me().Nick {
		buffer.Value = line
		buffer = buffer.Next()
		Count(line.Nick)
	}

	// lookup command
	if len(f) == 1 {
		cmd := strings.ToLower(f[0])
		if c, ok := commands[cmd]; ok {
			log.Println(line.Nick, f)
			go c.Handle(conn, line)
		}
	}

	// extract single link and fetch title
	for _, v := range f {
		if strings.HasPrefix(v, "http") {
			go func(url string) {
				log.Println(line.Nick, url)
				t, err := FetchTitle(url)
				if err != nil {
					log.Println(err)
				}
				if t != "" {
					conn.Privmsg(line.Target(), "Title: "+t)
				}
			}(v)
		}
	}
}

func init() {
	flag.Parse()
	Register("last", Last{
		Command{
			Help: "Return last 10 messages",
		},
	})
	Register("theo", Theo{
		Command{
			Help: "Quote Theo De Raadt",
		},
	})
	Register("news", RSS{
		Command{
			Help: "LOR news (msg private to see all)",
			Arg:  `https://www.linux.org.ru/section-rss.jsp?section=1`,
		},
	})
	Register("forum", RSS{
		Command{
			Help: "LOR forum (msg private to see all)",
			Arg:  `https://www.linux.org.ru/section-rss.jsp?section=2`,
		},
	})
	Register("gallery", RSS{
		Command{
			Help: "LOR gallery (msg private to see all)",
			Arg:  `https://www.linux.org.ru/section-rss.jsp?section=3`,
		},
	})
	Register("bsd", RSS{
		Command{
			Help: "Undeadly news (msg private to see all)",
			Arg:  `http://undeadly.org/cgi?action=rss`,
		},
	})
	Register("help", Help{
		Command{
			Help: "This help",
		},
	})
	Register("top", Top{
		Command{
			Help: "Top 10 flooder",
		},
	})
}

func main() {
	c := irc.SimpleClient(*name)
	c.EnableStateTracking()

	quit := make(chan bool)
	c.HandleFunc(irc.DISCONNECTED,
		func(conn *irc.Conn, line *irc.Line) { quit <- true })

	c.HandleFunc(irc.CONNECTED,
		func(conn *irc.Conn, line *irc.Line) { conn.Join(*room) })

	c.HandleFunc(irc.PRIVMSG, privmsg)

	if err := c.ConnectTo(*server); err != nil {
		log.Fatal(err)
	}

	go autoSave()

	<-quit
}