Add support for multiple channels

This commit is contained in:
Wim 2015-12-19 16:55:49 +01:00
parent 0dc5e042d2
commit 9f07a2cfd5
4 changed files with 74 additions and 13 deletions

View File

@ -28,6 +28,11 @@ matterbridge
2) Edit matterbridge.conf with the settings for your environment. See below for more config information. 2) Edit matterbridge.conf with the settings for your environment. See below for more config information.
3) Now you can run matterbridge. 3) Now you can run matterbridge.
```
Usage of matterbridge:
-conf="matterbridge.conf": config file
```
Matterbridge will: Matterbridge will:
* start a webserver listening on the port specified in the configuration. * start a webserver listening on the port specified in the configuration.
* connect to specified irc server and channel. * connect to specified irc server and channel.
@ -35,7 +40,7 @@ Matterbridge will:
## config ## config
### matterbridge ### matterbridge
matterbridge looks for matterbridge.conf in current directory. matterbridge looks for matterbridge.conf in current directory. (use -conf to specify another file)
Look at matterbridge.conf.sample for an example Look at matterbridge.conf.sample for an example
@ -58,10 +63,21 @@ port=9999
BindAddress="0.0.0.0" BindAddress="0.0.0.0"
showjoinpart=true #show irc users joining and parting showjoinpart=true #show irc users joining and parting
#the token you get from the outgoing webhook in mattermost. If empty no token check will be done. #the token you get from the outgoing webhook in mattermost. If empty no token check will be done.
#if you use multiple IRC channel (see below, this must be empty!)
token=yourtokenfrommattermost token=yourtokenfrommattermost
#disable certificate checking (selfsigned certificates) #disable certificate checking (selfsigned certificates)
#SkipTLSVerify=true #SkipTLSVerify=true
#multiple channel config
#token you can find in your outgoing webhook
[Token "outgoingwebhooktoken1"]
IRCChannel="#off-topic"
MMChannel="off-topic"
[Token "outgoingwebhooktoken2"]
IRCChannel="#testing"
MMChannel="testing"
[general] [general]
#request your API key on https://github.com/giphy/GiphyAPI. This is a public beta key #request your API key on https://github.com/giphy/GiphyAPI. This is a public beta key
GiphyApiKey="dc6zaTOxFJmzC" GiphyApiKey="dc6zaTOxFJmzC"

View File

@ -25,6 +25,10 @@ type Config struct {
BindAddress string BindAddress string
Channel string Channel string
} }
Token map[string]*struct {
IRCChannel string
MMChannel string
}
General struct { General struct {
GiphyAPIKey string GiphyAPIKey string
} }

View File

@ -10,10 +10,22 @@ channel="#matterbridge"
url="http://yourdomain/hooks/yourhookkey" url="http://yourdomain/hooks/yourhookkey"
port=9999 port=9999
showjoinpart=true showjoinpart=true
#token=yourtokenfrommattermost #remove token when using multiple channels!
token=yourtokenfrommattermost
IconURL="http://youricon.png" IconURL="http://youricon.png"
#SkipTLSVerify=true #SkipTLSVerify=true
#BindAddress="0.0.0.0" #BindAddress="0.0.0.0"
[general] [general]
GiphyAPIKey=dc6zaTOxFJmzC GiphyAPIKey=dc6zaTOxFJmzC
#multiple channel config
#token you can find in your outgoing webhook
[Token "outgoingwebhooktoken1"]
IRCChannel="#off-topic"
MMChannel="off-topic"
[Token "outgoingwebhooktoken2"]
IRCChannel="#testing"
MMChannel="testing"

View File

@ -15,12 +15,19 @@ import (
type Bridge struct { type Bridge struct {
i *irc.Connection i *irc.Connection
m *matterhook.Client m *matterhook.Client
cmap map[string]string
*Config *Config
} }
func NewBridge(name string, config *Config) *Bridge { func NewBridge(name string, config *Config) *Bridge {
b := &Bridge{} b := &Bridge{}
b.Config = config b.Config = config
b.cmap = make(map[string]string)
if len(b.Config.Token) > 0 {
for _, val := range b.Config.Token {
b.cmap[val.IRCChannel] = val.MMChannel
}
}
b.m = matterhook.New(b.Config.Mattermost.URL, b.m = matterhook.New(b.Config.Mattermost.URL,
matterhook.Config{Port: b.Config.Mattermost.Port, Token: b.Config.Mattermost.Token, matterhook.Config{Port: b.Config.Mattermost.Port, Token: b.Config.Mattermost.Token,
InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify, InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify,
@ -38,13 +45,17 @@ func (b *Bridge) createIRC(name string) *irc.Connection {
time.Sleep(time.Second) time.Sleep(time.Second)
log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick) log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick)
i.Join(b.Config.IRC.Channel) i.Join(b.Config.IRC.Channel)
for _, val := range b.Config.Token {
log.Println("Joining", val.IRCChannel, "as", b.Config.IRC.Nick)
i.Join(val.IRCChannel)
}
i.AddCallback("PRIVMSG", b.handlePrivMsg) i.AddCallback("PRIVMSG", b.handlePrivMsg)
i.AddCallback("CTCP_ACTION", b.handlePrivMsg) i.AddCallback("CTCP_ACTION", b.handlePrivMsg)
if b.Config.Mattermost.ShowJoinPart { if b.Config.Mattermost.ShowJoinPart {
i.AddCallback("JOIN", b.handleJoinPart) i.AddCallback("JOIN", b.handleJoinPart)
i.AddCallback("PART", b.handleJoinPart) i.AddCallback("PART", b.handleJoinPart)
} }
i.AddCallback("353", b.handleOther) //i.AddCallback("353", b.handleOther)
return i return i
} }
@ -54,18 +65,19 @@ func (b *Bridge) handlePrivMsg(event *irc.Event) {
msg = event.Nick + " " msg = event.Nick + " "
} }
msg += event.Message() msg += event.Message()
b.Send("irc-"+event.Nick, msg, b.Config.Mattermost.Channel) b.Send("irc-"+event.Nick, msg, b.getMMChannel(event.Arguments[0]))
} }
func (b *Bridge) handleJoinPart(event *irc.Event) { func (b *Bridge) handleJoinPart(event *irc.Event) {
b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.Send(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]))
b.Config.Mattermost.Channel, "join_leave") //b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave")
} }
func (b *Bridge) handleOther(event *irc.Event) { func (b *Bridge) handleOther(event *irc.Event) {
switch event.Code { switch event.Code {
case "353": case "353":
b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC", b.Config.Mattermost.Channel) log.Println("handleOther", b.getMMChannel(event.Arguments[0]))
b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC", b.getMMChannel(event.Arguments[0]))
} }
} }
@ -94,14 +106,14 @@ func (b *Bridge) handleMatter() {
switch cmd { switch cmd {
case "!users": case "!users":
log.Println("received !users from", message.UserName) log.Println("received !users from", message.UserName)
b.i.SendRaw("NAMES " + b.Config.IRC.Channel) b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token))
case "!gif": case "!gif":
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1))) message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
b.Send(b.Config.IRC.Nick, message.Text, b.Config.Mattermost.Channel) b.Send(b.Config.IRC.Nick, message.Text, b.getIRCChannel(message.Token))
} }
texts := strings.Split(message.Text, "\n") texts := strings.Split(message.Text, "\n")
for _, text := range texts { for _, text := range texts {
b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+text) b.i.Privmsg(b.getIRCChannel(message.Token), message.UserName+": "+text)
} }
} }
} }
@ -118,6 +130,23 @@ func (b *Bridge) giphyRandom(query []string) string {
return res.Data.FixedHeightDownsampledURL return res.Data.FixedHeightDownsampledURL
} }
func (b *Bridge) getMMChannel(ircChannel string) string {
mmchannel, ok := b.cmap[ircChannel]
if !ok {
mmchannel = b.Config.Mattermost.Channel
}
return mmchannel
}
func (b *Bridge) getIRCChannel(token string) string {
ircchannel := b.Config.IRC.Channel
_, ok := b.Config.Token[token]
if ok {
ircchannel = b.Config.Token[token].IRCChannel
}
return ircchannel
}
func main() { func main() {
flagConfig := flag.String("conf", "matterbridge.conf", "config file") flagConfig := flag.String("conf", "matterbridge.conf", "config file")
flag.Parse() flag.Parse()