From cbd01d4a552f05f36ef3d8b259c4305cd3ce06c8 Mon Sep 17 00:00:00 2001 From: Wim Date: Fri, 23 Oct 2015 22:34:37 +0200 Subject: [PATCH] Initial matterbridge commit --- README.md | 69 ++++++++++++++++++++++++++++++++++++++++ config.go | 35 ++++++++++++++++++++ matterbridge.conf.sample | 11 +++++++ matterbridge.go | 56 ++++++++++++++++++++++++++++++++ matterhook/matterhook.go | 1 - 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 config.go create mode 100644 matterbridge.conf.sample create mode 100644 matterbridge.go diff --git a/README.md b/README.md new file mode 100644 index 00000000..6b92d12c --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# matterbridge + +Simple bridge between mattermost and IRC. Uses the in/outgoing webhooks. +Relays public channel messages between mattermost and IRC. + +Work in progress. + +## building +Make sure you have [Go](https://golang.org/doc/install) properly installed, including setting up your [GOPATH] (https://golang.org/doc/code.html#GOPATH) + +``` +cd $GOPATH +go get https://github.com/42wim/matterbridge +``` + +You should now have matterbridge binary in the bin directory: + +``` +$ ls bin/ +matterbridge +``` + +## running +1) Copy the matterbridge.conf.sample to matterbridge.conf in the same directory as the matterbridge binary. +2) Edit matterbridge.conf with the settings for your environment. See below for more config information. +3) Now you can run matterbridge. + +Matterbridge will: +* start a webserver listening on the port specified in the configuration. +* connect to specified irc server and channel. +* send messages from mattermost to irc and vice versa, messages in mattermost will appear with irc-nick + +## config +### matterbridge +matterbridge looks for matterbridge.conf in current directory. + +Look at matterbridge.conf.sample for an example + + +``` +[IRC] +server="irc.freenode.net" +port=6667 +UseTLS=false +SkipTLSVerify=true +nick="matterbot" +channel="#matterbridge" + +[mattermost] +#url is your incoming webhook url (account settings - integrations - incoming webhooks) +url="http://mattermost.yourdomain.com/hooks/incomingwebhookkey" +#port the bridge webserver will listen on +port=9999 +``` + +### mattermost +You'll have to configure the incoming en outgoing webhooks. + +* incoming webhooks +Go to "account settings" - integrations - "incoming webhooks". +Choose a channel at "Add a new incoming webhook", this will create a webhook URL right below. +This URL should be set in the matterbridge.conf in the [mattermost] section (see above) + +* outgoing webhooks +Go to "account settings" - integrations - "outgoing webhooks". +Choose a channel (the same as the one from incoming webhooks) and fill in the address and port of the server matterbridge will run on. + +e.g. http://192.168.1.1:9999 (9999 is the port specified in [mattermost] section of matterbridge.conf) + diff --git a/config.go b/config.go new file mode 100644 index 00000000..17d4bbd8 --- /dev/null +++ b/config.go @@ -0,0 +1,35 @@ +package main + +import ( + "gopkg.in/gcfg.v1" + "io/ioutil" + "log" +) + +type Config struct { + IRC struct { + UseTLS bool + SkipTLSVerify bool + Server string + Port int + Nick string + Channel string + } + Mattermost struct { + URL string + Port int + } +} + +func NewConfig(cfgfile string) *Config { + var cfg Config + content, err := ioutil.ReadFile(cfgfile) + if err != nil { + log.Fatal(err) + } + err = gcfg.ReadStringInto(&cfg, string(content)) + if err != nil { + log.Fatal("Failed to parse "+cfgfile+":", err) + } + return &cfg +} diff --git a/matterbridge.conf.sample b/matterbridge.conf.sample new file mode 100644 index 00000000..a7cb275f --- /dev/null +++ b/matterbridge.conf.sample @@ -0,0 +1,11 @@ +[IRC] +server="irc.freenode.net" +port=6667 +UseTLS=false +SkipTLSVerify=true +nick="matterbot" +channel="#matterbridge" + +[mattermost] +url="http://yourdomain/hooks/yourhookkey" +port=9999 diff --git a/matterbridge.go b/matterbridge.go new file mode 100644 index 00000000..4ba3c00a --- /dev/null +++ b/matterbridge.go @@ -0,0 +1,56 @@ +package main + +import ( + "crypto/tls" + "github.com/42wim/matterbridge/matterhook" + "github.com/thoj/go-ircevent" + "log" + "strconv" + "time" +) + +type Bridge struct { + i *irc.Connection + m *matterhook.Client + *Config +} + +func NewBridge(name string, config *Config) *Bridge { + b := &Bridge{} + b.Config = config + b.m = matterhook.New(b.Config.Mattermost.URL, matterhook.Config{Port: b.Config.Mattermost.Port}) + b.i = b.createIRC(name) + go b.handleMatter() + return b +} + +func (b *Bridge) createIRC(name string) *irc.Connection { + i := irc.IRC(b.Config.IRC.Nick, b.Config.IRC.Nick) + i.UseTLS = b.Config.IRC.UseTLS + i.TLSConfig = &tls.Config{InsecureSkipVerify: b.Config.IRC.SkipTLSVerify} + i.Connect(b.Config.IRC.Server + ":" + strconv.Itoa(b.Config.IRC.Port)) + time.Sleep(time.Second) + log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick) + i.Join(b.Config.IRC.Channel) + i.AddCallback("PRIVMSG", b.handlePrivMsg) + return i +} + +func (b *Bridge) handlePrivMsg(event *irc.Event) { + matterMessage := matterhook.OMessage{} + matterMessage.Text = event.Message() + matterMessage.UserName = "irc-" + event.Nick + b.m.Send(matterMessage) +} + +func (b *Bridge) handleMatter() { + for { + message := b.m.Receive() + b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+message.Text) + } +} + +func main() { + NewBridge("matterbot", NewConfig("matterbridge.conf")) + select {} +} diff --git a/matterhook/matterhook.go b/matterhook/matterhook.go index 2f299cbb..29e5775e 100644 --- a/matterhook/matterhook.go +++ b/matterhook/matterhook.go @@ -51,7 +51,6 @@ type Config struct { // New Mattermost client. func New(url string, config Config) *Client { c := &Client{url: url, In: make(chan IMessage), Out: make(chan OMessage), Config: config} - log.Println(config.Port) if c.Port == 0 { c.Port = 9999 }