-
Notifications
You must be signed in to change notification settings - Fork 5
/
actions.go
82 lines (75 loc) · 2.44 KB
/
actions.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
package main
import (
"regexp"
"strings"
)
var re_cmd_join = regexp.MustCompile("^JOIN ([^ ]+).*")
var re_cmd_kick = regexp.MustCompile("^KICK ([^ ]+) ([^ ]+) :(.*)")
var re_cmd_kick_short = regexp.MustCompile("^KICK ([^ ]+) ([^ ]+).*")
var re_cmd_part = regexp.MustCompile("^PART ([^ ]+) :(.*)")
var re_cmd_part_short = regexp.MustCompile("^PART ([^ ]+).*")
var re_cmd_privmsg_chan = regexp.MustCompile("^PRIVMSG ([^ ]+) :(.*)")
// ExtractAction constructs an Action of any type from a Raw Action, this extra step is
// made to ensure that the bot is still aware of what's hapening even with
// raw actions (ie: a raw action "QUIT" has to remove the server from the bot)
func ExtractAction(raw_action *Action) *Action {
if m := re_cmd_kick.FindStringSubmatch(raw_action.Data); len(m) == 4 {
return newActionKICK(&raw_action.Server, &m[1], &m[2], &m[3])
}
if m := re_cmd_kick_short.FindStringSubmatch(raw_action.Data); len(m) == 3 {
return newActionKICK(&raw_action.Server, &m[1], &m[2], nil)
}
if m := re_cmd_join.FindStringSubmatch(raw_action.Data); len(m) == 2 {
return newActionJOIN(&raw_action.Server, &m[1])
}
if m := re_cmd_part.FindStringSubmatch(raw_action.Data); len(m) == 3 {
return newActionPART(&raw_action.Server, &m[1], &m[2])
}
if m := re_cmd_part_short.FindStringSubmatch(raw_action.Data); len(m) == 2 {
return newActionPART(&raw_action.Server, &m[1], nil)
}
if m := re_cmd_privmsg_chan.FindStringSubmatch(raw_action.Data); len(m) == 3 {
return newActionPRIVMSG(&raw_action.Server, &m[1], &m[2])
}
return nil
}
func newActionKICK(srv *string, channel *string, user *string, msg *string) *Action {
result := new(Action)
result.Server = *srv
result.Channel = *channel
result.User = *user
if msg != nil {
result.Data = *msg
}
result.Type = A_KICK
return result
}
func newActionJOIN(srv *string, channel *string) *Action {
result := new(Action)
result.Server = *srv
result.Channel = *channel
result.Type = A_JOIN
return result
}
func newActionPART(srv *string, channel *string, msg *string) *Action {
result := new(Action)
result.Server = *srv
result.Channel = *channel
if msg != nil {
result.Data = *msg
}
result.Type = A_PART
return result
}
func newActionPRIVMSG(srv *string, channel *string, msg *string) *Action {
result := new(Action)
result.Server = *srv
if strings.Index(*channel, "#") == 0 {
result.Channel = *channel
} else {
result.User = *channel
}
result.Data = *msg
result.Type = A_SAY
return result
}