Erebot  latest
A modular IRC bot for PHP 7.0+
Server.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 Server implements \Erebot\Interfaces\ReceivingConnection
31 {
33  protected $bot;
34 
36  protected $socket;
37 
39  protected $workerCls;
40 
63  public function __construct(
64  \Erebot\Interfaces\Core $bot,
65  $connector = '0.0.0.0:113',
66  $workerCls = '\\Erebot\\Identd\\Worker'
67  ) {
68  $this->bot = $bot;
69  $this->workerCls = $workerCls;
70  $this->socket = stream_socket_server('tcp://' . $connector, $errno, $errstr);
71  if (!$this->socket) {
72  throw new \Exception(
73  "Could not create identd server (".$errstr.")"
74  );
75  }
76  }
77 
79  public function __destruct()
80  {
81  $this->disconnect();
82  }
83 
84  public function connect()
85  {
86  $this->bot->addConnection($this);
87  }
88 
89  public function disconnect($quitMessage = null)
90  {
91  $this->bot->removeConnection($this);
92  if ($this->socket !== null) {
93  stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
94  }
95  $this->socket = null;
96  }
97 
98  public function isConnected()
99  {
100  return true;
101  }
102 
103  public function getSocket()
104  {
105  return $this->socket;
106  }
107 
108  public function emptyReadQueue()
109  {
110  return true;
111  }
112 
113  public function read()
114  {
115  $socket = stream_socket_accept($this->socket);
116  if (!$socket) {
117  return false;
118  }
119  $worker = new $this->workerCls($this->bot, $socket);
120  return $worker;
121  }
122 
124  public function process()
125  {
126  }
127 
128  public function getBot()
129  {
130  return $this->bot;
131  }
132 
133  public function getConfig($chan)
134  {
135  return null;
136  }
137 
138  public function getIO()
139  {
140  return null;
141  }
142 }
$bot
A bot object implementing the Erebot::Interfaces::Core interface.
Definition: Server.php:33
Provides core functionalities for Erebot.
Definition: Core.php:59
$workerCls
Class to use to process IdentD requests.
Definition: Server.php:39
$socket
The underlying socket, represented as a stream.
Definition: Server.php:36
__destruct()
Destructor.
Definition: Server.php:79
process()
Processes commands queued in the input buffer.
Definition: Server.php:124
A server compatible with the Identification Protocol (RFC 1413).
Definition: Server.php:30
__construct(\Erebot\Interfaces\Core $bot, $connector= '0.0.0.0:113', $workerCls= '\\Erebot\\Identd\\Worker')
Definition: Server.php:63