all repos — paprika @ f3575d038b3da006531180a877148dfe3895c4ff

go rewrite of taigabot

main.go (view raw)

 1package main
 2
 3import (
 4	"errors"
 5	"log"
 6	"net"
 7	"strings"
 8
 9	"git.icyphox.sh/paprika/config"
10	"git.icyphox.sh/paprika/database"
11	"git.icyphox.sh/paprika/plugins"
12	"gopkg.in/irc.v3"
13)
14
15func handleChatMessage(c *irc.Client, m *irc.Message) {
16	// Trim leading and trailing spaces to not trip up our
17	// plugins.
18	m.Params[1] = strings.TrimSpace(m.Params[1])
19	response, err := plugins.ProcessTrigger(m)
20
21	if errors.Is(err, plugins.NoReply) {
22		return
23	}
24
25	cmd := "PRIVMSG"
26	if errors.Is(err, plugins.IsNotice) {
27		err = nil
28		cmd = "NOTICE"
29	}
30
31	target := m.Params[0]
32	if serr, ok := err.(*plugins.IsPrivateNotice); ok {
33		target = serr.To
34		cmd = "NOTICE"
35		err = nil
36	}
37
38	msg := irc.Message{Command: cmd}
39	split := strings.Split(response, "\n")
40
41	if errors.Is(err, plugins.IsRaw) {
42		for _, line := range split {
43			c.Write(line)
44		}
45	} else if err != nil {
46		log.Printf("error: %v", err)
47	} else {
48		for _, line := range split {
49			msg.Params = []string{target, line}
50			c.WriteMessage(&msg)
51		}
52		return
53	}
54}
55
56func ircHandler(c *irc.Client, m *irc.Message) {
57	switch m.Command {
58	case "001":
59		c.Write(config.SplitChannelList(config.C.Channels))
60	case "PRIVMSG":
61		handleChatMessage(c, m)
62	}
63}
64
65func main() {
66	conn, err := net.Dial("tcp", config.C.Host)
67	if err != nil {
68		log.Fatal(err)
69	}
70	defer conn.Close()
71
72	ircConfig := irc.ClientConfig{
73		Nick:    config.C.Nick,
74		Pass:    config.C.Pass,
75		User:    "paprikabot",
76		Name:    config.C.Nick,
77		Handler: irc.HandlerFunc(ircHandler),
78	}
79
80	database.DB.DB, err = database.Open()
81	if err != nil {
82		log.Fatal(err)
83	}
84	defer database.DB.Close()
85
86	client := irc.NewClient(conn, ircConfig)
87	err = client.Run()
88	if err != nil {
89		log.Fatal(err)
90	}
91}