cvs: pearbot /modules pearcvscommits.php

[email protected] ("Amir Mohammad Saied") Sun, 04 May 2008 21:41:03 -0000
Newsgroups php.pear.cvs
Message-ID <cvsamir1209937263@cvsserver>
amir		Sun May  4 21:41:03 2008 UTC

  Added files:                 
    /pearbot/modules	pearcvscommits.php 
  Log:
  New module for PEARgirl
  

http://cvs.php.net/viewvc.cgi/pearbot/modules/pearcvscommits.php?view=markup&rev=1.1
Index: pearbot/modules/pearcvscommits.php
+++ pearbot/modules/pearcvscommits.php
<?php
/**
 * $Id: pearcvscommits.php,v 1.1 2008/05/04 21:41:03 amir Exp $
 * $Revision: 1.1 $
 * $Author: amir $
 * $Date: 2008/05/04 21:41:03 $
 *
 * Copyright (c) 2008 Amir Mohammad Saied <[email protected]>
 */

require_once 'HTTP/Request.php';
require_once 'PEAR/XMLParser.php';

class Net_SmartIRC_module_pearcvscommits
{
    var $name = 'pearcvscommits';
    var $description = 'Will return PEAR CVS commits';
    var $author = 'Amir Mohammad Saied <[email protected]>';
    var $version = '$Revision: 1.1 $';
    var $license = 'GPL';

    var $lastMessageID  = 0;
    var $parsedFeed     = '';
    var $maxToPost      = 3;
    var $timeout        = 600000;
    var $silent         = false;
    
    var $actionids = array();
    var $timeids   = array();
    
    function module_init(&$irc)
    {
        $this->actionids[] = $irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL,
            '^!cvs', $this, 'talk');
        $this->timeids[] = $irc->registerTimehandler($this->timeout, $this, 'postSummary');
    }
    
    function module_exit(&$irc)
    {
        foreach ($this->actionids as $value) {
            $irc->unregisterActionid($value);
        }
    }

    function talk(&$irc, &$data)
    {
        if (isset($data->messageex[1])) {
            if (strtolower($data->messageex[1]) == "on") {
                $this->silent = false;
                $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "OK, I'll tell you about CVS commits");
            } else if (strtolower($data->messageex[1]) == "off") {
                $this->silent = true;
                $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, "Shhhhh " . $data->nick . ' gonna sleep');
            }
        } else {
            $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $data->nick . ' Usage: !cvs on|off');
        }
    }


    function postSummary(&$irc)
    {
        $this->parsedFeed = $this->parseFeed();
        if (PEAR::isError($this->parsedFeed) || empty($this->parsedFeed)) {
            return false;
        }
        $lastID = explode('/', $this->parsedFeed['channel']['item']['19']['link']);
        $lastID = $lastID[4];
        if ($this->silent) {
            $this->lastMessageID = $lastID;
            return $this->lastMessageID;
        }

        if (!$this->lastMessageID) {
            $this->lastMessageID = $lastID;
        } else if ($this->lastMessageID < $lastID) {
            $finalOutput = array();
            $queueItems = ($lastID - $this->lastMessageID) > $this->maxToPost ? $this->maxToPost : $lastID - $this->lastMessageID;
            while($queueItems) {
                $item = $this->parsedFeed['channel']['item'][20 - $queueItems];
                $finalOutput[] = '"'.html_entity_decode(strip_tags($item['description'])) . '" has had a commit on ' . str_replace("cvs: ", "", $item['title']);
                $finalOutput[] = $item['link'];
                $queueItems--;
            }
            $this->lastMessageID = $lastID;
            $irc->message(SMARTIRC_TYPE_CHANNEL, "#pear", $finalOutput);
        }
    }

    function parseFeed()
    {
        $feed = 'http://news.php.net/group.php?group=php.pear.cvs&format=rss';
        $req  =& new HTTP_Request($feed);
        $req->setMethod(HTTP_REQUEST_METHOD_GET);
        $req->sendRequest();
        if (PEAR::isError($req->sendRequest())) {
            return PEAR::raiseError("Connection failed", 0);
        }

        $body = $req->getResponseBody();
        $parser = new PEAR_XMLParser;
        $parseError = $parser->parse($body);
        if (PEAR::isError($parseError)) {
            return PEAR::raiseError($parseError->getMessage(), 0);
        }

        return $parser->getData();
    }
}
?>