]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/implugin.php
daemon_screenname -> daemonScreenname
[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 sendNotice($screenname, $notice)
90     {
91         return $this->sendMessage($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 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 handle_incoming()
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 enqueue_outgoing_raw($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 enqueue_incoming_raw($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 get_user($screenname)
172     {
173         $user_im_prefs = $this->get_user_im_prefs_from_screenname($screenname);
174         if($user_im_prefs){
175             $user = User::staticGet('id', $user_im_prefs->user_id);
176             $user_im_prefs->free();
177             return $user;
178         }else{
179             return false;
180         }
181     }
182
183
184     /**
185      * given a screenname, get the User_im_prefs object for this transport
186      *
187      * @param string $screenname
188      *
189      * @return User_im_prefs user_im_prefs
190      */
191     function get_user_im_prefs_from_screenname($screenname)
192     {
193         $user_im_prefs = User_im_prefs::pkeyGet(
194             array('transport' => $this->transport,
195                   'screenname' => $this->normalize($screenname)));
196         if ($user_im_prefs) {
197             return $user_im_prefs;
198         } else {
199             return false;
200         }
201     }
202
203
204     /**
205      * given a User, get their screenname
206      *
207      * @param User $user
208      *
209      * @return string screenname of that user
210      */
211     function get_screenname($user)
212     {
213         $user_im_prefs = $this->get_user_im_prefs_from_user($user);
214         if ($user_im_prefs) {
215             return $user_im_prefs->screenname;
216         } else {
217             return false;
218         }
219     }
220
221
222     /**
223      * given a User, get their User_im_prefs
224      *
225      * @param User $user
226      *
227      * @return User_im_prefs user_im_prefs of that user
228      */
229     function get_user_im_prefs_from_user($user)
230     {
231         $user_im_prefs = User_im_prefs::pkeyGet(
232             array('transport' => $this->transport,
233                   'user_id' => $user->id));
234         if ($user_im_prefs){
235             return $user_im_prefs;
236         } else {
237             return false;
238         }
239     }
240     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - SENDING ========================\
241     /**
242      * Send a message to a given screenname from the site
243      *
244      * @param string $screenname screenname to send the message to
245      * @param string $msg message contents to send
246      *
247      * @param boolean success
248      */
249     protected function send_from_site($screenname, $msg)
250     {
251         $text = '['.common_config('site', 'name') . '] ' . $msg;
252         $this->sendMessage($screenname, $text);
253     }
254
255     /**
256      * send a confirmation code to a user
257      *
258      * @param string $screenname screenname sending to
259      * @param string $code the confirmation code
260      * @param User $user user sending to
261      *
262      * @return boolean success value
263      */
264     function send_confirmation_code($screenname, $code, $user)
265     {
266         $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
267           'If that\'s true, you can confirm by clicking on this URL: ' .
268           '%s' .
269           ' . (If you cannot click it, copy-and-paste it into the ' .
270           'address bar of your browser). If that user isn\'t you, ' .
271           'or if you didn\'t request this confirmation, just ignore this message.'),
272           $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', 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 public_notice($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 broadcast_notice($notice)
322     {
323
324         $ni = $notice->whoGets();
325
326         foreach ($ni as $user_id => $reason) {
327             $user = User::staticGet($user_id);
328             if (empty($user)) {
329                 // either not a local user, or just not found
330                 continue;
331             }
332             $user_im_prefs = $this->get_user_im_prefs_from_user($user);
333             if(!$user_im_prefs || !$user_im_prefs->notify){
334                 continue;
335             }
336
337             switch ($reason) {
338             case NOTICE_INBOX_SOURCE_REPLY:
339                 if (!$user_im_prefs->replies) {
340                     continue 2;
341                 }
342                 break;
343             case NOTICE_INBOX_SOURCE_SUB:
344                 $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
345                                                    'subscribed' => $notice->profile_id));
346                 if (empty($sub) || !$sub->jabber) {
347                     continue 2;
348                 }
349                 break;
350             case NOTICE_INBOX_SOURCE_GROUP:
351                 break;
352             default:
353                 throw new Exception(sprintf(_("Unknown inbox source %d."), $reason));
354             }
355
356             common_log(LOG_INFO,
357                        'Sending notice ' . $notice->id . ' to ' . $user_im_prefs->screenname,
358                        __FILE__);
359             $this->sendNotice($user_im_prefs->screenname, $notice);
360             $user_im_prefs->free();
361         }
362
363         return true;
364     }
365
366     /**
367      * makes a plain-text formatted version of a notice, suitable for IM distribution
368      *
369      * @param Notice  $notice  notice being sent
370      *
371      * @return string plain-text version of the notice, with user nickname prefixed
372      */
373
374     function format_notice($notice)
375     {
376         $profile = $notice->getProfile();
377         return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']';
378     }
379     //========================UTILITY FUNCTIONS USEFUL TO IMPLEMENTATIONS - RECEIVING ========================\
380
381     /**
382      * Attempt to handle a message as a command
383      * @param User $user user the message is from
384      * @param string $body message text
385      * @return boolean true if the message was a command and was executed, false if it was not a command
386      */
387     protected function handle_command($user, $body)
388     {
389         $inter = new CommandInterpreter();
390         $cmd = $inter->handle_command($user, $body);
391         if ($cmd) {
392             $chan = new IMChannel($this);
393             $cmd->execute($chan);
394             return true;
395         } else {
396             return false;
397         }
398     }
399
400     /**
401      * Is some text an autoreply message?
402      * @param string $txt message text
403      * @return boolean true if autoreply
404      */
405     protected function is_autoreply($txt)
406     {
407         if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
408             return true;
409         } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
410             return true;
411         } else {
412             return false;
413         }
414     }
415
416     /**
417      * Is some text an OTR message?
418      * @param string $txt message text
419      * @return boolean true if OTR
420      */
421     protected function is_otr($txt)
422     {
423         if (preg_match('/^\?OTR/', $txt)) {
424             return true;
425         } else {
426             return false;
427         }
428     }
429
430     /**
431      * Helper for handling incoming messages
432      * Your incoming message handler will probably want to call this function
433      *
434      * @param string $from screenname the message was sent from
435      * @param string $message message contents
436      *
437      * @param boolean success
438      */
439     protected function handle_incoming($from, $notice_text)
440     {
441         $user = $this->get_user($from);
442         // For common_current_user to work
443         global $_cur;
444         $_cur = $user;
445
446         if (!$user) {
447             $this->send_from_site($from, 'Unknown user; go to ' .
448                              common_local_url('imsettings') .
449                              ' to add your address to your account');
450             common_log(LOG_WARNING, 'Message from unknown user ' . $from);
451             return;
452         }
453         if ($this->handle_command($user, $notice_text)) {
454             common_log(LOG_INFO, "Command message by $from handled.");
455             return;
456         } else if ($this->is_autoreply($notice_text)) {
457             common_log(LOG_INFO, 'Ignoring auto reply from ' . $from);
458             return;
459         } else if ($this->is_otr($notice_text)) {
460             common_log(LOG_INFO, 'Ignoring OTR from ' . $from);
461             return;
462         } else {
463
464             common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
465
466             $this->add_notice($from, $user, $notice_text);
467         }
468
469         $user->free();
470         unset($user);
471         unset($_cur);
472         unset($message);
473     }
474
475     /**
476      * Helper for handling incoming messages
477      * Your incoming message handler will probably want to call this function
478      *
479      * @param string $from screenname the message was sent from
480      * @param string $message message contents
481      *
482      * @param boolean success
483      */
484     protected function add_notice($screenname, $user, $body)
485     {
486         $body = trim(strip_tags($body));
487         $content_shortened = common_shorten_links($body);
488         if (Notice::contentTooLong($content_shortened)) {
489           $this->send_from_site($screenname, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d.'),
490                                           Notice::maxContent(),
491                                           mb_strlen($content_shortened)));
492           return;
493         }
494
495         try {
496             $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
497         } catch (Exception $e) {
498             common_log(LOG_ERR, $e->getMessage());
499             $this->send_from_site($from, $e->getMessage());
500             return;
501         }
502
503         common_broadcast_notice($notice);
504         common_log(LOG_INFO,
505                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
506         $notice->free();
507         unset($notice);
508     }
509
510     //========================EVENT HANDLERS========================\
511     
512     /**
513      * Register notice queue handler
514      *
515      * @param QueueManager $manager
516      *
517      * @return boolean hook return
518      */
519     function onEndInitializeQueueManager($manager)
520     {
521         $manager->connect($this->transport . '-in', new ImReceiverQueueHandler($this), 'im');
522         $manager->connect($this->transport, new ImQueueHandler($this));
523         $manager->connect($this->transport . '-out', new ImSenderQueueHandler($this), 'im');
524         return true;
525     }
526
527     function onStartImDaemonIoManagers(&$classes)
528     {
529         //$classes[] = new ImManager($this); // handles sending/receiving/pings/reconnects
530         return true;
531     }
532
533     function onStartEnqueueNotice($notice, &$transports)
534     {
535         $profile = Profile::staticGet($notice->profile_id);
536
537         if (!$profile) {
538             common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
539                        'unknown profile ' . common_log_objstring($notice),
540                        __FILE__);
541         }else{
542             $transports[] = $this->transport;
543         }
544
545         return true;
546     }
547
548     function onEndShowHeadElements($action)
549     {
550         $aname = $action->trimmed('action');
551
552         if ($aname == 'shownotice') {
553
554             $user_im_prefs = new User_im_prefs();
555             $user_im_prefs->user_id = $action->profile->id;
556             $user_im_prefs->transport = $this->transport;
557
558             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) {
559                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
560                                   $action->notice->uri);
561                 $action->element('meta', array('name' => 'microid',
562                                              'content' => $id->toString()));
563             }
564
565         } else if ($aname == 'showstream') {
566
567             $user_im_prefs = new User_im_prefs();
568             $user_im_prefs->user_id = $action->user->id;
569             $user_im_prefs->transport = $this->transport;
570
571             if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->profile->profileurl) {
572                 $id = new Microid($this->microiduri($user_im_prefs->screenname),
573                                   $action->selfUrl());
574                 $action->element('meta', array('name' => 'microid',
575                                                'content' => $id->toString()));
576             }
577         }
578     }
579
580     function onNormalizeImScreenname($transport, &$screenname)
581     {
582         if($transport == $this->transport)
583         {
584             $screenname = $this->normalize($screenname);
585             return false;
586         }
587     }
588
589     function onValidateImScreenname($transport, $screenname, &$valid)
590     {
591         if($transport == $this->transport)
592         {
593             $valid = $this->validate($screenname);
594             return false;
595         }
596     }
597
598     function onGetImTransports(&$transports)
599     {
600         $transports[$this->transport] = array(
601             'display' => $this->getDisplayName(),
602             'daemonScreenname' => $this->daemonScreenname());
603     }
604
605     function onSendImConfirmationCode($transport, $screenname, $code, $user)
606     {
607         if($transport == $this->transport)
608         {
609             $this->send_confirmation_code($screenname, $code, $user);
610             return false;
611         }
612     }
613
614     function onUserDeleteRelated($user, &$tables)
615     {
616         $tables[] = 'User_im_prefs';
617         return true;
618     }
619
620     function initialize()
621     {
622         if( ! common_config('queue', 'enabled'))
623         {
624             throw new ServerException("Queueing must be enabled to use IM plugins");
625         }
626
627         if(is_null($this->transport)){
628             throw new ServerException('transport cannot be null');
629         }
630     }
631 }