Changeset 166
- Timestamp:
- 03/11/08 21:58:10 (5 years ago)
- Location:
- trunk/Phergie
- Files:
-
- 1 added
- 8 modified
-
Driver/Streams.php (modified) (10 diffs)
-
Event/Request.php (modified) (8 diffs)
-
Event/Response.php (modified) (2 diffs)
-
Plugin/Abstract/Base.php (modified) (4 diffs)
-
Plugin/Abstract/Cron.php (modified) (1 diff)
-
Plugin/Ctcp.php (added)
-
Plugin/Debug.php (modified) (2 diffs)
-
Plugin/FeedTicker.php (modified) (2 diffs)
-
Plugin/Pong.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Phergie/Driver/Streams.php
r148 r166 75 75 76 76 /** 77 * The time the bot started 78 * 79 * @var int 80 */ 81 protected $startTime; 82 83 /** 77 84 * Constructor to initialize instance properties. 78 85 */ … … 120 127 121 128 /** 129 * Returns the start time. 130 * 131 * @return int 132 */ 133 public function getStartTime() 134 { 135 return $this->startTime; 136 } 137 138 /** 122 139 * Executes a continuous loop in which the client listens for events from 123 140 * the server and processes them until the connection is terminated. … … 127 144 public function run() 128 145 { 146 $this->startTime = time(); 129 147 $returnCode = $this->getIni('keepalive') ? self::RETURN_KEEPALIVE : self::RETURN_END; 130 148 $server = $this->getIni('server'); … … 226 244 break; 227 245 case 'notice': 228 $args = explode(' :', $args); 246 $temp = preg_split('/ :?/', trim($args), 2); 247 if(substr($temp[1], 0, 1) === chr(1) && substr($temp[1], -1) === chr(1)) { 248 $ctcp = trim(substr($temp[1], 1, -1)); 249 list ($cmd, $args) = explode(' ', $ctcp.' ', 2); 250 $cmd = strtolower($cmd); 251 252 // Check for the type of Notice message and handle it accordingly 253 if ($cmd == 'action') { 254 // Return sender as source and the action as its first argument 255 $args = array($nick, trim($args)); 256 } elseif ($cmd == 'ping' || $cmd == 'version' || $cmd == 'time') { 257 // Return sender as source and the rest of the args as its first argument 258 $cmd = $cmd.'reply'; 259 $args = array($nick, trim($args)); 260 } else { 261 // Return sender as source and the ctcp as its first argument 262 $cmd = 'ctcpreply'; 263 $args = array($nick, $ctcp); 264 } 265 } else { 266 $args = preg_split('/ :?/', $args, 2); 267 } 229 268 break; 230 269 case 'oper': … … 240 279 break; 241 280 case 'privmsg': 242 if ($args[0] == chr(1)) { 243 /** 244 * This will likely need to be changed if support is added 245 * for CTCP commands other than ACTION. 246 */ 247 list ($target, $args) = explode(' ', $args, 2); 248 $args = explode(' ', substr($args, 1, -1), 2); 249 $cmd = strtolower(array_shift($args)); 250 array_unshift($args, $target); 281 $temp = preg_split('/ :?/', trim($args), 2); 282 // Check to see if its a CTCP request, if so, handle it accordingly 283 if(substr($temp[1], 0, 1) === chr(1) && substr($temp[1], -1) === chr(1)) { 284 $ctcp = trim(substr($temp[1], 1, -1)); 285 list ($cmd, $args) = explode(' ', $ctcp.' ', 2); 286 $cmd = strtolower($cmd); 287 288 // Check for the type of CTCP message and handle it accordingly 289 if ($cmd == 'action') { 290 $botnick = $this->getIni('nick'); 291 // If sent within a channel, use the chanel as the source, else use the sender 292 $source = (strtolower($temp[0]) == strtolower($botnick) ? $nick : $temp[0]); 293 // Return channel/sender as source and the action as its first argument 294 $args = array($source, trim($args)); 295 } elseif ($cmd == 'ping' && !empty($args)) { 296 // Return nick of sender as source and the handshake as its first argument 297 $args = array($nick, trim($args)); 298 } elseif (($cmd == 'version' || $cmd == 'time') && empty($args)) { 299 // Return nick of sender as source and the ctcp as its first argument 300 $args = array($nick, $ctcp); 301 } else { 302 // Return nick of sender as source and the ctcp as its first argument 303 $cmd = 'ctcp'; 304 $args = array($nick, $ctcp); 305 } 251 306 } else { 252 $args = explode(' :', $args, 2);307 $args = preg_split('/ :?/', $args, 2); 253 308 } 254 309 break; … … 259 314 $event->setCode($cmd); 260 315 $event->setDescription($args); 316 $event->setRawBuffer($buffer); 261 317 } else { 262 318 $event = new Phergie_Event_Request(); … … 268 324 $event->setNick($nick); 269 325 } 326 $event->setRawBuffer($buffer); 270 327 } 271 328 … … 282 339 call_user_func(array($plugin, 'on' . ucfirst($event->getType()))); 283 340 } 341 // onRaw Handler 342 $plugin->onRaw(); 284 343 } 285 344 $this->queueing = false; … … 534 593 * (optional) 535 594 */ 536 protected function sendCtcp($target, $command, array $arguments = array()) 537 { 538 $this->doPrivmsg($target, chr(1) . strtoupper($command) . ' ' . implode(' ', $arguments) . chr(1)); 595 public function doCtcp($target, $command, $arguments = '') 596 { 597 if (is_array($arguments)) { 598 $arguments = implode(' ', $arguments); 599 } 600 $this->doPrivmsg($target, chr(1) . trim(strtoupper($command) . ' ' . $arguments) . chr(1)); 539 601 } 540 602 … … 547 609 public function doAction($target, $text) 548 610 { 549 $this->sendCtcp($target, Phergie_Event_Request::TYPE_ACTION, array($text)); 611 $this->doCtcp($target, Phergie_Event_Request::TYPE_ACTION, array($text)); 612 } 613 614 /** 615 * Sends a ping reply to a nick. 616 * 617 * @param string $target User nickname 618 * @param string $hash The ping hash to use in the handshake 619 */ 620 public function doPingReply($target, $hash) 621 { 622 $this->doCtcpReply($target, 'ping', $hash); 623 } 624 625 /** 626 * Sends a version reply to a nick. 627 * 628 * @param string $target User nickname 629 * @param string $version The version to send 630 */ 631 public function doVersionReply($target, $version='') 632 { 633 $version = trim($version); 634 if (empty($version)) { 635 $version = 'Phergie '.PHERGIE_VERSION.' - An IRC bot written in PHP (http://www.phergie.com)'; 636 } 637 $this->doCtcpReply($target, 'version', $version); 638 } 639 640 /** 641 * Sends a time reply to a nick 642 * 643 * @param string $target User nickname 644 * @param string $time The time to send 645 */ 646 public function doTimeReply($target, $time='') 647 { 648 $time = trim($time); 649 if (empty($time) || ctype_digit($time)) { 650 if (empty($time)) $time = time(); 651 $time = date('D M d H:i:s o', $time); 652 } 653 $this->doCtcpReply($target, 'time', $time); 654 } 655 656 /** 657 * Sends a ctcp reply to a nick 658 * 659 * @param string $target User nickname 660 * @param string $reply The CTCP reply to send 661 */ 662 public function doCtcpReply($target, $command, $reply='') 663 { 664 $command = trim($command); 665 if (empty($command)) 666 return; 667 $this->send(Phergie_Event_Request::TYPE_NOTICE, array($target, chr(1).trim(strtoupper($command).' '.$reply).chr(1))); 550 668 } 551 669 } -
trunk/Phergie/Event/Request.php
r86 r166 86 86 87 87 /** 88 * Mapping of event types to their named parameters 88 * Mapping of event types to their named parameters 89 89 * 90 90 * @var array … … 99 99 'channel' => 0 100 100 ), 101 101 102 102 self::TYPE_KICK => array( 103 103 'channel' => 0, … … 115 115 'limit' => 2, 116 116 'user' => 3, 117 'banmask' => 4 117 'banmask' => 4 118 118 ), 119 119 … … 132 132 'text' => 1 133 133 ), 134 134 135 135 self::TYPE_ACTION => array( 136 136 'target' => 0, … … 173 173 */ 174 174 protected $arguments; 175 176 /** 177 * The raw buffer that was sent by the server 178 * 179 * @var string 180 */ 181 protected $rawBuffer; 175 182 176 183 /** … … 314 321 315 322 /** 323 * Sets the raw buffer for the given event 324 * 325 * @param string $buffer 326 * @return void 327 */ 328 public function setRawBuffer($buffer) 329 { 330 $this->rawBuffer = $buffer; 331 } 332 333 /** 334 * Returns the raw buffer that was sent from the server for that event 335 * 336 * @return string 337 */ 338 public function getRawBuffer() 339 { 340 return $this->rawBuffer; 341 } 342 343 /** 316 344 * Returns the channel name or user nick representing the source of the 317 345 * event. … … 361 389 * 362 390 * @param string $name Name of the method called 363 * @param array $arguments Arguments passed to the method (should always 391 * @param array $arguments Arguments passed to the method (should always 364 392 * be empty) 365 393 * @return mixed … … 369 397 if (count($arguments) == 0 370 398 && substr($name, 0, 3) == 'get') { 371 return $this->getArgument(substr($name, 3)); 399 return $this->getArgument(substr($name, 3)); 372 400 } 373 401 } -
trunk/Phergie/Event/Response.php
r57 r166 846 846 847 847 /** 848 * The raw buffer that was sent by the server 849 * 850 * @var string 851 */ 852 protected $rawBuffer; 853 854 /** 848 855 * Sets the reply code sent by the server. 849 856 * … … 884 891 return $this->description; 885 892 } 893 894 /** 895 * Sets the raw buffer for the given event 896 * 897 * @param string $buffer 898 * @return void 899 */ 900 public function setRawBuffer($buffer) 901 { 902 $this->rawBuffer = $buffer; 903 } 904 905 /** 906 * Returns the raw buffer that was sent from the server for that event 907 * 908 * @return string 909 */ 910 public function getRawBuffer() 911 { 912 return $this->rawBuffer; 913 } 886 914 } -
trunk/Phergie/Plugin/Abstract/Base.php
r160 r166 370 370 371 371 /** 372 * Converts a given integer/timestamp into days, minutes and seconds 373 * 374 * @param int $time The time/integer to calulate the values from 375 * @return string 376 */ 377 public function getCountdown($time) 378 { 379 $return = array(); 380 381 $days = floor($time / 86400); 382 if ($days > 0) { 383 $return[] = $days . 'd'; 384 $time = $time % 86400; 385 } 386 387 $hours = floor($time / 3600); 388 if ($hours > 0) { 389 $return[] = $hours . 'h'; 390 $time = $time % 3600; 391 } 392 393 $minutes = floor($time / 60); 394 if ($minutes > 0) { 395 $return[] = $minutes . 'm'; 396 $time = $time % 60; 397 } 398 399 $return[] = $time . 's'; 400 401 return implode(' ', $return); 402 } 403 404 /** 372 405 * Returns whether or not the current environment meets the requirements 373 406 * of the plugin in order for it to be run, including the PHP version, … … 458 491 459 492 /** 493 * Handler for when an action is received from a channel or user 494 * 495 * @return void 496 */ 497 public function onAction() { } 498 499 /** 460 500 * Handler for when a notice is received. 461 501 * … … 472 512 473 513 /** 474 * Handler for when the server checks the client connection to ensure475 * activity.514 * Handler for when the server or a userchecks the client connection to 515 * ensure activity. 476 516 * 477 517 * @return void 478 518 */ 479 519 public function onPing() { } 520 521 /** 522 * Handler for when the server sends a CTCP Time request 523 * 524 * @return void 525 */ 526 public function onTime() { } 527 528 /** 529 * Handler for when the server sends a CTCP Version request 530 * 531 * @return void 532 */ 533 public function onVersion() { } 534 535 /** 536 * Handler for when the server sends a CTCP request 537 * 538 * @return void 539 */ 540 public function onCtcp() { } 541 542 /** 543 * Handler for the reply received when a ping CTCP is sent 544 * 545 * 546 * @return void 547 */ 548 public function onPingreply() { } 549 550 /** 551 * Handler for the reply received when a time CTCP is sent 552 * 553 * @return void 554 */ 555 public function onTimereply() { } 556 557 /** 558 * Handler for the reply received when a version CTCP is sent 559 * 560 * @return void 561 */ 562 public function onVersionreply() { } 563 564 /** 565 * Handler for the reply received when a CTCP is sent 566 * 567 * @return void 568 */ 569 public function onCtcpreply() { } 570 571 /** 572 * Handler for raw requests from the server 573 * 574 * @return void 575 */ 576 public function onRaw() { } 480 577 481 578 /** … … 512 609 return false; 513 610 } 611 514 612 return call_user_func_array(array($this->client, $method), $arguments); 515 613 } -
trunk/Phergie/Plugin/Abstract/Cron.php
r71 r166 64 64 65 65 /** 66 * Checks to see if the run method should be called when a PINGevent66 * Checks to see if the run method should be called when a raw event 67 67 * occurs. 68 68 * 69 69 * @return void 70 70 */ 71 public function onPing() 72 { 73 $this->check(); 74 } 75 76 /** 77 * Checks to see if the run method should be called when a message is 78 * received. 79 * 80 * @return void 81 */ 82 public function onPrivmsg() 71 public function onRaw() 83 72 { 84 73 $this->check(); -
trunk/Phergie/Plugin/Debug.php
r146 r166 12 12 class Phergie_Plugin_Debug extends Phergie_Plugin_Abstract_AdminCommand 13 13 { 14 /**15 * Timestamp used to calculate uptime16 *17 * @var int18 */19 protected $uptime;20 21 /**22 * Initializes a local timestamp used to calculate uptime.23 *24 * @return void25 */26 public function init()27 {28 parent::init();29 30 $this->uptime = time();31 }32 33 14 /** 34 15 * Responds to requests for statistics related to the bot's memory … … 52 33 public function onDoUptime() 53 34 { 54 $diff = time() - $this->uptime; 55 $uptime = array(); 56 57 $days = floor($diff / 86400); 58 if ($days > 0) { 59 $uptime[] = $days . 'd'; 60 $diff = $diff % 86400; 61 } 62 63 $hours = floor($diff / 3600); 64 if ($hours > 0) { 65 $uptime[] = $hours . 'h'; 66 $diff = $diff % 3600; 67 } 68 69 $minutes = floor($diff / 60); 70 if ($minutes > 0) { 71 $uptime[] = $minutes . 'm'; 72 $diff = $diff % 60; 73 } 74 75 $uptime[] = $diff . 's'; 76 77 $uptime = 'uptime : ' . implode(' ', $uptime); 35 $uptime = 'Uptime: ' . $this->getCountdown(time() - $this->getStartTime()); 78 36 79 37 $this->doPrivmsg($this->event->getSource(), $uptime); -
trunk/Phergie/Plugin/FeedTicker.php
r138 r166 122 122 123 123 /** 124 * Overrides Cron method to add checkQueue to this, so that whenever a user 125 * sends a message to a channel in which the bot is present, the queue is 126 * checked. This behavior attempts to prevent the bot from spamming the 127 * channel if no users are conversing and everything is retained in the 128 * queue until channel activity is detected. 124 * Whenever a user sends a message to a channel in which the bot is present, 125 * the queue is checked. This behavior attempts to prevent the bot from 126 * spamming the channel if no users are conversing and everything is retained 127 * in the queue until channel activity is detected. 129 128 * 130 129 * @return void … … 132 131 public function onPrivmsg() 133 132 { 134 parent::onPrivmsg();135 136 133 $this->checkQueue(); 137 134 } -
trunk/Phergie/Plugin/Pong.php
r68 r166 20 20 public function onPing() 21 21 { 22 $this->doPong($this->event->getArgument(0)); 22 // Check for a handshake hash, otherwise its a server ping request 23 if (!$this->event->getArgument(1)) { 24 $this->doPong($this->event->getArgument(0)); 25 } 23 26 } 24 27 }