PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/core/phpIRC.php

https://github.com/SimonSickle/PhpIRCbot
PHP | 144 lines | 143 code | 0 blank | 1 comment | 31 complexity | 12f2cfec13cbdca0c5d1032c4f900b17 MD5 | raw file
  1. <?php
  2. class IRC
  3. {
  4. protected $crl;
  5. protected $fp;
  6. protected $host;
  7. protected $port;
  8. protected $username;
  9. protected $password;
  10. protected $nick;
  11. protected $localhost;
  12. protected $fullname;
  13. public $channel;
  14. protected $commands;
  15. protected $chat_commands;
  16. protected $message_trigger;
  17. protected $verbose;
  18. private $err_num;
  19. private $err_msg;
  20. function __construct($host, $port, $username, $password, $nick, $channel, $fullname)
  21. {
  22. $this->crl = "\r\n";
  23. $this->host = $host;
  24. $this->port = $port;
  25. $this->username = $username;
  26. $this->password = $password;
  27. $this->nick = $nick;
  28. $this->localhost = 'localhost';
  29. $this->fullname = $fullname;
  30. $this->channel = $channel;
  31. $this->chat_commands = array();
  32. $this->commands = array();
  33. $this->message_trigger = false;
  34. $this->verbose = true;
  35. }
  36. function connect(){
  37. $this->fp = fsockopen($this->host,$this->port, $err_num, $err_msg);
  38. if(!$this->fp){
  39. return false;
  40. } else {
  41. return true;
  42. }
  43. }
  44. function set_var($var, $val){
  45. $this->$var = $val;
  46. }
  47. function get_var($var){
  48. return $this->$var;
  49. }
  50. function chat_trigger($trigger_name, $response, $type = "CHANNEL"){
  51. array_push($this->chat_commands, array('trigger_name' => $trigger_name, 'response' => $response, 'type' => $type));
  52. return true;
  53. }
  54. function function_trigger($trigger_name, $function, $type = "CHANNEL"){
  55. array_push($this->commands, array('trigger_name' => $trigger_name, 'function' => $function, 'type' => $type));
  56. return true;
  57. }
  58. function send($data){
  59. fputs($this->fp, $data . $this->crl);
  60. }
  61. function say($to, $msg){
  62. echo "Said " .'PRIVMSG '.$to.' :' . $msg. $this->crl;
  63. fputs($this->fp,'PRIVMSG '.$to.' :' . $msg. $this->crl);
  64. return true;
  65. }
  66. function nick($nick){
  67. $this->nick = $nick;
  68. fputs($this->fp, "NICK ".$nick.$this->crl);
  69. }
  70. function leave_channel($channel){
  71. fputs($this->fp, "PART ".$this->channel.$this->crl);
  72. return true;
  73. }
  74. function join_channel($channel){
  75. fputs($this->fp, "PART ".$this->channel.$this->crl);
  76. $this->channel = $channel;
  77. fputs($this->fp, "JOIN ".$this->channel.$this->crl);
  78. return true;
  79. }
  80. function start($identify = false){
  81. $Header = 'NICK '.$this->nick . $this->crl;
  82. $Header .= 'USER '.$this->username.' '.$this->localhost.' '.$this->host.' :'.$this->fullname . $this->crl;
  83. fputs($this->fp, $Header);
  84. sleep(2);
  85. if($identify == true){
  86. fputs($this->fp, 'PRIVMSG nickserv :identify '.$this->username.' '.$this->password. $this->crl);
  87. }
  88. while (!feof($this->fp)) {
  89. $response .= fgets($this->fp, 1024);
  90. //echo $response . $this->crl;
  91. if(substr($response, 0, 4) == "PING"){
  92. $tmp = explode("PING :", $response);
  93. $tmp = trim($tmp[1]);
  94. fputs($this->fp,'PONG ' . $tmp . $this->crl);
  95. }
  96. if(strpos($response, 'You are now identified for')){
  97. fputs($this->fp, 'JOIN ' . $this->channel . $this->crl);
  98. }
  99. $offset = strpos($response, $this->crl);
  100. $data = substr($response,0,$offset);
  101. $response = substr($response,$offset+2);
  102. if (substr($data,0,1) == ':') {
  103. $offsetA = strpos($data, ' ');
  104. $dFrom = substr($data,1,$offsetA-1);
  105. $offsetB = strpos($data, ' :');
  106. $dCommand = substr($data,$offsetA+1,$offsetB-$offsetA-1);
  107. $offsetC = strpos($data, '!');
  108. $dNick = substr($data,1,$offsetC-1);
  109. $iText = substr($data,$offsetB+2);
  110. if($this->verbose == true){ echo "$offsetA : $dFrom : $offsetB : $dCommand : $offsetC : $dNick : $iText\n"; }
  111. if(substr($dCommand, 0, 7) == "PRIVMSG"){
  112. $tmp = explode(" ", $dCommand, 2);
  113. $from = $tmp[1];
  114. if($from[0] == '#'){
  115. $type = "CHANNEL";
  116. $from = $dNick;
  117. }else{ $type = "PRIVMSG"; $from = $dNick; }
  118. $ret = array("type" => $type, "from" => $from, "message" => $iText);
  119. foreach($this->chat_commands as $k=>$v){
  120. if(substr($iText, 0, strlen($v['trigger_name'])) == $v['trigger_name']){
  121. if($v['type'] == 'CHANNEL' && $type == 'CHANNEL'){
  122. $this->say($this->channel, $v['response']);
  123. } elseif($v['type'] == 'PRIVMSG' && $type == 'PRIVMSG'){
  124. $this->say($from, $v['response']);
  125. }
  126. }
  127. }
  128. foreach($this->commands as $k=>$v){
  129. if(substr($iText, 0, strlen($v['trigger_name'])) == $v['trigger_name']){
  130. if($v['type'] == 'CHANNEL'){
  131. call_user_func($v['function'], array("from" => $from, "type" => $type, "command" => str_replace("$v[trigger_name] ", "", $iText), 'channel' => $this->channel), $this);
  132. } elseif($v['type'] == 'PRIVMSG'){
  133. call_user_func($v['function'], array("from" => $from, "type" => $type, "command" => str_replace("$v[trigger_name] ", "", $iText)), $this);
  134. }
  135. }
  136. }
  137. $ret = array("type" => $type, "from" => $from, "message" => $iText, 'channel' => $this->channel);
  138. if($this->message_trigger){ call_user_func($this->message_trigger, $ret, $this); }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. ?>