about summary refs log tree commit diff
path: root/fun/clbot/clbot.go
blob: 40b044f45d392268e6e34c68f973a133fbdc7740 (plain) (blame)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main

import (
	"context"
	"crypto/tls"
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"os/signal"
	"strings"
	"time"

	"code.tvl.fyi/fun/clbot/backoffutil"
	"code.tvl.fyi/fun/clbot/gerrit"
	"code.tvl.fyi/fun/clbot/gerrit/gerritevents"
	log "github.com/golang/glog"
	"golang.org/x/crypto/ssh"
	"gopkg.in/irc.v3"
)

var (
	gerritAddr       = flag.String("gerrit_host", "cl.tvl.fyi:29418", "Gerrit SSH host:port")
	gerritSSHHostKey = flag.String("gerrit_ssh_pubkey", "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIUNYBYPCCBNDFSd0BuCR+8kgeuJ7IA5S2nTNQmkQUYNyXK+ot5os7rHtCk96+grd5+J8jFCuFBWisUe8h8NC0Q=", "Gerrit SSH public key")
	gerritSSHTimeout = flag.Duration("gerrit_tcp_timeout", 5*time.Second, "Gerrit SSH TCP connect timeout")

	gerritAuthUsername = flag.String("gerrit_ssh_auth_username", "", "Gerrit SSH username")
	gerritAuthKeyPath  = flag.String("gerrit_ssh_auth_key", "", "Gerrit SSH private key path")

	ircServer    = flag.String("irc_server", "irc.hackint.org:6697", "IRC server to connect to")
	ircTls       = flag.Bool("irc_tls", false, "Does the server connection need TLS?")
	ircNick      = flag.String("irc_nick", "clbot", "Nick to use when connecting to IRC")
	ircUser      = flag.String("irc_user", "clbot", "User string to use for IRC")
	ircName      = flag.String("irc_name", "clbot", "Name string to use for IRC")
	ircChannel   = flag.String("irc_channel", "#tvl", "Channel to send messages to")
	ircPassword  = flag.String("irc_pass", "", "Password to use for IRC")
	ircSendLimit = flag.Duration("irc_send_limit", 100*time.Millisecond, "Delay between messages")
	ircSendBurst = flag.Int("irc_send_burst", 10, "Number of messages which can be sent in a burst")

	notifyRepo     = flag.String("notify_repo", "depot", "Repo name to notify about")
	notifyBranches = stringSetFlag{}

	neverPing   = flag.String("never_ping", "marcus", "Comma-separated terms that should never ping users")
	onlyDisplay = flag.String("only_display", "", "Comma-separated substrings of the gerrit CL Change Subject that should be shown (everything else is dropped)")
)

func init() {
	flag.Var(&notifyBranches, "notify_branches", "Branch names (comma-separated, or repeated flags, or both) to notify users about")
}

type stringSetFlag map[string]bool

func (f stringSetFlag) String() string {
	return fmt.Sprintf("%v", map[string]bool(f))
}
func (f stringSetFlag) Set(s string) error {
	if s == "" {
		return nil
	}
	for _, k := range strings.Split(s, ",") {
		if k != "" {
			f[k] = true
		}
	}
	return nil
}

func mustFixedHostKey(f string) ssh.HostKeyCallback {
	pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(f))
	if err != nil {
		log.Exitf("ParseAuthorizedKey(%q): %v", f, err)
	}
	return ssh.FixedHostKey(pk)
}

func mustPrivateKey(p string) ssh.AuthMethod {
	pkBytes, err := ioutil.ReadFile(p)
	if err != nil {
		log.Exitf("reading SSH private key from %q: %v", p, err)
	}
	pk, err := ssh.ParsePrivateKey(pkBytes)
	if err != nil {
		log.Exitf("parsing private key from %q: %v", p, err)
	}
	return ssh.PublicKeys(pk)
}

var shutdownFuncs []func()

func callOnShutdown(f func()) {
	shutdownFuncs = append(shutdownFuncs, f)
}

// Unicode U+200B zero-width-space, to avoid triggering other bots
// or highlighting people on IRC.
const zeroWidthSpace = "\u200b"

func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string) {
	bo := backoffutil.NewDefaultBackOff()
	ircCfg.Handler = irc.HandlerFunc(func(c *irc.Client, m *irc.Message) {
		if m.Command == "NOTICE" && m.Prefix.Name == "NickServ" && strings.Contains(m.Trailing(), "dentified") {
			// We're probably identified now, go join the channel.
			c.Writef("JOIN %s", *ircChannel)
		}
	})
	for {
		timer := time.NewTimer(bo.NextBackOff())
		select {
		case <-ctx.Done():
			timer.Stop()
			return
		case <-timer.C:
			break
		}

		(func() {
			connectedStart := time.Now()

			var ircConn net.Conn
			var err error

			if *ircTls {
				ircConn, err = tls.Dial("tcp", *ircServer, nil)
			} else {
				ircConn, err = net.Dial("tcp", *ircServer)
			}

			if err != nil {
				log.Errorf("connecting to IRC at tcp/%s (tls: %v): %v", *ircServer, *ircTls, err)
				return
			}

			ircClient := irc.NewClient(ircConn, ircCfg)
			ircClientCtx, cancel := context.WithCancel(ctx)
			defer cancel()
			go func() {
				for {
					select {
					case <-ircClientCtx.Done():
						return
					case msg := <-sendMsg:
						log.Infof("sending message %q to %v", msg, *ircChannel)
						ircClient.Writef("PRIVMSG %s :%s%s", *ircChannel, zeroWidthSpace, msg)
					}
				}
			}()
			log.Infof("connecting to IRC on tcp/%s", *ircServer)
			if err := ircClient.RunContext(ircClientCtx); err != nil {
				connectedEnd := time.Now()
				connectedFor := connectedEnd.Sub(connectedStart)
				if connectedFor > 60*time.Second {
					bo.Reset()
				}
				log.Errorf("IRC RunContext: %v", err)
				return
			}
		})()
	}
}

func username(a gerritevents.Account) string {
	options := []string{
		a.Username,
		a.Name,
		a.Email,
	}
	for _, opt := range options {
		if opt != "" {
			return opt
		}
	}
	return "UNKNOWN USER"
}

// noping inserts a Unicode zero-width space between the first and rest characters of `user`
// in an effort to avoid pinging that user on IRC.
func noping(user string) string {
	un := []rune(user)
	return string(un[0:1]) + zeroWidthSpace + string(un[1:])
}

// Apply noping to each instance of the username in the supplied
// message. With this users will not be pinged for their own CLs, but
// they will be notified if someone else writes a CL that includes
// their username.
//
// Also applies noping to all instances of the words in `neverPing`.
func nopingAll(username, message string) string {
	for _, word := range strings.Split(*neverPing, ",") {
		message = strings.ReplaceAll(message, word, noping(word))
	}

	return strings.ReplaceAll(message, username, noping(username))
}

// changeShouldBeSkipped applies the list of channels in `onlyDisplay`
// to whether we should skip displaying a CL.
func changeShouldBeSkipped(onlyDisplay string, changeSubject string) bool {
	// case when we don’t want to filter
	if onlyDisplay == "" {
		return false
	}
	for _, needle := range strings.Split(onlyDisplay, ",") {
		if strings.Contains(changeSubject, needle) {
			return false
		}
	}
	return true
}

func patchSetURL(c gerritevents.Change, p gerritevents.PatchSet) string {
	return fmt.Sprintf("https://cl.tvl.fyi/%d", c.Number)
}

func main() {
	flag.Parse()
	failed := false
	if *gerritAuthUsername == "" {
		log.Errorf("gerrit_ssh_auth_username must be set")
		failed = true
	}
	if *gerritAuthKeyPath == "" {
		log.Errorf("gerrit_ssh_auth_key must be set")
		failed = true
	}
	if failed {
		os.Exit(2)
	}

	shutdownCh := make(chan os.Signal)
	signal.Notify(shutdownCh, os.Interrupt)
	go func() {
		<-shutdownCh
		signal.Reset(os.Interrupt)
		for n := len(shutdownFuncs) - 1; n >= 0; n-- {
			shutdownFuncs[n]()
		}
	}()

	ctx, cancel := context.WithCancel(context.Background())
	callOnShutdown(cancel)
	cfg := &ssh.ClientConfig{
		User:            *gerritAuthUsername,
		Auth:            []ssh.AuthMethod{mustPrivateKey(*gerritAuthKeyPath)},
		HostKeyCallback: mustFixedHostKey(*gerritSSHHostKey),
		Timeout:         *gerritSSHTimeout,
	}
	cfg.SetDefaults()

	gw, err := gerrit.New(ctx, "tcp", *gerritAddr, cfg)
	if err != nil {
		log.Exitf("gerrit.New(%q): %v", *gerritAddr, err)
	}
	callOnShutdown(func() {
		ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
		defer cancel()
		gw.Close(ctx)
	})

	sendMsgChan := make(chan string, 5)
	go func() {
		for e := range gw.Events() {
			var parsedMsg string
			switch e := e.(type) {
			case *gerritevents.PatchSetCreated:
				if e.Change.Project != *notifyRepo || !notifyBranches[e.Change.Branch] || e.PatchSet.Number != 1 || changeShouldBeSkipped(*onlyDisplay, e.Change.Subject) {
					continue
				}
				user := username(e.PatchSet.Uploader)
				parsedMsg = nopingAll(user, fmt.Sprintf("CL/%d proposed by %s - %s - %s", e.Change.Number, user, e.Change.Subject, patchSetURL(e.Change, e.PatchSet)))
			case *gerritevents.ChangeMerged:
				if e.Change.Project != *notifyRepo || !notifyBranches[e.Change.Branch] || changeShouldBeSkipped(*onlyDisplay, e.Change.Subject) {
					continue
				}
				owner := username(e.Change.Owner)
				submitter := e.Submitter.Username
				url := patchSetURL(e.Change, e.PatchSet)

				if submitter != owner && submitter == "clbot" {
					parsedMsg = nopingAll(owner, fmt.Sprintf("CL/%d by %s autosubmitted - %s - %s", e.Change.Number, owner, e.Change.Subject, url))
				} else {
					parsedMsg = nopingAll(owner, fmt.Sprintf("CL/%d applied by %s - %s - %s", e.Change.Number, owner, e.Change.Subject, url))
				}
			}
			if parsedMsg != "" {
				sendMsgChan <- parsedMsg
			}
		}
	}()

	ircCtx, ircCancel := context.WithCancel(ctx)
	callOnShutdown(ircCancel)
	go runIRC(ircCtx, irc.ClientConfig{
		Nick: *ircNick,
		User: *ircUser,
		Name: *ircName,
		Pass: *ircPassword,

		SendLimit: *ircSendLimit,
		SendBurst: *ircSendBurst,
	}, sendMsgChan)

	<-ctx.Done()
}