based up on the work of Eli White * @copyright 2008-2014 Phergie Development Team (http://phergie.org), 2014 Ron Guerin * @license http://phergie.org/license New BSD License */ /** * Log all pertinent messages that come across, in Eggdrop formatted text files. * Intended to be used with IRC stats programs and programs that create HTML pages, * that support Eggdrop log files. Someone who understands Phergie could undoubtedly * do a much better job coding this. * * @category Phergie * @package Phergie_Plugin_EggLogging * @author Ron Guerin * @license http://phergie.org/license New BSD License * @uses Phergie_Plugin_UserInfo pear.phergie.org */ class Phergie_Plugin_EggLogging extends Phergie_Plugin_Abstract { /** * Logged channels, their users, date of last write, and handles logfiles * * @array channel data: $channel, $users, $loghandle, $date */ protected $channels; // key: $channel array: $loghandle, $users /** * onLoad * * Prepare for logging * * @return void */ public function onLoad() { // Load UserInfo plugin $this->plugins->getPlugin('UserInfo'); // Use the configuration settings foreach ($this->getConfig('egglogging.channels') as $channel => $logsubdir) $this->channels[$channel]['logdir'] = $this->getConfig('egglogging.dir').'/'.$logsubdir.'/logs'; } /** * getChannelUsers * * Get the list of users * * @return void */ public function getChannelUsers($channel) { $users = $this->plugins->getPlugin('UserInfo')->getUsers($channel); if ((! isset($users[0])) || $users[0] == '') return; foreach ($users as $user) $this->channels[$channel]['users'][$user] = TRUE; } /** * log * * Writes to the logfile. Reopens on invalid handle or deleted logfile. * * @param Phergie_Event_Request $event The event we are operating on * @param string $nick Who did this? * @param string $message What was said? * @return void */ protected function log(Phergie_Event_Request $event, $channel, $nick, $message = NULL) { if ((trim($message) == '') || (! array_key_exists($channel, $this->channels))) return; // Read in a few pieces of data from the connection $connection = $this->getConnection(); $host = $connection->getHost().':'.$connection->getPort(); // Now from the event $type = $event->getType(); $thisdate = date('Ymd'); $thistime = date('H:i'); $filename = substr(str_replace('#', '_', $channel), 1); $logfile = $this->channels[$channel]['logdir']."/$filename.$thisdate.log"; // Open file if not yet opened, close and open new file if date changes, re-open file if no longer open. if (! array_key_exists('loghandle', $this->channels[$channel])) { $this->channels[$channel]['date'] = $thisdate; $this->channels[$channel]['loghandle'] = fopen($logfile, 'a+'); } elseif ($this->channels[$channel]['date'] != $thisdate) { $this->channels[$channel]['date'] = $thisdate; @fclose($this->channels[$channel]['loghandle']); $this->channels[$channel]['loghandle'] = fopen($logfile, 'a+'); } elseif ((fstat($this->channels[$channel]['loghandle']) === FALSE) || (! file_exists($logfile))) { @fclose($this->channels[$channel]['loghandle']); $this->channels[$channel]['loghandle'] = fopen($logfile, 'a+'); } // Attempt to write to the logfile if (fwrite($this->channels[$channel]['loghandle'], '['.$thistime."] $message\n") === FALSE) { @fclose($this->channels[$channel]['loghandle']); $this->channels[$channel]['loghandle'] = fopen($logfile, 'a+'); fwrite($this->channels[$channel]['loghandle'], '['.$thistime."] $message\n"); } } /** * onQuit * * Handler for when a user leaves a network. * * @return void */ public function onQuit() { // Push it to the log $connection = $this->getConnection(); $host = $connection->getHost(); $event = $this->getEvent(); $message = $event->getArgument('message'); $nick = $event->getNick(); $hostmask = $event->getHostmask(); if (stristr($hostmask, '/')) $hostmask = str_replace('!~', '!', $hostmask); $msg = $nick." ($hostmask) left irc: "; if ($message) $msg .= '('.$message.')'; foreach ($this->channels as $channel => $array) { if (! is_array($array['users'])) continue; foreach ($array['users'] as $user => $true) { if ($user != $nick) continue; #[17:55] rajesh (~rajesh@rrcs-24-173-240-14.sw.biz.rr.com) left irc: ""homeward bound"" $this->log($event, $channel, $nick, $msg); unset($this->channels[$channel]['users'][$user]); } } } /** * onMode * * Handler for when a mode change happens * * @return void */ public function onMode() { // Push it to the log $event = $this->getEvent(); $event->getNick(); $mode = $event->getArgument('mode'); $channel = $event->getSource(); $nick = $event->getNick(); $this->getChannelUsers($channel); $hostmask = $event->getHostmask(); if (stristr($hostmask, '/')) $hostmask = str_replace('!~', '!', $hostmask); $msg = $event->getArgument('target').": mode change '$mode "; #[08:26] #nylug: mode change '+v akiaki' by ChanServ!ChanServ@services. #[02:19] #nylug: mode change '+ovvv ChanServ openfly h3rb1 sunny' by irc.freenode.net #[16:36] #execs: mode change '+o rguerin' by ChanServ!ChanServ@services.nylug.org #[02:19] #nylug: mode change '+ovvv ChanServ openfly h3rb1 sunny' by irc.freenode.net #[06:27] #volunteers: mode change '+nt ' by irc.nylug.org #[16:35] #volunteers: mode change '+s ' by tuxibot!tuxibot@nylug.org #[12:09] #execs: mode change '+b *!*RonG@*.res.rr.com' by ChanServ!ChanServ@services.nylug.org $msg .= $event->getArgument('limit'); # $msg .= "' by ".$event->getArgument('user'); $msg .= "' by $hostmask"; #if ($mode == '+v') $msg .= '.'; # $this->log($event, $channel, $event->getNick(), $event->getArgument('target').' mode:'.$event->getArgument('mode').' limit:'.$event->getArgument('limit'). # ' user:'.$event->getArgument('user').' banmask:'.$event->getArgument('banmask')); $this->log($event, $channel, $nick, $msg); } /** * onNick * * Handler for when a user changes their nick * * @return void */ public function onNick() { // Push it to the log $event = $this->getEvent(); $nick = $event->getNick(); $newnick = $event->getArgument('nickname'); foreach ($this->channels as $channel => $array) { if (! is_array($array['users'])) continue; foreach ($array['users'] as $user => $true) { if ($user != $nick) continue; #[11:28] Nick change: checkmate -> Guest86497 $this->log($event, $channel, $nick, "Nick change: $nick -> $newnick"); unset($this->channels[$channel]['users'][$user]); $this->channels[$channel]['users'][$newnick] = TRUE; } } } /** * onKick * * Handler for when a user gets kicked * * @return void */ public function onKick() { // Push it to the log $event = $this->getEvent(); $nick = $event->getNick(); $channel = $event->getSource(); $this->getChannelUsers($channel); #[11:28] Rawn_ kicked from #nylug by Daishi: be gone, RonGs evil brother $this->log($event, $channel, $nick, $nick." kicked from $channel by ".$event->getArgument('user').': '.$event->getArgument('comment')); } /** * onTopic * * Handler for when a user changes the topic * * @return void */ public function onTopic() { // Push it to the log $event = $this->getEvent(); $channel = $event->getSource(); $nick = $event->getNick(); $hostmask = $event->getHostmask(); if (stristr($hostmask, '/')) $hostmask = str_replace('!~', '!', $hostmask); #Daishi!meeeep@pool-162-83-227-139.ny5030.east.verizon.net #[16:02] Topic changed on #nylug by !services.oftc.net: We're probably all over in #nylug at freenode right now $this->log($event, $channel, $nick, "Topic changed on $channel by $hostmask: ".$event->getArgument('topic')); } /** * onPrivmsg * * Watches for & logs any incoming messages from the channel * * @return void */ public function onPrivmsg() { // Figure out our data $event = $this->getEvent(); $channel = $event->getSource(); $nick = $event->getNick(); $this->getChannelUsers($channel); #[10:18] sounds like KFC if ($event->isInChannel()) $this->log($event, $channel, $nick, "<$nick> ".$event->getText()); } /** * onJoin * * Handler for when a user joins a channel. * * @return void */ public function onJoin() { // Push it to the log $event = $this->getEvent(); $channel = $event->getSource(); $this->getChannelUsers($channel); $nick = $event->getNick(); $hostmask = $event->getHostmask(); if (stristr($hostmask, '/')) $hostmask = str_replace('!~', '!', $hostmask); #[12:02] Rawn (~RonG@72.229.139.219) joined #nylug. #[18:46] lunybot (lunybot!lunybot@2600:3c03::f03c:91ff:fedf:8b90) joined #luny. $this->log($event, $channel, $nick, "$nick ($hostmask) joined ".$event->getArgument('channel').'.'); } /** * onPart * * Handler for when a user leaves a channel. * * @return void */ public function onPart() { // Push it to the log $connection = $this->getConnection(); $host = $connection->getHost(); $event = $this->getEvent(); $channel = $event->getSource(); $nick = $event->getNick(); $this->getChannelUsers($channel); $hostmask = $event->getHostmask(); if (stristr($hostmask, '/')) $hostmask = str_replace('!~', '!', $hostmask); foreach ($this->channels[$channel]['users'] as $key => $user) if ($user == $nick) unset($this->channels[$channel]['users'][$key]); #[18:28] sysfault (~enigma@ool-44c0244a.dyn.optonline.net) left #nylug. #[16:44] kickme (kickme!~zorg@ool-18bf46e9.dyn.optonline.net) left #luny. $this->log($event, $channel, $nick, "$nick ($hostmask) left ". $event->getArgument('channel').' '.$event->getArgument('message').'.'); } /** * onAction * * Handler for when the bot receives a CTCP ACTION request. * * @return void */ public function onAction() { // Push it to the log $event = $this->getEvent(); $channel = $event->getSource(); $this->getChannelUsers($channel); $nick = $event->getNick(); #[10:18] Action: RonG is hungry $this->log($event, $channel, $nick, "Action: $nick ".$event->getText()); } }