45 class Prompt implements \Erebot\Interfaces\ReceivingConnection
100 \Erebot\Interfaces\
Core $bot,
107 if ($connector === null) {
108 $connector = sys_get_temp_dir() .
109 DIRECTORY_SEPARATOR .
112 $this->socket = stream_socket_server(
118 if (!$this->socket) {
119 throw new \Exception(
"Could not create prompt (".$errstr.
")");
123 register_shutdown_function(
131 if ($group !== null) {
132 if (!@chgrp($connector, $group)) {
133 throw new \Exception(
134 "Could not change group to '$group' for '$connector'"
140 if (!chmod($connector, $perms)) {
141 throw new \Exception(
142 "Could not set permissions to $perms on '$connector'"
149 $flush = array($this->socket);
151 while (stream_select($flush, $dummy, $dummy, 0) == 1) {
152 if (fread($this->socket, 8192) ===
false) {
153 throw new \Exception(
"Error while flushing the socket");
157 $this->io = new \Erebot\LineIO(\Erebot\LineIO::EOL_ANY, $this->socket);
158 $logger = \Plop\Plop::getInstance();
160 $bot->gettext(
'Prompt started in "%(path)s"'),
161 array(
'path' => $connector)
171 public function connect()
173 $this->bot->addConnection($this);
176 public function disconnect($quitMessage = null)
178 $this->bot->removeConnection($this);
179 if ($this->socket !== null) {
180 stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
182 $this->socket = null;
185 public function isConnected()
190 public function getSocket()
192 return $this->socket;
195 public function getIO()
200 public function read()
202 $res = $this->io->read();
203 if ($res ===
false) {
204 throw new \Erebot\ConnectionFailureException(
'Disconnected');
212 for ($i = $this->io->inReadQueue(); $i > 0; $i--) {
213 $this->handleMessage($this->io->pop());
226 $pos = strpos($line,
' ');
227 if ($pos ===
false) {
231 $pattern = preg_quote(substr($line, 0, $pos),
'@');
232 $pattern = strtr($pattern, array(
'\\?' =>
'.?',
'\\*' =>
'.*'));
233 $line = substr($line, $pos + 1);
234 if ($line ===
false) {
238 foreach ($this->bot->getConnections() as $connection) {
239 if (!($connection instanceof \Erebot\Interfaces\SendingConnection) || $connection == $this) {
243 $config = $connection->getConfig(null);
244 $netConfig = $config->getNetworkCfg();
245 if (preg_match(
'@^'.$pattern.
'$@Di', $netConfig->getName())) {
246 $connection->getIO()->push($line);
251 public function getBot()
256 public function getConfig($chan)
A simple prompt which can be used to send commands remotely.
$bot
A bot object implementing the Erebot::Interfaces::Core interface.
Provides core functionalities for Erebot.
process()
Processes commands queued in the input buffer.
$socket
The underlying socket, represented as a stream.
$io
I/O manager for the socket.
__construct(\Erebot\Interfaces\Core $bot, $connector=null, $group=null, $perms=0660)