2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910) *
4 * This program is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 3 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ************************************************************************
19 * Bots/ScalaGod.class.php
21 * simple Scala debugger...
25 class {$_NAME} extends Bot {
27 private $bot, $execcache = array();
29 public function load($uplink, $old = false) {
30 $this->uplink = $uplink;
34 $host = "services.....";
36 $realname = "Scala Scala Scala Scala Scala";
38 $this->bot = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
39 if(is_a($this->bot, "P10_User")) {
40 $this->uplink->join($this->bot, "#dev", P10_Channel::USERPRIV_VOICE);
41 $this->uplink->join($this->bot, "#opers", P10_Channel::USERPRIV_OPED);
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'].".scala");
70 unset($this->execcache[$id]);
75 function recive_privmsg($user, $channel, $message) {
76 $opOnScalaChannel = false;
77 $ScalaChannel = P10_Channel::getChannelByName("#dev");
79 $privs = $ScalaChannel->getUserPrivs($user);
80 $opOnScalaChannel = ($privs & P10_Channel::USERPRIV_OPED);
82 if(!$user->getModes()->hasMode('o') && !$opOnScalaChannel) return 0;
83 $exp=explode(" ", $message, 2);
84 switch (strtolower($exp[0])) {
86 if(count($this->execcache) > 5) {
87 $this->uplink->notice($this->bot, $user, "too many running scala processes at the moment!");
91 $entry['channel'] = $channel;
92 $entry['id'] = rand(1, 999999);
93 if(preg_match("#pastebin\.com/([a-zA-Z0-9]*)$#i", $exp[1])) {
94 $pasteid = explode("/", $exp[1]);
95 $pasteid = $pasteid[count($pasteid)-1];
96 $scalacontent = file_get_contents("http://pastebin.com/download.php?i=".$pasteid);
97 if(preg_match("#Unknown Paste ID!#i", $scalacontent)) {
98 $this->uplink->notice($this->bot, $user, "Unknown Paste ID!");
101 $scalacode = "import scala._
105 $scalacode = "import scala._
109 $fp = fopen("tmp/Debug_".$entry['id'].".scala", "w");
110 fwrite($fp, $scalacode);
112 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
113 $entry['proc'] = proc_open('scala tmp/Debug_'.$entry['id'].'.scala', $descriptor, $entry['pipes']);
114 if(!is_resource($entry['proc'])) {
115 $this->uplink->notice($this->bot, $user, "error while loading c!");
118 $entry['time'] = time();
119 fclose($entry['pipes'][0]);
120 $this->execcache[] = $entry;
125 function recive_quit($user, $reason) {
126 if($user === $this->bot) {
127 $this->load($this->uplink);
131 function checkstate($entry) {
132 $data = proc_get_status($entry['proc']);
133 if(!$data['running']) {
135 while(!feof($entry['pipes'][1])) {
136 $out .= fgets($entry['pipes'][1], 128);
139 while(!feof($entry['pipes'][2])) {
140 $eout .= fgets($entry['pipes'][2], 128);
143 $out=str_replace("\r","",$out);
144 $lines=explode("\n",$out);
146 foreach($lines as $line) {
147 if($line == "") continue;
150 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
153 $this->uplink->privmsg($this->bot, $entry['channel'], $line);
157 $eout=str_replace("\r","",$eout);
158 $lines=explode("\n",$eout);
160 foreach($lines as $line) {
161 if($line == "") continue;
164 $this->uplink->privmsg($this->bot, $entry['channel'], "too many lines!");
167 $this->uplink->privmsg($this->bot, $entry['channel'], "
\ 34".$line."
\ 3");
170 fclose($entry['pipes'][1]);
171 fclose($entry['pipes'][2]);
172 proc_close($entry['proc']);
175 if($entry['time']+10 > time()) {
180 fclose($entry['pipes'][1]);
181 fclose($entry['pipes'][2]);
182 proc_terminate($entry['proc'],9);
183 $this->uplink->privmsg($this->bot, $entry['channel'], "Scala hard timeout. sending SIGKILL");
186 proc_terminate($entry['proc']);
188 $this->uplink->privmsg($this->bot, $entry['channel'], "Scala timeout. (maximum of 10 seconds exceeded) sending SIGTERM");