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