-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
94 lines (79 loc) · 1.82 KB
/
handlers.go
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
package main
import (
"log"
"net/http"
"strings"
"github.com/ergochat/irc-go/ircevent"
"github.com/ergochat/irc-go/ircmsg"
"github.com/gin-gonic/gin"
"github.com/gowon-irc/go-gowon"
)
func createIrcHandler(irccon *ircevent.Connection, cr *CommandRouter) func(event ircmsg.Message) {
return func(event ircmsg.Message) {
nuh, err := ircmsg.ParseNUH(event.Source)
if err != nil {
log.Println(err)
}
line, err := event.Line()
if err != nil {
log.Println(err)
}
var msg, dest, command, args string
if event.Command == "PRIVMSG" {
msg = event.Params[1]
dest = event.Params[0]
command = gowon.GetCommand(event.Params[1])
args = gowon.GetArgs(event.Params[1])
}
m := &gowon.Message{
Module: "gowon",
Nick: event.Nick(),
Code: event.Command,
Raw: line,
Host: nuh.Host,
Source: event.Source,
User: nuh.User,
Arguments: event.Params,
Tags: event.AllTags(),
Msg: msg,
Dest: dest,
Command: command,
Args: args,
}
rc, err := cr.Route(msg)
if err != nil {
return
}
output := rc.Send(m)
if output == nil {
return
}
for _, line := range strings.Split(output.Msg, "\n") {
coloured := colourMsg(line)
for _, sm := range splitMsg(coloured, 400) {
err := irccon.Privmsg(output.Dest, sm)
if err != nil {
log.Println(err)
}
}
}
}
}
func createHttpHandler(irccon *ircevent.Connection) func(*gin.Context) {
return func(c *gin.Context) {
var m gowon.Message
if err := c.BindJSON(&m); err != nil {
return
}
for _, line := range strings.Split(m.Msg, "\n") {
coloured := colourMsg(line)
for _, sm := range splitMsg(coloured, 400) {
err := irccon.Privmsg(m.Dest, sm)
if err != nil {
log.Println(err)
}
}
}
c.IndentedJSON(http.StatusCreated, m)
}
}