Merged in changes to Phergie

This commit is contained in:
Luke Fitzgerald 2010-07-18 06:28:15 -07:00
parent 8abef1fd65
commit 7caff948f6
10 changed files with 222 additions and 416 deletions

View File

@ -82,6 +82,22 @@ class Phergie_Config implements ArrayAccess
return $this;
}
/**
* Merges an associative array of configuration setting values into the
* current configuration settings.
*
* @param array $settings Associative array of configuration setting
* values keyed by setting name
*
* @return Phergie_Config Provides a fluent interface
*/
public function readArray(array $settings)
{
$this->settings += $settings;
return $this;
}
/**
* Writes the values of the current configuration settings back to their
* originating files.

View File

@ -1,39 +0,0 @@
<?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
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
/**
* Exceptions related to handling databases for plugins.
*
* @category Phergie
* @package Phergie
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
class Phergie_Db_Exception extends Phergie_Exception
{
/**
* Error indicating that a directory needed to support database
* functionality was unable to be created.
*/
const ERR_UNABLE_TO_CREATE_DIRECTORY = 1;
}

View File

@ -1,49 +0,0 @@
<?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
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Command
*/
/**
* Database management class. Provides a base API for managing databases
* within
*
* @category Phergie
* @package Phergie
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Db_Manager
*/
abstract class Phergie_Db_Manager
{
/**
* Returns a connection to the database.
*
* @return object
*/
public abstract function getDb();
/**
* Checks if a table/collection exists within the database.
*
* @param string $table Table/collection name to check for
*
* @return boolean
*/
public abstract function hasTable($table);
}

View File

@ -1,117 +0,0 @@
<?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
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
/**
* Provides basic management for SQLite databases
*
* @category Phergie
* @package Phergie
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
class Phergie_Db_Sqlite extends Phergie_Db_Manager
{
/**
* Database connection
*
* @var PDO
*/
protected $db;
/**
* Database file path
*
* @var string
*/
protected $dbFile;
/**
* Allows setting of the database file path when creating the class.
*
* @param string $dbFile database file path (optional)
*
* @return void
*/
public function __construct($dbFile = null)
{
if ($dbFile != null) {
$this->setDbFile($dbFile);
}
}
/**
* Sets the database file path.
*
* @param string $dbFile SQLite database file path
*
* @return null
*/
public function setDbFile($dbFile)
{
if (is_string($dbFile) && !empty($dbFile)) {
$this->dbFile = $dbFile;
}
}
/**
* Returns a configured database connection.
*
* @return PDO
*/
public function getDb()
{
if (!empty($this->db)) {
return $this->db;
}
$dir = dirname($this->dbFile);
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
throw new Phergie_Db_Exception(
'Unable to create directory',
Phergie_Db_Exception::ERR_UNABLE_TO_CREATE_DIRECTORY
);
}
$this->db = new PDO('sqlite:' . $this->dbFile);
return $this->db;
}
/**
* Returns whether a given table exists in the database.
*
* @param string $table Name of the table to check for
*
* @return boolean TRUE if the table exists, FALSE otherwise
*/
public function hasTable($table)
{
$db = $this->getDb();
return (bool) $db->query(
'SELECT COUNT(*) FROM sqlite_master WHERE name = '
. $db->quote($table)
)->fetchColumn();
}
}

View File

@ -1,51 +0,0 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Extends the configuration class (Phergie_Config) to allow passing config
* array instead of loading from file
*
* @category Phergie
* @package Phergie_Extended_Config
* @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class Phergie_Extended_Config extends Phergie_Config {
/**
* Incorporates an associative array of settings into the current
* configuration settings.
*
* @param array $array Array of settings
*
* @return Phergie_Config Provides a fluent interface
* @throws Phergie_Config_Exception
*/
public function readArray($array) {
$settings = $array;
if (!is_array($settings)) {
throw new Phergie_Config_Exception(
'Parameter is not an array',
Phergie_Config_Exception::ERR_ARRAY_NOT_RETURNED
);
}
$this->settings += $settings;
return $this;
}
}

View File

@ -1,4 +1,23 @@
<?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_NickServ
* @author Phergie Development Team <team@phergie.org>
* @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_NickServ
*/
/**
* Intercepts and responds to messages from the NickServ agent requesting that
@ -6,10 +25,18 @@
*
* The password configuration setting should contain the password registered
* with NickServ for the nick used by the bot.
*
* @category Phergie
* @package Phergie_Plugin_NickServ
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_NickServ
* @uses Phergie_Plugin_Command pear.phergie.org
*/
class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract
{
/**
* The name of the nickserv bot
* Nick of the NickServ bot
*
* @var string
*/
@ -21,20 +48,25 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
protected $identifyMessage;
/**
* Initializes instance variables.
* Checks for dependencies and required configuration settings.
*
* @return void
*/
public function onLoad() {
public function onLoad()
{
$this->getPluginHandler()->getPlugin('Command');
// Get the name of the NickServ bot, defaults to NickServ
$this->botNick = $this->config['nickserv.botnick'];
if (!$this->botNick) $this->botNick = 'NickServ';
if (!$this->botNick) {
$this->botNick = 'NickServ';
}
// Get the identify message
$this->identifyMessage = $this->config['nickserv.identify_message'];
if (!$this->identifyMessage) $this->identifyMessage = 'This nickname is registered.';
if (!$this->identifyMessage) {
$this->identifyMessage = 'This nickname is registered.';
}
}
/**
@ -44,7 +76,8 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
*
* @return void
*/
public function onNotice() {
public function onNotice()
{
$event = $this->event;
if (strtolower($event->getNick()) == strtolower($this->botNick)) {
$message = $event->getArgument(1);
@ -62,14 +95,15 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
}
/**
* Checks to see if the original Nick has quit, if so, take the name back
* Checks to see if the original nick has quit; if so, take the name back.
*
* @return void
*/
public function onQuit() {
$eventnick = $this->event->getNick();
public function onQuit()
{
$eventNick = $this->event->getNick();
$nick = $this->connection->getNick();
if ($eventnick == $nick) {
if ($eventNick == $nick) {
$this->doNick($nick);
}
}
@ -80,7 +114,8 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
*
* @return void
*/
public function onNick() {
public function onNick()
{
$event = $this->event;
$connection = $this->connection;
if ($event->getNick() == $connection->getNick()) {
@ -93,7 +128,8 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
*
* @return void
*/
public function onDoGhostbust() {
public function onCommandGhostbust()
{
$event = $this->event;
$user = $event->getNick();
$conn = $this->connection;
@ -102,42 +138,44 @@ class Phergie_Plugin_NickServ extends Phergie_Plugin_Abstract {
if ($nick != $this->config['connections'][$conn->getHost()]['nick']) {
$password = $this->config['nickserv.password'];
if (!empty($password)) {
$this->doPrivmsg($this->event->getSource(), $user . ': Attempting to ghost ' . $nick .'.');
$this->doPrivmsg(
$this->event->getSource(),
$user . ': Attempting to ghost ' . $nick .'.'
);
$this->doPrivmsg(
$this->botNick,
'GHOST ' . $nick . ' ' . $password,
true
);
}
unset($password);
}
}
/**
* Automatically send the GHOST command if the Nickname is in use
* Automatically send the GHOST command if the bot's nick is in use.
*
* @return void
*/
public function onResponse() {
public function onResponse()
{
if ($this->event->getCode() == Phergie_Event_Response::ERR_NICKNAMEINUSE) {
$password = $this->config['nickserv.password'];
if (!empty($password)) {
$this->doPrivmsg(
$this->botNick,
'GHOST ' . $this->connection->getNick() . ' ' . $password,
true
'GHOST ' . $this->connection->getNick() . ' ' . $password
);
}
unset($password);
}
}
/**
* The server sent a KILL request, so quit the server
* Handle the server sending a KILL request.
*
* @return void
*/
public function onKill() {
public function onKill()
{
$this->doQuit($this->event->getArgument(1));
}
}

View File

@ -30,6 +30,8 @@
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Tld
* @uses Phergie_Plugin_Http pear.phergie.org
* @uses extension PDO
* @uses extension pdo_sqlite
*
* @pluginDesc Provides information for a top level domain.
*/
@ -78,116 +80,22 @@ class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
);
}
$dbFile = dirname(__FILE__) . '/Tld/tld.db';
try {
$dbFile = dirname(__FILE__) . '/Tld/tld.db';
$dbManager = new Phergie_Db_Sqlite($dbFile);
$this->db = $dbManager->getDb();
if (!$dbManager->hasTable('tld')) {
$query = 'CREATE TABLE tld ('
. 'tld VARCHAR(20), '
. 'type VARCHAR(20), '
. 'description VARCHAR(255))';
$this->db = new PDO('sqlite:' . $dbFile);
$this->db->exec($query);
$this->select = $this->db->prepare('
SELECT type, description
FROM tld
WHERE LOWER(tld) = LOWER(:tld)
');
// prepare a statement to populate the table with
// tld information
$insert = $this->db->prepare(
'INSERT INTO tld
(tld, type, description)
VALUES (:tld, :type, :description)'
);
// grab tld data from iana.org...
$contents = file_get_contents(
'http://www.iana.org/domains/root/db/'
);
// ...and then parse it out
$regex = '{<tr class="iana-group[^>]*><td><a[^>]*>\s*\.?([^<]+)\s*'
. '(?:<br/><span[^>]*>[^<]*</span>)?</a></td><td>\s*'
. '([^<]+)\s*</td><td>\s*([^<]+)\s*}i';
preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
list(, $tld, $type, $description) = array_pad($match, 4, null);
$type = trim(strtolower($type));
if ($type != 'test') {
$tld = trim(strtolower($tld));
$description = trim($description);
switch ($tld) {
case 'com':
$description = 'Commercial';
break;
case 'info':
$description = 'Information';
break;
case 'net':
$description = 'Network';
break;
case 'org':
$description = 'Organization';
break;
case 'edu':
$description = 'Educational';
break;
case 'name':
$description = 'Individuals, by name';
break;
}
if (empty($tld) || empty($description)) {
continue;
}
$regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
. '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
. '\s*|\s*as advised.*$)}i';
$description = preg_replace($regex, '', $description);
$description = ucfirst(trim($description));
$data = array_map(
'html_entity_decode', array(
'tld' => $tld,
'type' => $type,
'description' => $description
)
);
$insert->execute($data);
}
}
unset(
$insert,
$matches,
$match,
$contents,
$tld,
$type,
$description,
$data,
$regex
);
}
// Create a prepared statements for retrieving TLDs
$this->select = $this->db->prepare(
'SELECT type, description '
. 'FROM tld WHERE LOWER(tld) = LOWER(:tld)'
);
$this->selectAll = $this->db->prepare(
'SELECT tld, type, description FROM tld'
);
$this->selectAll = $this->db->prepare('
SELECT tld, type, description
FROM btld
');
} catch (PDOException $e) {
$this->getPluginHandler()->removePlugin($this);
}
}
@ -258,4 +166,3 @@ class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
return false;
}
}

View File

@ -0,0 +1,68 @@
<?php
$dbFile = 'tld.db';
if (file_exists($dbFile)) {
exit;
}
$db = new PDO('sqlite:' . dirname(__FILE__) . '/' . $dbFile);
$query = '
CREATE TABLE tld (
tld VARCHAR(20),
type VARCHAR(20),
description VARCHAR(255)
)
';
$db->exec($query);
$insert = $db->prepare('
INSERT INTO tld (tld, type, description)
VALUES (:tld, :type, :description)
');
$contents = file_get_contents(
'http://www.iana.org/domains/root/db/'
);
libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->loadHTML($contents);
libxml_clear_errors();
$descriptions = array(
'com' => 'Commercial',
'info' => 'Information',
'net' => 'Network',
'org' => 'Organization',
'edu' => 'Educational',
'name' => 'Individuals, by name'
);
$xpath = new DOMXPath($doc);
$rows = $xpath->query('//tr[contains(@class, "iana-group")]');
foreach (range(0, $rows->length - 1) as $index) {
$row = $rows->item($index);
$tld = strtolower(ltrim($row->childNodes->item(0)->textContent, '.'));
$type = $row->childNodes->item(1)->nodeValue;
if (isset($descriptions[$tld])) {
$description = $descriptions[$tld];
} else {
$description = $row->childNodes->item(2)->textContent;
$regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
. '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
. '\s*|\s*as advised.*$)}i';
$description = preg_replace($regex, '', $description);
$description = ucfirst(trim($description));
}
$data = array_map(
'html_entity_decode',
array(
'tld' => $tld,
'type' => $type,
'description' => $description
)
);
$insert->execute($data);
}

View File

@ -67,11 +67,6 @@ class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
*/
protected $twitterpassword = null;
/**
* Allow only admins to tweet
*/
protected $requireAdmin = true;
/**
* Register with the URL plugin, if possible
*
@ -91,13 +86,6 @@ class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
*/
public function onLoad()
{
// see if tweetrequireadmin defined in config
if (isset($this->config['twitter.tweetrequireadmin'])
&& $req = $this->config['twitter.tweetrequireadmin']
) {
// if so, override default
$this->requireAdmin = $req;
}
if (!isset($this->config['twitter.class'])
|| !$twitterClass = $this->config['twitter.class']
) {
@ -150,14 +138,10 @@ class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
*/
public function onCommandTweet($txt)
{
echo "Tweet!\n";
$nick = $this->getEvent()->getNick();
if (!$this->twitteruser) {
return;
}
if ($this->requireAdmin && !$this->fromAdmin(true)) {
return;
}
$source = $this->getEvent()->getSource();
if ($tweet = $this->twitter->sendTweet($txt)) {
$this->doPrivmsg(
@ -218,6 +202,5 @@ class Phergie_Plugin_Twitter extends Phergie_Plugin_Abstract
// if we get this far, we haven't satisfied the URL, so bail:
return false;
}
}

70
plugins/Irc/extlib/phergie/Phergie/Plugin/Weather.php Executable file → Normal file
View File

@ -55,6 +55,30 @@ class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
}
}
/**
* Converts a temperature in Celsius to Fahrenheit.
*
* @param int $temp Temperature in Celsius
*
* @return int Temperature converted to Fahrenheit
*/
public function convertCelsiusToFahrenheit($temp)
{
return round(((((int) $temp * 9) / 5) + 32));
}
/**
* Converts a temperature in Fahrenheit to Celsius.
*
* @param int $temp Temperature in Fahrenheit
*
* @return int Temperature converted to Celsius
*/
public function convertFahrenheitToCelsius($temp)
{
return round(((((int) $temp - 32) * 5) / 9));
}
/**
* Returns a weather report for a specified location.
*
@ -107,18 +131,44 @@ class Phergie_Plugin_Weather extends Phergie_Plugin_Abstract
$xml = $response->getContent();
$weather = 'Weather for ' . (string) $xml->loc->dnam . ' - ';
$weather .= 'Current temperature ' .
(string) $xml->cc->tmp .
(string) $xml->head->ut . ' / ';
if ((string) $xml->head->ut == 'F') {
$weather .= round(((((int) $xml->cc->tmp - 32) * 5) / 9)) . 'C';
} else {
$weather .= round(((((int) $xml->cc->tmp * 9) / 5) + 32)) . 'F';
switch ($xml->head->ut) {
case 'F':
$tempF = $xml->cc->tmp;
$tempC = $this->convertFahrenheitToCelsius($tempF);
break;
case 'C':
$tempC = $xml->cc->tmp;
$tempF = $this->convertCelsiusToFahrenheit($tempC);
break;
default:
$this->doNotice(
$this->event->getNick(),
'ERROR: No scale information given.');
break;
}
$r = $xml->cc->hmid;
$tempF2 = $tempF * $tempF;
$r2 = $r * $r;
$hiF = round(
-42.379 +
(2.04901523 * $tempF) +
(10.14333127 * $r) -
(.22475541 * $tempF * $r) -
(6.83783 * pow(10,-3) * $tempF2) -
(5.481717 * pow(10,-2) * $r2) +
(1.22874 * pow(10,-3) * $tempF2 * $r) +
(8.5282 * pow(10,-4) * $tempF * $r2) -
(1.99 * pow(10,-6) * $tempF2 * $r2)
);
$hiC = $this->convertFahrenheitToCelsius($hiF);
$weather .= 'Temperature: ' . $tempF . 'F/' . $tempC . 'C';
$weather .= ', Humidity: ' . (string) $xml->cc->hmid . '%';
if ($hiF > $tempF || $hiC > $tempC) {
$weather .= ', Heat Index: ' . $hiF . 'F/' . $hiC . 'C';
}
$weather .=
', Relative humidity ' . (string) $xml->cc->hmid . '%' .
', Current conditions ' . (string) $xml->cc->t .
', Last update ' . (string) $xml->cc->lsup .
', Conditions: ' . (string) $xml->cc->t .
', Updated: ' . (string) $xml->cc->lsup .
' [ http://weather.com/weather/today/' .
str_replace(
array('(', ')', ',', ' '),