/
plusplusbot.go
176 lines (152 loc) · 3.5 KB
/
plusplusbot.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
package main
import (
"database/sql"
"fmt"
irc "github.com/fluffle/goirc/client"
_ "github.com/mattn/go-sqlite3"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var db *sql.DB
var lock1 = new(sync.Mutex)
var lock2 = new(sync.Mutex)
var plus = regexp.MustCompile(`^\s*([a-zA-Z0-9_{^}]+)\+\+\s*$`)
var minus = regexp.MustCompile(`^\s*([a-zA-Z0-9_{^}]+)--\s*$`)
var pluseq = regexp.MustCompile(`^\s*([a-zA-Z0-9_{^}]+)\+=([0-9])\s*$`)
var minuseq = regexp.MustCompile(`^\s*([a-zA-Z0-9_{^}]+)\-=([0-9])\s*$`)
var ref = 0
func atoi(a string) int {
i, _ := strconv.Atoi(a)
return i
}
func parse(message string, callback func(nick string, plus int)) {
if plus.MatchString(message) {
m := plus.FindStringSubmatch(message)
callback(m[1], 1)
} else if minus.MatchString(message) {
m := minus.FindStringSubmatch(message)
callback(m[1], -1)
} else if pluseq.MatchString(message) {
m := pluseq.FindStringSubmatch(message)
callback(m[1], atoi(m[2]))
} else if minuseq.MatchString(message) {
m := minuseq.FindStringSubmatch(message)
callback(m[1], -atoi(m[2]))
}
}
func incr() {
lock2.Lock()
defer lock2.Unlock()
ref++
}
func decr() {
lock2.Lock()
defer lock2.Unlock()
ref--
}
func plusplus(c *irc.Conn, line *irc.Line, nick string, plus int) {
score := 0
incr()
lock1.Lock()
defer func() {
lock1.Unlock()
<-time.After(1 * time.Second)
decr()
if ref == 0 {
c.Notice(line.Args[0], fmt.Sprintf("%s (%d)", nick, score))
}
}()
tx, err := db.Begin()
if err != nil {
fmt.Printf("Database error: %v\n", err)
return
}
defer tx.Rollback()
row, err := tx.Query(`select score from plusplus where nick = ?`, strings.ToLower(nick))
if err != nil {
fmt.Printf("Database error: %v\n", err)
return
}
if row.Next() {
err = row.Scan(&score)
if err != nil {
fmt.Printf("Database error: %v\n", err)
row.Close()
return
}
}
score += plus
row.Close()
stmt, err := tx.Prepare(`insert or replace into plusplus (nick, score) values (?, ?)`)
if err != nil {
fmt.Printf("Database error: %v\n", err)
return
}
defer stmt.Close()
_, err = stmt.Exec(strings.ToLower(nick), score)
if err != nil {
fmt.Printf("Database error: %v\n", err)
return
}
tx.Commit()
}
func ranking(c *irc.Conn, line *irc.Line) {
lock1.Lock()
defer lock1.Unlock()
rows, err := db.Query(`select nick, score from plusplus order by score desc`)
if err != nil {
fmt.Printf("Database error: %v\n", err)
return
}
defer rows.Close()
rank, nick, score := 1, "", 0
for rows.Next() {
rows.Scan(&nick, &score)
c.Notice(line.Args[0], fmt.Sprintf("%03d: %s (%d)\n", rank, nick, score))
rank++
if rank > 5 {
break
}
}
}
func main() {
var err error
db, err = sql.Open("sqlite3", "./plusplus.db")
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
c := irc.SimpleClient("plusplusbot", "plusplusbot")
c.EnableStateTracking()
c.HandleFunc("connected", func(conn *irc.Conn, line *irc.Line) {
for _, room := range os.Args[1:] {
c.Join("#" + room)
}
})
quit := make(chan bool)
c.HandleFunc("disconnected", func(conn *irc.Conn, line *irc.Line) {
quit <- true
})
c.HandleFunc("privmsg", func(conn *irc.Conn, line *irc.Line) {
println(line.Src, line.Args[0], line.Args[1])
if line.Args[1] == "!++" {
go ranking(c, line)
} else {
parse(line.Args[1], func(nick string, plus int) {
go plusplus(c, line, nick, plus)
})
}
})
for {
if err := c.ConnectTo("irc.freenode.net:6667"); err != nil {
fmt.Printf("Connection error: %s\n", err)
return
}
<-quit
}
}