-
Notifications
You must be signed in to change notification settings - Fork 7
/
greet.go
184 lines (156 loc) · 3.91 KB
/
greet.go
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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"text/template"
"time"
"gopkg.in/sorcix/irc.v2"
)
var lastSeenLimit = 30 * 24 * time.Hour
var lastSeenWriteThresh = time.Minute
var greeting *template.Template
// maps channel -> (nick -> last seen)
var lastSeen = struct {
mtx sync.Mutex
m map[string]map[string]time.Time
}{}
func init() {
readLastSeen()
readGreeting()
}
func touchLastSeen(channel string, nick string) (absent time.Duration) {
lastSeen.mtx.Lock()
defer lastSeen.mtx.Unlock()
c := lastSeen.m[channel]
if c == nil {
c = make(map[string]time.Time)
lastSeen.m[channel] = c
}
t := c[nick]
c[nick] = time.Now()
return time.Now().Sub(t)
}
func runnerGreet(parsed *irc.Message) error {
botnick := *nick
if botnick == Nick(parsed) {
// we ignore ourselves
return nil
}
nick := Nick(parsed)
var channel string
switch parsed.Command {
case "JOIN":
channel = parsed.Trailing()
case "PART":
channel = Target(parsed)
case "PRIVMSG":
channel = Target(parsed)
case "QUIT":
// QUIT affects all channels. We check if that nick in in #chaos-hd, if
// so, let's set channel to that.
if IsMember(nick, "#chaos-hd") {
channel = "#chaos-hd"
}
}
// TODO: Make channels configurable
if channel != "#chaos-hd" {
return nil
}
if channel == "" || channel[0] != '#' {
log.Printf("Not greeting in non-channel %q", channel)
return nil
}
// To handle renames of users correctly, we also save the hostmask. Only if
// we've seen neither it's a genuinely new user. We strip trailing _, they
// usually appear for duplicate links when the original nick is taken by a
// ghost.
absentNick := touchLastSeen(channel, strings.TrimRight(nick, "_"))
absentHostmask := touchLastSeen(channel, Hostmask(parsed))
seen := (absentNick <= lastSeenLimit) || (absentHostmask <= lastSeenLimit)
if parsed.Command == "JOIN" && !seen {
log.Printf("I have not seen %q in %q recently, so I'm greeting them", nick, channel)
params := struct {
Nick string
Bot string
}{nick, botnick}
var msg string
msgBuf := new(bytes.Buffer)
if greeting == nil {
msg = fmt.Sprintf("Hey %s! o/", nick)
} else if err := greeting.Execute(msgBuf, params); err != nil {
log.Println("Could not render greeting:", err)
msg = fmt.Sprintf("Hey %s! o/", nick)
} else {
msg = msgBuf.String()
}
Privmsg(channel, msg)
}
if absentNick > lastSeenWriteThresh || absentHostmask > lastSeenWriteThresh {
writeLastSeen()
}
return nil
}
func writeLastSeen() {
lastSeen.mtx.Lock()
defer lastSeen.mtx.Unlock()
log.Println("Writing last-seen")
// Take out the garbage
for _, c := range lastSeen.m {
for nick, last := range c {
if time.Now().Sub(last) > lastSeenLimit {
delete(c, nick)
}
}
}
// We write the file atomically to prevent corruption
tmp, err := ioutil.TempFile(".", ".last-seen")
if err != nil {
log.Println("Could not create temporary file for last-seen:", err)
return
}
defer os.Remove(tmp.Name())
if err := gob.NewEncoder(tmp).Encode(lastSeen.m); err != nil {
log.Println("Could not write last-seen:", err)
return
}
if err := os.Rename(tmp.Name(), "last-seen"); err != nil {
log.Println("Could not write last-seen:", err)
return
}
}
func readLastSeen() {
lastSeen.mtx.Lock()
defer lastSeen.mtx.Unlock()
// Make sure, lastSeen is initialized to a sane, if empty, value.
defer func() {
if lastSeen.m == nil {
lastSeen.m = make(map[string]map[string]time.Time)
}
}()
f, err := os.Open("last-seen")
if err != nil {
log.Println("Could not read last-seen:", err)
return
}
defer f.Close()
var m map[string]map[string]time.Time
if err := gob.NewDecoder(f).Decode(&m); err != nil {
log.Println("Could not read last-seen:", err)
return
}
lastSeen.m = m
}
func readGreeting() {
t, err := template.ParseFiles("greeting.txt")
if err != nil {
log.Println("Could not parse greeting:", err)
return
}
greeting = t
}