]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/IrcPlugin.php
Merge in Phergie changes
[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 $nickservidentifyregexp = null;
59     public $nickservpassword = null;
60     public $channels = null;
61     public $transporttype = null;
62     public $encoding = null;
63
64     public $regcheck = null;
65     public $unregregexp = null;
66     public $regregexp = null;
67
68     public $transport = 'irc';
69     public $fake_irc;
70
71     /**
72      * Get the internationalized/translated display name of this IM service
73      *
74      * @return string Name of service
75      */
76     public function getDisplayName() {
77         return _m('IRC');
78     }
79
80     /**
81      * Normalize a screenname for comparison
82      *
83      * @param string $screenname screenname to normalize
84      * @return string an equivalent screenname in normalized form
85      */
86     public function normalize($screenname) {
87         $screenname = str_replace(" ","", $screenname);
88         return strtolower($screenname);
89     }
90
91     /**
92      * Get the screenname of the daemon that sends and receives messages
93      *
94      * @return string Screenname
95      */
96     public function daemon_screenname() {
97         return $this->nick;
98     }
99
100     /**
101      * Validate (ensure the validity of) a screenname
102      *
103      * @param string $screenname screenname to validate
104      * @return boolean
105      */
106     public function validate($screenname) {
107         if (preg_match('/\A[a-z0-9\-_]{1,1000}\z/i', $screenname)) {
108             return true;
109         } else {
110             return false;
111         }
112     }
113
114     /**
115      * Load related modules when needed
116      *
117      * @param string $cls Name of the class to be loaded
118      * @return boolean hook value; true means continue processing, false means stop.
119      */
120     public function onAutoload($cls) {
121         $dir = dirname(__FILE__);
122
123         switch ($cls) {
124             case 'IrcManager':
125                 include_once $dir . '/'.strtolower($cls).'.php';
126                 return false;
127             case 'Fake_Irc':
128                 include_once $dir . '/'. $cls .'.php';
129                 return false;
130             default:
131                 if (substr($cls, 0, 7) == 'Phergie') {
132                     include_once str_replace('_', DIRECTORY_SEPARATOR, $cls) . '.php';
133                     return false;
134                 }
135                 return true;
136         }
137     }
138
139     /*
140      * Start manager on daemon start
141      *
142      * @return boolean
143      */
144     public function onStartImDaemonIoManagers(&$classes) {
145         parent::onStartImDaemonIoManagers(&$classes);
146         $classes[] = new IrcManager($this); // handles sending/receiving
147         return true;
148     }
149
150     /**
151     * Get a microid URI for the given screenname
152     *
153     * @param string $screenname
154     * @return string microid URI
155     */
156     public function microiduri($screenname) {
157         return 'irc:' . $screenname;
158     }
159
160     /**
161      * Send a message to a given screenname
162      *
163      * @param string $screenname Screenname to send to
164      * @param string $body Text to send
165      * @return boolean success value
166      */
167     public function send_message($screenname, $body) {
168         $lines = explode("\n", $body);
169         foreach ($lines as $line) {
170             $this->fake_irc->doPrivmsg($screenname, $line);
171             $this->enqueue_outgoing_raw(array('type' => 'message', 'data' => $this->fake_irc->would_be_sent));
172         }
173         return true;
174     }
175
176     /**
177      * Accept a queued input message.
178      *
179      * @return true if processing completed, false if message should be reprocessed
180      */
181     public function receive_raw_message($data) {
182         $this->handle_incoming($data['sender'], $data['message']);
183         return true;
184     }
185
186     /**
187      * Send a confirmation code to a user
188      *
189      * @param string $screenname screenname sending to
190      * @param string $code the confirmation code
191      * @param User $user user sending to
192      * @return boolean success value
193      */
194     public function send_confirmation_code($screenname, $code, $user, $checked = false) {
195         $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
196           'If that\'s true, you can confirm by clicking on this URL: ' .
197           '%s' .
198           ' . (If you cannot click it, copy-and-paste it into the ' .
199           'address bar of your browser). If that user isn\'t you, ' .
200           'or if you didn\'t request this confirmation, just ignore this message.'),
201           $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code)));
202
203         if ($this->regcheck && !$checked) {
204             return $this->checked_send_confirmation_code($screenname, $code, $user);
205         } else {
206             return $this->send_message($screenname, $body);
207         }
208     }
209
210     /**
211     * Only sends the confirmation message if the nick is
212     * registered
213     *
214     * @param string $screenname screenname sending to
215     * @param string $code the confirmation code
216     * @param User $user user sending to
217     * @return boolean success value
218     */
219     public function checked_send_confirmation_code($screenname, $code, $user) {
220         $this->fake_irc->doPrivmsg('NickServ', 'INFO '.$screenname);
221         $this->enqueue_outgoing_raw(
222             array(
223                 'type' => 'nickcheck',
224                 'data' => $this->fake_irc->would_be_sent,
225                 'nickdata' =>
226                     array(
227                         'screenname' => $screenname,
228                         'code' => $code,
229                         'user' => $user
230                     )
231             )
232         );
233         return true;
234     }
235
236     /**
237     * Initialize plugin
238     *
239     * @return boolean
240     */
241     public function initialize() {
242         if (!isset($this->host)) {
243             throw new Exception('must specify a host');
244         }
245         if (!isset($this->username)) {
246             throw new Exception('must specify a username');
247         }
248         if (!isset($this->realname)) {
249             throw new Exception('must specify a "real name"');
250         }
251         if (!isset($this->nick)) {
252             throw new Exception('must specify a nickname');
253         }
254
255         if (!isset($this->port)) {
256             $this->port = 6667;
257         }
258         if (!isset($this->transporttype)) {
259             $this->transporttype = 'tcp';
260         }
261         if (!isset($this->encoding)) {
262             $this->encoding = 'UTF-8';
263         }
264
265         if (!isset($this->regcheck)) {
266             $this->regcheck = true;
267         }
268
269         $this->fake_irc = new Fake_Irc;
270         return true;
271     }
272
273     /**
274      * Get plugin information
275      *
276      * @param array $versions array to insert information into
277      * @return void
278      */
279     public function onPluginVersion(&$versions) {
280         $versions[] = array('name' => 'IRC',
281                             'version' => STATUSNET_VERSION,
282                             'author' => 'Luke Fitzgerald',
283                             'homepage' => 'http://status.net/wiki/Plugin:IRC',
284                             'rawdescription' =>
285                             _m('The IRC plugin allows users to send and receive notices over an IRC network.'));
286         return true;
287     }
288 }