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 msg := irc.Message{Command: cmd}
31 target := m.Params[0]
32 split := strings.Split(response, "\n")
33
34 if errors.Is(err, plugins.IsRaw) {
35 for _, line := range split {
36 c.Write(line)
37 }
38 } else if err != nil {
39 log.Printf("error: %v", err)
40 } else {
41 for _, line := range split {
42 msg.Params = []string{target, line}
43 c.WriteMessage(&msg)
44 }
45 return
46 }
47}
48
49func ircHandler(c *irc.Client, m *irc.Message) {
50 switch m.Command {
51 case "001":
52 c.Write(config.SplitChannelList(config.C.Channels))
53 case "PRIVMSG":
54 handleChatMessage(c, m)
55 }
56}
57
58func main() {
59 conn, err := net.Dial("tcp", config.C.Host)
60 if err != nil {
61 log.Fatal(err)
62 }
63 defer conn.Close()
64
65 ircConfig := irc.ClientConfig{
66 Nick: config.C.Nick,
67 Pass: config.C.Pass,
68 User: "paprikabot",
69 Name: config.C.Nick,
70 Handler: irc.HandlerFunc(ircHandler),
71 }
72
73 database.DB.DB, err = database.Open()
74 if err != nil {
75 log.Fatal(err)
76 }
77 defer database.DB.Close()
78
79 client := irc.NewClient(conn, ircConfig)
80 err = client.Run()
81 if err != nil {
82 log.Fatal(err)
83 }
84}