2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011 Philipp Kreil (pk910) *
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. *
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. *
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/>. *
18 ************************************************************************
20 * Bots/JavaGod.class.php
22 * simple Java debugger...
26 class {$_NAME} extends Bot {
28 private $bot, $execcache = array();
30 public function load($uplink, $old = false) {
31 $this->uplink = $uplink;
35 $host = "Services.WebGamesNet.net";
37 $realname = "Java Java Java Java Java";
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);
48 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
49 ModCMD::bind($this, BIND_QUIT, "recive_quit");
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);
58 $this->execcache = array();
62 $this->uplink->delUser($this->bot, "Bye.");
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]);
76 function recive_privmsg($user, $channel, $message) {
77 $opOnJavaChannel = false;
78 $JavaChannel = P10_Channel::getChannelByName("#Java");
80 $privs = $JavaChannel->getUserPrivs($user);
81 $opOnJavaChannel = ($privs & P10_Channel::USERPRIV_OPED);
83 if(!$user->getModes()->hasMode('o') && !$opOnJavaChannel) return 0;
84 $exp=explode(" ", $message, 2);
85 switch (strtolower($exp[0])) {
87 if(count($this->execcache) > 5) {
88 $this->uplink->notice($this->bot, $user, "too many running java processes at the moment!");
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']." {
101 fwrite($fp, $javacode);
103 $err = shell_exec("javac tmp/debug_".$entry['id'].".java 2>&1");
105 $err=str_replace("\r","",$err);
106 $lines=explode("\n",$err);
108 foreach($lines as $line) {
109 if($line == "") continue;
112 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
115 $this->uplink->privmsg($this->bot, $entry['channel'], $line);
118 if(!file_exists("tmp/debug_".$entry['id'].".class")) {
119 unlink("tmp/debug_".$entry['id'].".java");
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!");
128 $entry['time'] = time();
129 fclose($entry['pipes'][0]);
130 $this->execcache[] = $entry;
135 function recive_quit($user, $reason) {
136 if($user === $this->bot) {
137 $this->load($this->uplink);
141 function checkstate($entry) {
142 $data = proc_get_status($entry['proc']);
143 if(!$data['running']) {
145 while(!feof($entry['pipes'][1])) {
146 $out .= fgets($entry['pipes'][1], 128);
149 while(!feof($entry['pipes'][2])) {
150 $eout .= fgets($entry['pipes'][2], 128);
153 $out=str_replace("\r","",$out);
154 $lines=explode("\n",$out);
156 foreach($lines as $line) {
157 if($line == "") continue;
160 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
163 $this->uplink->privmsg($this->bot, $entry['channel'], $line);
167 $eout=str_replace("\r","",$eout);
168 $lines=explode("\n",$eout);
170 foreach($lines as $line) {
171 if($line == "") continue;
174 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
177 $this->uplink->privmsg($this->bot, $entry['channel'], "
\ 34".$line."
\ 3");
180 fclose($entry['pipes'][1]);
181 fclose($entry['pipes'][2]);
182 proc_close($entry['proc']);
185 if($entry['time']+10 > time()) {
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");
196 proc_terminate($entry['proc']);
198 $this->uplink->privmsg($this->bot, $entry['channel'], "Java timeout. (maximum of 10 seconds exceeded) sending SIGTERM");