]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/implugin.php
Merge branch 'master' of https://git.gnu.io/gnu/gnu-social into social-master
[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 abstract class ImPlugin extends Plugin
46 {
47     //name of this IM transport
48     public $transport = null;
49     //list of screennames that should get all public notices
50     public $public = array();
51
52     protected $requires_cli = true;
53
54     /**
55      * normalize a screenname for comparison
56      *
57      * @param string $screenname screenname to normalize
58      *
59      * @return string an equivalent screenname in normalized form
60      */
61     abstract function normalize($screenname);
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 enqueueOutgoingRaw()
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 sendNotice($screenname, Notice $notice)
90     {
91         return $this->sendMessage($screenname, $this->formatNotice($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 enqueueOutgoingRaw()
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 sendMessage($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 handleIncoming()
110      *
111      * Returning false may CAUSE REPROCESSING OF THE QUEUE ITEM, and should
112      * be used for temporary failures only. For permanent failures such as
113      * unrecognized addresses, return true to indicate your processing has
114      * completed.
115      *
116      * @param object $data raw IM data
117      *
118      * @return boolean true if processing completed, false for temporary failures
119      */
120     abstract function receiveRawMessage($data);
121
122     /**
123      * get the screenname of the daemon that sends and receives message for this service
124      *
125      * @return string screenname of this plugin
126      */
127     abstract function daemonScreenname();
128
129     /**
130      * get the microid uri of a given screenname
131      *
132      * @param string $screenname screenname
133      *
134      * @return string microid uri
135      */
136     function microiduri($screenname)
137     {
138         return $this->transport . ':' . $screenname;
139     }
140     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - MISC ========================\
141
142     /**
143      * Put raw message data (ready to send) into the outgoing queue
144      *
145      * @param object $data
146      */
147     function enqueueOutgoingRaw($data)
148     {
149         $qm = QueueManager::get();
150         $qm->enqueue($data, $this->transport . '-out');
151     }
152
153     /**
154      * Put raw message data (received, ready to be processed) into the incoming queue
155      *
156      * @param object $data
157      */
158     function enqueueIncomingRaw($data)
159     {
160         $qm = QueueManager::get();
161         $qm->enqueue($data, $this->transport . '-in');
162     }
163
164     /**
165      * given a screenname, get the corresponding user
166      *
167      * @param string $screenname
168      *
169      * @return User user
170      */
171     function getUser($screenname)
172     {
173         $user_im_prefs = $this->getUserImPrefsFromScreenname($screenname);
174         if($user_im_prefs){
175             $user = User::getKV('id', $user_im_prefs->user_id);
176             $user_im_prefs->free();
177             return $user;
178         }else{
179             return false;
180         }
181     }
182
183     /**
184      * given a screenname, get the User_im_prefs object for this transport
185      *
186      * @param string $screenname
187      *
188      * @return User_im_prefs user_im_prefs
189      */
190     function getUserImPrefsFromScreenname($screenname)
191     {
192         $user_im_prefs = User_im_prefs::pkeyGet(
193             array('transport' => $this->transport,
194                   'screenname' => $this->normalize($screenname)));
195         if ($user_im_prefs) {
196             return $user_im_prefs;
197         } else {
198             return false;
199         }
200     }
201
202     /**
203      * given a User, get their screenname
204      *
205      * @param User $user
206      *
207      * @return string screenname of that user
208      */
209     function getScreenname($user)
210     {
211         $user_im_prefs = $this->getUserImPrefsFromUser($user);
212         if ($user_im_prefs) {
213             return $user_im_prefs->screenname;
214         } else {
215             return false;
216         }
217     }
218
219     /**
220      * given a User, get their User_im_prefs
221      *
222      * @param User $user
223      *
224      * @return User_im_prefs user_im_prefs of that user
225      */
226     function getUserImPrefsFromUser($user)
227     {
228         $user_im_prefs = User_im_prefs::pkeyGet(
229             array('transport' => $this->transport,
230                   'user_id' => $user->id));
231         if ($user_im_prefs){
232             return $user_im_prefs;
233         } else {
234             return false;
235         }
236     }
237     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - SENDING ========================\
238     /**
239      * Send a message to a given screenname from the site
240      *
241      * @param string $screenname screenname to send the message to
242      * @param string $msg message contents to send
243      *
244      * @param boolean success
245      */
246     protected function sendFromSite($screenname, $msg)
247     {
248         $text = '['.common_config('site', 'name') . '] ' . $msg;
249         $this->sendMessage($screenname, $text);
250     }
251
252     /**
253      * Send a confirmation code to a user
254      *
255      * @param string $screenname screenname sending to
256      * @param string $code the confirmation code
257      * @param User $user user sending to
258      *
259      * @return boolean success value
260      */
261     function sendConfirmationCode($screenname, $code, $user)
262     {
263         // TRANS: Body text for confirmation code e-mail.
264         // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename,
265         // TRANS: %3$s is the display name of an IM plugin.
266         $body = sprintf(_('User "%1$s" on %2$s has said that your %3$s screenname belongs to them. ' .
267           'If that is true, you can confirm by clicking on this URL: ' .
268           '%4$s' .
269           ' . (If you cannot click it, copy-and-paste it into the ' .
270           'address bar of your browser). If that user is not you, ' .
271           'or if you did not request this confirmation, just ignore this message.'),
272           $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', null, array('code' => $code)));
273
274         return $this->sendMessage($screenname, $body);
275     }
276
277     /**
278      * send a notice to all public listeners
279      *
280      * For notices that are generated on the local system (by users), we can optionally
281      * forward them to remote listeners by XMPP.
282      *
283      * @param Notice $notice notice to broadcast
284      *
285      * @return boolean success flag
286      */
287
288     function publicNotice($notice)
289     {
290         // Now, users who want everything
291
292         // FIXME PRIV don't send out private messages here
293         // XXX: should we send out non-local messages if public,localonly
294         // = false? I think not
295
296         foreach ($this->public as $screenname) {
297             common_log(LOG_INFO,
298                        'Sending notice ' . $notice->id .
299                        ' to public listener ' . $screenname,
300                        __FILE__);
301             $this->sendNotice($screenname, $notice);
302         }
303
304         return true;
305     }
306
307     /**
308      * broadcast a notice to all subscribers and reply recipients
309      *
310      * This function will send a notice to all subscribers on the local server
311      * who have IM addresses, and have IM notification enabled, and
312      * have this subscription enabled for IM. It also sends the notice to
313      * all recipients of @-replies who have IM addresses and IM notification
314      * enabled. This is really the heart of IM distribution in StatusNet.
315      *
316      * @param Notice $notice The notice to broadcast
317      *
318      * @return boolean success flag
319      */
320
321     function broadcastNotice($notice)
322     {
323         $ni = $notice->whoGets();
324
325         foreach ($ni as $user_id => $reason) {
326             $user = User::getKV($user_id);
327             if (empty($user)) {
328                 // either not a local user, or just not found
329                 continue;
330             }
331             $user_im_prefs = $this->getUserImPrefsFromUser($user);
332             if(!$user_im_prefs || !$user_im_prefs->notify){
333                 continue;
334             }
335
336             switch ($reason) {
337             case NOTICE_INBOX_SOURCE_REPLY:
338                 if (!$user_im_prefs->replies) {
339                     continue 2;
340                 }
341                 break;
342             case NOTICE_INBOX_SOURCE_SUB:
343                 $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
344                                                    'subscribed' => $notice->profile_id));
345                 if (empty($sub) || !$sub->jabber) {
346                     continue 2;
347                 }
348                 break;
349             case NOTICE_INBOX_SOURCE_GROUP:
350                 break;
351             default:
352                 // TRANS: Exception thrown when trying to deliver a notice to an unknown inbox.
353                 // TRANS: %d is the unknown inbox ID (number).
354                 throw new Exception(sprintf(_('Unknown inbox source %d.'), $reason));
355             }
356
357             common_log(LOG_INFO,
358                        'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname,
359                        __FILE__);
360             $this->sendNotice($user_im_prefs->screenname, $notice);
361             $user_im_prefs->free();
362         }
363
364         return true;
365     }
366
367     /**
368      * makes a plain-text formatted version of a notice, suitable for IM distribution
369      *
370      * @param Notice  $notice  notice being sent
371      *
372      * @return string plain-text version of the notice, with user nickname prefixed
373      */
374
375     protected function formatNotice(Notice $notice)
376     {
377         $profile = $notice->getProfile();
378
379         try {
380             $parent = $notice->getParent();
381             $orig_profile = $parent->getProfile();
382             $nicknames = sprintf('%1$s => %2$s', $profile->nickname, $orig_profile->nickname);
383         } catch (Exception $e) {
384             $nicknames = $profile->nickname;
385         }
386
387         return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id);
388     }
389     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - RECEIVING ========================\
390
391     /**
392      * Attempt to handle a message as a command
393      * @param User $user user the message is from
394      * @param string $body message text
395      * @return boolean true if the message was a command and was executed, false if it was not a command
396      */
397     protected function handleCommand($user, $body)
398     {
399         $inter = new CommandInterpreter();
400         $cmd = $inter->handle_command($user, $body);
401         if ($cmd) {
402             $chan = new IMChannel($this);
403             $cmd->execute($chan);
404             return true;
405         } else {
406             return false;
407         }
408     }
409
410     /**
411      * Is some text an autoreply message?
412      * @param string $txt message text
413      * @return boolean true if autoreply
414      */
415     protected function isAutoreply($txt)
416     {
417         if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
418             return true;
419         } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
420             return true;
421         } else {
422             return false;
423         }
424     }
425
426     /**
427      * Is some text an OTR message?
428      * @param string $txt message text
429      * @return boolean true if OTR
430      */
431     protected function isOtr($txt)
432     {
433         if (preg_match('/^\?OTR/', $txt)) {
434             return true;
435         } else {
436             return false;
437         }
438     }
439
440     /**
441      * Helper for handling incoming messages
442      * Your incoming message handler will probably want to call this function
443      *
444      * @param string $from screenname the message was sent from
445      * @param string $message message contents
446      *
447      * @param boolean success
448      */
449     protected function handleIncoming($from, $notice_text)
450     {
451         $user = $this->getUser($from);
452         // For common_current_user to work
453         global $_cur;
454         $_cur = $user;
455
456         if (!$user) {
457             $this->sendFromSite($from, 'Unknown user; go to ' .
458                              common_local_url('imsettings') .
459                              ' to add your address to your account');
460             common_log(LOG_WARNING, 'Message from unknown user ' . $from);
461             return;
462         }
463         if ($this->handleCommand($user, $notice_text)) {
464             common_log(LOG_INFO, "Command message by $from handled.");
465             return;
466         } else if ($this->isAutoreply($notice_text)) {
467             common_log(LOG_INFO, 'Ignoring auto reply from ' . $from);
468             return;
469         } else if ($this->isOtr($notice_text)) {
470             common_log(LOG_INFO, 'Ignoring OTR from ' . $from);
471             return;
472         } else {
473
474             common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
475
476             $this->addNotice($from, $user, $notice_text);
477         }
478
479         $user->free();
480         unset($user);
481         unset($_cur);
482         unset($message);
483     }
484
485     /**
486      * Helper for handling incoming messages
487      * Your incoming message handler will probably want to call this function
488      *
489      * @param string $from screenname the message was sent from
490      * @param string $message message contents
491      *
492      * @param boolean success
493      */
494     protected function addNotice($screenname, $user, $body)
495     {
496         $body = trim(strip_tags($body));
497         $content_shortened = common_shorten_links($body);
498         if (Notice::contentTooLong($content_shortened)) {
499           $this->sendFromSite($screenname,
500                               // TRANS: Message given when a status is too long. %1$s is the maximum number of characters,
501                               // TRANS: %2$s is the number of characters sent (used for plural).
502                               sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
503                                          'Message too long - maximum is %1$d characters, you sent %2$d.',
504                                          Notice::maxContent()),
505                                       Notice::maxContent(),
506                                       mb_strlen($content_shortened)));
507           return;
508         }
509
510         try {
511             $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
512         } catch (Exception $e) {
513             common_log(LOG_ERR, $e->getMessage());
514             $this->sendFromSite($from, $e->getMessage());
515             return;
516         }
517
518         common_log(LOG_INFO,
519                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
520         $notice->free();
521         unset($notice);
522     }
523
524     //========================EVENT HANDLERS========================\
525
526     /**
527      * Register notice queue handler
528      *
529      * @param QueueManager $manager
530      *
531      * @return boolean hook return
532      */
533     function onEndInitializeQueueManager(QueueManager $manager)
534     {
535         // If we don't require CLI mode, or if we do and GNUSOCIAL_CLI _is_ set, then connect the transports
536         // This check is made mostly because some IM plugins can't deliver to transports unless they
537         // have continously running daemons (such as XMPP) and we can't have that over HTTP requests.
538         if (!$this->requires_cli || defined('GNUSOCIAL_CLI')) {
539             $manager->connect($this->transport . '-in', new ImReceiverQueueHandler($this), 'im');
540             $manager->connect($this->transport, new ImQueueHandler($this));
541             $manager->connect($this->transport . '-out', new ImSenderQueueHandler($this), 'im');
542         }
543         return true;
544     }
545
546     function onStartImDaemonIoManagers(&$classes)
547     {
548         //$classes[] = new ImManager($this); // handles sending/receiving/pings/reconnects
549         return true;
550     }
551
552     function onStartEnqueueNotice(Notice $notice, array &$transports)
553     {
554         $profile = Profile::getKV($notice->profile_id);
555
556         if (!$profile) {
557             common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
558                        'unknown profile ' . common_log_objstring($notice),
559                        __FILE__);
560         }else{
561             $transports[] = $this->transport;
562         }
563
564         return true;
565     }
566
567     function onEndShowHeadElements($action)
568     {
569         $aname = $action->trimmed('action');
570
571         if ($aname == 'shownotice') {
572
573             $user_im_prefs = new User_im_prefs();
574             $user_im_prefs->user_id = $action->profile->id;
575             $user_im_prefs->transport = $this->transport;
576
577             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) {
578                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
579                                   $action->notice->uri);
580                 $action->element('meta', array('name' => 'microid',
581                                              'content' => $id->toString()));
582             }
583
584         } else if ($aname == 'showstream') {
585
586             $user_im_prefs = new User_im_prefs();
587             $user_im_prefs->user_id = $action->user->id;
588             $user_im_prefs->transport = $this->transport;
589
590             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->profile->profileurl) {
591                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
592                                   $action->selfUrl());
593                 $action->element('meta', array('name' => 'microid',
594                                                'content' => $id->toString()));
595             }
596         }
597     }
598
599     function onNormalizeImScreenname($transport, &$screenname)
600     {
601         if($transport == $this->transport)
602         {
603             $screenname = $this->normalize($screenname);
604             return false;
605         }
606     }
607
608     function onValidateImScreenname($transport, $screenname, &$valid)
609     {
610         if($transport == $this->transport)
611         {
612             $valid = $this->validate($screenname);
613             return false;
614         }
615     }
616
617     function onGetImTransports(&$transports)
618     {
619         $transports[$this->transport] = array(
620             'display' => $this->getDisplayName(),
621             'daemonScreenname' => $this->daemonScreenname());
622     }
623
624     function onSendImConfirmationCode($transport, $screenname, $code, $user)
625     {
626         if($transport == $this->transport)
627         {
628             $this->sendConfirmationCode($screenname, $code, $user);
629             return false;
630         }
631     }
632
633     function onUserDeleteRelated($user, &$tables)
634     {
635         $tables[] = 'User_im_prefs';
636         return true;
637     }
638
639     function onHaveImPlugin(&$haveImPlugin) {
640         $haveImPlugin = true; // set flag true (we're loaded, after all!)
641         return false; // stop looking
642     }
643
644     function initialize()
645     {
646         if( ! common_config('queue', 'enabled'))
647         {
648             // TRANS: Server exception thrown trying to initialise an IM plugin without meeting all prerequisites.
649             throw new ServerException(_('Queueing must be enabled to use IM plugins.'));
650         }
651
652         if(is_null($this->transport)){
653             // TRANS: Server exception thrown trying to initialise an IM plugin without a transport method.
654             throw new ServerException(_('Transport cannot be null.'));
655         }
656     }
657 }