Permalink
Browse files

Pulled upstream

  • Loading branch information...
2 parents f6b2cd2 + fed43d7 commit 7a08a4b8cbcc5c2e9d8d0f186648260af2854991 @elazar elazar committed Mar 10, 2012
View
32 Phergie/Bot.php
@@ -135,6 +135,27 @@ public function setConfig(Phergie_Config $config)
}
/**
+ * Tries to locate the default configuration file
+ *
+ * @return string|bool Returns false when no file was found
+ */
+ public function getDefaultConfiguration()
+ {
+ $paths = array(
+ dirname(__FILE__) . '/../Settings.php',
+ dirname(__FILE__) . '/Settings.php'
+ );
+
+ foreach ($paths as $path) {
+ if (file_exists($path)) {
+ return $path;
+ }
+ }
+
+ return false;
+ }
+
+ /**
* Returns the entire configuration in use or the value of a specific
* configuration setting.
*
@@ -148,9 +169,18 @@ public function setConfig(Phergie_Config $config)
public function getConfig($index = null, $default = null)
{
if (empty($this->config)) {
+ $config = $this->getDefaultConfiguration();
+ if (false === $config) {
+ throw new Exception(
+ 'Phergie could not locate file Settings.php, '
+ . 'try "phergie path/to/file/Settings.php"'
+ );
+ }
+
$this->config = new Phergie_Config;
- $this->config->read(dirname(__FILE__) . '/../Settings.php');
+ $this->config->read($config);
}
+
if ($index !== null) {
if (isset($this->config[$index])) {
return $this->config[$index];
View
9 Phergie/Driver/Streams.php
@@ -100,7 +100,7 @@ protected function send($command, $args = '')
$args = ':' . $args;
}
- $buffer .= ' ' . preg_replace('/\v+/', ' ', $args);
+ $buffer .= ' ' . preg_replace('/\v+/u', ' ', $args);
}
// Transmit the command over the socket connection
@@ -294,6 +294,10 @@ public function getEvent()
break;
}
}
+ // This fixes the issue that seems to occur, but why does it?
+ if (!is_array($args)) {
+ $args = array($args);
+ }
break;
case 'topic':
@@ -645,7 +649,7 @@ protected function doCtcp($nick, $command, $args = null)
{
$buffer = rtrim(strtoupper($command) . ' ' . $args);
- $this->doNotice($nick, chr(1) . $buffer . chr(1));
+ $this->doPrivmsg($nick, chr(1) . $buffer . chr(1));
}
/**
@@ -724,3 +728,4 @@ public function doRaw($command)
$this->send($command);
}
}
+
View
4 Phergie/Plugin/Dice.php
@@ -60,7 +60,9 @@ public function onCommandRoll($message)
list (, $num, $die, $mod, $rest) = $matches;
$roll = 0;
for ($i = 0; $i < $num; $i++) {
- $roll += mt_rand(1, $die);
+ if ($die >= 1) { //BUGFIX: mt_rand issues E_WARNING (since PHP5.3) if max is lower than min (which is hardcoded to 1)
+ $roll += mt_rand(1, $die);
+ }
}
$roll = min($roll, $num * $die);
if (!empty($mod)) {
View
2 Phergie/Plugin/Http/Response.php
@@ -136,7 +136,7 @@ public function getCodeAsString()
$code = $this->code;
if (!isset(self::$codeStrings[$code])) {
- return 'Unkown HTTP Status';
+ return 'Unknown HTTP Status';
}
return self::$codeStrings[$code];
View
87 Phergie/Plugin/Message.php
@@ -32,6 +32,19 @@
class Phergie_Plugin_Message extends Phergie_Plugin_Abstract
{
/**
+ * Returns a regular expression that matches the bot's nick or aliases.
+ *
+ * @return string
+ */
+ private function getSelfRegex()
+ {
+ $me = preg_quote($this->connection->getNick());
+ $aliases = $this->getConfig('message.aliases');
+
+ return '(?:' . implode('|', array_merge((array) $me, (array) $aliases)) . ')';
+ }
+
+ /**
* Check whether a message is specifically targeted at the bot.
* This is the case when the message starts with the bot's name
* followed by [,:>] or when it is a private message.
@@ -41,7 +54,18 @@ class Phergie_Plugin_Message extends Phergie_Plugin_Abstract
*/
public function isTargetedMessage()
{
- return $this->getMessage() !== false;
+ $event = $this->getEvent();
+
+ $self = $this->getSelfRegex();
+
+ $targetPattern = <<<REGEX
+ {^
+ \s*{$self}\s*[:>,\s].* # expect the bots name, followed by a [:>,\s]
+ $}ix
+REGEX;
+
+ return !$event->isInChannel()
+ || preg_match($targetPattern, $event->getText()) > 0;
}
/**
@@ -52,36 +76,49 @@ public function isTargetedMessage()
*/
public function getMessage()
{
- $event = $this->getEvent();
+ $event = $this->getEvent();
+
+ $prefix = preg_quote($this->getConfig('command.prefix'));
+ $self = $this->getSelfRegex();
$message = $event->getText();
- $prefix = $this->getConfig('command.prefix');
- $symbols = array(':', ' ', ',', '>');
- $nicks = array_merge(
- (array) $this->connection->getNick(),
- (array) $this->getConfig('message.aliases')
- );
- // Format '<nick>[:|,|>|<space>] <message>'
- foreach ($nicks as $nick) {
- $length = strlen($nick);
- if (substr($message, 0, $length) == $nick
- && in_array(substr($message, $length, 1), $symbols)
- ) {
- return trim(substr($message, $length + 1));
- }
- }
+ // $prefixPattern matches : Phergie, do command <parameters>
+ // where $prefix = 'do' : do command <parameters>
+ // : Phergie, command <parameters>
+ $prefixPattern = <<<REGEX
+ {^
+ (?:
+ \s*{$self}\s*[:>,\s]\s* # start with bot name
+ (?:{$prefix})? # which is optionally followed by the prefix
+ |
+ \s*{$prefix} # or start with the prefix
+ )
+ \s*(.*) # always end with the message
+ $}ix
+REGEX;
- // Format '<prefix><message>' (note that $prefix could be null)
- $length = strlen($prefix);
- if (substr($message, 0, $length) === $prefix) {
- return trim(substr($message, $length));
+ // $noPrefixPattern matches : Phergie, command <parameters>
+ // : command <parameters>
+ $noPrefixPattern = <<<REGEX
+ {^
+ \s*(?:{$self}\s*[:>,\s]\s*)? # optionally start with the bot name
+ (.*?) # always end with the message
+ $}ix
+REGEX;
+
+ $pattern = $noPrefixPattern;
+
+ // If a prefix is set, force it as a requirement
+ if ($prefix && $event->isInChannel()) {
+ $pattern = $prefixPattern;
}
- // Private message or without configured prefix
- if (!$event->isInChannel() || $prefix === null) {
- return trim($message);
+ $match = null;
+
+ if (!preg_match($pattern, $message, $match)) {
+ return false;
}
- return false;
+ return $match[1];
}
}
View
10 Phergie/Plugin/Twitter.php
@@ -158,26 +158,22 @@ public function onCommandTwitter($tweeter = null, $num = 1)
/**
* Formats a Tweet into a message suitable for output.
*
- * @param object $tweet JSON-decoded tweet object from Twitter
+ * @param object $tweet JSON-decoded tweet object from Twitter
*
* @return string
*/
protected function formatTweet(StdClass $tweet)
{
- $format = $this->getConfig(
- 'twitter.format',
- '<@{username}> {text} - {countdown} ago ({url})'
- );
+ $format = $this->getConfig('twitter.format', '<@{screen_name}> {text} - '
+ . '{countdown} ago ({url})');
$out = str_replace(array(
- '{username}',
'{screen_name}',
'{text}',
'{time}',
'{countdown}',
'{url}',
), array(
- $tweet->user->username,
$tweet->user->screen_name,
$tweet->text,
$tweet->created_at,
View
4 Phergie/Plugin/UserInfo.php
@@ -113,7 +113,7 @@ public function onMode()
*/
public function onJoin()
{
- $chan = trim(strtolower($this->event->getArgument(0)));
+ $chan = ltrim(trim(strtolower($this->event->getArgument(0))), ":");
$nick = trim($this->event->getNick());
$this->store[$chan][$nick] = self::REGULAR;
@@ -180,7 +180,7 @@ public function onResponse()
}
$array = explode(' ', $this->event->getDescription());
- $chan = $array[1];
+ $chan = trim(strtolower($array[1]));
$count = count($array);
for ($i = 3; $i < $count; $i++) {
View
7 Phergie/Process/Async.php
@@ -47,13 +47,6 @@ class Phergie_Process_Async extends Phergie_Process_Abstract
protected $usec = 200000;
/**
- * Length of time to wait between ticks.
- *
- * @var int
- */
- protected $wait = 0;
-
- /**
* Overrides the parent class to set the poll time.
*
* @param Phergie_Bot $bot Main bot class
View
14 Tests/Phergie/Driver/StreamsTest.php
@@ -713,7 +713,7 @@ public function testDoAction()
public function testDoPing()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'PING hash' . chr(1),
+ 'PRIVMSG nick :' . chr(1) . 'PING hash' . chr(1),
'doPing', array('nick', 'hash')
);
}
@@ -728,7 +728,7 @@ public function testDoPing()
public function testDoVersionWithoutVersion()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'VERSION' . chr(1), 'doVersion', array('nick')
+ 'PRIVMSG nick :' . chr(1) . 'VERSION' . chr(1), 'doVersion', array('nick')
);
}
@@ -742,7 +742,7 @@ public function testDoVersionWithoutVersion()
public function testDoVersionWithVersion()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'VERSION version'
+ 'PRIVMSG nick :' . chr(1) . 'VERSION version'
. chr(1), 'doVersion', array('nick', 'version')
);
}
@@ -757,7 +757,7 @@ public function testDoVersionWithVersion()
public function testDoTimeWithoutTime()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'TIME' . chr(1), 'doTime', array('nick')
+ 'PRIVMSG nick :' . chr(1) . 'TIME' . chr(1), 'doTime', array('nick')
);
}
@@ -771,7 +771,7 @@ public function testDoTimeWithoutTime()
public function testDoTimeWithTime()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'TIME time'
+ 'PRIVMSG nick :' . chr(1) . 'TIME time'
. chr(1), 'doTime', array('nick', 'time')
);
}
@@ -786,7 +786,7 @@ public function testDoTimeWithTime()
public function testDoFingerWithoutFingerString()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'FINGER' . chr(1), 'doFinger', array('nick')
+ 'PRIVMSG nick :' . chr(1) . 'FINGER' . chr(1), 'doFinger', array('nick')
);
}
@@ -800,7 +800,7 @@ public function testDoFingerWithoutFingerString()
public function testDoFingerWithFingerString()
{
$this->doCommandTest(
- 'NOTICE nick :' . chr(1) . 'FINGER finger string'
+ 'PRIVMSG nick :' . chr(1) . 'FINGER finger string'
. chr(1), 'doFinger', array('nick', 'finger string')
);
}
View
4 Tests/Phergie/Plugin/WeatherTest.php
@@ -157,6 +157,10 @@ public function testNoConfig()
$this->fail('Exception should have been thrown');
} catch( Exception $e) {
$this->assertInstanceOf('Phergie_Plugin_Exception', $e);
+ $this->assertEquals(
+ 'weather.partner_id and weather.license_key must be specified',
+ $e->getMessage()
+ );
}
}

0 comments on commit 7a08a4b

Please sign in to comment.