forked from void-linux/ircbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (149 loc) · 3.89 KB
/
main.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
package main
import (
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
irc "github.com/thoj/go-ircevent"
"gopkg.in/go-playground/webhooks.v5/github"
)
func main() {
// Config data
serverssl := os.Getenv("IRC_SERVER")
channel := os.Getenv("IRC_CHANNEL")
ircnick1 := os.Getenv("IRC_NICK")
// Set up the connection
conn := irc.IRC(ircnick1, ircnick1)
if conn == nil {
log.Println("conn is nil! Did you set IRC_NICK?")
return
}
// IRC auth config
if _, use := os.LookupEnv("IRC_SASL"); use {
conn.UseSASL = true
conn.SASLLogin = os.Getenv("IRC_USER")
conn.SASLPassword = os.Getenv("IRC_PASS")
conn.SASLMech = "PLAIN"
}
// IRC startup
conn.QuitMessage = "I've probably crashed..."
conn.UseTLS = true
conn.AddCallback("001", func(e *irc.Event) {
log.Println("Connected to Server")
conn.Join(channel)
})
conn.AddCallback("366", func(e *irc.Event) {
log.Println("Connected to Channel")
})
conn.AddCallback("PRIVMSG", func(e *irc.Event) {
switch e.Message() {
case ircnick1 + ": hello?":
conn.Privmsgf(channel, "%s: go away, I'm busy", e.Nick)
}
})
err := conn.Connect(serverssl)
if err != nil {
log.Println(err)
return
}
// HTTP Setup
hook, _ := github.New(github.Options.Secret(os.Getenv("WEBHOOK_SECRET")))
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
payload, err := hook.Parse(r,
github.PushEvent,
github.PullRequestEvent,
github.ForkEvent,
github.IssuesEvent,
)
if err != nil {
log.Println("Error parsing hook")
return
}
switch p := payload.(type) {
case github.IssuesPayload:
if p.Repository.Private {
return
}
action := ""
switch p.Action {
case "opened":
action = "\x0303opened\x0f"
case "closed":
action = "\x0305closed\x0f"
case "reopened":
action = "\x0308reopened\x0f"
default:
return
}
conn.Noticef(channel, "[\x0302%s\x0f] \x0307%s\x0f %s issue \x0313#%d\x0f (\x0310%s\x0f): \x0314%s\x0f",
p.Repository.Name, p.Sender.Login, action, p.Issue.Number, p.Issue.Title, p.Issue.HTMLURL)
case github.PullRequestPayload:
if p.Repository.Private {
return
}
action := ""
switch p.Action {
case "opened":
action = "\x0303opened\x0f"
case "closed":
action = "\x0305closed\x0f"
case "reopened":
action = "\x0308reopened\x0f"
default:
return
}
conn.Noticef(channel, "[\x0302%s\x0f] \x0307%s\x0f %s pull request \x0313#%d\x0f (\x0310%s\x0f): \x0314%s\x0f",
p.Repository.Name, p.Sender.Login, action, p.Number, p.PullRequest.Title, p.PullRequest.HTMLURL)
case github.PushPayload:
if p.Repository.Private {
return
}
if p.Ref != "refs/heads/master" {
return
}
forced := ""
if p.Forced {
forced = "\x0304force-"
}
n_commits := len(p.Commits)
commit_sfx := ""
if n_commits != 1 {
commit_sfx = "s"
}
before_sha := p.Before[0:8]
after_sha := p.After[0:8]
shortMsg := p.HeadCommit.Message
idx := strings.Index(shortMsg, "\n")
if idx != -1 {
shortMsg = shortMsg[0:idx]
}
conn.Noticef(channel, "[\x0302%s\x0f] \x0307%s\x0f %spushed\x0f \x0308%d\x0f commit%s (%s -> %s, new HEAD: \x0310%s\x0f): \x0314%s\x0f",
p.Repository.Name, p.Sender.Login, forced, n_commits, commit_sfx, before_sha, after_sha, shortMsg, p.Compare)
case github.ForkPayload:
if p.Repository.Private {
return
}
conn.Noticef(channel, "[\x0302%s\x0f] \x0307%s\x0f forked the repository to \x0307%s\x0f: \x0314%s\x0f",
p.Repository.Name, p.Sender.Login, p.Forkee.FullName, p.Forkee.HTMLURL)
}
})
// Shutdown handler setup
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Println(sig)
done <- true
}()
// Startup Serving
go conn.Loop()
go http.ListenAndServe(":3000", nil)
// Shut down
<-done
log.Println("exiting")
conn.Privmsg(channel, "I'm going away now")
conn.Quit()
}