added JavaGod Bot ;)
[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                 $fp = fopen("tmp/debug_".$entry['id'].".c", "w");
95                 $javacode = "import java.*;
96                 @SuppressWarnings("unused")
97                     public class Debug_".$entry['id']." {
98                         ".$exp[1]."
99                     }
100                 ";
101                 fwrite($fp, $javacode);
102                 fclose($fp);
103                 $err = shell_exec("javac tmp/debug_".$entry['id'].".java 2>&1");
104                 if($err) {
105                     $err=str_replace("\r","",$err);
106                     $lines=explode("\n",$err);
107                     $i=0;
108                     foreach($lines as $line) {
109                         if($line == "") continue;
110                         $i++;
111                         if($i>100) {
112                             $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
113                             break; 
114                         }
115                         $this->uplink->privmsg($this->bot, $entry['channel'], $line);
116                     }
117                 }
118                 if(!file_exists("tmp/debug_".$entry['id'].".class")) {
119                     unlink("tmp/debug_".$entry['id'].".java");
120                     break;
121                 }
122                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
123                                 $entry['proc'] = proc_open('java tmp/debug_'.$entry['id'].'.class', $descriptor, $entry['pipes']);
124                                 if(!is_resource($entry['proc'])) {
125                                         $this->uplink->notice($this->bot, $user, "error while loading c!");
126                                         return;
127                                 }
128                                 $entry['time'] = time();
129                                 fclose($entry['pipes'][0]);
130                                 $this->execcache[] = $entry;
131                                 break;
132                 }
133         }
134         
135         function recive_quit($user, $reason) {
136                 if($user === $this->bot) {
137                         $this->load($this->uplink);
138                 }
139         }
140         
141         function checkstate($entry) {
142                 $data = proc_get_status($entry['proc']);
143                 if(!$data['running']) {
144                         $out="";
145                         while(!feof($entry['pipes'][1])) {
146                                 $out .= fgets($entry['pipes'][1], 128);
147                         }
148                         $eout="";
149                         while(!feof($entry['pipes'][2])) {
150                                 $eout .= fgets($entry['pipes'][2], 128);
151                         }
152                         if($out != "") {
153                                 $out=str_replace("\r","",$out);
154                                 $lines=explode("\n",$out);
155                                 $i=0;
156                                 foreach($lines as $line) {
157                     if($line == "") continue;
158                                         $i++;
159                                         if($i>1000) {
160                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
161                                                 break; 
162                                         }
163                                         $this->uplink->privmsg($this->bot, $entry['channel'], $line);
164                                 }
165                         }
166                         if($eout != "") {
167                                 $eout=str_replace("\r","",$eout);
168                                 $lines=explode("\n",$eout);
169                                 $i=0;
170                                 foreach($lines as $line) {
171                     if($line == "") continue;
172                                         $i++;
173                                         if($i>1000) { 
174                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
175                                                 break; 
176                                         }
177                                         $this->uplink->privmsg($this->bot, $entry['channel'], "\ 34".$line."\ 3");
178                                 }
179                         }
180                         fclose($entry['pipes'][1]);
181                         fclose($entry['pipes'][2]);
182                         proc_close($entry['proc']);
183                         return false;
184                 } else {
185                         if($entry['time']+10 > time()) {
186                                 return true;
187                         } else {
188                                 //TIMEOUT
189                                 if($entry['term']) {
190                                         fclose($entry['pipes'][1]);
191                                         fclose($entry['pipes'][2]);
192                                         proc_terminate($entry['proc'],9);
193                                         $this->uplink->privmsg($this->bot, $entry['channel'], "Java hard timeout. sending SIGKILL");
194                                         return false;
195                                 } else {
196                                         proc_terminate($entry['proc']);
197                                         $entry['term']=true;
198                                         $this->uplink->privmsg($this->bot, $entry['channel'], "Java timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
199                                         return true;
200                                 }
201                         }
202                 }
203         }
204         
205 }
206
207 ?>