irc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. require_once(dirname(dirname(__FILE__))."/header.php");
  3. require_once('xmlrpc.php');
  4. function atheme_login($hostname,$port, $path, $username, $password){
  5. $message = new xmlrpcmsg("atheme.login");
  6. $message->addParam(new xmlrpcval($username, "string"));
  7. $message->addParam(new xmlrpcval($password, "string"));
  8. $client = new xmlrpc_client($path, $hostname, $port);
  9. $response = $client->send($message);
  10. if(!$response->faultCode()){
  11. $session = explode("<string>", $response->serialize());
  12. $session = explode("</string", $session[1]);
  13. $session = $session[0];
  14. return Array(true,$session);
  15. }else{
  16. return Array(
  17. false,
  18. '['.$response->faultCode().'] '.$response->faultString()
  19. );
  20. }
  21. }
  22. function atheme_command($hostname, $port, $path, $sourceip, $username, $password, $service, $command, $params=NULL){
  23. $message = new xmlrpcmsg("atheme.login");
  24. $message->addParam(new xmlrpcval($username, "string"));
  25. $message->addParam(new xmlrpcval($password, "string"));
  26. $client = new xmlrpc_client($path, $hostname, $port);
  27. $response = $client->send($message);
  28. $session = NULL;
  29. if(!$response->faultCode()){
  30. $session = explode("<string>", $response->serialize());
  31. $session = explode("</string", $session[1]);
  32. $session = $session[0];
  33. }else{
  34. switch($response->faultCode()){
  35. case 1:
  36. $m = __('Insufficient Parameters to login');
  37. break;
  38. case 3:
  39. $m = __("Account is not registered");
  40. break;
  41. case 5:
  42. $m = __("Invalid Username/Password");
  43. break;
  44. case 6:
  45. $m = __("Account is frozen");
  46. break;
  47. default:
  48. $m = __("Could not log in");
  49. }
  50. return Array(false,$m);
  51. }
  52. $message = new xmlrpcmsg("atheme.command");
  53. $message->addParam(new xmlrpcval($session, "string"));
  54. $message->addParam(new xmlrpcval($username, "string"));
  55. $message->addParam(new xmlrpcval($sourceip, "string"));
  56. $message->addParam(new xmlrpcval($service, "string"));
  57. $message->addParam(new xmlrpcval($command, "string"));
  58. if($params != NULL){
  59. if(sizeof($params) < 2){
  60. foreach($params as $param){
  61. $message->addParam(new xmlrpcval($param, "string"));
  62. }
  63. }else{
  64. $firstparam = $params[0];
  65. $secondparam = "";
  66. for($i = 1; $i < sizeof($params); $i++){
  67. $secondparam .= $params[$i] . " ";
  68. }
  69. $secondparam = rtrim($secondparam);
  70. $message->addParam(new xmlrpcval($firstparam, "string"));
  71. $message->addParam(new xmlrpcval($secondparam, "string"));
  72. }
  73. }
  74. $response = $client->send($message);
  75. if(!$response->faultCode()){
  76. $response = explode("<string>", $response->serialize());
  77. $response = explode("</string", $response[1]);
  78. $response = $response[0];
  79. return Array(true,$response);
  80. }else{
  81. return Array(false,"Command failed: " . $response->faultString());
  82. }
  83. }
  84. $ircret = "";
  85. function ircputs($line){
  86. global $msg;
  87. global $irc;
  88. $msg .= str_replace(get_conf('rehash-pass','string'),'**********',$line);
  89. try{
  90. error_reporting(0);
  91. $r = fputs($irc,$line);
  92. error_reporting(E_ALL);
  93. }catch(Exception $e){
  94. $r = false;
  95. ircclose($e->code,$e->message);
  96. }
  97. return $r;
  98. }
  99. function ircclose($code=0,$message=null,$ret_type='string'){
  100. global $msg;
  101. global $irc;
  102. global $ircret;
  103. try{
  104. error_reporting(0);
  105. $msg .= 'QUIT :'.$message;
  106. fputs($irc,'QUIT :'.$message);
  107. error_reporting(E_ALL);
  108. }catch(Exception $e){}
  109. while(!feof($irc) && $line = fgets($irc,128)){
  110. if(is_string($line)){
  111. $msg .= $line;
  112. }
  113. }
  114. fclose($irc);
  115. if($ret_type == 'string'){
  116. $ircret = '{"code":'.$code.',"message":"'.$message.'","log":'.json_encode($msg).'}';
  117. }else{
  118. $ircret = Array(
  119. 'code'=>$code,
  120. 'message'=>$message,
  121. 'log'=>$msg
  122. );
  123. }
  124. return $ircret;
  125. }
  126. function isval($src,$prop,$val){
  127. return isset($src[$prop]) && $src[$prop] == $val;
  128. }
  129. function ircrehash(){
  130. global $msg;
  131. global $irc;
  132. global $ircret;
  133. global $u;
  134. global $user;
  135. if(!isset($u)){
  136. $u = $user;
  137. }
  138. $msg = '';
  139. if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,__("Could not connect."));}
  140. stream_set_timeout($irc,1) or ircclose(2,__("Could not set timeout."));
  141. while(!feof($irc)&&!$msg = fgets($irc,128)){}
  142. if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
  143. if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
  144. while(!feof($irc)){
  145. $line = fgets($irc,128);
  146. if(is_string($line)){
  147. $msg .= $line;
  148. $data = explode(' ',$line);
  149. if(isval($data,1,'433')){
  150. return ircclose(4,__("RehashServ is already running."));
  151. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  152. return ircclose(3,__("IRC Server refused the connection."));
  153. }elseif($data[0] == 'PING'){
  154. if(!ircputs("PONG {$data[1]}")){return $ircret;}
  155. }elseif(isval($data,1,'001')){
  156. break;
  157. }
  158. }
  159. }
  160. if(!ircputs("IDENTIFY ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  161. while(!feof($irc)){
  162. $line = fgets($irc,128);
  163. if(is_string($line)){
  164. $msg .= $line;
  165. $data = explode(' ',$line);
  166. if(isval($data,1,'433')){
  167. return ircclose(4,__("RehashServ is already running."));
  168. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  169. return ircclose(3,__("IRC Server refused the connection."));
  170. }elseif(strrpos($line,":You are now identified for") !== false){
  171. break;
  172. }elseif(strrpos($line,'Password incorrect.') !== false){
  173. return ircclose(5,__("Failed to authenticate with NickServ"));
  174. }
  175. }
  176. }
  177. if(!ircputs("HS ON\r\n")){return $ircret;}
  178. while(!feof($irc)){
  179. $line = fgets($irc,128);
  180. if(is_string($line)){
  181. $msg .= $line;
  182. $data = explode(' ',$line);
  183. if(isval($data,1,'433')){
  184. return ircclose(4,__("RehashServ is already running."));
  185. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  186. return ircclose(3,__("IRC Server refused the connection."));
  187. }elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
  188. break;
  189. }elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
  190. return ircclose(6,__("vhost not set."));
  191. }
  192. }
  193. }
  194. if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  195. if(!ircputs("REHASH -global\r\n")){return $ircret;}
  196. if(!ircputs("WALLOPS :{$u['nick']} has rehashed the server\r\n")){return $ircret;}
  197. try{
  198. error_reporting(0);
  199. $msg .= 'QUIT :'.$message;
  200. fputs($irc,'QUIT :'.$message);
  201. error_reporting(E_ALL);
  202. }catch(Exception $e){}
  203. while(!feof($irc) && $line = fgets($irc,128)){
  204. if(is_string($line)){
  205. $msg .= $line;
  206. }
  207. }
  208. fclose($irc);
  209. if(strrpos($msg,':*** Notice -- Configuration loaded without any problems ..') === false){
  210. return '{"code":6,"message":"'.__('There is an error in the config. See console for output.').'","log":'.json_encode($msg).'}';
  211. }
  212. return '{"code":0,"message":"'.__('Rehashed. View console for output.').'","log":'.json_encode($msg).'}';
  213. }
  214. function irccommands($commands,$runas="RehashServ"){
  215. global $msg;
  216. global $irc;
  217. global $ircret;
  218. global $u;
  219. global $user;
  220. $ircret = Array(
  221. 'code'=>1
  222. );
  223. if(!isset($u)){
  224. $u = $user;
  225. }
  226. $msg = '';
  227. if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,__("Could not connect."),'array');}
  228. stream_set_timeout($irc,1) or ircclose(2,__("Could not set timeout."),'array');
  229. while(!feof($irc)&&!$msg = fgets($irc,128)){}
  230. if($runas == 'RehashServ'){
  231. if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
  232. if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
  233. }else{
  234. if(!ircputs("NICK RunServ\r\n")){return $ircret;}
  235. if(!ircputs("USER {$runas} omni.irc.omnimaga.org {$runas} :{$runas}\r\n")){return $ircret;}
  236. }
  237. while(!feof($irc)){
  238. $line = fgets($irc,128);
  239. if(is_string($line)){
  240. $msg .= $line;
  241. $data = explode(' ',$line);
  242. if(isval($data,1,'433')){
  243. return ircclose(4,__("RunServ is already running."),'array');
  244. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  245. return ircclose(3,__("IRC Server refused the connection."),'array');
  246. }elseif($data[0] == 'PING'){
  247. if(!ircputs("PONG {$data[1]}")){return $ircret;}
  248. }elseif(isval($data,1,'001')){
  249. break;
  250. }
  251. }
  252. }
  253. if($runas == 'RehashServ'){
  254. if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  255. if(!ircputs("IDENTIFY RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
  256. }else{
  257. if(!ircputs("IDENTIFY {$runas} ".$_SESSION['password']."\r\n")){return $ircret;}
  258. }
  259. while(!feof($irc)){
  260. $line = fgets($irc,128);
  261. if(is_string($line)){
  262. $msg .= $line;
  263. $data = explode(' ',$line);
  264. if(isval($data,1,'433')){
  265. return ircclose(4,__("RunServ is already running."),'array');
  266. }elseif(isval($data,1,'375')){
  267. while(!feof($irc)){
  268. $line = fgets($irc,128);
  269. if(is_string($line)){
  270. $msg .= $line;
  271. $data = explode(' ',$line);
  272. if(isval($data,1,'376')){
  273. break;
  274. }
  275. }
  276. }
  277. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  278. return ircclose(3,__("IRC Server refused the connection."),'array');
  279. }elseif(strrpos($line,":You are now identified for") !== false){
  280. break;
  281. }elseif(strrpos($line,'Password incorrect.') !== false){
  282. return ircclose(5,__("Failed to authenticate with NickServ"),'array');
  283. }
  284. }
  285. }
  286. if($runas == 'RehashServ'){
  287. if(!ircputs("HS ON\r\n")){return $ircret;}
  288. while(!feof($irc)){
  289. $line = fgets($irc,128);
  290. if(is_string($line)){
  291. $msg .= $line;
  292. $data = explode(' ',$line);
  293. if(isval($data,1,'433')){
  294. return ircclose(4,__("RunServ is already running."),'array');
  295. }elseif(strrpos($line,'ERROR :Closing Link:') !== false){
  296. return ircclose(3,__("IRC Server refused the connection."),'array');
  297. }elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
  298. break;
  299. }elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
  300. return ircclose(6,__("vhost not set."),'array');
  301. }
  302. }
  303. }
  304. }
  305. foreach($commands as $k => $command){
  306. if(!ircputs($command."\r\n")){return $ircret;}
  307. }
  308. try{
  309. error_reporting(0);
  310. $msg .= 'QUIT :'.$message;
  311. fputs($irc,'QUIT :'.$message);
  312. error_reporting(E_ALL);
  313. }catch(Exception $e){}
  314. while(!feof($irc) && $line = fgets($irc,128)){
  315. if(is_string($line)){
  316. $msg .= $line;
  317. }
  318. }
  319. fclose($irc);
  320. return array(
  321. 'code'=>0,
  322. 'log'=>$msg
  323. );
  324. }
  325. function channel_flag_name($flag){
  326. switch($flag){
  327. case 'v':$name=__('Voice');break;
  328. case 'V':$name=__('Automatic voice');break;
  329. case 'h':$name=__('Halfop');break;
  330. case 'H':$name=__('Automatic Halfop');break;
  331. case 'o':$name=__('Op');break;
  332. case 'O':$name=__('Automatic Op');break;
  333. case 'a':$name=__('Admin');break;
  334. case 'q':$name=__('Owner');break;
  335. case 's':$name=__('Set');break;
  336. case 'i':$name=__('Invite/Getkey');break;
  337. case 'r':$name=__('Kick/Ban');break;
  338. case 'R':$name=__('Recover/Clear');break;
  339. case 'f':$name=__('Modify access lists');break;
  340. case 't':$name=__('Topic');break;
  341. case 'A':$name=__('View access lists');break;
  342. case 'F':$name=__('Founder');break;
  343. case 'b':$name=__('Banned');break;
  344. default:$name=$flag;
  345. }
  346. return $name;
  347. }
  348. ?>