added PHPgod like CGod ;)
[PHP-P10.git] / Bots / CGod.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/CGod.class.php
21  *
22  * simple C debugger...
23  *
24  */
25
26 class {$_NAME} extends Bot {
27         private $uplink;
28         private $c, $ccache = array();
29         
30         public function load($uplink, $old = false) {
31                 $this->uplink = $uplink;
32                 if(!$old) {
33                         $nick = "C";
34                         $ident = "c";
35                         $host = "Services.WebGamesNet.net";
36                         $ip = "0::0";
37                         $realname = "C C C C C";
38                         $modes = "ioknISD";
39                         $this->c = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
40                         if(is_a($this->c, "P10_User")) {
41                                 $this->uplink->join($this->c, "#c", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
42                                 $this->uplink->join($this->c, "#dev", P10_Channel::USERPRIV_VOICE);
43                         }
44                 } else {
45                         $this->c = $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->ccache as $id => $c) {
54                         fclose($c['pipes'][1]);
55                         fclose($c['pipes'][2]);
56                         proc_terminate($c['proc'],9);
57                 }
58                 $this->ccache = array();
59                 if($rehash) {
60                         return $this->c;
61                 } else {
62                         $this->uplink->delUser($this->c, "Bye.");
63                 }
64         }
65         
66         public function loop() {
67                 foreach($this->ccache as $id => $c) {
68                         if(!$this->checkstate($c)) {
69                                 unset($this->ccache[$id]);
70                         }
71                 }
72         }
73         
74         function recive_privmsg($user, $channel, $message) {
75                 $opOnPHPChannel = false;
76                 $PHPChannel = P10_Channel::getChannelByName("#PHP");
77                 if($PHPChannel) {
78                         $privs = $PHPChannel->getUserPrivs($user);
79                         $opOnPHPChannel = ($privs & P10_Channel::USERPRIV_OPED);
80                 }
81                 if(!$user->getModes()->hasMode('o') && !$opOnPHPChannel) return 0;
82                 $exp=explode(" ", $message, 2);
83                 switch (strtolower($exp[0])) {
84                         case "~c":
85                                 if(count($this->ccache) > 5) {
86                                         $this->uplink->notice($this->c, $user, "too many running c processes at the moment!");
87                                         return;
88                                 }
89                                 $entry=array();
90                                 $entry['channel'] = $channel;
91                 $entry['id'] = rand(1, 999999);
92                 $fp = fopen("tmp/debug_".$entry['id'].".c", "w");
93                 fwrite($fp, "#include \"includes.h\"\n".$exp[1]);
94                 fclose($fp);
95                 $err = shell_exec("gcc -o tmp/debug_".$entry['id']." tmp/debug_".$entry['id'].".c");
96                 if($err) {
97                     $err=str_replace("\r","",$err);
98                     $lines=explode("\n",$err);
99                     $i=0;
100                     foreach($lines as $line) {
101                         $i++;
102                         if($i>100) {
103                             $this->uplink->privmsg($this->c, $entry['channel'], "too many lines!");
104                             break; 
105                         }
106                         $this->uplink->privmsg($this->c, $entry['channel'], $line);
107                     }
108                 }
109                 if(!file_exists("tmp/debug_".$entry['id']))
110                     break;
111                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
112                                 $entry['proc'] = proc_open('./debug_'.$entry['id'], $descriptor, $entry['pipes']);
113                                 if(!is_resource($entry['proc'])) {
114                                         $this->uplink->notice($this->c, $user, "error while loading c!");
115                                         return;
116                                 }
117                                 $entry['time'] = time();
118                                 fclose($entry['pipes'][0]);
119                                 $this->ccache[] = $entry;
120                                 break;
121                 }
122         }
123         
124         function recive_quit($user, $reason) {
125                 if($user === $this->c) {
126                         $this->load($this->uplink);
127                 }
128         }
129         
130         function checkstate($c) {
131                 $data = proc_get_status($c['proc']);
132                 if(!$data['running']) {
133                         $out="";
134                         while(!feof($c['pipes'][1])) {
135                                 $out .= fgets($c['pipes'][1], 128);
136                         }
137                         $eout="";
138                         while(!feof($c['pipes'][2])) {
139                                 $eout .= fgets($c['pipes'][2], 128);
140                         }
141                         if($out != "") {
142                                 $out=str_replace("\r","",$out);
143                                 $lines=explode("\n",$out);
144                                 $i=0;
145                                 foreach($lines as $line) {
146                                         $i++;
147                                         if($i>1000) {
148                                                 $this->uplink->privmsg($this->c, $c['channel'], "too many lines!");
149                                                 break; 
150                                         }
151                                         $this->uplink->privmsg($this->c, $c['channel'], $line);
152                                 }
153                         }
154                         if($eout != "") {
155                                 $eout=str_replace("\r","",$eout);
156                                 $lines=explode("\n",$eout);
157                                 $i=0;
158                                 foreach($lines as $line) {
159                                         $i++;
160                                         if($i>1000) { 
161                                                 $this->uplink->privmsg($this->c, $c['channel'], "too many lines!");
162                                                 break; 
163                                         }
164                                         $this->uplink->privmsg($this->c, $c['channel'], "\ 34".$line."\ 3");
165                                 }
166                         }
167                         fclose($c['pipes'][1]);
168                         fclose($c['pipes'][2]);
169                         proc_close($c['proc']);
170                         return false;
171                 } else {
172                         if($c['time']+10 > time()) {
173                                 return true;
174                         } else {
175                                 //TIMEOUT
176                                 if($c['term']) {
177                                         fclose($c['pipes'][1]);
178                                         fclose($c['pipes'][2]);
179                                         proc_terminate($c['proc'],9);
180                                         $this->uplink->privmsg($this->c, $c['channel'], "c hard timeout. sending SIGKILL");
181                                         return false;
182                                 } else {
183                                         proc_terminate($c['proc']);
184                                         $c['term']=true;
185                                         $this->uplink->privmsg($this->c, $c['channel'], "c timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
186                                         return true;
187                                 }
188                         }
189                 }
190         }
191         
192 }
193
194 ?>