Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/elazar/phergie
Browse files Browse the repository at this point in the history
  • Loading branch information
Bittarman committed Apr 2, 2010
2 parents 3b43129 + 6d2fc8a commit d73b36c
Show file tree
Hide file tree
Showing 13 changed files with 1,158 additions and 46 deletions.
65 changes: 43 additions & 22 deletions Phergie/Plugin/AudioScrobbler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
*/

/**
* Uses a self CTCP PING to ensure that the client connection has not been
* dropped.
*
* @category Phergie
* @package Phergie_Plugin_AudioScrobbler
* @author Phergie Development Team <[email protected]>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_AudioScrobbler
* @uses Phergie_Plugin_Command pear.phergie.org
* @uses Phergie_Plugin_Http pear.phergie.org
* @uses extension simplexml
*/
class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract
Expand All @@ -53,9 +52,16 @@ class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract
* @var string
*/
protected $query = '?method=user.getrecenttracks&user=%s&api_key=%s';

/**
* HTTP plugin
*
* @var Phergie_Plugin_Http
*/
protected $http;

/**
* check and load plugin's dependencies.
* Check for dependencies.
*
* @return void
*/
Expand All @@ -65,11 +71,13 @@ public function onLoad()
$this->fail('SimpleXML php extension is required');
}

$this->getPluginHandler()->getPlugin('Command');
$plugins = $this->getPluginHandler();
$plugins->getPlugin('Command');
$this->http = $plugins->getPlugin('Http');
}

/**
* Command function to get user's status on last.fm
* Command function to get a user's status on last.fm.
*
* @param string $user User identifier
*
Expand All @@ -78,13 +86,15 @@ public function onLoad()
public function onCommandLastfm($user = null)
{
if ($key = $this->config['audioscrobbler.lastfm_api_key']) {
$scrobbled = $this->getScrobbled($user, $this->lastfm_url, $key);
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
$scrobbled = $this->getScrobbled($user, $this->lastfmUrl, $key);
if ($scrobbled) {
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
}
}
}

/**
* Command function to get users status on libre.fm
* Command function to get a user's status on libre.fm.
*
* @param string $user User identifier
*
Expand All @@ -93,35 +103,46 @@ public function onCommandLastfm($user = null)
public function onCommandLibrefm($user = null)
{
if ($key = $this->config['audioscrobbler.librefm_api_key']) {
$scrobbled = $this->getScrobbled($user, $this->librefm_url, $key);
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
$scrobbled = $this->getScrobbled($user, $this->librefmUrl, $key);
if ($scrobbled) {
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
}
}
}

/**
* Simple Scrobbler API function to get recent track formatted in string
* Simple Scrobbler API function to get a formatted string of the most
* recent track played by a user.
*
* @param string $user User name to lookup
* @param string $user Username to look up
* @param string $url Base URL of the scrobbler service
* @param string $key Scrobbler service API key
*
* @return string A formatted string of the most recent track played.
* @return string Formatted string of the most recent track played
*/
public function getScrobbled($user, $url, $key)
{
$user = $user ? $user : $this->getEvent()->getNick();
$event = $this->getEvent();
$user = $user ? $user : $event->getNick();
$url = sprintf($url . $this->query, urlencode($user), urlencode($key));

$response = file_get_contents($url);
try {
$xml = new SimpleXMLElement($response);
}
catch (Exception $e) {
return 'Can\'t find status for ' . $user;

$response = $this->http->get($url);
if ($response->isError()) {
$this->doNotice(
$event->getSource(),
'Can\'t find status for ' . $user . ': HTTP ' .
$response->getCode() . ' ' . $response->getMessage()
);
return false;
}

$xml = $response->getContent();
if ($xml->error) {
return 'Can\'t find status for ' . $user;
$this->doNotice(
$event->getSource(),
'Can\'t find status for ' . $user . ': API ' . $xml->error
);
return false;
}

$recenttracks = $xml->recenttracks;
Expand Down
32 changes: 25 additions & 7 deletions Phergie/Plugin/BeerScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Phergie Development Team <[email protected]>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_BeerScore
* @uses Phergie_Plugin_Http pear.phergie.org
*/
class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract
{
Expand Down Expand Up @@ -58,6 +59,23 @@ class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract
*/
const API_BASE_URL = 'http://caedmon.net/beerscore/';

/**
* HTTP plugin
*
* @var Phergie_Plugin_Http
*/
protected $http;

/**
* Checks for dependencies.
*
* @return void
*/
public function onLoad()
{
$this->http = $this->getPluginHandler()->getPlugin('Http');
}

/**
* Handles beerscore commands.
*
Expand All @@ -67,17 +85,19 @@ class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract
*/
public function onCommandBeerscore($searchstring)
{
$target = $this->getEvent()->getNick();
$source = $this->getEvent()->getSource();
$event = $this->getEvent();
$target = $event->getNick();
$source = $event->getSource();

$apiurl = self::API_BASE_URL . rawurlencode($searchstring);
$result = json_decode(file_get_contents($apiurl));
$response = $this->http->get($apiurl);

if (!$result || !isset($result->type) || !is_array($result->beer)) {
if ($response->isError()) {
$this->doNotice($target, 'Score not found (or failed to contact API)');
return;
}

$result = $response->getContent();
switch ($result->type) {
case self::TYPE_SCORE:
// small enough number to get scores
Expand Down Expand Up @@ -128,11 +148,9 @@ public function onCommandBeerscore($searchstring)
$num = 'at least 100';
}
$resultsword = (($result->num > 1) ? 'results' : 'result');
$str = "{$target}: {$num} {$resultsword}; {$beer->searchurl}";
$str = "{$target}: {$num} {$resultsword}; {$result->searchurl}";
$this->doPrivmsg($source, $str);
break;
}
}
}

// vim: language=php tab=4 et indent=4
106 changes: 106 additions & 0 deletions Phergie/Plugin/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Plugin_Cache
* @author Phergie Development Team <[email protected]>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Cache
*/

/**
* Implements a generic cache to be used by other plugins.
*
* @category Phergie
* @package Phergie_Plugin_Cache
* @author Phergie Development Team <[email protected]>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Cache
*/
class Phergie_Plugin_Cache extends Phergie_Plugin_Abstract
{
/**
* Key-value data storage for the cache
*
* @var array
*/
protected $cache = array();

/**
* Stores a value in the cache.
*
* @param string $key Key to associate with the value
* @param mixed $data Data to be stored
* @param int|null $ttl Time to live in seconds or NULL for forever
* @param bool $overwrite TRUE to overwrite any existing value
* associated with the specified key
*
* @return bool
*/
public function store($key, $data, $ttl = 3600, $overwrite = true)
{
if (!$overwrite && isset($this->cache[$key])) {
return false;
}

if ($ttl) {
$expires = time()+$ttl;
} else {
$expires = null;
}

$this->cache[$key] = array('data' => $data, 'expires' => $expires);
return true;

}

/**
* Fetches a previously stored value.
*
* @param string $key Key associated with the value
*
* @return mixed Stored value or FALSE if no value or an expired value
* is associated with the specified key
*/
public function fetch($key)
{
if (!isset($this->cache[$key])) {
return false;
}

$item = $this->cache[$key];
if (!is_null($item['expires']) && $item['expires'] < time()) {
$this->expire($key);
return false;
}

return $item['data'];
}

/**
* Expires a value that has exceeded its time to live.
*
* @param string $key Key associated with the value to expire
*
* @return bool
*/
protected function expire($key)
{
if (!isset($this->cache[$key])) {
return false;
}
unset($this->cache[$key]);
return true;
}
}
7 changes: 4 additions & 3 deletions Phergie/Plugin/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ public function onPrivmsg()
// Get the content of the message
$event = $this->getEvent();
$msg = trim($event->getText());
$prefix = $this->getConfig('command.prefix');

// Check for the command prefix if one is set and needed
if ($this->config['command.prefix'] && $event->isInChannel()) {
if (strpos($msg, $this->config['command.prefix']) !== 0) {
if ($prefix && $event->isInChannel()) {
if (strpos($msg, $prefix) !== 0) {
return;
} else {
$msg = substr($msg, strlen($this->config['command.prefix']));
$msg = substr($msg, strlen($prefix));
}
}

Expand Down
6 changes: 6 additions & 0 deletions Phergie/Plugin/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ class Phergie_Plugin_Exception extends Phergie_Exception
* such situations
*/
const ERR_INVALID_CALL = 12;

/**
* Error indicating that a fatal runtime issue was encountered within a
* plugin
*/
const ERR_FATAL_ERROR = 13;
}
Loading

0 comments on commit d73b36c

Please sign in to comment.