]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/implugin.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / lib / implugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Superclass for plugins that do instant messaging
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('STATUSNET') && !defined('LACONICA')) {
30     exit(1);
31 }
32
33 /**
34  * Superclass for plugins that do authentication
35  *
36  * Implementations will likely want to override onStartIoManagerClasses() so that their
37  *   IO manager is used
38  *
39  * @category Plugin
40  * @package  StatusNet
41  * @author   Craig Andrews <candrews@integralblue.com>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 abstract class ImPlugin extends Plugin
47 {
48     //name of this IM transport
49     public $transport = null;
50     //list of screennames that should get all public notices
51     public $public = array();
52
53     /**
54      * normalize a screenname for comparison
55      *
56      * @param string $screenname screenname to normalize
57      *
58      * @return string an equivalent screenname in normalized form
59      */
60     abstract function normalize($screenname);
61
62
63     /**
64      * validate (ensure the validity of) a screenname
65      *
66      * @param string $screenname screenname to validate
67      *
68      * @return boolean
69      */
70     abstract function validate($screenname);
71
72     /**
73      * get the internationalized/translated display name of this IM service
74      *
75      * @return string
76      */
77     abstract function getDisplayName();
78
79     /**
80      * send a single notice to a given screenname
81      * The implementation should put raw data, ready to send, into the outgoing
82      *   queue using enqueue_outgoing_raw()
83      *
84      * @param string $screenname screenname to send to
85      * @param Notice $notice notice to send
86      *
87      * @return boolean success value
88      */
89     function send_notice($screenname, $notice)
90     {
91         return $this->send_message($screenname, $this->format_notice($notice));
92     }
93
94     /**
95      * send a message (text) to a given screenname
96      * The implementation should put raw data, ready to send, into the outgoing
97      *   queue using enqueue_outgoing_raw()
98      *
99      * @param string $screenname screenname to send to
100      * @param Notice $body text to send
101      *
102      * @return boolean success value
103      */
104     abstract function send_message($screenname, $body);
105
106     /**
107      * receive a raw message
108      * Raw IM data is taken from the incoming queue, and passed to this function.
109      * It should parse the raw message and call handle_incoming()
110      *
111      * @param object $data raw IM data
112      *
113      * @return boolean success value
114      */
115     abstract function receive_raw_message($data);
116
117     /**
118      * get the screenname of the daemon that sends and receives message for this service
119      *
120      * @return string screenname of this plugin
121      */
122     abstract function daemon_screenname();
123
124     /**
125      * get the microid uri of a given screenname
126      *
127      * @param string $screenname screenname
128      *
129      * @return string microid uri
130      */
131     function microiduri($screenname)
132     {
133         return $this->transport . ':' . $screenname;    
134     }
135     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - MISC ========================\
136
137     /**
138      * Put raw message data (ready to send) into the outgoing queue
139      *
140      * @param object $data
141      */
142     function enqueue_outgoing_raw($data)
143     {
144         $qm = QueueManager::get();
145         $qm->enqueue($data, $this->transport . '-out');
146     }
147
148     /**
149      * Put raw message data (received, ready to be processed) into the incoming queue
150      *
151      * @param object $data
152      */
153     function enqueue_incoming_raw($data)
154     {
155         $qm = QueueManager::get();
156         $qm->enqueue($data, $this->transport . '-in');
157     }
158
159     /**
160      * given a screenname, get the corresponding user
161      *
162      * @param string $screenname
163      *
164      * @return User user
165      */
166     function get_user($screenname)
167     {
168         $user_im_prefs = $this->get_user_im_prefs_from_screenname($screenname);
169         if($user_im_prefs){
170             $user = User::staticGet('id', $user_im_prefs->user_id);
171             $user_im_prefs->free();
172             return $user;
173         }else{
174             return false;
175         }
176     }
177
178
179     /**
180      * given a screenname, get the User_im_prefs object for this transport
181      *
182      * @param string $screenname
183      *
184      * @return User_im_prefs user_im_prefs
185      */
186     function get_user_im_prefs_from_screenname($screenname)
187     {
188         if($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $this->transport, 'screenname' => $screenname) )){
189             return $user_im_prefs;
190         }else{
191             return false;
192         }
193     }
194
195
196     /**
197      * given a User, get their screenname
198      *
199      * @param User $user
200      *
201      * @return string screenname of that user
202      */
203     function get_screenname($user)
204     {
205         $user_im_prefs = $this->get_user_im_prefs_from_user($user);
206         if($user_im_prefs){
207             return $user_im_prefs->screenname;
208         }else{
209             return false;
210         }
211     }
212
213
214     /**
215      * given a User, get their User_im_prefs
216      *
217      * @param User $user
218      *
219      * @return User_im_prefs user_im_prefs of that user
220      */
221     function get_user_im_prefs_from_user($user)
222     {
223         if($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $this->transport, 'user_id' => $user->id) )){
224             return $user_im_prefs;
225         }else{
226             return false;
227         }
228     }
229     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - SENDING ========================\
230     /**
231      * Send a message to a given screenname from the site
232      *
233      * @param string $screenname screenname to send the message to
234      * @param string $msg message contents to send
235      *
236      * @param boolean success
237      */
238     protected function send_from_site($screenname, $msg)
239     {
240         $text = '['.common_config('site', 'name') . '] ' . $msg;
241         $this->send_message($screenname, $text);
242     }
243
244     /**
245      * send a confirmation code to a user
246      *
247      * @param string $screenname screenname sending to
248      * @param string $code the confirmation code
249      * @param User $user user sending to
250      *
251      * @return boolean success value
252      */
253     function send_confirmation_code($screenname, $code, $user)
254     {
255         $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
256           'If that\'s true, you can confirm by clicking on this URL: ' .
257           '%s' .
258           ' . (If you cannot click it, copy-and-paste it into the ' .
259           'address bar of your browser). If that user isn\'t you, ' .
260           'or if you didn\'t request this confirmation, just ignore this message.'),
261           $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code)));
262
263         return $this->send_message($screenname, $body);
264     }
265
266     /**
267      * send a notice to all public listeners
268      *
269      * For notices that are generated on the local system (by users), we can optionally
270      * forward them to remote listeners by XMPP.
271      *
272      * @param Notice $notice notice to broadcast
273      *
274      * @return boolean success flag
275      */
276
277     function public_notice($notice)
278     {
279         // Now, users who want everything
280
281         // FIXME PRIV don't send out private messages here
282         // XXX: should we send out non-local messages if public,localonly
283         // = false? I think not
284
285         foreach ($this->public as $screenname) {
286             common_log(LOG_INFO,
287                        'Sending notice ' . $notice->id .
288                        ' to public listener ' . $screenname,
289                        __FILE__);
290             $this->send_notice($screenname, $notice);
291         }
292
293         return true;
294     }
295
296     /**
297      * broadcast a notice to all subscribers and reply recipients
298      *
299      * This function will send a notice to all subscribers on the local server
300      * who have IM addresses, and have IM notification enabled, and
301      * have this subscription enabled for IM. It also sends the notice to
302      * all recipients of @-replies who have IM addresses and IM notification
303      * enabled. This is really the heart of IM distribution in StatusNet.
304      *
305      * @param Notice $notice The notice to broadcast
306      *
307      * @return boolean success flag
308      */
309
310     function broadcast_notice($notice)
311     {
312
313         $ni = $notice->whoGets();
314
315         foreach ($ni as $user_id => $reason) {
316             $user = User::staticGet($user_id);
317             if (empty($user)) {
318                 // either not a local user, or just not found
319                 continue;
320             }
321             $user_im_prefs = $this->get_user_im_prefs_from_user($user);
322             if(!$user_im_prefs || !$user_im_prefs->notify){
323                 continue;
324             }
325
326             switch ($reason) {
327             case NOTICE_INBOX_SOURCE_REPLY:
328                 if (!$user_im_prefs->replies) {
329                     continue 2;
330                 }
331                 break;
332             case NOTICE_INBOX_SOURCE_SUB:
333                 $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
334                                                    'subscribed' => $notice->profile_id));
335                 if (empty($sub) || !$sub->jabber) {
336                     continue 2;
337                 }
338                 break;
339             case NOTICE_INBOX_SOURCE_GROUP:
340                 break;
341             default:
342                 throw new Exception(sprintf(_("Unknown inbox source %d."), $reason));
343             }
344
345             common_log(LOG_INFO,
346                        'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname,
347                        __FILE__);
348             $this->send_notice($user_im_prefs->screenname, $notice);
349             $user_im_prefs->free();
350         }
351
352         return true;
353     }
354
355     /**
356      * makes a plain-text formatted version of a notice, suitable for IM distribution
357      *
358      * @param Notice  $notice  notice being sent
359      *
360      * @return string plain-text version of the notice, with user nickname prefixed
361      */
362
363     function format_notice($notice)
364     {
365         $profile = $notice->getProfile();
366         return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']';
367     }
368     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - RECEIVING ========================\
369
370     /**
371      * Attempt to handle a message as a command
372      * @param User $user user the message is from
373      * @param string $body message text
374      * @return boolean true if the message was a command and was executed, false if it was not a command
375      */
376     protected function handle_command($user, $body)
377     {
378         $inter = new CommandInterpreter();
379         $cmd = $inter->handle_command($user, $body);
380         if ($cmd) {
381             $chan = new IMChannel($this);
382             $cmd->execute($chan);
383             return true;
384         } else {
385             return false;
386         }
387     }
388
389     /**
390      * Is some text an autoreply message?
391      * @param string $txt message text
392      * @return boolean true if autoreply
393      */
394     protected function is_autoreply($txt)
395     {
396         if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
397             return true;
398         } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
399             return true;
400         } else {
401             return false;
402         }
403     }
404
405     /**
406      * Is some text an OTR message?
407      * @param string $txt message text
408      * @return boolean true if OTR
409      */
410     protected function is_otr($txt)
411     {
412         if (preg_match('/^\?OTR/', $txt)) {
413             return true;
414         } else {
415             return false;
416         }
417     }
418
419     /**
420      * Helper for handling incoming messages
421      * Your incoming message handler will probably want to call this function
422      *
423      * @param string $from screenname the message was sent from
424      * @param string $message message contents
425      *
426      * @param boolean success
427      */
428     protected function handle_incoming($from, $notice_text)
429     {
430         $user = $this->get_user($from);
431         // For common_current_user to work
432         global $_cur;
433         $_cur = $user;
434
435         if (!$user) {
436             $this->send_from_site($from, 'Unknown user; go to ' .
437                              common_local_url('imsettings') .
438                              ' to add your address to your account');
439             common_log(LOG_WARNING, 'Message from unknown user ' . $from);
440             return;
441         }
442         if ($this->handle_command($user, $notice_text)) {
443             common_log(LOG_INFO, "Command message by $from handled.");
444             return;
445         } else if ($this->is_autoreply($notice_text)) {
446             common_log(LOG_INFO, 'Ignoring auto reply from ' . $from);
447             return;
448         } else if ($this->is_otr($notice_text)) {
449             common_log(LOG_INFO, 'Ignoring OTR from ' . $from);
450             return;
451         } else {
452
453             common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
454
455             $this->add_notice($from, $user, $notice_text);
456         }
457
458         $user->free();
459         unset($user);
460         unset($_cur);
461         unset($message);
462     }
463
464     /**
465      * Helper for handling incoming messages
466      * Your incoming message handler will probably want to call this function
467      *
468      * @param string $from screenname the message was sent from
469      * @param string $message message contents
470      *
471      * @param boolean success
472      */
473     protected function add_notice($screenname, $user, $body)
474     {
475         $body = trim(strip_tags($body));
476         $content_shortened = common_shorten_links($body);
477         if (Notice::contentTooLong($content_shortened)) {
478           $this->send_from_site($screenname, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d.'),
479                                           Notice::maxContent(),
480                                           mb_strlen($content_shortened)));
481           return;
482         }
483
484         try {
485             $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
486         } catch (Exception $e) {
487             common_log(LOG_ERR, $e->getMessage());
488             $this->send_from_site($from, $e->getMessage());
489             return;
490         }
491
492         common_broadcast_notice($notice);
493         common_log(LOG_INFO,
494                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
495         $notice->free();
496         unset($notice);
497     }
498
499     //========================EVENT HANDLERS========================\
500     
501     /**
502      * Register notice queue handler
503      *
504      * @param QueueManager $manager
505      *
506      * @return boolean hook return
507      */
508     function onEndInitializeQueueManager($manager)
509     {
510         $manager->connect($this->transport . '-in', new ImReceiverQueueHandler($this), 'im');
511         $manager->connect($this->transport, new ImQueueHandler($this));
512         $manager->connect($this->transport . '-out', new ImSenderQueueHandler($this), 'im');
513         return true;
514     }
515
516     function onStartImDaemonIoManagers(&$classes)
517     {
518         //$classes[] = new ImManager($this); // handles sending/receiving/pings/reconnects
519         return true;
520     }
521
522     function onStartEnqueueNotice($notice, &$transports)
523     {
524         $profile = Profile::staticGet($notice->profile_id);
525
526         if (!$profile) {
527             common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
528                        'unknown profile ' . common_log_objstring($notice),
529                        __FILE__);
530         }else{
531             $transports[] = $this->transport;
532         }
533
534         return true;
535     }
536
537     function onEndShowHeadElements($action)
538     {
539         $aname = $action->trimmed('action');
540
541         if ($aname == 'shownotice') {
542
543             $user_im_prefs = new User_im_prefs();
544             $user_im_prefs->user_id = $action->profile->id;
545             $user_im_prefs->transport = $this->transport;
546
547             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) {
548                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
549                                   $action->notice->uri);
550                 $action->element('meta', array('name' => 'microid',
551                                              'content' => $id->toString()));
552             }
553
554         } else if ($aname == 'showstream') {
555
556             $user_im_prefs = new User_im_prefs();
557             $user_im_prefs->user_id = $action->user->id;
558             $user_im_prefs->transport = $this->transport;
559
560             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->profile->profileurl) {
561                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
562                                   $action->selfUrl());
563                 $action->element('meta', array('name' => 'microid',
564                                                'content' => $id->toString()));
565             }
566         }
567     }
568
569     function onNormalizeImScreenname($transport, &$screenname)
570     {
571         if($transport == $this->transport)
572         {
573             $screenname = $this->normalize($screenname);
574             return false;
575         }
576     }
577
578     function onValidateImScreenname($transport, $screenname, &$valid)
579     {
580         if($transport == $this->transport)
581         {
582             $valid = $this->validate($screenname);
583             return false;
584         }
585     }
586
587     function onGetImTransports(&$transports)
588     {
589         $transports[$this->transport] = array(
590             'display' => $this->getDisplayName(),
591             'daemon_screenname' => $this->daemon_screenname());
592     }
593
594     function onSendImConfirmationCode($transport, $screenname, $code, $user)
595     {
596         if($transport == $this->transport)
597         {
598             $this->send_confirmation_code($screenname, $code, $user);
599             return false;
600         }
601     }
602
603     function onUserDeleteRelated($user, &$tables)
604     {
605         $tables[] = 'User_im_prefs';
606         return true;
607     }
608
609     function initialize()
610     {
611         if(is_null($this->transport)){
612             throw new Exception('transport cannot be null');
613         }
614     }
615 }