/
handlers.go
114 lines (104 loc) · 2.4 KB
/
handlers.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
// Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/nickvanw/ircx/v2"
"gopkg.in/sorcix/irc.v2"
)
func onWelcome(s ircx.Sender, m *irc.Message) {
log.Printf("Connected.")
err := s.Send(&irc.Message{
Command: irc.JOIN,
Params: []string{strings.Join(config.Chans, ",")},
})
if err != nil {
log.Printf("Could not send JOIN: %v", err)
}
}
func onPing(s ircx.Sender, m *irc.Message) {
err := s.Send(&irc.Message{
Command: irc.PONG,
Params: m.Params,
})
if err != nil {
log.Printf("Could not reply to PING: %v", err)
}
}
func onPrivmsg(s ircx.Sender, m *irc.Message) {
channel := m.Params[0]
line := m.Trailing()
for _, m := range allRe.FindAllString(line, -1) {
for _, r := range repos {
if ss := r.IssueRe.FindStringSubmatch(m); ss != nil && ss[0] == m {
id, err := strconv.Atoi(ss[2])
if err != nil {
continue
}
body, err := r.IssueInfo(id)
if err != nil {
log.Printf("#%d: %v", id, err)
continue
}
go sendNotice(channel, r.Name, body)
}
if ss := r.PullRe.FindStringSubmatch(m); ss != nil && ss[0] == m {
id, err := strconv.Atoi(ss[2])
if err != nil {
continue
}
body, err := r.PullInfo(id)
if err != nil {
log.Printf("!%d: %v", id, err)
continue
}
go sendNotice(channel, r.Name, body)
}
if ss := r.CommitRe.FindString(m); ss == m {
body, err := r.CommitInfo(ss)
if err != nil {
log.Printf("%s: %v", ss, err)
continue
}
go sendNotice(channel, r.Name, body)
}
}
}
}
func sendNotice(channel, categ, body string) {
message := fmt.Sprintf("[%s] %s", categ, body)
throttle.Send(&irc.Message{
Command: irc.NOTICE,
Params: []string{channel, message},
})
}
func sendNotices(chans []string, categ string, body ...string) {
for _, channel := range chans {
if !prejoinedChans[channel] {
throttle.Send(&irc.Message{
Command: irc.JOIN,
Params: []string{channel},
})
}
}
for _, channel := range chans {
for _, body := range body {
message := fmt.Sprintf("[%s] %s", categ, body)
throttle.Send(&irc.Message{
Command: irc.NOTICE,
Params: []string{channel, message},
})
}
}
for _, channel := range chans {
if !prejoinedChans[channel] {
throttle.Send(&irc.Message{
Command: irc.PART,
Params: []string{channel},
})
}
}
}