9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Ideone
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Ideone
23 * Interfaces with ideone.com to execute code and return the result.
26 * @package Phergie_Plugin_Ideone
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Plugin_Ideone
30 * @uses Phergie_Plugin_Command pear.phergie.org
32 class Phergie_Plugin_Ideone extends Phergie_Plugin_Abstract
35 * Checks for dependencies.
39 public function onLoad()
41 $this->plugins->getPlugin('Command');
45 * Checks a service response for an error, sends a notice to the event
46 * source if an error has occurred, and returns whether an error was found.
48 * @param array $result Associative array representing the service response
50 * @return boolean TRUE if an error is found, FALSE otherwise
52 protected function isError($result)
54 if ($result['error'] != 'OK') {
55 $this->doNotice($this->event->getNick(), 'ideone error: ' . $result['error']);
62 * Executes a source code sequence in a specified language and returns
65 * @param string $language Programming language the source code is in
66 * @param string $code Source code to execute
70 public function onCommandIdeone($language, $code)
72 $source = $this->event->getSource();
73 $nick = $this->event->getNick();
75 // Get authentication credentials
76 $user = $this->getConfig('ideone.user', 'test');
77 $pass = $this->getConfig('ideone.pass', 'test');
79 // Normalize the command parameters
80 $language = strtolower($language);
82 // Massage PHP code to allow for convenient shorthand
83 if ($language == 'php') {
84 if (!preg_match('/^<\?(?:php)?/', $code)) {
85 $code = '<?php ' . $code;
87 switch (substr($code, -1)) {
97 // Identify the language to use
98 $client = new SoapClient('http://ideone.com/api/1/service.wsdl');
99 $response = $client->getLanguages($user, $pass);
100 if ($this->isError($response)) {
103 $languageLength = strlen($language);
104 foreach ($response['languages'] as $languageId => $languageName) {
105 if (strncasecmp($language, $languageName, $languageLength) == 0) {
110 // Send the paste data
111 $response = $client->createSubmission(
116 null, // string input - data from stdin
117 true, // boolean run - TRUE to execute the code
118 false // boolean private - FALSE to make the paste public
120 if ($this->isError($response)) {
123 $link = $response['link'];
125 // Wait until the paste data is processed or the service fails
126 $attempts = $this->getConfig('ideone.attempts', 10);
127 foreach (range(1, $attempts) as $attempt) {
128 $response = $client->getSubmissionStatus($user, $pass, $link);
129 if ($this->isError($response)) {
132 if ($response['status'] == 0) {
133 $result = $response['result'];
140 if ($result == null) {
141 $this->doNotice($nick, 'ideone error: Timed out');
145 $this->doNotice($nick, 'ideone error: Status code ' . $result);
149 // Get details for the created paste
150 $response = $client->getSubmissionDetails(
154 false, // boolean withSource - FALSE to not return the source code
155 false, // boolean withInput - FALSE to not return stdin data
156 true, // boolean withOutput - TRUE to include output
157 true, // boolean withStderr - TRUE to return stderr data
158 false // boolean withCmpinfo - TRUE to return compilation info
160 if ($this->isError($response)) {
164 // Replace the output if it exceeds a specified maximum length
165 $outputLimit = $this->getConfig('ideone.output_limit', 100);
167 if ($outputLimit && strlen($response['output']) > $outputLimit) {
168 $response['output'] = 'Output is too long to post';
171 // Format the message
172 $msg = $this->getConfig('ideone.format', '%nick%: [ %link% ] %output%');
173 $response['nick'] = $nick;
174 $response['link'] = 'http://ideone.com/' . $link;
175 foreach ($response as $key => $value) {
176 $msg = str_replace('%' . $key . '%', $value, $msg);
178 $this->doPrivmsg($source, $msg);