summaryrefslogtreecommitdiffstats
path: root/bridge/irc/irc.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2018-02-27 00:33:21 +0100
committerWim <wim@42.be>2018-02-27 23:22:12 +0100
commit2bac8673829516d8314c459133e85a8c0cc1959c (patch)
treeed4123232727f9c53c685b1687725206f0b02cb6 /bridge/irc/irc.go
parent5fbd8a3be05cdecdde515ad459b7a5d87b056956 (diff)
downloadmatterbridge-msglm-2bac8673829516d8314c459133e85a8c0cc1959c.tar.gz
matterbridge-msglm-2bac8673829516d8314c459133e85a8c0cc1959c.tar.bz2
matterbridge-msglm-2bac8673829516d8314c459133e85a8c0cc1959c.zip
Refactor using factory
Diffstat (limited to 'bridge/irc/irc.go')
-rw-r--r--bridge/irc/irc.go61
1 files changed, 27 insertions, 34 deletions
diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go
index fe517331..afe436bc 100644
--- a/bridge/irc/irc.go
+++ b/bridge/irc/irc.go
@@ -4,13 +4,13 @@ import (
"bytes"
"crypto/tls"
"fmt"
+ "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/lrstanley/girc"
"github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data"
"github.com/saintfish/chardet"
- log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net"
@@ -33,14 +33,7 @@ type Birc struct {
*config.BridgeConfig
}
-var flog *log.Entry
-var protocol = "irc"
-
-func init() {
- flog = log.WithFields(log.Fields{"prefix": protocol})
-}
-
-func New(cfg *config.BridgeConfig) *Birc {
+func New(cfg *config.BridgeConfig) bridge.Bridger {
b := &Birc{}
b.BridgeConfig = cfg
b.Nick = b.Config.Nick
@@ -71,7 +64,7 @@ func (b *Birc) Command(msg *config.Message) string {
func (b *Birc) Connect() error {
b.Local = make(chan config.Message, b.Config.MessageQueue+10)
- flog.Infof("Connecting %s", b.Config.Server)
+ b.Log.Infof("Connecting %s", b.Config.Server)
server, portstr, err := net.SplitHostPort(b.Config.Server)
if err != nil {
return err
@@ -112,8 +105,8 @@ func (b *Birc) Connect() error {
go func() {
for {
if err := i.Connect(); err != nil {
- flog.Errorf("error: %s", err)
- flog.Info("reconnecting in 30 seconds...")
+ b.Log.Errorf("error: %s", err)
+ b.Log.Info("reconnecting in 30 seconds...")
time.Sleep(30 * time.Second)
i.Handlers.Clear(girc.RPL_WELCOME)
i.Handlers.Add(girc.RPL_WELCOME, func(client *girc.Client, event girc.Event) {
@@ -129,7 +122,7 @@ func (b *Birc) Connect() error {
b.i = i
select {
case <-b.connected:
- flog.Info("Connection succeeded")
+ b.Log.Info("Connection succeeded")
case <-time.After(time.Second * 30):
return fmt.Errorf("connection timed out")
}
@@ -149,7 +142,7 @@ func (b *Birc) Disconnect() error {
func (b *Birc) JoinChannel(channel config.ChannelInfo) error {
if channel.Options.Key != "" {
- flog.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
+ b.Log.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
b.i.Cmd.JoinKey(channel.Name, channel.Options.Key)
} else {
b.i.Cmd.Join(channel.Name)
@@ -163,7 +156,7 @@ func (b *Birc) Send(msg config.Message) (string, error) {
return "", nil
}
- flog.Debugf("Receiving %#v", msg)
+ b.Log.Debugf("Receiving %#v", msg)
// Execute a command
if strings.HasPrefix(msg.Text, "!") {
@@ -175,7 +168,7 @@ func (b *Birc) Send(msg config.Message) (string, error) {
buf := new(bytes.Buffer)
w, err := charset.NewWriter(b.Config.Charset, buf)
if err != nil {
- flog.Errorf("charset from utf-8 conversion failed: %s", err)
+ b.Log.Errorf("charset from utf-8 conversion failed: %s", err)
return "", err
}
fmt.Fprintf(w, msg.Text)
@@ -221,7 +214,7 @@ func (b *Birc) Send(msg config.Message) (string, error) {
}
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
} else {
- flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
+ b.Log.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
}
}
return "", nil
@@ -259,7 +252,7 @@ func (b *Birc) endNames(client *girc.Client, event girc.Event) {
}
func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
- flog.Debug("Registering callbacks")
+ b.Log.Debug("Registering callbacks")
i := b.i
b.Nick = event.Params[0]
@@ -278,31 +271,31 @@ func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
if len(event.Params) == 0 {
- flog.Debugf("handleJoinPart: empty Params? %#v", event)
+ b.Log.Debugf("handleJoinPart: empty Params? %#v", event)
return
}
channel := strings.ToLower(event.Params[0])
if event.Command == "KICK" {
- flog.Infof("Got kicked from %s by %s", channel, event.Source.Name)
+ b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
time.Sleep(time.Duration(b.Config.RejoinDelay) * time.Second)
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
return
}
if event.Command == "QUIT" {
if event.Source.Name == b.Nick && strings.Contains(event.Trailing, "Ping timeout") {
- flog.Infof("%s reconnecting ..", b.Account)
+ b.Log.Infof("%s reconnecting ..", b.Account)
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EVENT_FAILURE}
return
}
}
if event.Source.Name != b.Nick {
- flog.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
+ b.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EVENT_JOIN_LEAVE}
- flog.Debugf("Message is %#v", msg)
+ b.Log.Debugf("Message is %#v", msg)
b.Remote <- msg
return
}
- flog.Debugf("handle %#v", event)
+ b.Log.Debugf("handle %#v", event)
}
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
@@ -317,7 +310,7 @@ func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
if b.Config.DebugLevel == 1 {
if event.Command != "CLIENT_STATE_UPDATED" &&
event.Command != "CLIENT_GENERAL_UPDATED" {
- flog.Debugf("%#v", event.String())
+ b.Log.Debugf("%#v", event.String())
}
return
}
@@ -325,12 +318,12 @@ func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
case "372", "375", "376", "250", "251", "252", "253", "254", "255", "265", "266", "002", "003", "004", "005":
return
}
- flog.Debugf("%#v", event.String())
+ b.Log.Debugf("%#v", event.String())
}
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
if strings.EqualFold(b.Config.NickServNick, "Q@CServe.quakenet.org") {
- flog.Debugf("Authenticating %s against %s", b.Config.NickServUsername, b.Config.NickServNick)
+ b.Log.Debugf("Authenticating %s against %s", b.Config.NickServUsername, b.Config.NickServNick)
b.i.Cmd.Message(b.Config.NickServNick, "AUTH "+b.Config.NickServUsername+" "+b.Config.NickServPassword)
}
}
@@ -359,7 +352,7 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
return
}
rmsg := config.Message{Username: event.Source.Name, Channel: strings.ToLower(event.Params[0]), Account: b.Account, UserID: event.Source.Ident + "@" + event.Source.Host}
- flog.Debugf("Receiving PRIVMSG: %s %s %#v", event.Source.Name, event.Trailing, event)
+ b.Log.Debugf("Receiving PRIVMSG: %s %s %#v", event.Source.Name, event.Trailing, event)
// set action event
if event.IsAction() {
@@ -382,10 +375,10 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
detector := chardet.NewTextDetector()
result, err := detector.DetectBest([]byte(rmsg.Text))
if err != nil {
- flog.Infof("detection failed for rmsg.Text: %#v", rmsg.Text)
+ b.Log.Infof("detection failed for rmsg.Text: %#v", rmsg.Text)
return
}
- flog.Debugf("detected %s confidence %#v", result.Charset, result.Confidence)
+ b.Log.Debugf("detected %s confidence %#v", result.Charset, result.Confidence)
mycharset = result.Charset
// if we're not sure, just pick ISO-8859-1
if result.Confidence < 80 {
@@ -394,13 +387,13 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
}
r, err = charset.NewReader(mycharset, strings.NewReader(rmsg.Text))
if err != nil {
- flog.Errorf("charset to utf-8 conversion failed: %s", err)
+ b.Log.Errorf("charset to utf-8 conversion failed: %s", err)
return
}
output, _ := ioutil.ReadAll(r)
rmsg.Text = string(output)
- flog.Debugf("Sending message from %s on %s to gateway", event.Params[0], b.Account)
+ b.Log.Debugf("Sending message from %s on %s to gateway", event.Params[0], b.Account)
b.Remote <- rmsg
}
@@ -408,13 +401,13 @@ func (b *Birc) handleTopicWhoTime(client *girc.Client, event girc.Event) {
parts := strings.Split(event.Params[2], "!")
t, err := strconv.ParseInt(event.Params[3], 10, 64)
if err != nil {
- flog.Errorf("Invalid time stamp: %s", event.Params[3])
+ b.Log.Errorf("Invalid time stamp: %s", event.Params[3])
}
user := parts[0]
if len(parts) > 1 {
user += " [" + parts[1] + "]"
}
- flog.Debugf("%s: Topic set by %s [%s]", event.Command, user, time.Unix(t, 0))
+ b.Log.Debugf("%s: Topic set by %s [%s]", event.Command, user, time.Unix(t, 0))
}
func (b *Birc) nicksPerRow() int {