-
Notifications
You must be signed in to change notification settings - Fork 5
/
events.go
158 lines (145 loc) · 4.39 KB
/
events.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"log"
"regexp"
"strconv"
"strings"
)
// Events are built from the output of the IRC server, and are sent to modules
// Please keep this is in order of use, as some expression may overlap others
var re_server_notice = regexp.MustCompile("^:[^ ]+ NOTICE [^:]+ :(.*)")
var re_event_names = regexp.MustCompile("^:[^ ]+ 353 ([^ ]+) [@+=] ([^ ]+) :(.*)")
var re_event_names_end = regexp.MustCompile("^:[^ ]+ 366 ([^ ]+) ([^ ]+) :.*")
var re_server_message = regexp.MustCompile("^:[^ ]+ ([0-9]+) [^:]+ :(.*)")
var re_server_ping = regexp.MustCompile("^PING :(.*)")
var re_event_join = regexp.MustCompile("^:([^!]+)![^ ]* JOIN :(.+)")
var re_event_part = regexp.MustCompile("^:([^!]+)![^ ]* PART ([^ ]+).*")
var re_event_privmsg = regexp.MustCompile("^:([^!]+)![^ ]* PRIVMSG ([^ ]+) :(.+)")
var re_event_kick = regexp.MustCompile("^:([^!]+)![^ ]* KICK ([^ ]+) ([^ ]+) :(.*)")
var re_event_quit = regexp.MustCompile("^:([^!]+)![^ ]* QUIT :(.*)")
var re_event_nick = regexp.MustCompile("^:([^!]+)![^ ]* NICK :(.*)")
func ExtractEvent(line string) *Event {
if m := re_server_notice.FindStringSubmatch(line); len(m) == 2 {
return newEventNOTICE(line, m[1], 0)
}
if m := re_event_names.FindStringSubmatch(line); len(m) == 4 {
log.Printf("re_event_names")
return newEventNAMES(line, m[1], m[2], m[3])
}
if m := re_event_names_end.FindStringSubmatch(line); len(m) == 3 {
log.Printf("re_event_names_end")
return newEventENDOFNAMES(line, m[1], m[2])
}
if m := re_server_message.FindStringSubmatch(line); len(m) == 3 {
cmd_id, _ := strconv.Atoi(m[1])
return newEventNOTICE(line, m[2], cmd_id)
}
if m := re_server_ping.FindStringSubmatch(line); len(m) == 2 {
return newEventPING(line, m[1])
}
if m := re_event_join.FindStringSubmatch(line); len(m) == 3 {
return newEventJOIN(line, m[1], m[2])
}
if m := re_event_part.FindStringSubmatch(line); len(m) == 3 {
return newEventPART(line, m[1], m[2])
}
if m := re_event_privmsg.FindStringSubmatch(line); len(m) == 4 {
return newEventPRIVMSG(line, m[1], m[2], m[3])
}
if m := re_event_kick.FindStringSubmatch(line); len(m) == 5 {
return newEventKICK(line, m[1], m[2], m[3], m[4])
}
if m := re_event_quit.FindStringSubmatch(line); len(m) == 3 {
return newEventQUIT(line, m[1], m[2])
}
if m := re_event_nick.FindStringSubmatch(line); len(m) == 3 {
return newEventNICK(line, m[1], m[2])
}
log.Printf("Ignored message: %s", line)
return nil
}
func newEventPING(line string, server string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_PING
event.Data = server
return event
}
func newEventNOTICE(line string, message string, cmd_id int) *Event {
event := new(Event)
event.Raw = line
event.Type = E_NOTICE
event.Data = message
event.CmdId = cmd_id
return event
}
func newEventJOIN(line string, user string, channel string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_JOIN
event.Channel = channel
event.Data = channel
event.User = user
return event
}
func newEventPART(line string, user string, channel string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_PART
event.Channel = channel
event.User = user
return event
}
func newEventPRIVMSG(line string, user string, channel string, msg string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_PRIVMSG
event.Data = msg
if strings.Index(channel, "#") == 0 {
event.Channel = channel
}
event.User = user
return event
}
func newEventKICK(line string, user string, channel string, target string, msg string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_KICK
event.Data = target
event.Channel = channel
event.User = user
return event
}
func newEventQUIT(line string, user string, msg string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_QUIT
event.Data = msg
event.User = user
return event
}
func newEventNICK(line string, user string, newuser string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_NICK
event.Data = newuser
event.User = user
return event
}
func newEventNAMES(line string, user string, channel string, names string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_NAMES
event.Data = names
event.User = user
event.Channel = channel
return event
}
func newEventENDOFNAMES(line string, user string, channel string) *Event {
event := new(Event)
event.Raw = line
event.Type = E_ENDOFNAMES
event.User = user
event.Channel = channel
return event
}