c7c388ab12c0fef280e897914745dccf0627af85
[PHP-P10.git] / Bots / PerlGod.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/PerlGod.class.php
21  *
22  * simple Perl 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 = "Perl";
34                         $ident = "perl";
35                         $host = "Services.WebGamesNet.net";
36                         $ip = "0::0";
37                         $realname = "Perl Perl Perl Perl Perl";
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, "#perl", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
42                                 $this->uplink->join($this->bot, "#dev", P10_Channel::USERPRIV_VOICE);
43                 $this->uplink->join($this->bot, "#CoderCom", P10_Channel::USERPRIV_VOICE);
44                         }
45                 } else {
46                         $this->bot = $old;
47                 }
48                 
49                 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
50                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
51         }
52         
53         public function unload($rehash = false) {
54                 foreach($this->execcache as $id => $entry) {
55                         fclose($entry['pipes'][1]);
56                         fclose($entry['pipes'][2]);
57                         proc_terminate($entry['proc'],9);
58                 }
59                 $this->execcache = array();
60                 if($rehash) {
61                         return $this->bot;
62                 } else {
63                         $this->uplink->delUser($this->bot, "Bye.");
64                 }
65         }
66         
67         public function loop() {
68                 foreach($this->execcache as $id => $entry) {
69                         if(!$this->checkstate($entry)) {
70                                 unset($this->execcache[$id]);
71                         }
72                 }
73         }
74         
75         function recive_privmsg($user, $channel, $message) {
76                 $opOnPerlChannel = false;
77                 $entryChannel = P10_Channel::getChannelByName("#Perl");
78                 if($entryChannel) {
79                         $privs = $entryChannel->getUserPrivs($user);
80                         $opOnPerlChannel = ($privs & P10_Channel::USERPRIV_OPED);
81                 }
82                 if(!$user->getModes()->hasMode('o') && !$opOnPerlChannel) return 0;
83                 $exp=explode(" ", $message, 2);
84                 switch (strtolower($exp[0])) {
85                         case "~perl":
86                                 if(count($this->execcache) > 5) {
87                                         $this->uplink->notice($this->bot, $user, "too many running perl processes at the moment!");
88                                         return;
89                                 }
90                                 $entry=array();
91                                 $entry['channel'] = $channel;
92                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
93                                 $entry['proc'] = proc_open('perl', $descriptor, $entry['pipes']);
94                                 if(!is_resource($entry['proc'])) {
95                                         $this->uplink->notice($this->bot, $user, "error while loading perl!");
96                                         return;
97                                 }
98                                 $entry['time'] = time();
99                 if(preg_match("#pastebin\.com/([a-zA-Z0-9]*)$#i", $exp[1])) {
100                     $pasteid = explode("/", $exp[1]);
101                     $pasteid = $pasteid[count($pasteid)-1];
102                     $codecontent = file_get_contents("http://pastebin.com/download.php?i=".$pasteid);
103                     if(preg_match("#Unknown Paste ID!#i", $codecontent)) {
104                         $this->uplink->notice($this->bot, $user, "Unknown Paste ID!");
105                         return;
106                     }
107                     $code = $codecontent;
108                 } else {
109                     $code = $exp[1];
110                 }
111                                 fwrite($entry['pipes'][0], $code);
112                                 fclose($entry['pipes'][0]);
113                                 $this->execcache[] = $entry;
114                                 break;
115                 }
116         }
117         
118         function recive_quit($user, $reason) {
119                 if($user === $this->bot) {
120                         $this->load($this->uplink);
121                 }
122         }
123         
124         function checkstate($entry) {
125                 $data = proc_get_status($entry['proc']);
126                 if(!$data['running']) {
127                         $out="";
128                         while(!feof($entry['pipes'][1])) {
129                                 $out .= fgets($entry['pipes'][1], 128);
130                         }
131                         $eout="";
132                         while(!feof($entry['pipes'][2])) {
133                                 $eout .= fgets($entry['pipes'][2], 128);
134                         }
135                         if($out != "") {
136                                 $out=str_replace("\r","",$out);
137                                 $lines=explode("\n",$out);
138                                 $i=0;
139                                 foreach($lines as $line) {
140                                         $i++;
141                                         if($i>1000) {
142                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
143                                                 break; 
144                                         }
145                                         $this->uplink->privmsg($this->bot, $entry['channel'], $line);
146                                 }
147                         }
148                         if($eout != "") {
149                                 $eout=str_replace("\r","",$eout);
150                                 $lines=explode("\n",$eout);
151                                 $i=0;
152                                 foreach($lines as $line) {
153                                         $i++;
154                                         if($i>1000) { 
155                                                 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
156                                                 break; 
157                                         }
158                                         $this->uplink->privmsg($this->bot, $entry['channel'], "\ 34".$line."\ 3");
159                                 }
160                         }
161                         fclose($entry['pipes'][1]);
162                         fclose($entry['pipes'][2]);
163                         proc_close($entry['proc']);
164                         return false;
165                 } else {
166                         if($entry['time']+10 > time()) {
167                                 return true;
168                         } else {
169                                 //TIMEOUT
170                                 if($entry['term']) {
171                                         fclose($entry['pipes'][1]);
172                                         fclose($entry['pipes'][2]);
173                                         proc_terminate($entry['proc'],9);
174                                         $this->uplink->privmsg($this->bot, $entry['channel'], "perl hard timeout. sending SIGKILL");
175                                         return false;
176                                 } else {
177                                         proc_terminate($entry['proc']);
178                                         $entry['term']=true;
179                                         $this->uplink->privmsg($this->bot, $entry['channel'], "perl timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
180                                         return true;
181                                 }
182                         }
183                 }
184         }
185         
186 }
187
188 ?>