Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit baaeda364b39f245eb5e48b011c60fbb2eccd320 Author: Wim <wim@42.be> Date: Tue Nov 01 22:52:28 2016 +0000 Add anti-flooding settings (irc). See #40 diff --git a/bridge/config/config.go b/bridge/config/config.go index aa881a7..8922289 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -346 +348 @@ type Protocol struct {   Password string // IRC,mattermost,XMPP   PrefixMessagesWithNick bool // mattemost, slack   Protocol string //all protocols + MessageQueue int // IRC, size of message queue for flood control + MessageDelay int // IRC, time in millisecond to wait between messages   RemoteNickFormat string // all protocols   Server string // IRC,mattermost,XMPP,discord   ShowJoinPart bool // all protocols diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index e5b6858..30f2772 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -236 +237 @@ type Birc struct {   protocol string   Remote chan config.Message   connected chan struct{} + Local chan config.Message // local queue for flood control  }    var flog *log.Entry @@ -3215 +3322 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(config config.Protocol, origin string, c chan config.Message) *Birc { +func New(cfg config.Protocol, origin string, c chan config.Message) *Birc {   b := &Birc{} - b.Config = &config + b.Config = &cfg   b.Nick = b.Config.Nick   b.Remote = c   b.names = make(map[string][]string)   b.origin = origin   b.protocol = protocol   b.connected = make(chan struct{}) + if b.Config.MessageDelay == 0 { + b.Config.MessageDelay = 1300 + } + if b.Config.MessageQueue == 0 { + b.Config.MessageQueue = 30 + } + b.Local = make(chan config.Message, b.Config.MessageQueue+10)   return b  }   @@ -816 +897 @@ func (b *Birc) Connect() error {   return fmt.Errorf("connection timed out")   }   i.Debug = false + go b.doSend()   return nil  }   @@ -11511 +12427 @@ func (b *Birc) Send(msg config.Message) error {   return nil   }   for _, text := range strings.Split(msg.Text, "\n") { - b.i.Privmsg(msg.Channel, msg.Username+text) + if len(b.Local) < b.Config.MessageQueue { + if len(b.Local) == b.Config.MessageQueue-1 { + text = text + " <message clipped>" + } + b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel} + } else { + flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local)) + }   }   return nil  }   +func (b *Birc) doSend() { + rate := time.Millisecond * time.Duration(b.Config.MessageDelay) + throttle := time.Tick(rate) + for msg := range b.Local { + <-throttle + b.i.Privmsg(msg.Channel, msg.Username+msg.Text) + } +} +  func (b *Birc) endNames(event *irc.Event) {   channel := event.Arguments[1]   sort.Strings(b.names[channel])