-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
78 lines (66 loc) · 1.48 KB
/
remote.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
package main
import (
"crypto/tls"
"fmt"
"github.com/v0l/go-ircevent"
)
type RemoteIrc struct {
Main *irc.Connection `json:"-"`
I *irc.Connection `json:"-"`
Server string `json:"server"`
Nick string `json:"nick"`
Channels []string `json:"channels"`
Ssl bool `json:"ssl"`
}
func (r *RemoteIrc) HasChan(ch string) bool {
for _, v := range r.Channels {
if v == ch {
return true
}
}
return false
}
func (r *RemoteIrc) OnPrivMsg(e *irc.Event) {
r.Main.Privmsgf(fmt.Sprintf("#m_%v", e.Arguments[0]), "<%s>: %s", e.Nick, e.Message())
}
func (r *RemoteIrc) JoinChan(ch string) {
r.I.Join(ch)
r.Main.Join(fmt.Sprintf("#m_%v", ch))
r.Main.SendRawf("TOPIC %v :%v\n", ch, r.Server)
}
func (r *RemoteIrc) Run(server, nick, ch string, t bool) bool {
r.Server = server
r.Nick = nick
r.Channels = append(r.Channels, ch)
r.Ssl = t
return r.Start()
}
func (r *RemoteIrc) Start() bool {
r.I = irc.IRC(r.Nick, r.Nick)
r.I.UseTLS = r.Ssl
r.I.TLSConfig = &tls.Config{InsecureSkipVerify: true}
err := r.I.Connect(r.Server)
if err == nil {
r.I.AddCallback("001", func(e *irc.Event) {
for _, v := range r.Channels {
r.JoinChan(v)
}
})
r.I.AddCallback("PRIVMSG", r.OnPrivMsg)
return true
}
return false
}
func (r *RemoteIrc) SendPrivmsg(msg string, ch string) {
if r.I != nil {
r.I.Privmsg(ch, msg)
}
}
func (r *RemoteIrc) Stop() {
if r.I != nil {
if r.I.Connected() {
r.I.Quit()
r.I.Disconnect()
}
}
}