|
@@ -1,7 +1,6 @@
|
1
|
1
|
package main
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
|
- "bytes"
|
5
|
4
|
"crypto/tls"
|
6
|
5
|
"fmt"
|
7
|
6
|
"github.com/irccloud/irccat/httplistener"
|
|
@@ -10,9 +9,7 @@ import (
|
10
|
9
|
"github.com/spf13/viper"
|
11
|
10
|
"github.com/thoj/go-ircevent"
|
12
|
11
|
"os"
|
13
|
|
- "os/exec"
|
14
|
12
|
"os/signal"
|
15
|
|
- "strings"
|
16
|
13
|
"syscall"
|
17
|
14
|
)
|
18
|
15
|
|
|
@@ -20,6 +17,7 @@ var log = loggo.GetLogger("main")
|
20
|
17
|
|
21
|
18
|
type IRCCat struct {
|
22
|
19
|
control_chan string
|
|
20
|
+ auth_users map[string]bool
|
23
|
21
|
irc *irc.Connection
|
24
|
22
|
tcp *tcplistener.TCPListener
|
25
|
23
|
signals chan os.Signal
|
|
@@ -38,9 +36,8 @@ func main() {
|
38
|
36
|
log.Errorf("Error reading config file - exiting. I'm looking for irccat.[json|yaml|toml|hcl] in . or /etc")
|
39
|
37
|
return
|
40
|
38
|
}
|
|
39
|
+ irccat := IRCCat{auth_users: map[string]bool{}, signals: make(chan os.Signal, 1)}
|
41
|
40
|
|
42
|
|
- irccat := IRCCat{}
|
43
|
|
- irccat.signals = make(chan os.Signal, 1)
|
44
|
41
|
signal.Notify(irccat.signals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
45
|
42
|
go irccat.signalHandler()
|
46
|
43
|
|
|
@@ -71,10 +68,9 @@ func (i *IRCCat) signalHandler() {
|
71
|
68
|
|
72
|
69
|
func (i *IRCCat) connectIRC() error {
|
73
|
70
|
irccon := irc.IRC(viper.GetString("irc.nick"), viper.GetString("irc.realname"))
|
74
|
|
- irccon.Debug = true
|
75
|
71
|
irccon.UseTLS = viper.GetBool("irc.tls")
|
76
|
72
|
if viper.GetBool("irc.tls_skip_verify") {
|
77
|
|
- irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true, MaxVersion: tls.VersionTLS11}
|
|
73
|
+ irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
78
|
74
|
}
|
79
|
75
|
|
80
|
76
|
err := irccon.Connect(viper.GetString("irc.server"))
|
|
@@ -89,67 +85,17 @@ func (i *IRCCat) connectIRC() error {
|
89
|
85
|
}
|
90
|
86
|
})
|
91
|
87
|
|
|
88
|
+ irccon.AddCallback("353", i.handleNames)
|
|
89
|
+ irccon.AddCallback("JOIN", i.handleJoin)
|
|
90
|
+ irccon.AddCallback("PART", i.handlePart)
|
|
91
|
+ irccon.AddCallback("QUIT", i.handlePart)
|
|
92
|
+
|
92
|
93
|
i.irc = irccon
|
93
|
94
|
return nil
|
94
|
95
|
}
|
95
|
96
|
|
96
|
|
-func (i *IRCCat) authorisedUser(nick string) bool {
|
97
|
|
- return false
|
98
|
|
-}
|
99
|
|
-
|
100
|
97
|
func (i *IRCCat) handleWelcome(e *irc.Event) {
|
101
|
98
|
for _, channel := range viper.GetStringSlice("irc.channels") {
|
102
|
99
|
i.irc.Join(channel)
|
103
|
100
|
}
|
104
|
101
|
}
|
105
|
|
-
|
106
|
|
-func (i *IRCCat) handleCommand(event *irc.Event) {
|
107
|
|
- msg := event.Message()
|
108
|
|
- respond_to := event.Arguments[0]
|
109
|
|
-
|
110
|
|
- if respond_to[0] != '#' && !i.authorisedUser(event.Nick) {
|
111
|
|
- // Command not in a channel, or not from an authorised user
|
112
|
|
- log.Infof("Unauthorised command: %s (%s) %s", event.Nick, respond_to, msg)
|
113
|
|
- return
|
114
|
|
- }
|
115
|
|
- log.Infof("Authorised command: %s (%s) %s", event.Nick, respond_to, msg)
|
116
|
|
-
|
117
|
|
- channel := ""
|
118
|
|
- if respond_to[0] == '#' {
|
119
|
|
- channel = respond_to
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- parts := strings.SplitN(msg, " ", 1)
|
123
|
|
-
|
124
|
|
- var cmd *exec.Cmd
|
125
|
|
- if len(parts) == 1 {
|
126
|
|
- cmd = exec.Command(viper.GetString("commands.handler"), event.Nick, channel, respond_to, parts[0][1:])
|
127
|
|
- } else {
|
128
|
|
- cmd = exec.Command(viper.GetString("commands.handler"), event.Nick, channel, respond_to, parts[0][1:], parts[1])
|
129
|
|
- }
|
130
|
|
- i.runCommand(cmd, respond_to)
|
131
|
|
-}
|
132
|
|
-
|
133
|
|
-// Run a command with the output going to the nick/channel identified by respond_to
|
134
|
|
-func (i *IRCCat) runCommand(cmd *exec.Cmd, respond_to string) {
|
135
|
|
- var out bytes.Buffer
|
136
|
|
- cmd.Stdout = &out
|
137
|
|
- cmd.Stderr = &out
|
138
|
|
- err := cmd.Run()
|
139
|
|
- if err != nil {
|
140
|
|
- log.Errorf("Running command %s failed: %s", cmd.Args, err)
|
141
|
|
- i.irc.Privmsgf(respond_to, "Command failed: %s", err)
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- lines := strings.Split(out.String(), "\n")
|
145
|
|
- line_count := len(lines)
|
146
|
|
- if line_count > viper.GetInt("commands.max_response_lines") {
|
147
|
|
- line_count = viper.GetInt("commands.max_response_lines")
|
148
|
|
- }
|
149
|
|
-
|
150
|
|
- for _, line := range lines[0:line_count] {
|
151
|
|
- if line != "" {
|
152
|
|
- i.irc.Privmsg(respond_to, line)
|
153
|
|
- }
|
154
|
|
- }
|
155
|
|
-}
|