summaryrefslogtreecommitdiff
path: root/main.go
blob: 3e63b6399a085fa6edad6b10167aaf9c0cc44280 (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
package main

import (
	"crypto/tls"
	"flag"
	"fmt"
	"log"
	"strings"

	irc "github.com/fluffle/goirc/client"
	lru "github.com/hashicorp/golang-lru"
)

const maxLen = 450

type notify struct {
	conn   *irc.Conn
	target string
}

func newNotify(conn *irc.Conn, target string) *notify {
	return &notify{conn: conn, target: target}
}

func limit(n int, s string) string {
	r := []rune(s)
	if sz := len(r); sz > n {
		r = append(r[:n-3], []rune("...")...)
	}
	return string(r)
}

func (n *notify) Write(p []byte) (int, error) {
	s := limit(maxLen, string(p))
	log.Println("send", s)
	n.conn.Notice(n.target, s)
	return len(p), nil
}

func privmsg(room string) irc.HandlerFunc {
	var (
		last, _   = lru.New(100)
		titles, _ = lru.New(100)
	)
	return func(conn *irc.Conn, line *irc.Line) {
		defer func() {
			if r := recover(); r != nil {
				log.Println("panic", r)
			}
		}()
		t := line.Text()
		l, okLast := last.Get(line.Nick)
		switch {
		case line.Nick == conn.Me().Nick:
			// ignore self
		case isFlood(t):
			log.Println("flood", line.Nick)
			conn.Kick(room, line.Nick)
		case okLast && noSpaceCompare(t, l.(string)):
			log.Println("kick", line.Nick)
			conn.Kick(room, line.Nick)
		case strings.HasPrefix(t, "s"):
			global := strings.HasSuffix(t, "g")
			if tofix, ok := last.Get(line.Nick); ok {
				fixed, err := re(tofix.(string), t[1:], global)
				if err == nil && fixed != tofix {
					log.Println("regexp", t)
					fmt.Fprintf(newNotify(conn, line.Target()), "%v meant to say: %s", line.Nick, fixed)
					return
				}

			}
			fallthrough
		default:
			for _, v := range getLinks(t) {
				title, ok := titles.Get(v)
				if !ok {
					var err error
					title, err = getTitle(v)
					if err != nil {
						log.Println(v, err)
					}
					titles.Add(v, title)
				}
				if title != "" {
					w := newNotify(conn, line.Target())
					if ok {
						fmt.Fprintf(w, "Title: %v (cached)", title)
					} else {
						fmt.Fprintf(w, "Title: %v", title)
					}
				}
			}
			last.Add(line.Nick, t)
		}
	}
}

func main() {
	var (
		node  = flag.String("node", "irc.freenode.org", "IRC server")
		notls = flag.Bool("notls", false, "disable TLS")
		room  = flag.String("room", "#lor", "IRC channel")
		name  = flag.String("name", "dim13", "bots name")
	)
	flag.Parse()

	conf := irc.NewConfig(*name)
	conf.Server = *node
	if !*notls {
		conf.SSL = true
		conf.SSLConfig = &tls.Config{ServerName: *node}
	}
	conn := irc.Client(conf)

	done := make(chan struct{})

	conn.HandleFunc(irc.DISCONNECTED, func(_ *irc.Conn, _ *irc.Line) {
		close(done)
	})

	conn.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, _ *irc.Line) {
		conn.Join(*room)
		watch(newNotify(conn, *room), feeds)
	})

	conn.HandleBG(irc.PRIVMSG, privmsg(*room))

	conn.HandleFunc(irc.KICK, func(conn *irc.Conn, _ *irc.Line) {
		conn.Join(*room)
	})

	if err := conn.Connect(); err != nil {
		log.Fatal(err)
	}
	<-done
}