Erebot  latest
A modular IRC bot for PHP 7.0+
Worker.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Erebot is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Erebot\Identd;
22 
30 class Worker implements
31  \Erebot\Interfaces\SendingConnection,
32  \Erebot\Interfaces\ReceivingConnection
33 {
35  protected $bot;
36 
38  protected $socket;
39 
41  protected $io;
42 
57  public function __construct(\Erebot\Interfaces\Core $bot, $socket)
58  {
59  if (!is_resource($socket)) {
60  throw new \Erebot\InvalidValueException('Not a valid socket');
61  }
62  $this->bot = $bot;
63  $this->socket = $socket;
64  $this->io = new \Erebot\LineIO(
65  \Erebot\LineIO::EOL_WIN,
66  $this->socket
67  );
68  }
69 
71  public function __destruct()
72  {
73  }
74 
75  public function connect()
76  {
77  }
78 
79  public function disconnect($quitMessage = null)
80  {
81  $this->bot->removeConnection($this);
82  if ($this->socket !== null) {
83  stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
84  }
85  $this->socket = null;
86  }
87 
88  public function read()
89  {
90  return $this->io->read();
91  }
92 
94  public function process()
95  {
96  if (!$this->io->inReadQueue()) {
97  return;
98  }
99 
100  $line = $this->handleMessage($this->io->pop());
101  if ($line) {
102  $this->io->push($line);
103  $this->io->write();
104  }
105 
106  $this->bot->removeConnection($this);
107  $this->disconnect();
108  }
109 
123  protected function handleMessage($line)
124  {
125  if (!is_string($line)) {
126  return false;
127  }
128 
129  $parts = array_map('trim', explode(',', $line));
130  if (count($parts) != 2) {
131  return false;
132  }
133 
134  $line = implode(" , ", $parts);
135  if (!ctype_digit($parts[0]) || !ctype_digit($parts[1])) {
136  return $line . " : ERROR : INVALID-PORT";
137  }
138 
139  $cport = (int) $parts[0];
140  $sport = (int) $parts[1];
141 
142  if ($sport <= 0 || $sport > 65535 || $cport <= 0 || $cport > 65535) {
143  return $line . " : ERROR : INVALID-PORT";
144  }
145 
146  foreach ($this->bot->getConnections() as $connection) {
147  if ($connection == $this) {
148  continue;
149  }
150 
151  $socket = $connection->getSocket();
152 
153  $rport = (int) substr(strrchr(stream_socket_get_name($socket, true), ':'), 1);
154  if ($rport != $sport) {
155  continue;
156  }
157 
158  $lport = (int) substr(strrchr(stream_socket_get_name($socket, false), ':'), 1);
159  if ($lport != $cport) {
160  continue;
161  }
162 
163  try {
164  $config = $connection->getConfig(null);
165  $identity = $config->parseString(
166  '\\Erebot\\Module\\IrcConnector',
167  'identity',
168  ''
169  );
170  return $line . " : USERID : UNIX : " . $identity;
171  } catch (\Erebot\Exception $e) {
172  return $line . " : ERROR : HIDDEN-USER ";
173  }
174  }
175 
176  return $line . " : ERROR : NO-USER";
177  }
178 
179  public function isConnected()
180  {
181  return true;
182  }
183 
184  public function getSocket()
185  {
186  return $this->socket;
187  }
188 
189  public function write()
190  {
191  }
192 
193  public function getBot()
194  {
195  return $this->bot;
196  }
197 
198  public function getConfig($chan)
199  {
200  return null;
201  }
202 
203  public function getIO()
204  {
205  return $this->io;
206  }
207 }
const EOL_WIN
Windows line-ending.
Definition: LineIO.php:34
$socket
The underlying socket, represented as a stream.
Definition: Worker.php:38
Provides core functionalities for Erebot.
Definition: Core.php:59
process()
Processes commands queued in the input buffer.
Definition: Worker.php:94
A worker process for the Identification Protocol (RFC 1413).
Definition: Worker.php:30
$bot
A bot object implementing the Erebot::Interfaces::Core interface.
Definition: Worker.php:35
handleMessage($line)
Definition: Worker.php:123
__construct(\Erebot\Interfaces\Core $bot, $socket)
Definition: Worker.php:57
$io
I/O manager for the socket.
Definition: Worker.php:41
__destruct()
Destructor.
Definition: Worker.php:71