From ba636e9617a6a0bf7d2eb46c203592d124889168 Mon Sep 17 00:00:00 2001 From: Olen Date: Fri, 2 Oct 2020 14:54:12 +0200 Subject: [PATCH] IRC Add simple tag-parsing of colors and highlights in alerts (#12138) * Add simple tag-parsing of colors and highlights in alerts * Fixing a few CodeClimate-issues * Fixing a missing ; * Fixing some more typography * Whitespace... * Deduplicate code * Missing ; - too much python lately... * Fix end of function name * Simplifying sendAlert a bit * Keeping up with PR#12141 * ; again... * this->sendAlert... --- LibreNMS/IRCBot.php | 111 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/LibreNMS/IRCBot.php b/LibreNMS/IRCBot.php index 0e886ec10a..b0572388df 100644 --- a/LibreNMS/IRCBot.php +++ b/LibreNMS/IRCBot.php @@ -274,38 +274,60 @@ class IRCBot if ($this->config['irc_alert_chan']) { foreach ($this->config['irc_alert_chan'] as $chan) { - $this->ircRaw('PRIVMSG ' . $chan . ' :' . $severity . trim($alert['title'])); - if (! $this->config['irc_alert_short']) { // Only send the title if set to short - foreach (explode("\n", $alert['msg']) as $line) { - // We don't need to repeat the title - $line = strip_tags($line); - if (trim($line) != trim($alert['title'])) { - $this->ircRaw('PRIVMSG ' . $chan . ' :' . $line); - } - } - } + $this->sendAlert($chan, $severity, $alert); } - } else { - foreach ($this->authd as $nick => $data) { - if ($data['expire'] >= time()) { - $this->ircRaw('PRIVMSG ' . $nick . ' :' . $severity . trim($alert['title'])); - if (! $this->config['irc_alert_short']) { // Only send the title if set to short - foreach (explode("\n", $alert['msg']) as $line) { - // We don't need to repeat the title - $line = strip_tags($line); - if (trim($line) != trim($alert['title'])) { - $this->ircRaw('PRIVMSG ' . $nick . ' :' . $line); - } - } - } - } + + return; + }//end if + + foreach ($this->authd as $nick => $data) { + if ($data['expire'] >= time()) { + $this->sendAlert($nick, $severity, $alert); } } - }//end if + } } //end alertData() + private function sendAlert($sendto, $severity, $alert) + { + $this->ircRaw('PRIVMSG ' . $sendto . ' :' . $severity . trim($alert['title'])); + if ($this->config['irc_alert_short']) { + // Only send the title if set to short + + return; + } + + foreach (explode("\n", $alert['msg']) as $line) { + $line = trim($line); + if (strlen($line) < 1) { + continue; + } + $line = $this->_html2irc($line); + $line = strip_tags($line); + + // We don't need to repeat the title + if (trim($line) != trim($alert['title'])) { + $this->log("Sending alert $line"); + if ($this->config['irc_floodlimit'] > 100) { + $this->floodcount += strlen($line); + } elseif ($this->config['irc_floodlimit'] > 1) { + $this->floodcount += 1; + } + if (($this->config['irc_floodlimit'] > 0) && ($this->floodcount > $this->config['irc_floodlimit'])) { + $this->log('Reached floodlimit ' . $this->floodcount); + $this->ircRaw('BOTFLOODCHECK'); + sleep(2); + $this->floodcount = 0; + } + $this->ircRaw('PRIVMSG ' . $sendto . ' :' . $line); + } + } + } + + //end sendAlert() + private function getData() { if (($data = $this->read('irc')) !== false) { @@ -995,4 +1017,43 @@ class IRCBot } // end _color + + private function _html2irc($string) + { + $string = urldecode($string); + $string = preg_replace('##i', chr(2), $string); + $string = preg_replace('##i', chr(2), $string); + $string = preg_replace('##i', chr(22), $string); + $string = preg_replace('##i', chr(22), $string); + $string = preg_replace('##i', chr(31), $string); + $string = preg_replace('##i', chr(31), $string); + + $colors = [ + 'white' => '00', + 'black' => '01', + 'blue' => '02', + 'green' => '03', + 'red' => '04', + 'brown' => '05', + 'purple' => '06', + 'orange' => '07', + 'yellow' => '08', + 'lightgreen' => '09', + 'cyan' => '10', + 'lightcyan' => '11', + 'lightblue' => '12', + 'pink' => '13', + 'grey' => '14', + 'lightgrey' => '15', + ]; + + foreach ($colors as $color => $code) { + $string = preg_replace("#<$color>#i", chr(3) . $code, $string); + $string = preg_replace("##i", chr(3), $string); + } + + return $string; + } + + // end _html2irc }//end class