aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/wurgurboo/main.go
blob: 999b84edc91c9be3ce2470df009bfe77be47219b (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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2021 Jason A. Donenfeld. All Rights Reserved.
 */

package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"syscall"
	"time"

	"golang.zx2c4.com/irc/hbot"
)

func main() {
	shortlink := NewShortLink(filepath.Join(os.Getenv("STATE_DIRECTORY"), "links.txt"))
	http.HandleFunc("/l/", shortlink.HandleRequest)
	listener, err := net.FileListener(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		err = http.Serve(listener, nil)
		if err != nil {
			log.Fatal(err)
		}
	}()

	feeds := NewCgitFeedMonitorer(time.Second * 10)
	feeds.AddFeed("https://git.zx2c4.com/wireguard-linux/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-tools/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-linux-compat/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-windows/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-go/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-freebsd/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-openbsd/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-android/")
	feeds.AddFeed("https://git.zx2c4.com/android-wireguard-module-builder/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-apple/")
	feeds.AddFeed("https://git.zx2c4.com/wireguard-rs/")
	feeds.AddFeed("https://git.zx2c4.com/wintun/")
	feeds.AddFeed("https://git.zx2c4.com/wg-dynamic/")

	const channel = "#wireguard"
	bot := hbot.NewBot(&hbot.Config{
		Host:     "irc.libera.chat:6697",
		Nick:     "WurGurBoo",
		Realname: "Your Friendly Neighborhood WurGur Bot",
		Channels: []string{channel},
		Logger:   hbot.Logger{Verbosef: log.Printf, Errorf: log.Printf},
		Password: os.Getenv("WURGURBOO_PASSWORD"),
	})

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		for range c {
			bot.Close()
			shortlink.SaveToDisk()
			os.Exit(0)
		}
	}()

	type seenCommit struct {
		repo    string
		subject string
	}
	seenCommits := make(map[seenCommit]string, 4096)

	go func() {
		for commit := range feeds.Updates() {
			<-bot.Joined()
			sc := seenCommit{commit.RepoTitle, commit.Commit.Title}
			if short, ok := seenCommits[sc]; ok {
				log.Printf("Updated commit %s in %s", commit.Commit.ID, commit.RepoTitle)
				shortlink.Set(short, commit.Commit.Link.Href, true)
				continue
			}
			log.Printf("New commit %s in %s", commit.Commit.ID, commit.RepoTitle)
			short := shortlink.New(commit.Commit.Link.Href)
			seenCommits[sc] = short
			bot.Msg(channel, fmt.Sprintf("\x01ACTION found a new commit in \x0303%s\x0f - \x0306%s\x0f - %s\x01",
				sc.repo, sc.subject,
				fmt.Sprintf("https://w-g.pw/l/%s", short)),
			)
		}
	}()

	for {
		bot.Run()
		time.Sleep(time.Second * 5)
	}
}