/vendors/php-irc/modules/bash/bash_mod.php

http://github.com/cakephp/cakebot · PHP · 178 lines · 143 code · 33 blank · 2 comment · 26 complexity · 7c5a8a785d17337c473994af20728e43 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. class bash_mod extends module {
  4. public $title = "Bash Parser";
  5. public $author = "Manick";
  6. public $version = "0.1";
  7. private $delay = 0;
  8. public function priv_bash($line, $args)
  9. {
  10. $channel = $line['to'];
  11. $random = false;
  12. if (strpos($channel, "#") === false)
  13. {
  14. return;
  15. }
  16. if ($this->ircClass->getStatusRaw() != STATUS_CONNECTED_REGISTERED)
  17. {
  18. return;
  19. }
  20. if (time() < $this->delay)
  21. {
  22. $this->ircClass->notice($line['fromNick'], "Please wait " . ($this->delay - time()) . " seconds before using this function again.", 0);
  23. return;
  24. }
  25. //?search=test&sort=0&show=25
  26. if ($args['nargs'] > 0)
  27. {
  28. if ($args['arg1'] == "+")
  29. {
  30. $random = true;
  31. $query = "random1&show=1";
  32. }
  33. else if ($args['arg1'] == "-")
  34. {
  35. $this->ircClass->notice($line['fromNick'], "This is an unsupported function.");
  36. return;
  37. }
  38. else
  39. {
  40. $num = $args['arg1'];
  41. if (substr($num, 0, 1) == "#")
  42. {
  43. $num = substr($num, 1);
  44. }
  45. $num = intval($num);
  46. if ($num <= 0)
  47. {
  48. $search = urlencode($args['query']);
  49. $random = true;
  50. $query = "search=".$search."&show=1&sort=0";
  51. }
  52. else
  53. {
  54. $query = "$num";
  55. }
  56. }
  57. }
  58. else
  59. {
  60. $random = true;
  61. $query = "random&show=1";
  62. }
  63. $line['channel'] = $channel;
  64. $line['random'] = $random;
  65. $host = "www.bash.org";
  66. $port = 80;
  67. $path = "/";
  68. $getQuery = socket::generateGetQuery($query, $host, $path, "1.0");
  69. $this->ircClass->addQuery($host, $port, $getQuery, $line, $this, "priv_bash_stageTwo");
  70. }
  71. public function priv_bash_stageTwo($line, $args, $result, $data)
  72. {
  73. if ($result == QUERY_ERROR)
  74. {
  75. $this->ircClass->notice($line['fromNick'], "An error was encountered: " . $data);
  76. return;
  77. }
  78. $channel = $line['channel'];
  79. $random = $line['random'];
  80. $data = preg_replace("/\r/i", "", $data);
  81. preg_match_all("/<p class=\"quote\">(.+?)<\/p><p class=\"qt\">(.+?)<\/p>/is", $data, $dataArray, PREG_PATTERN_ORDER);
  82. $num = 0;
  83. $t_count = count($dataArray[2]);
  84. $our = rand(0, $t_count - 1);
  85. for ($i = 0; $i < $our - 1; $i++)
  86. {
  87. array_shift($dataArray[2]);
  88. array_shift($dataArray[1]);
  89. }
  90. foreach ($dataArray[2] AS $index => $item)
  91. {
  92. if (trim($item) != "")
  93. {
  94. $item = str_replace("<br />", "", $item);
  95. $item = html_entity_decode($item, ENT_QUOTES);
  96. $newLines = substr_count($item, "\n");
  97. if ($random == true)
  98. {
  99. if ($newLines > 20)
  100. {
  101. continue;
  102. }
  103. }
  104. else
  105. {
  106. if ($newLines > 20)
  107. {
  108. $this->ircClass->notice($line['fromNick'], "The quote to be displayed was over the required 20 line limit. ($newLines lines)");
  109. return;
  110. }
  111. }
  112. $num = $dataArray[1][$index];
  113. $num = preg_replace("/<(.+?)>/i", "", $num);
  114. $offsetA = strpos($num, "(");
  115. $offsetB = strpos($num, ")");
  116. $rating = substr($num, $offsetA + 1, $offsetB - $offsetA - 1);
  117. $num = intval(trim(substr($num, 1, strpos($num, chr(32)))));
  118. break;
  119. }
  120. }
  121. if ($num == 0)
  122. {
  123. if ($random == true)
  124. {
  125. $this->ircClass->notice($line['fromNick'], "None of the data returned was short enough to display. Try again.");
  126. }
  127. else
  128. {
  129. $this->ircClass->notice($line['fromNick'], "No Data was Returned.");
  130. }
  131. }
  132. else
  133. {
  134. $count = 0;
  135. $items = explode("\n", $item);
  136. foreach ($items AS $item)
  137. {
  138. $this->ircClass->privMsg($channel, BOLD . "Bash #" . $num . BOLD . " (Rating: " . $rating . "): " . $item);
  139. $count++;
  140. }
  141. }
  142. $this->delay = time() + $count + 3;
  143. }
  144. }
  145. ?>