Browse Source

Config reloading

tags/v0.1.0
Russ Garrett 7 years ago
parent
commit
c4f2aab120
No account linked to committer's email address
3 changed files with 43 additions and 6 deletions
  1. 3
    4
      auth.go
  2. 1
    0
      irccat.json
  3. 39
    2
      main.go

+ 3
- 4
auth.go View File

@@ -1,7 +1,6 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"github.com/spf13/viper"
5 4
 	"github.com/thoj/go-ircevent"
6 5
 	"strings"
7 6
 )
@@ -12,19 +11,19 @@ func (i *IRCCat) authorisedUser(nick string) bool {
12 11
 }
13 12
 
14 13
 func (i *IRCCat) handleJoin(e *irc.Event) {
15
-	if e.Arguments[0] == viper.GetString("commands.auth_channel") {
14
+	if e.Arguments[0] == i.auth_channel {
16 15
 		i.auth_users[e.Nick] = true
17 16
 	}
18 17
 }
19 18
 
20 19
 func (i *IRCCat) handlePart(e *irc.Event) {
21
-	if e.Arguments[0] == viper.GetString("commands.auth_channel") {
20
+	if e.Arguments[0] == i.auth_channel {
22 21
 		delete(i.auth_users, e.Nick)
23 22
 	}
24 23
 }
25 24
 
26 25
 func (i *IRCCat) handleNames(e *irc.Event) {
27
-	if e.Arguments[2] == viper.GetString("commands.auth_channel") {
26
+	if e.Arguments[2] == i.auth_channel {
28 27
 		nicks := strings.Split(e.Arguments[3], " ")
29 28
 		for _, nick := range nicks {
30 29
 			i.auth_users[nick] = true

+ 1
- 0
irccat.json View File

@@ -15,6 +15,7 @@
15 15
     "tls_skip_verify": true,
16 16
     "nick": "irccat2",
17 17
     "realname": "IRCCat",
18
+    "identify_pass": "",
18 19
     "channels": ["#russtest"]
19 20
   },
20 21
   "commands": {

+ 39
- 2
main.go View File

@@ -3,6 +3,8 @@ package main
3 3
 import (
4 4
 	"crypto/tls"
5 5
 	"fmt"
6
+	"github.com/deckarep/golang-set"
7
+	"github.com/fsnotify/fsnotify"
6 8
 	"github.com/irccloud/irccat/httplistener"
7 9
 	"github.com/irccloud/irccat/tcplistener"
8 10
 	"github.com/juju/loggo"
@@ -16,7 +18,8 @@ import (
16 18
 var log = loggo.GetLogger("main")
17 19
 
18 20
 type IRCCat struct {
19
-	control_chan string
21
+	auth_channel string
22
+	channels     mapset.Set
20 23
 	auth_users   map[string]bool
21 24
 	irc          *irc.Connection
22 25
 	tcp          *tcplistener.TCPListener
@@ -36,7 +39,14 @@ func main() {
36 39
 		log.Errorf("Error reading config file - exiting. I'm looking for irccat.[json|yaml|toml|hcl] in . or /etc")
37 40
 		return
38 41
 	}
39
-	irccat := IRCCat{auth_users: map[string]bool{}, signals: make(chan os.Signal, 1)}
42
+
43
+	irccat := IRCCat{auth_users: map[string]bool{},
44
+		signals:      make(chan os.Signal, 1),
45
+		channels:     mapset.NewSet(),
46
+		auth_channel: viper.GetString("commands.auth_channel")}
47
+
48
+	viper.WatchConfig()
49
+	viper.OnConfigChange(irccat.handleConfigChange)
40 50
 
41 51
 	signal.Notify(irccat.signals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
42 52
 	go irccat.signalHandler()
@@ -97,8 +107,35 @@ func (i *IRCCat) connectIRC() error {
97 107
 }
98 108
 
99 109
 func (i *IRCCat) handleWelcome(e *irc.Event) {
110
+	if viper.IsSet("irc.identify_pass") && viper.GetString("irc.identify_pass") != "" {
111
+		i.irc.SendRawf("NICKSERV IDENTIFY %s", viper.GetString("irc.identify_pass"))
112
+	}
113
+
100 114
 	log.Infof("Connected, joining channels...")
101 115
 	for _, channel := range viper.GetStringSlice("irc.channels") {
102 116
 		i.irc.Join(channel)
117
+		i.channels.Add(channel)
118
+	}
119
+}
120
+
121
+func (i *IRCCat) handleConfigChange(e fsnotify.Event) {
122
+	log.Infof("Reloaded config")
123
+
124
+	new_channels := mapset.NewSet()
125
+
126
+	for _, channel := range viper.GetStringSlice("irc.channels") {
127
+		new_channels.Add(channel)
128
+		if !i.channels.Contains(channel) {
129
+			log.Infof("Joining new channel %s", channel)
130
+			i.irc.Join(channel)
131
+			i.channels.Add(channel)
132
+		}
133
+	}
134
+
135
+	it := i.channels.Difference(new_channels).Iterator()
136
+	for channel := range it.C {
137
+		log.Infof("Leaving channel %s", channel)
138
+		i.irc.Part(channel.(string))
139
+		i.channels.Remove(channel)
103 140
 	}
104 141
 }

Loading…
Cancel
Save