added ability to use pastebin.com for the debugger bots
[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                 unlink("tmp/debug_".$c['id'].".c");
70                 unlink("tmp/debug_".$c['id']);
71                                 unset($this->ccache[$id]);
72                         }
73                 }
74         }
75         
76         function recive_privmsg($user, $channel, $message) {
77                 $opOnCChannel = false;
78                 $CChannel = P10_Channel::getChannelByName("#C");
79                 if($CChannel) {
80                         $privs = $CChannel->getUserPrivs($user);
81                         $opOnCChannel = ($privs & P10_Channel::USERPRIV_OPED);
82                 }
83                 if(!$user->getModes()->hasMode('o') && !$opOnCChannel) return 0;
84                 $exp=explode(" ", $message, 2);
85                 switch (strtolower($exp[0])) {
86                         case "~c":
87                                 if(count($this->ccache) > 5) {
88                                         $this->uplink->notice($this->c, $user, "too many running c 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                     $codecontent = file_get_contents("http://pastebin.com/download.php?i=".$pasteid);
98                     if(preg_match("#Unknown Paste ID!#i", $codecontent)) {
99                         $this->uplink->notice($this->bot, $user, "Unknown Paste ID!");
100                         return;
101                     }
102                     $code = "#include \"includes.h\"
103                     ".$codecontent;
104                 } else {
105                     $code = "#include \"includes.h\"
106                     ".$exp[1];
107                 };
108                 $fp = fopen("tmp/debug_".$entry['id'].".c", "w");
109                 fwrite($fp, $code);
110                 fclose($fp);
111                 $err = shell_exec("gcc -o tmp/debug_".$entry['id']." tmp/debug_".$entry['id'].".c 2>&1");
112                 if($err) {
113                     $err=str_replace("\r","",$err);
114                     $lines=explode("\n",$err);
115                     $i=0;
116                     foreach($lines as $line) {
117                         if($line == "") continue;
118                         $i++;
119                         if($i>100) {
120                             $this->uplink->privmsg($this->c, $entry['channel'], "too many lines!");
121                             break; 
122                         }
123                         $this->uplink->privmsg($this->c, $entry['channel'], $line);
124                     }
125                 }
126                 if(!file_exists("tmp/debug_".$entry['id'])) {
127                     unlink("tmp/debug_".$entry['id'].".c");
128                     break;
129                 }
130                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
131                                 $entry['proc'] = proc_open('tmp/debug_'.$entry['id'], $descriptor, $entry['pipes']);
132                                 if(!is_resource($entry['proc'])) {
133                                         $this->uplink->notice($this->c, $user, "error while loading c!");
134                                         return;
135                                 }
136                                 $entry['time'] = time();
137                                 fclose($entry['pipes'][0]);
138                                 $this->ccache[] = $entry;
139                                 break;
140                 }
141         }
142         
143         function recive_quit($user, $reason) {
144                 if($user === $this->c) {
145                         $this->load($this->uplink);
146                 }
147         }
148         
149         function checkstate($c) {
150                 $data = proc_get_status($c['proc']);
151                 if(!$data['running']) {
152                         $out="";
153                         while(!feof($c['pipes'][1])) {
154                                 $out .= fgets($c['pipes'][1], 128);
155                         }
156                         $eout="";
157                         while(!feof($c['pipes'][2])) {
158                                 $eout .= fgets($c['pipes'][2], 128);
159                         }
160                         if($out != "") {
161                                 $out=str_replace("\r","",$out);
162                                 $lines=explode("\n",$out);
163                                 $i=0;
164                                 foreach($lines as $line) {
165                     if($line == "") continue;
166                                         $i++;
167                                         if($i>1000) {
168                                                 $this->uplink->privmsg($this->c, $c['channel'], "too many lines!");
169                                                 break; 
170                                         }
171                                         $this->uplink->privmsg($this->c, $c['channel'], $line);
172                                 }
173                         }
174                         if($eout != "") {
175                                 $eout=str_replace("\r","",$eout);
176                                 $lines=explode("\n",$eout);
177                                 $i=0;
178                                 foreach($lines as $line) {
179                     if($line == "") continue;
180                                         $i++;
181                                         if($i>1000) { 
182                                                 $this->uplink->privmsg($this->c, $c['channel'], "too many lines!");
183                                                 break; 
184                                         }
185                                         $this->uplink->privmsg($this->c, $c['channel'], "\ 34".$line."\ 3");
186                                 }
187                         }
188                         fclose($c['pipes'][1]);
189                         fclose($c['pipes'][2]);
190                         proc_close($c['proc']);
191                         return false;
192                 } else {
193                         if($c['time']+10 > time()) {
194                                 return true;
195                         } else {
196                                 //TIMEOUT
197                                 if($c['term']) {
198                                         fclose($c['pipes'][1]);
199                                         fclose($c['pipes'][2]);
200                                         proc_terminate($c['proc'],9);
201                                         $this->uplink->privmsg($this->c, $c['channel'], "c hard timeout. sending SIGKILL");
202                                         return false;
203                                 } else {
204                                         proc_terminate($c['proc']);
205                                         $c['term']=true;
206                                         $this->uplink->privmsg($this->c, $c['channel'], "c timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
207                                         return true;
208                                 }
209                         }
210                 }
211         }
212         
213 }
214
215 ?>