fixes small issue in JavaGod.class.php
[PHP-P10.git] / Bots / JavaGod.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  Philipp Kreil (pk910)                            *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Bots/JavaGod.class.php
21  *
22  * simple Java debugger...
23  *
24  */
25
26 class {$_NAME} extends Bot {
27         private $uplink;
28         private $bot, $execcache = array();
29         
30         public function load($uplink, $old = false) {
31                 $this->uplink = $uplink;
32                 if(!$old) {
33                         $nick = "Java";
34                         $ident = "Java";
35                         $host = "Services.WebGamesNet.net";
36                         $ip = "0::0";
37                         $realname = "Java Java Java Java Java";
38                         $modes = "ioknISD";
39                         $this->bot = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
40                         if(is_a($this->bot, "P10_User")) {
41                                 $this->uplink->join($this->bot, "#java", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
42                                 $this->uplink->join($this->bot, "#dev", P10_Channel::USERPRIV_VOICE);
43                         }
44                 } else {
45                         $this->bot = $old;
46                 }
47                 
48                 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
49                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
50         }
51         
52         public function unload($rehash = false) {
53                 foreach($this->execcache as $id => $entry) {
54                         fclose($entry['pipes'][1]);
55                         fclose($entry['pipes'][2]);
56                         proc_terminate($entry['proc'],9);
57                 }
58                 $this->execcache = array();
59                 if($rehash) {
60                         return $this->bot;
61                 } else {
62                         $this->uplink->delUser($this->bot, "Bye.");
63                 }
64         }
65         
66         public function loop() {
67                 foreach($this->execcache as $id => $entry) {
68                         if(!$this->checkstate($entry)) {
69                 unlink("tmp/debug_".$entry['id'].".java");
70                 unlink("tmp/debug_".$entry['id'].".class");
71                                 unset($this->execcache[$id]);
72                         }
73                 }
74         }
75         
76         function recive_privmsg($user, $channel, $message) {
77                 $opOnJavaChannel = false;
78                 $JavaChannel = P10_Channel::getChannelByName("#Java");
79                 if($JavaChannel) {
80                         $privs = $JavaChannel->getUserPrivs($user);
81                         $opOnJavaChannel = ($privs & P10_Channel::USERPRIV_OPED);
82                 }
83                 if(!$user->getModes()->hasMode('o') && !$opOnJavaChannel) return 0;
84                 $exp=explode(" ", $message, 2);
85                 switch (strtolower($exp[0])) {
86                         case "~java":
87                                 if(count($this->execcache) > 5) {
88                                         $this->uplink->notice($this->bot, $user, "too many running java processes at the moment!");
89                                         return;
90                                 }
91                                 $entry=array();
92                                 $entry['channel'] = $channel;
93                 $entry['id'] = rand(1, 999999);
94                 if(preg_match("#pastebin\.com/([a-zA-Z0-9]*)$#i", $exp[1])) {
95                     $pasteid = explode("/", $exp[1]);
96                     $pasteid = $pasteid[count($pasteid)-1];
97                     $javacontent = file_get_contents("http://pastebin.com/download.php?i=".$pasteid);
98                     if(preg_match("#Unknown Paste ID!#i", $javacontent)) {
99                         $this->uplink->notice($this->bot, $user, "Unknown Paste ID!");
100                         return;
101                     }
102                     $javacode = "import java.*;
103                     @SuppressWarnings(\"unused\")
104                         public class Debug_".$entry['id']." {
105                             ".$javacontent."
106                         }
107                     ";
108                 } else {
109                     $javacode = "import java.*;
110                     @SuppressWarnings(\"unused\")
111                         public class Debug_".$entry['id']." {
112                             ".$exp[1]."
113                         }
114                     ";
115                 };
116                 $fp = fopen("tmp/debug_".$entry['id'].".java", "w");
117                 fwrite($fp, $javacode);
118                 fclose($fp);
119                 $err = shell_exec("javac tmp/debug_".$entry['id'].".java 2>&1");
120                 if($err) {
121                     $err=str_replace("\r","",$err);
122                     $lines=explode("\n",$err);
123                     $i=0;
124                     foreach($lines as $line) {
125                         if($line == "") continue;
126                         $i++;
127                         if($i>100) {
128                             $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
129                             break; 
130                         }
131                         $this->uplink->privmsg($this->bot, $entry['channel'], $line);
132                     }
133                 }
134                 if(!file_exists("tmp/debug_".$entry['id'].".class")) {
135                     unlink("tmp/debug_".$entry['id'].".java");
136                     break;
137                 }
138                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
139                                 $entry['proc'] = proc_open('java tmp/debug_'.$entry['id'].'.class', $descriptor, $entry['pipes']);
140                                 if(!is_resource($entry['proc'])) {
141                                         $this->uplink->notice($this->bot, $user, "error while loading c!");
142                                         return;
143                                 }
144                                 $entry['time'] = time();
145                                 fclose($entry['pipes'][0]);
146                                 $this->execcache[] = $entry;
147                                 break;
148                 }
149         }
150         
151         function recive_quit($user, $reason) {
152                 if($user === $this->bot) {
153                         $this->load($this->uplink);
154                 }
155         }
156         
157         function checkstate($entry) {
158                 $data = proc_get_status($entry['proc']);
159                 if(!$data['running']) {
160                         $out="";
161                         while(!feof($entry['pipes'][1])) {
162                                 $out .= fgets($entry['pipes'][1], 128);
163                         }
164                         $eout="";
165                         while(!feof($entry['pipes'][2])) {
166                                 $eout .= fgets($entry['pipes'][2], 128);
167                         }
168                         if($out != "") {
169                                 $out=str_replace("\r","",$out);
170                                 $lines=explode("\n",$out);
171                                 $i=0;
172                                 foreach($lines as $line) {
173                     if($line == "") continue;
174                                         $i++;
175                                         if($i>1000) {
176                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
177                                                 break; 
178                                         }
179                                         $this->uplink->privmsg($this->bot, $entry['channel'], $line);
180                                 }
181                         }
182                         if($eout != "") {
183                                 $eout=str_replace("\r","",$eout);
184                                 $lines=explode("\n",$eout);
185                                 $i=0;
186                                 foreach($lines as $line) {
187                     if($line == "") continue;
188                                         $i++;
189                                         if($i>1000) { 
190                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
191                                                 break; 
192                                         }
193                                         $this->uplink->privmsg($this->bot, $entry['channel'], "\ 34".$line."\ 3");
194                                 }
195                         }
196                         fclose($entry['pipes'][1]);
197                         fclose($entry['pipes'][2]);
198                         proc_close($entry['proc']);
199                         return false;
200                 } else {
201                         if($entry['time']+10 > time()) {
202                                 return true;
203                         } else {
204                                 //TIMEOUT
205                                 if($entry['term']) {
206                                         fclose($entry['pipes'][1]);
207                                         fclose($entry['pipes'][2]);
208                                         proc_terminate($entry['proc'],9);
209                                         $this->uplink->privmsg($this->bot, $entry['channel'], "Java hard timeout. sending SIGKILL");
210                                         return false;
211                                 } else {
212                                         proc_terminate($entry['proc']);
213                                         $entry['term']=true;
214                                         $this->uplink->privmsg($this->bot, $entry['channel'], "Java timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
215                                         return true;
216                                 }
217                         }
218                 }
219         }
220         
221 }
222
223 ?>