Merge in Phergie changes

This commit is contained in:
Luke Fitzgerald
2010-08-12 11:58:53 -07:00
parent d2c72d8ae1
commit a3fea6f673
16 changed files with 1513 additions and 590 deletions

View File

@@ -110,7 +110,7 @@ class Phergie_Plugin_Cron extends Phergie_Plugin_Abstract
*/
public function onTick()
{
$now = time();
$time = time();
foreach ($this->callbacks as $key => &$callback) {
$callbackString = $this->getCallbackString($callback);

View File

@@ -98,6 +98,12 @@ class Phergie_Plugin_Handler implements IteratorAggregate, Countable
$this->paths = array();
$this->autoload = false;
if (!empty($config['plugins.paths'])) {
foreach ($config['plugins.paths'] as $dir => $prefix) {
$this->addPath($dir, $prefix);
}
}
$this->addPath(dirname(__FILE__), 'Phergie_Plugin_');
}
@@ -134,6 +140,7 @@ class Phergie_Plugin_Handler implements IteratorAggregate, Countable
* Returns metadata corresponding to a specified plugin.
*
* @param string $plugin Short name of the plugin class
*
* @throws Phergie_Plugin_Exception Class file can't be found
*
* @return array|boolean Associative array containing the path to the
@@ -142,7 +149,7 @@ class Phergie_Plugin_Handler implements IteratorAggregate, Countable
*/
public function getPluginInfo($plugin)
{
foreach (array_reverse($this->paths) as $path) {
foreach (array_reverse($this->paths) as $path) {
$file = $path['path'] . $plugin . '.php';
if (file_exists($file)) {
$path = array(
@@ -444,15 +451,21 @@ class Phergie_Plugin_Handler implements IteratorAggregate, Countable
$valid = true;
try {
$error_reporting = error_reporting(0); // ignore autoloader errors
$r = new ReflectionClass($class);
$valid = $r->isSubclassOf('FilterIterator');
error_reporting($error_reporting);
if (!$r->isSubclassOf('FilterIterator')) {
$message = 'Class ' . $class . ' is not a subclass of FilterIterator';
$valid = false;
}
} catch (ReflectionException $e) {
$message = $e->getMessage();
$valid = false;
}
if (!$valid) {
throw new Phergie_Plugin_Exception(
$e->getMessage(),
$message,
Phergie_Plugin_Exception::ERR_INVALID_ITERATOR_CLASS
);
}

View File

@@ -31,7 +31,6 @@
* @uses extension PDO
* @uses extension pdo_sqlite
* @uses Phergie_Plugin_Command pear.phergie.org
* @uses Phergie_Plugin_Message pear.phergie.org
*/
class Phergie_Plugin_Karma extends Phergie_Plugin_Abstract
{
@@ -94,11 +93,16 @@ class Phergie_Plugin_Karma extends Phergie_Plugin_Abstract
{
$plugins = $this->getPluginHandler();
$plugins->getPlugin('Command');
$plugins->getPlugin('Message');
$file = dirname(__FILE__) . '/Karma/karma.db';
$this->db = new PDO('sqlite:' . $file);
$this->getDb();
}
/**
* Initializes prepared statements used by the plugin.
*
* @return void
*/
protected function initializePreparedStatements()
{
$this->fetchKarma = $this->db->prepare('
SELECT karma
FROM karmas
@@ -139,6 +143,36 @@ class Phergie_Plugin_Karma extends Phergie_Plugin_Abstract
');
}
/**
* Returns a connection to the plugin database, initializing one if none
* is explicitly set.
*
* @return PDO Database connection
*/
public function getDb()
{
if (empty($this->db)) {
$this->db = new PDO('sqlite:' . dirname(__FILE__) . '/Karma/karma.db');
$this->initializePreparedStatements();
}
return $this->db;
}
/**
* Sets the connection to the plugin database, mainly intended for unit
* testing.
*
* @param PDO $db Database connection
*
* @return Phergie_Plugin_Karma Provides a fluent interface
*/
public function setDb(PDO $db)
{
$this->db = $db;
$this->initializePreparedStatements();
return $this;
}
/**
* Get the canonical form of a given term.
*
@@ -228,15 +262,11 @@ REGEX;
$source = $this->getEvent()->getSource();
$nick = $this->getEvent()->getNick();
if (empty($term)) {
return;
}
$canonicalTerm = $this->getCanonicalTerm($term);
$fixedKarma = $this->fetchFixedKarma($canonicalTerm);
if ($fixedKarma) {
$message = $nick . ': ' . $term . ' ' . $fixedKarma . '.';
$message = $nick . ': ' . $term . ' ' . $fixedKarma;
$this->doPrivmsg($source, $message);
return;
}
@@ -302,33 +332,29 @@ REGEX;
$fixedKarma0 = $this->fetchFixedKarma($canonicalTerm0);
$fixedKarma1 = $this->fetchFixedKarma($canonicalTerm1);
if ($fixedKarma0
|| $fixedKarma1
|| empty($canonicalTerm0)
|| empty($canonicalTerm1)
) {
if ($fixedKarma0 || $fixedKarma1) {
return;
}
if ($canonicalTerm0 == 'everything') {
$change = $method == '<' ? '++' : '--';
$this->modifyKarma($canonicalTerm1, $change);
$karma0 = 0;
$karma1 = $this->fetchKarma($canonicalTerm1);
$karma1 = $this->modifyKarma($canonicalTerm1, $change);
} elseif ($canonicalTerm1 == 'everything') {
$change = $method == '<' ? '--' : '++';
$this->modifyKarma($canonicalTerm0, $change);
$karma0 = $this->fetchKarma($canonicalTerm1);
$karma0 = $this->modifyKarma($canonicalTerm0, $change);
$karma1 = 0;
} else {
$karma0 = $this->fetchKarma($canonicalTerm0);
$karma1 = $this->fetchKarma($canonicalTerm1);
}
if (($method == '<'
&& $karma0 < $karma1)
|| ($method == '>'
&& $karma0 > $karma1)) {
// Combining the first and second branches here causes an odd
// single-line lapse in code coverage, but the lapse disappears if
// they're separated
if ($method == '<' && $karma0 < $karma1) {
$replies = $this->fetchPositiveAnswer;
} elseif ($method == '>' && $karma0 > $karma1) {
$replies = $this->fetchPositiveAnswer;
} else {
$replies = $this->fetchNegativeAnswer;
@@ -356,14 +382,10 @@ REGEX;
* @param string $term Term to modify
* @param string $action Karma action (either ++ or --)
*
* @return void
* @return int Modified karma rating
*/
protected function modifyKarma($term, $action)
{
if (empty($term)) {
return;
}
$karma = $this->fetchKarma($term);
if ($karma !== false) {
$statement = $this->updateKarma;
@@ -378,6 +400,8 @@ REGEX;
':karma' => $karma
);
$statement->execute($args);
return $karma;
}
/**

View File

@@ -52,14 +52,14 @@ class Phergie_Plugin_Message extends Phergie_Plugin_Abstract
$}ix
REGEX;
return !$event->isInChannel()
return !$event->isInChannel()
|| preg_match($targetPattern, $event->getText()) > 0;
}
/**
* Allow for prefix and bot name aware extraction of a message
*
* @return string|bool $message The message, which is possibly targeted at the
* @return string|bool $message The message, which is possibly targeted at the
* bot or false if a prefix requirement failed
*/
public function getMessage()

View File

@@ -54,10 +54,27 @@ class Phergie_Plugin_Reload extends Phergie_Plugin_Abstract
{
$plugin = ucfirst($plugin);
$evalClass = true;
if (strpos($plugin, ' ') !== false) {
$args = explode(' ', $plugin);
$plugin = $args[0];
if (strtolower($args[1]) == 'force') {
$evalClass = false;
}
}
if (!$this->plugins->hasPlugin($plugin)) {
echo 'DEBUG(Reload): ' . ucfirst($plugin) . ' is not loaded yet, loading', PHP_EOL;
$this->plugins->getPlugin($plugin);
$this->plugins->command->populateMethodCache();
try {
$this->plugins->getPlugin($plugin);
$this->plugins->command->populateMethodCache();
} catch (Phergie_Plugin_Exception $e) {
if ($e->getCode() == Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND) {
echo 'DEBUG(Reload): ', $e->getMessage(), PHP_EOL;
} else {
throw $e;
}
}
return;
}
@@ -75,17 +92,19 @@ class Phergie_Plugin_Reload extends Phergie_Plugin_Abstract
$newClass = $class . '_' . sha1($contents);
if (class_exists($newClass, false)) {
echo 'DEBUG(Reload): Class ', $class, ' has not changed since last reload', PHP_EOL;
return;
if ($evalClass == true) {
echo 'DEBUG(Reload): Class ', $class, ' has not changed since last reload', PHP_EOL;
return;
}
} else {
$contents = preg_replace(
array('/^<\?(?:php)?/', '/class\s+' . $class . '/i'),
array('', 'class ' . $newClass),
$contents
);
eval($contents);
}
$contents = preg_replace(
array('/^<\?(?:php)?/', '/class\s+' . $class . '/i'),
array('', 'class ' . $newClass),
$contents
);
eval($contents);
$instance = new $newClass;
$instance->setName($plugin);
$instance->setEvent($this->event);

View File

@@ -1,6 +1,6 @@
<?php
/**
* Phergie
* Phergie
*
* PHP version 5
*
@@ -11,7 +11,7 @@
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @category Phergie
* @package Phergie_Plugin_TerryChay
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
@@ -24,7 +24,7 @@
* either confirmation of correctly spelled words or potential correct
* spellings for misspelled words.
*
* @category Phergie
* @category Phergie
* @package Phergie_Plugin_SpellCheck
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
@@ -34,7 +34,6 @@
*/
class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
{
/**
* Spell check dictionary handler
*
@@ -65,7 +64,7 @@ class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
}
$this->plugins->getPlugin('Command');
set_error_handler(array($this, 'loadDictionaryError'));
$this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
restore_error_handler();
@@ -86,11 +85,11 @@ class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
$target = $this->event->getNick();
$message = $target . ': The word "' . $word;
$message .= '" seems to be spelt correctly.';
$message .= '" seems to be spelled correctly.';
if (!pspell_check($this->pspell, $word)) {
$suggestions = pspell_suggest($this->pspell, $word);
$message = $target;
$message = $target;
$message .= ': I could not find any suggestions for "' . $word . '".';
if (!empty($suggestions)) {
$suggestions = array_splice($suggestions, 0, $this->limit);
@@ -98,7 +97,7 @@ class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
$message .= $word . '": ' . implode(', ', $suggestions) . '.';
}
}
$this->doPrivmsg($source, $message);
}
@@ -116,5 +115,4 @@ class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
{
$this->fail($errstr);
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Phergie
* Phergie
*
* PHP version 5
*
@@ -11,7 +11,7 @@
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @category Phergie
* @package Phergie_Plugin_TerryChay
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
@@ -21,9 +21,9 @@
/**
* Parses incoming messages for the words "Terry Chay" or tychay and responds
* with a random Terry fact retrieved from the Chayism web service.
* with a random Terry fact retrieved from the Chayism web service.
*
* @category Phergie
* @category Phergie
* @package Phergie_Plugin_TerryChay
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
@@ -53,21 +53,25 @@ class Phergie_Plugin_TerryChay extends Phergie_Plugin_Abstract
*/
public function onLoad()
{
$this->http = $this->getPluginHandler()->getPlugin('Http');
$this->getPluginHandler()->getPlugin('Http');
}
/**
* Fetches a chayism.
*
* @return string|bool Fetched chayism or FALSE if the operation failed
* @return string|bool Fetched chayism or FALSE if the operation failed
*/
public function getChayism()
{
return $this->http->get(self::URL)->getContent();
return $this
->getPluginHandler()
->getPlugin('Http')
->get(self::URL)
->getContent();
}
/**
* Parses incoming messages for "Terry Chay" and related variations and
* Parses incoming messages for "Terry Chay" and related variations and
* responds with a chayism.
*
* @return void
@@ -77,33 +81,14 @@ class Phergie_Plugin_TerryChay extends Phergie_Plugin_Abstract
$event = $this->getEvent();
$source = $event->getSource();
$message = $event->getText();
$pattern
= '{^(' . preg_quote($this->getConfig('command.prefix')) .
$pattern
= '{^(' . preg_quote($this->getConfig('command.prefix')) .
'\s*)?.*(terry\s+chay|tychay)}ix';
if (preg_match($pattern, $message)
&& $fact = $this->getChayism()
) {
$this->doPrivmsg($source, 'Fact: ' . $fact);
}
}
/**
* Parses incoming CTCP request for "Terry Chay" and related variations
* and responds with a chayism.
*
* @return void
*/
public function onCtcp()
{
$event = $this->getEvent();
$source = $event->getSource();
$ctcp = $event->getArgument(1);
if (preg_match('({terry[\s_+-]*chay}|tychay)ix', $ctcp)
&& $fact = $this->getChayism()
) {
$this->doCtcpReply($source, 'TERRYCHAY', $fact);
if (preg_match($pattern, $message)) {
if($fact = $this->getChayism()) {
$this->doPrivmsg($source, 'Fact: ' . $fact);
}
}
}
}