From e4b5b48f61a8415ccd36fe61697574ddb0c16324 Mon Sep 17 00:00:00 2001 From: f0o Date: Sun, 15 Mar 2015 13:00:25 +0000 Subject: [PATCH 1/3] Added posix-fifo to deliver alerts to the IRC-bot --- includes/alerts/transport.irc.php | 9 ++++++++- irc.php | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/includes/alerts/transport.irc.php b/includes/alerts/transport.irc.php index 71e0591c0..5e98f6ca8 100644 --- a/includes/alerts/transport.irc.php +++ b/includes/alerts/transport.irc.php @@ -21,4 +21,11 @@ * @subpackage Alerts */ -return file_put_contents($config['install_dir']."/.ircbot.alert", json_encode($obj)."\n", FILE_APPEND); +$f = $config['install_dir']."/.ircbot.alert"; +$f = fopen($f,"w+"); +$f = fwrite($f,json_encode($obj)."\n"); +if( $f === false ) { + return false; +} else { + return true; +} diff --git a/irc.php b/irc.php index ed3e832a2..84ac73ccb 100755 --- a/irc.php +++ b/irc.php @@ -128,11 +128,11 @@ class ircbot { private function connect_alert() { $f = $this->config['install_dir']."/.ircbot.alert"; - if( (file_exists($f) || !touch($f)) && (!unlink($f) || !touch($f)) ) { + if( ( file_exists($f) && filetype($f) != "fifo" && !unlink($f) ) || ( !file_exists($f) && !posix_mkfifo($f,0644) ) ) { $this->log("Error - Cannot create Alert-File"); return false; } - if( ($this->socket['alert'] = fopen($f,'r')) ) { + if( ($this->socket['alert'] = fopen($f,'r+')) ) { $this->log("Opened Alert-File"); stream_set_blocking($this->socket['alert'], false); return true; @@ -147,7 +147,7 @@ class ircbot { $r = strlen($r); if(strstr($this->buff[$buff],"\n")) { $tmp = explode("\n",$this->buff[$buff],2); - $this->buff[$buff] = substr($this->buff[$buff], strlen($tmp[0])+2); + $this->buff[$buff] = substr($this->buff[$buff], strlen($tmp[0])+1); if( $this->debug ) { $this->log("Returning buffer '$buff': '".trim($tmp[0])."'"); } From 478916af4bf63996e6d61971c13b07fe16c98968 Mon Sep 17 00:00:00 2001 From: f0o Date: Sun, 15 Mar 2015 13:02:50 +0000 Subject: [PATCH 2/3] close open files --- includes/alerts/transport.irc.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/alerts/transport.irc.php b/includes/alerts/transport.irc.php index 5e98f6ca8..1380fb57b 100644 --- a/includes/alerts/transport.irc.php +++ b/includes/alerts/transport.irc.php @@ -23,8 +23,9 @@ $f = $config['install_dir']."/.ircbot.alert"; $f = fopen($f,"w+"); -$f = fwrite($f,json_encode($obj)."\n"); -if( $f === false ) { +$r = fwrite($f,json_encode($obj)."\n"); +$f = fclose($f); +if( $r === false ) { return false; } else { return true; From adebcf6c291315eca5788aa2d7b862fa66e19b4c Mon Sep 17 00:00:00 2001 From: f0o Date: Sun, 15 Mar 2015 15:14:10 +0000 Subject: [PATCH 3/3] Fallback to shell_exec as some systems lack posix_mkfifo --- includes/alerts/transport.irc.php | 16 +++++++++------- irc.php | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/alerts/transport.irc.php b/includes/alerts/transport.irc.php index 1380fb57b..03fcc3cda 100644 --- a/includes/alerts/transport.irc.php +++ b/includes/alerts/transport.irc.php @@ -22,11 +22,13 @@ */ $f = $config['install_dir']."/.ircbot.alert"; -$f = fopen($f,"w+"); -$r = fwrite($f,json_encode($obj)."\n"); -$f = fclose($f); -if( $r === false ) { - return false; -} else { - return true; +if( file_exists($f) && filetype($f) == "fifo" ) { + $f = fopen($f,"w+"); + $r = fwrite($f,json_encode($obj)."\n"); + $f = fclose($f); + if( $r === false ) { + return false; + } else { + return true; + } } diff --git a/irc.php b/irc.php index 84ac73ccb..9e4025305 100755 --- a/irc.php +++ b/irc.php @@ -128,7 +128,7 @@ class ircbot { private function connect_alert() { $f = $this->config['install_dir']."/.ircbot.alert"; - if( ( file_exists($f) && filetype($f) != "fifo" && !unlink($f) ) || ( !file_exists($f) && !posix_mkfifo($f,0644) ) ) { + if( ( file_exists($f) && filetype($f) != "fifo" && !unlink($f) ) || ( !file_exists($f) && !shell_exec("mkfifo $f && echo 1") ) ) { $this->log("Error - Cannot create Alert-File"); return false; }