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 msg.Params = []string{target, response}
47 c.WriteMessage(&msg)
48 log.Printf("error: %v", err)
49 } else {
50 for _, line := range split {
51 msg.Params = []string{target, line}
52 c.WriteMessage(&msg)
53 }
54 return
55 }
56}
57
58func ircHandler(c *irc.Client, m *irc.Message) {
59 switch m.Command {
60 case "001":
61 c.Write(config.SplitChannelList(config.C.Channels))
62 case "PRIVMSG":
63 handleChatMessage(c, m)
64 }
65}
66
67func main() {
68 conn, err := net.Dial("tcp", config.C.Host)
69 if err != nil {
70 log.Fatal(err)
71 }
72 defer conn.Close()
73
74 ircConfig := irc.ClientConfig{
75 Nick: config.C.Nick,
76 Pass: config.C.Pass,
77 User: "paprikabot",
78 Name: config.C.Nick,
79 Handler: irc.HandlerFunc(ircHandler),
80 }
81
82 database.DB.DB, err = database.Open()
83 if err != nil {
84 log.Fatal(err)
85 }
86 defer database.DB.Close()
87
88 client := irc.NewClient(conn, ircConfig)
89 err = client.Run()
90 if err != nil {
91 log.Fatal(err)
92 }
93}