5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-10 03:10:26 +00:00

Add -debug option. Run go fmt. Some cleanups

This commit is contained in:
Wim 2016-03-22 00:25:47 +01:00
parent 0e6fe4070a
commit 93db74e7e1
2 changed files with 44 additions and 27 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
log "github.com/Sirupsen/logrus"
"gopkg.in/gcfg.v1" "gopkg.in/gcfg.v1"
"io/ioutil" "io/ioutil"
"log"
) )
type Config struct { type Config struct {

View File

@ -4,9 +4,9 @@ import (
"crypto/tls" "crypto/tls"
"flag" "flag"
"github.com/42wim/matterbridge/matterhook" "github.com/42wim/matterbridge/matterhook"
log "github.com/Sirupsen/logrus"
"github.com/peterhellberg/giphy" "github.com/peterhellberg/giphy"
"github.com/thoj/go-ircevent" "github.com/thoj/go-ircevent"
"log"
"strconv" "strconv"
"strings" "strings"
) )
@ -58,10 +58,10 @@ func (b *Bridge) handleNewConnection(event *irc.Event) {
func (b *Bridge) setupChannels() { func (b *Bridge) setupChannels() {
i := b.i i := b.i
log.Println("Joining", b.Config.IRC.Channel, "as", b.ircNick) log.Info("Joining ", b.Config.IRC.Channel, " as ", b.ircNick)
i.Join(b.Config.IRC.Channel) i.Join(b.Config.IRC.Channel)
for _, val := range b.Config.Token { for _, val := range b.Config.Token {
log.Println("Joining", val.IRCChannel, "as", b.ircNick) log.Info("Joining ", val.IRCChannel, " as ", b.ircNick)
i.Join(val.IRCChannel) i.Join(val.IRCChannel)
} }
i.AddCallback("PRIVMSG", b.handlePrivMsg) i.AddCallback("PRIVMSG", b.handlePrivMsg)
@ -83,16 +83,15 @@ func (b *Bridge) handlePrivMsg(event *irc.Event) {
func (b *Bridge) handleJoinPart(event *irc.Event) { func (b *Bridge) handleJoinPart(event *irc.Event) {
b.Send(b.ircNick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0])) b.Send(b.ircNick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]))
//b.SendType(b.ircNick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave")
} }
func (b *Bridge) handleNotice(event *irc.Event) { func (b *Bridge) handleNotice(event *irc.Event) {
if (strings.Contains(event.Message(), "This nickname is registered")) { if strings.Contains(event.Message(), "This nickname is registered") {
b.i.Privmsg(b.Config.IRC.NickServNick, "IDENTIFY " + b.Config.IRC.NickServPassword) b.i.Privmsg(b.Config.IRC.NickServNick, "IDENTIFY "+b.Config.IRC.NickServPassword)
} }
} }
func tableformatter (nicks_s string, nicksPerRow int) string { func tableformatter(nicks_s string, nicksPerRow int) string {
nicks := strings.Split(nicks_s, " ") nicks := strings.Split(nicks_s, " ")
result := "|IRC users" result := "|IRC users"
if nicksPerRow < 1 { if nicksPerRow < 1 {
@ -110,7 +109,7 @@ func tableformatter (nicks_s string, nicksPerRow int) string {
} }
result += nicks[0] + "|" result += nicks[0] + "|"
for i := 1; i < len(nicks); i++ { for i := 1; i < len(nicks); i++ {
if i % nicksPerRow == 0 { if i%nicksPerRow == 0 {
result += "\r\n|" + nicks[i] + "|" result += "\r\n|" + nicks[i] + "|"
} else { } else {
result += nicks[i] + "|" result += nicks[i] + "|"
@ -119,12 +118,12 @@ func tableformatter (nicks_s string, nicksPerRow int) string {
return result return result
} }
func plainformatter (nicks string, nicksPerRow int) string { func plainformatter(nicks string, nicksPerRow int) string {
return nicks + " currently on IRC" return nicks + " currently on IRC"
} }
func (b *Bridge) formatnicks (nicks string) string { func (b *Bridge) formatnicks(nicks string) string {
switch (b.Config.Mattermost.NickFormatter) { switch b.Config.Mattermost.NickFormatter {
case "table": case "table":
return tableformatter(nicks, b.Config.Mattermost.NicksPerRow) return tableformatter(nicks, b.Config.Mattermost.NicksPerRow)
default: default:
@ -137,15 +136,15 @@ func (b *Bridge) handleOther(event *irc.Event) {
case "001": case "001":
b.handleNewConnection(event) b.handleNewConnection(event)
case "353": case "353":
log.Println("handleOther", b.getMMChannel(event.Arguments[0])) log.Debug("handleOther ", b.getMMChannel(event.Arguments[0]))
b.Send(b.ircNick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0])) b.Send(b.ircNick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0]))
case "NOTICE": case "NOTICE":
b.handleNotice(event) b.handleNotice(event)
default: default:
log.Printf("UNKNOWN EVENT: %+v\n", event); log.Debugf("UNKNOWN EVENT: %+v", event)
return return
} }
log.Printf("%+v\n", event); log.Debugf("%+v", event)
} }
func (b *Bridge) Send(nick string, message string, channel string) error { func (b *Bridge) Send(nick string, message string, channel string) error {
@ -153,16 +152,25 @@ func (b *Bridge) Send(nick string, message string, channel string) error {
} }
func IsMarkup(message string) bool { func IsMarkup(message string) bool {
switch (message[0]) { switch message[0] {
case '|': fallthrough case '|':
case '#': fallthrough fallthrough
case '_': fallthrough case '#':
case '*': fallthrough fallthrough
case '~': fallthrough case '_':
case '-': fallthrough fallthrough
case ':': fallthrough case '*':
case '>': fallthrough fallthrough
case '=': return true case '~':
fallthrough
case '-':
fallthrough
case ':':
fallthrough
case '>':
fallthrough
case '=':
return true
} }
return false return false
} }
@ -183,7 +191,7 @@ func (b *Bridge) SendType(nick string, message string, channel string, mtype str
} }
err := b.m.Send(matterMessage) err := b.m.Send(matterMessage)
if err != nil { if err != nil {
log.Println(err) log.Info(err)
return err return err
} }
return nil return nil
@ -200,7 +208,7 @@ func (b *Bridge) handleMatter() {
cmd := strings.Fields(message.Text)[0] cmd := strings.Fields(message.Text)[0]
switch cmd { switch cmd {
case "!users": case "!users":
log.Println("received !users from", message.UserName) log.Info("received !users from ", message.UserName)
b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token)) b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token))
return return
case "!gif": case "!gif":
@ -244,9 +252,18 @@ func (b *Bridge) getIRCChannel(token string) string {
return ircchannel return ircchannel
} }
func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
}
func main() { func main() {
flagConfig := flag.String("conf", "matterbridge.conf", "config file") flagConfig := flag.String("conf", "matterbridge.conf", "config file")
flagDebug := flag.Bool("debug", false, "enable debug")
flag.Parse() flag.Parse()
if *flagDebug {
log.Info("enabling debug")
log.SetLevel(log.DebugLevel)
}
NewBridge("matterbot", NewConfig(*flagConfig)) NewBridge("matterbot", NewConfig(*flagConfig))
select {} select {}
} }