]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/IrcPlugin.php
Cosmetic: Rearrange code
[quix0rs-gnu-social.git] / plugins / Irc / IrcPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Send and receive notices using an IRC network
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  IM
24  * @package   StatusNet
25  * @author    Luke Fitzgerald <lw.fitzgerald@googlemail.com>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 // We bundle the Phergie library...
38 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phergie');
39
40 /**
41  * Plugin for IRC
42  *
43  * @category  Plugin
44  * @package   StatusNet
45  * @author    Luke Fitzgerald <lw.fitzgerald@googlemail.com>
46  * @copyright 2010 StatusNet, Inc.
47  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48  * @link      http://status.net/
49  */
50
51 class IrcPlugin extends ImPlugin {
52     public $host =  null;
53     public $port = null;
54     public $username = null;
55     public $realname = null;
56     public $nick = null;
57     public $password = null;
58     public $nickservpassword = null;
59     public $channels = null;
60     public $transporttype = null;
61     public $encoding = null;
62
63     public $regcheck = null;
64
65     public $transport = 'irc';
66     public $fake_irc;
67
68     /**
69      * Get the internationalized/translated display name of this IM service
70      *
71      * @return string Name of service
72      */
73     public function getDisplayName() {
74         return _m('IRC');
75     }
76
77     /**
78      * Normalize a screenname for comparison
79      *
80      * @param string $screenname screenname to normalize
81      * @return string an equivalent screenname in normalized form
82      */
83     public function normalize($screenname) {
84         $screenname = str_replace(" ","", $screenname);
85         return strtolower($screenname);
86     }
87
88     /**
89      * Get the screenname of the daemon that sends and receives messages
90      *
91      * @return string Screenname
92      */
93     public function daemon_screenname() {
94         return $this->nick;
95     }
96
97     /**
98      * Validate (ensure the validity of) a screenname
99      *
100      * @param string $screenname screenname to validate
101      * @return boolean
102      */
103     public function validate($screenname) {
104         if (preg_match('/\A[a-z0-9\-_]{1,1000}\z/i', $screenname)) {
105             return true;
106         } else {
107             return false;
108         }
109     }
110
111     /**
112      * Load related modules when needed
113      *
114      * @param string $cls Name of the class to be loaded
115      * @return boolean hook value; true means continue processing, false means stop.
116      */
117     public function onAutoload($cls) {
118         $dir = dirname(__FILE__);
119
120         switch ($cls) {
121             case 'IrcManager':
122                 include_once $dir . '/'.strtolower($cls).'.php';
123                 return false;
124             case 'Fake_Irc':
125                 include_once $dir . '/'. $cls .'.php';
126                 return false;
127             default:
128                 if (substr($cls, 0, 7) == 'Phergie') {
129                     include_once str_replace('_', DIRECTORY_SEPARATOR, $cls) . '.php';
130                     return false;
131                 }
132                 return true;
133         }
134     }
135
136     /*
137      * Start manager on daemon start
138      *
139      * @return boolean
140      */
141     public function onStartImDaemonIoManagers(&$classes) {
142         parent::onStartImDaemonIoManagers(&$classes);
143         $classes[] = new IrcManager($this); // handles sending/receiving
144         return true;
145     }
146
147     /**
148     * Get a microid URI for the given screenname
149     *
150     * @param string $screenname
151     * @return string microid URI
152     */
153     public function microiduri($screenname) {
154         return 'irc:' . $screenname;
155     }
156
157     /**
158      * Send a message to a given screenname
159      *
160      * @param string $screenname Screenname to send to
161      * @param string $body Text to send
162      * @return boolean success value
163      */
164     public function send_message($screenname, $body) {
165         $this->fake_irc->doPrivmsg($screenname, $body);
166         $this->enqueue_outgoing_raw(array('type' => 'message', 'data' => $this->fake_irc->would_be_sent));
167         return true;
168     }
169
170     /**
171      * Accept a queued input message.
172      *
173      * @return true if processing completed, false if message should be reprocessed
174      */
175     public function receive_raw_message($data) {
176         $this->handle_incoming($data['sender'], $data['message']);
177         return true;
178     }
179
180     /**
181      * Send a confirmation code to a user
182      *
183      * @param string $screenname screenname sending to
184      * @param string $code the confirmation code
185      * @param User $user user sending to
186      * @return boolean success value
187      */
188     public function send_confirmation_code($screenname, $code, $user, $checked = false) {
189         $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
190           'If that\'s true, you can confirm by clicking on this URL: ' .
191           '%s' .
192           ' . (If you cannot click it, copy-and-paste it into the ' .
193           'address bar of your browser). If that user isn\'t you, ' .
194           'or if you didn\'t request this confirmation, just ignore this message.'),
195           $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code)));
196
197         if ($this->regcheck && !$checked) {
198             return $this->checked_send_confirmation_code($screenname, $code, $user);
199         } else {
200             return $this->send_message($screenname, $body);
201         }
202     }
203
204     /**
205     * Only sends the confirmation message if the nick is
206     * registered
207     *
208     * @param string $screenname screenname sending to
209     * @param string $code the confirmation code
210     * @param User $user user sending to
211     * @return boolean success value
212     */
213     public function checked_send_confirmation_code($screenname, $code, $user) {
214         $this->fake_irc->doPrivmsg('NickServ', 'INFO '.$screenname);
215         $this->enqueue_outgoing_raw(
216             array(
217                 'type' => 'nickcheck',
218                 'data' => $this->fake_irc->would_be_sent,
219                 'nickdata' =>
220                     array(
221                         'screenname' => $screenname,
222                         'code' => $code,
223                         'user' => $user
224                     )
225             )
226         );
227         return true;
228     }
229
230     /**
231     * Initialize plugin
232     *
233     * @return boolean
234     */
235     public function initialize() {
236         if (!isset($this->host)) {
237             throw new Exception('must specify a host');
238         }
239         if (!isset($this->username)) {
240             throw new Exception('must specify a username');
241         }
242         if (!isset($this->realname)) {
243             throw new Exception('must specify a "real name"');
244         }
245         if (!isset($this->nick)) {
246             throw new Exception('must specify a nickname');
247         }
248
249         if (!isset($this->port)) {
250             $this->port = 6667;
251         }
252         if (!isset($this->password)) {
253             $this->password = '';
254         }
255         if (!isset($this->transporttype)) {
256             $this->transporttype = 'tcp';
257         }
258         if (!isset($this->encoding)) {
259             $this->encoding = 'UTF-8';
260         }
261         if (!isset($this->nickservpassword)) {
262             $this->nickservpassword = '';
263         }
264         if (!isset($this->channels)) {
265             $this->channels = array();
266         }
267
268         if (!isset($this->regcheck)) {
269             $this->regcheck = true;
270         }
271
272         $this->fake_irc = new Fake_Irc;
273         return true;
274     }
275
276     /**
277      * Get plugin information
278      *
279      * @param array $versions array to insert information into
280      * @return void
281      */
282     public function onPluginVersion(&$versions) {
283         $versions[] = array('name' => 'IRC',
284                             'version' => STATUSNET_VERSION,
285                             'author' => 'Luke Fitzgerald',
286                             'homepage' => 'http://status.net/wiki/Plugin:IRC',
287                             'rawdescription' =>
288                             _m('The IRC plugin allows users to send and receive notices over an IRC network.'));
289         return true;
290     }
291 }