-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstick.go
110 lines (102 loc) · 3.14 KB
/
stick.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
package stick
import (
"github.com/thoj/Go-IRC-Client-Library"
// irc "../Go-IRC-Client-Library/_obj/irc"
"fmt"
"os"
"regexp"
)
type Stick struct {
Conf *StickConf
Conns map[string]*irc.IRCConnection
}
func (st *Stick) msgDispatcher (e *irc.IRCEvent, net string) {
//make a copy of the event so it doesn't get modified (when replying to (i.e.) learn messages, the action is auto-triggered)
newevent := *e
e = &newevent
if e.Nick == st.Conf.Conf.Networks[net].Nick {
return
}
var chancfg ChanConf
for _, chancfg = range st.Conf.Conf.Networks[net].Channels {
if chancfg.Name == e.Arguments[0] {
break
}
}
for _, action := range chancfg.Actions {
action = st.replaceVars(action, e, net)
re, err := regexp.Compile(action.Match)
if err == nil && re.MatchString(e.Message) {
st.actionDispatcher(&action, e, net)
}
}
fmt.Printf("%#v\n", e)
}
func (st *Stick) replaceVars(c ChanActConf, e *irc.IRCEvent, net string) ChanActConf {
info := st.Conf.Conf.Networks[net]
re, _ := regexp.Compile(`{\$.+}`)
tmpre, err := regexp.Compile(c.Match)
if err != nil {
st.Conns[net].Privmsg(e.Arguments[0], "oops in replaceVars: " + err.String())
return c
}
f := func(s string) string{
switch s {
case "{$victim}":
s = e.Nick
case "{$message}":
s = e.Message
case "{$message-match}":
s = tmpre.ReplaceAllString(e.Message, "")
case "{$mynick}":
s = info.Nick
}
return s
}
match2 := re.ReplaceAllStringFunc(c.Match, f)
tmpre, err = regexp.Compile(match2)
if err == nil {
c.Match = match2
}
c.Parms = re.ReplaceAllStringFunc(c.Parms, f)
return c
}
func (st *Stick) actionDispatcher(act *ChanActConf, e *irc.IRCEvent, net string) {
switch act.Action {
case "say":
st.Conns[net].Privmsg(e.Arguments[0], act.Parms)
case "action":
IrcAction(e.Arguments[0], act.Parms, st.Conns[net])
case "learn":
st.Learn(act.Parms, e, net)
case "part":
st.Conns[net].Part(e.Arguments[0])
case "quit":
st.Conns[net].Quit()
default:
break
}
}
func Init(confpath *string) (*Stick, os.Error) {
st := new(Stick)
var err os.Error
st.Conf, err = InitConf(*confpath)
if err != nil {
return st, os.NewError("Couldn't read configuration: " + err.String())
}
st.Conns = make(map[string]*irc.IRCConnection)
for net, info := range st.Conf.Conf.Networks {
st.Conns[net] = irc.IRC(info.Nick, info.Realname)
if err :=st.Conns[net].Connect(net); err != nil {
fmt.Printf("%s\n", err)
fmt.Printf("%#v\n", st.Conns[net])
os.Exit(1)
}
for _, cn := range info.Channels {
st.Conns[net].Join(cn.String())
}
st.Conns[net].AddCallback("PRIVMSG", func(e *irc.IRCEvent){ st.msgDispatcher(e, net)})
st.Conns[net].Loop();
}
return st, nil
}