wurgurboo: convert into systemd service with short links
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -6,56 +6,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.zx2c4.com/irc/hbot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
channelArg := flag.String("channel", "", "channel to join")
|
||||
serverArg := flag.String("server", "", "server and port")
|
||||
nickArg := flag.String("nick", "", "nickname")
|
||||
passwordArg := flag.String("password-file", "", "optional file with password")
|
||||
flag.Parse()
|
||||
if matched, _ := regexp.MatchString(`^#[a-zA-Z0-9_-]+$`, *channelArg); !matched {
|
||||
fmt.Fprintln(os.Stderr, "Invalid channel")
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
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)
|
||||
}
|
||||
if _, _, err := net.SplitHostPort(*serverArg); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Invalid server")
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
if matched, _ := regexp.MatchString(`^[a-zA-Z0-9\[\]_-]{1,16}$`, *nickArg); !matched {
|
||||
fmt.Fprintln(os.Stderr, "Invalid nick")
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
password := ""
|
||||
if len(*passwordArg) > 0 {
|
||||
f, err := os.Open(*passwordArg)
|
||||
go func() {
|
||||
err = http.Serve(listener, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to open password file: %v\n", err)
|
||||
os.Exit(1)
|
||||
log.Fatal(err)
|
||||
}
|
||||
password, err = bufio.NewReader(f).ReadString('\n')
|
||||
f.Close()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to read password file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(password) > 0 && password[len(password)-1] == '\n' {
|
||||
password = password[:len(password)-1]
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
feeds := NewCgitFeedMonitorer(time.Second * 10)
|
||||
feeds.AddFeed("https://git.zx2c4.com/wireguard-linux/")
|
||||
@@ -72,25 +48,48 @@ func main() {
|
||||
feeds.AddFeed("https://git.zx2c4.com/wintun/")
|
||||
feeds.AddFeed("https://git.zx2c4.com/wg-dynamic/")
|
||||
|
||||
const channel = "#wireguard"
|
||||
bot := hbot.NewBot(&hbot.Config{
|
||||
Host: *serverArg,
|
||||
Nick: *nickArg,
|
||||
Host: "irc.libera.chat:6697",
|
||||
Nick: "WurGurBoo",
|
||||
Realname: "Your Friendly Neighborhood WurGur Bot",
|
||||
Channels: []string{*channelArg},
|
||||
Channels: []string{channel},
|
||||
Logger: hbot.Logger{Verbosef: log.Printf, Errorf: log.Printf},
|
||||
Password: password,
|
||||
Password: os.Getenv("WURGURBOO_PASSWORD"),
|
||||
})
|
||||
|
||||
urlShortener := regexp.MustCompile(`(.*\?id=[a-f0-9]{8})([a-f0-9]+)$`)
|
||||
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()
|
||||
log.Printf("New commit %s in %s", commit.Commit.ID, commit.RepoTitle)
|
||||
url := commit.Commit.Link.Href
|
||||
if matches := urlShortener.FindStringSubmatch(url); len(matches) == 3 {
|
||||
url = matches[1]
|
||||
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
|
||||
}
|
||||
bot.Msg(*channelArg, fmt.Sprintf("\x01ACTION found a new commit in \x0303%s\x0f - \x0306%s\x0f - %s\x01", commit.RepoTitle, commit.Commit.Title, url))
|
||||
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)),
|
||||
)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user