]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Connection.php
Added Phergie PHP IRC library
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Connection.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie 
15  * @package   Phergie
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
20  */
21
22 /**
23  * Data structure for connection metadata.
24  *
25  * @category Phergie 
26  * @package  Phergie
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
30  */
31 class Phergie_Connection
32 {
33     /**
34      * Host to which the client will connect
35      *
36      * @var string
37      */
38     protected $host;
39
40     /**
41      * Port on which the client will connect, defaults to the standard IRC 
42      * port
43      *
44      * @var int
45      */
46     protected $port;
47
48     /**
49      * Transport for the connection, defaults to tcp but can be set to ssl 
50      * or variations thereof to connect over SSL
51      *
52      * @var string 
53      */
54     protected $transport;
55
56     /**
57      * Nick that the client will use
58      *
59      * @var string
60      */
61     protected $nick;
62
63     /**
64      * Username that the client will use
65      *
66      * @var string
67      */
68     protected $username;
69
70     /**
71      * Realname that the client will use
72      *
73      * @var string
74      */
75     protected $realname;
76
77     /**
78      * Password that the client will use
79      *
80      * @var string
81      */
82     protected $password;
83
84     /**
85      * Hostmask for the connection
86      *
87      * @var Phergie_Hostmask
88      */
89     protected $hostmask;
90
91     /**
92      * Constructor to initialize instance properties.
93      *
94      * @param array $options Optional associative array of property values 
95      *        to initialize
96      *
97      * @return void
98      */
99     public function __construct(array $options = array())
100     {
101         $this->transport = 'tcp';
102
103         $this->setOptions($options);
104     }
105
106     /**
107      * Emits an error related to a required connection setting does not have
108      * value set for it.
109      *
110      * @param string $setting Name of the setting
111      *
112      * @return void
113      */
114     protected function checkSetting($setting)
115     {
116         if (empty($this->$setting)) {
117             throw new Phergie_Connection_Exception(
118                 'Required connection setting "' . $setting . '" missing',
119                 Phergie_Connection_Exception::ERR_REQUIRED_SETTING_MISSING
120             );
121         }
122     }
123  
124     /**
125      * Returns a hostmask that uniquely identifies the connection.
126      *
127      * @return string
128      */
129     public function getHostmask()
130     {
131         if (empty($this->hostmask)) {
132             $this->hostmask = new Phergie_Hostmask(
133                 $this->nick,
134                 $this->username,
135                 $this->host
136             );
137         }
138
139         return $this->hostmask; 
140     }
141
142     /**
143      * Sets the host to which the client will connect.
144      *
145      * @param string $host Hostname
146      *
147      * @return Phergie_Connection Provides a fluent interface 
148      */
149     public function setHost($host)
150     {
151         if (empty($this->host)) {
152             $this->host = (string) $host;
153         }
154
155         return $this;
156     }
157    
158     /**
159      * Returns the host to which the client will connect if it is set or 
160      * emits an error if it is not set.
161      *
162      * @return string
163      */
164     public function getHost()
165     {
166         $this->checkSetting('host');
167
168         return $this->host;
169     }
170
171     /**
172      * Sets the port on which the client will connect.
173      *
174      * @param int $port Port
175      *
176      * @return Phergie_Connection Provides a fluent interface 
177      */
178     public function setPort($port)
179     {
180         if (empty($this->port)) {
181             $this->port = (int) $port;
182         }
183
184         return $this;
185     }
186
187     /**
188      * Returns the port on which the client will connect.
189      *
190      * @return int
191      */
192     public function getPort()
193     {
194         if (empty($this->port)) {
195             $this->port = 6667;
196         }
197
198         return $this->port;
199     }
200
201     /**
202      * Sets the transport for the connection to use. 
203      *
204      * @param string $transport Transport (ex: tcp, ssl, etc.) 
205      *
206      * @return Phergie_Connection Provides a fluent interface
207      */
208     public function setTransport($transport)
209     {
210         $this->transport = (string) $transport;
211
212         if (!in_array($this->transport, stream_get_transports())) {
213             throw new Phergie_Connection_Exception(
214                 'Transport ' . $this->transport . ' is not supported',
215                 Phergie_Connection_Exception::TRANSPORT_NOT_SUPPORTED
216             );
217         }
218
219         return $this;
220     }
221
222     /**
223      * Returns the transport in use by the connection. 
224      *
225      * @return string Transport (ex: tcp, ssl, etc.) 
226      */
227     public function getTransport()
228     {
229         return $this->transport;
230     }
231
232     /**
233      * Sets the nick that the client will use.
234      *
235      * @param string $nick Nickname
236      *
237      * @return Phergie_Connection Provides a fluent interface 
238      */
239     public function setNick($nick)
240     {
241         if (empty($this->nick)) {
242             $this->nick = (string) $nick;
243         }
244
245         return $this;
246     }
247
248     /**
249      * Returns the nick that the client will use.
250      *
251      * @return string
252      */
253     public function getNick()
254     {
255         $this->checkSetting('nick');
256
257         return $this->nick;
258     }
259
260     /**
261      * Sets the username that the client will use.
262      *
263      * @param string $username Username
264      *
265      * @return Phergie_Connection Provides a fluent interface 
266      */
267     public function setUsername($username)
268     {
269         if (empty($this->username)) {
270             $this->username = (string) $username;
271         }
272
273         return $this;
274     }
275
276     /**
277      * Returns the username that the client will use.
278      *
279      * @return string
280      */
281     public function getUsername()
282     {
283         $this->checkSetting('username');
284
285         return $this->username;
286     }
287
288     /**
289      * Sets the realname that the client will use.
290      *
291      * @param string $realname Real name
292      *
293      * @return Phergie_Connection Provides a fluent interface 
294      */
295     public function setRealname($realname)
296     {
297         if (empty($this->realname)) {
298             $this->realname = (string) $realname;
299         }
300
301         return $this;
302     }
303
304     /**
305      * Returns the realname that the client will use.
306      *
307      * @return string
308      */
309     public function getRealname()
310     {
311         $this->checkSetting('realname');
312
313         return $this->realname;
314     }
315
316     /**
317      * Sets the password that the client will use.
318      *
319      * @param string $password Password
320      *
321      * @return Phergie_Connection Provides a fluent interface 
322      */
323     public function setPassword($password)
324     {
325         if (empty($this->password)) {
326             $this->password = (string) $password;
327         }
328
329         return $this;
330     }
331
332     /**
333      * Returns the password that the client will use.
334      *
335      * @return string
336      */
337     public function getPassword()
338     {
339         return $this->password;
340     }
341
342     /**
343      * Sets multiple connection settings using an array.
344      *
345      * @param array $options Associative array of setting names mapped to 
346      *        corresponding values
347      *
348      * @return Phergie_Connection Provides a fluent interface 
349      */
350     public function setOptions(array $options)
351     {
352         foreach ($options as $option => $value) {
353             $method = 'set' . ucfirst($option);
354             if (method_exists($this, $method)) {
355                 $this->$method($value);
356             }
357         }
358     }
359 }