]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
try and fix diaspora multiple items queued issue
[friendica.git] / include / notifier.php
1 <?php
2
3 require_once("boot.php");
4 require_once('include/queue_fn.php');
5
6 /*
7  * This file was at one time responsible for doing all deliveries, but this caused
8  * big problems on shared hosting systems, where the process might get killed by the 
9  * hosting provider and nothing would get delivered. 
10  * It now only delivers one message under certain cases, and invokes a queued
11  * delivery mechanism (include/deliver.php) to deliver individual contacts at 
12  * controlled intervals.
13  * This has a much better chance of surviving random processes getting killed
14  * by the hosting provider. 
15  * A lot of this code is duplicated in include/deliver.php until we have time to go back
16  * and re-structure the delivery procedure based on the obstacles that have been thrown at 
17  * us by hosting providers. 
18  */
19
20 function notifier_run($argv, $argc){
21         global $a, $db;
22
23         if(is_null($a)){
24                 $a = new App;
25         }
26   
27         if(is_null($db)) {
28                 @include(".htconfig.php");
29                 require_once("dba.php");
30                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
31                         unset($db_host, $db_user, $db_pass, $db_data);
32         }
33
34         require_once("session.php");
35         require_once("datetime.php");
36         require_once('include/items.php');
37         require_once('include/bbcode.php');
38
39         load_config('config');
40         load_config('system');
41
42         load_hooks();
43
44         if($argc < 3)
45                 return;
46
47         $a->set_baseurl(get_config('system','url'));
48
49         logger('notifier: invoked: ' . print_r($argv,true));
50
51         $cmd = $argv[1];
52
53         switch($cmd) {
54                 case 'mail':
55                 default:
56                         $item_id = intval($argv[2]);
57                         if(! $item_id){
58                                 return;
59                         }
60                         break;
61         }
62
63         $expire = false;
64         $mail = false;
65         $fsuggest = false;
66         $top_level = false;
67         $recipients = array();
68         $url_recipients = array();
69
70         $normal_mode = true;
71
72         if($cmd === 'mail') {
73                 $normal_mode = false;
74                 $mail = true;
75                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
76                                 intval($item_id)
77                 );
78                 if(! count($message)){
79                         return;
80                 }
81                 $uid = $message[0]['uid'];
82                 $recipients[] = $message[0]['contact-id'];
83                 $item = $message[0];
84
85         }
86         elseif($cmd === 'expire') {
87                 $normal_mode = false;
88                 $expire = true;
89                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
90                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
91                         intval($item_id)
92                 );
93                 $uid = $item_id;
94                 $item_id = 0;
95                 if(! count($items))
96                         return;
97         }
98         elseif($cmd === 'suggest') {
99                 $normal_mode = false;
100                 $fsuggest = true;
101
102                 $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
103                         intval($item_id)
104                 );
105                 if(! count($suggest))
106                         return;
107                 $uid = $suggest[0]['uid'];
108                 $recipients[] = $suggest[0]['cid'];
109                 $item = $suggest[0];
110         }
111         else {
112
113                 // find ancestors
114                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
115                         intval($item_id)
116                 );
117
118                 if((! count($r)) || (! intval($r[0]['parent']))) {
119                         return;
120                 }
121
122                 $target_item = $r[0];
123                 $parent_id = intval($r[0]['parent']);
124                 $uid = $r[0]['uid'];
125                 $updated = $r[0]['edited'];
126
127                 if(! $parent_id)
128                         return;
129
130                 $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` 
131                         FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC",
132                         intval($parent_id)
133                 );
134
135                 if(! count($items)) {
136                         return;
137                 }
138
139                 // avoid race condition with deleting entries
140
141                 if($items[0]['deleted']) {
142                         foreach($items as $item)
143                                 $item['deleted'] = 1;
144                 }
145
146                 if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
147                         logger('notifier: top level post');
148                         $top_level = true;
149                 }
150
151         }
152
153         $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
154                 `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, 
155                 `user`.`page-flags`, `user`.`prvnets`
156                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
157                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
158                 intval($uid)
159         );
160
161         if(! count($r))
162                 return;
163
164         $owner = $r[0];
165
166         $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
167
168         $hub = get_config('system','huburl');
169
170         // If this is a public conversation, notify the feed hub
171         $public_message = true;
172
173         // fill this in with a single salmon slap if applicable
174         $slap = '';
175
176         if(! ($mail || $fsuggest)) {
177
178                 require_once('include/group.php');
179
180                 $parent = $items[0];
181
182                 // This is IMPORTANT!!!!
183
184                 // We will only send a "notify owner to relay" or followup message if the referenced post
185                 // originated on our system by virtue of having our hostname somewhere
186                 // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
187
188                 // if $parent['wall'] == 1 we will already have the parent message in our array
189                 // and we will relay the whole lot.
190  
191                 // expire sends an entire group of expire messages and cannot be forwarded.
192                 // However the conversation owner will be a part of the conversation and will 
193                 // be notified during this run.
194                 // Other DFRN conversation members will be alerted during polled updates.
195
196
197
198                 // Diaspora members currently are not notified of expirations, and other networks have
199                 // either limited or no ability to process deletions. We should at least fix Diaspora 
200                 // by stringing togther an array of retractions and sending them onward.
201                  
202         
203                 $localhost = $a->get_hostname();
204                 if(strpos($localhost,':'))
205                         $localhost = substr($localhost,0,strpos($localhost,':'));
206
207                 /**
208                  *
209                  * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes 
210                  * have been known to cause runaway conditions which affected several servers, along with 
211                  * permissions issues. 
212                  *
213                  */
214  
215                 $relay_to_owner = false;
216
217                 if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
218                         $relay_to_owner = true;
219                 }
220
221                 // until the 'origin' flag has been in use for several months
222                 // we will just use it as a fallback test
223                 // later we will be able to use it as the primary test of whether or not to relay.
224
225                 if(! $target_item['origin'])
226                         $relay_to_owner = false;
227
228                 if($parent['origin'])
229                         $relay_to_owner = false;
230
231
232
233                 if($relay_to_owner) {
234                         logger('notifier: followup', LOGGER_DEBUG);
235                         // local followup to remote post
236                         $followup = true;
237                         $public_message = false; // not public
238                         $conversant_str = dbesc($parent['contact-id']);
239                 }
240                 else {
241                         $followup = false;
242
243                         // don't send deletions onward for other people's stuff
244
245                         if($target_item['deleted'] && (! intval($target_item['wall']))) {
246                                 logger('notifier: ignoring delete notification for non-wall item');
247                                 return;
248                         }
249
250                         if((strlen($parent['allow_cid'])) 
251                                 || (strlen($parent['allow_gid'])) 
252                                 || (strlen($parent['deny_cid'])) 
253                                 || (strlen($parent['deny_gid']))) {
254                                 $public_message = false; // private recipients, not public
255                         }
256
257                         $allow_people = expand_acl($parent['allow_cid']);
258                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
259                         $deny_people  = expand_acl($parent['deny_cid']);
260                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
261
262                         $conversants = array();
263
264                         foreach($items as $item) {
265                                 $recipients[] = $item['contact-id'];
266                                 $conversants[] = $item['contact-id'];
267                                 // pull out additional tagged people to notify (if public message)
268                                 if($public_message && strlen($item['inform'])) {
269                                         $people = explode(',',$item['inform']);
270                                         foreach($people as $person) {
271                                                 if(substr($person,0,4) === 'cid:') {
272                                                         $recipients[] = intval(substr($person,4));
273                                                         $conversants[] = intval(substr($person,4));
274                                                 }
275                                                 else {
276                                                         $url_recipients[] = substr($person,4);
277                                                 }
278                                         }
279                                 }
280                         }
281
282                         logger('notifier: url_recipients' . print_r($url_recipients,true));
283
284                         $conversants = array_unique($conversants);
285
286
287                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
288                         $deny = array_unique(array_merge($deny_people,$deny_groups));
289                         $recipients = array_diff($recipients,$deny);
290
291                         $conversant_str = dbesc(implode(', ',$conversants));
292                 }
293
294                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
295
296                 if(count($r))
297                         $contacts = $r;
298         }
299
300         $feed_template = get_markup_template('atom_feed.tpl');
301         $mail_template = get_markup_template('atom_mail.tpl');
302
303         $atom = '';
304         $slaps = array();
305
306         $hubxml = feed_hublinks();
307
308         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
309
310         if(strlen($birthday))
311                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
312
313         $atom .= replace_macros($feed_template, array(
314                         '$version'      => xmlify(FRIENDIKA_VERSION),
315                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
316                         '$feed_title'   => xmlify($owner['name']),
317                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
318                         '$hub'          => $hubxml,
319                         '$salmon'       => '',  // private feed, we don't use salmon here
320                         '$name'         => xmlify($owner['name']),
321                         '$profile_page' => xmlify($owner['url']),
322                         '$photo'        => xmlify($owner['photo']),
323                         '$thumb'        => xmlify($owner['thumb']),
324                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
325                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
326                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
327                         '$birthday'     => $birthday
328         ));
329
330         if($mail) {
331                 $public_message = false;  // mail is  not public
332
333                 $body = fix_private_photos($item['body'],$owner['uid']);
334
335                 $atom .= replace_macros($mail_template, array(
336                         '$name'         => xmlify($owner['name']),
337                         '$profile_page' => xmlify($owner['url']),
338                         '$thumb'        => xmlify($owner['thumb']),
339                         '$item_id'      => xmlify($item['uri']),
340                         '$subject'      => xmlify($item['title']),
341                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
342                         '$content'      => xmlify($body),
343                         '$parent_id'    => xmlify($item['parent-uri'])
344                 ));
345         }
346         elseif($fsuggest) {
347                 $public_message = false;  // suggestions are not public
348
349                 $sugg_template = get_markup_template('atom_suggest.tpl');
350
351                 $atom .= replace_macros($sugg_template, array(
352                         '$name'         => xmlify($item['name']),
353                         '$url'          => xmlify($item['url']),
354                         '$photo'        => xmlify($item['photo']),
355                         '$request'      => xmlify($item['request']),
356                         '$note'         => xmlify($item['note'])
357                 ));
358
359                 // We don't need this any more
360
361                 q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
362                         intval($item['id'])
363                 );
364
365         }
366         else {
367                 if($followup) {
368                         foreach($items as $item) {  // there is only one item
369                                 if(! $item['parent'])
370                                         continue;
371                                 if($item['id'] == $item_id) {
372                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
373                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
374                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
375                                 }
376                         }
377                 }
378                 else {
379                         foreach($items as $item) {
380
381                                 if(! $item['parent'])
382                                         continue;
383
384                                 // private emails may be in included in public conversations. Filter them.
385
386                                 if(($public_message) && $item['private'])
387                                         continue;
388
389
390                                 $contact = get_item_contact($item,$contacts);
391
392                                 if(! $contact)
393                                         continue;
394
395                                 if($normal_mode) {
396
397                                         // we only need the current item, but include the parent because without it
398                                         // older sites without a corresponding dfrn_notify change may do the wrong thing.
399
400                                     if($item_id == $item['id'] || $item['id'] == $item['parent'])
401                                                 $atom .= atom_entry($item,'text',$contact,$owner,true);
402                                 }
403                                 else
404                                         $atom .= atom_entry($item,'text',$contact,$owner,true);
405
406                                 if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
407                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
408                         }
409                 }
410         }
411         $atom .= '</feed>' . "\r\n";
412
413         logger('notifier: ' . $atom, LOGGER_DATA);
414
415         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
416
417         // If this is a public message and pubmail is set on the parent, include all your email contacts
418
419         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
420
421         if(! $mail_disabled) {
422                 if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid'])) 
423                         && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid'])) 
424                         && (intval($target_item['pubmail']))) {
425                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
426                                 intval($uid),
427                                 dbesc(NETWORK_MAIL)
428                         );
429                         if(count($r)) {
430                                 foreach($r as $rr)
431                                         $recipients[] = $rr['id'];
432                         }
433                 }
434         }
435
436         if($followup)
437                 $recip_str = $parent['contact-id'];
438         else
439                 $recip_str = implode(', ', $recipients);
440
441         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
442                 dbesc($recip_str)
443         );
444
445
446         require_once('include/salmon.php');
447
448         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
449
450         // delivery loop
451
452         if(count($r)) {
453
454                 foreach($r as $contact) {
455                         if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
456                                 if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
457                                         continue;
458                                 q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
459                                         dbesc($cmd),
460                                         intval($item_id),
461                                         intval($contact['id'])
462                                 );
463                         }
464                 }
465
466                 foreach($r as $contact) {
467                         if($contact['self'])
468                                 continue;
469
470                         // potentially more than one recipient. Start a new process and space them out a bit.
471                         // we will deliver single recipient types of message and email receipients here. 
472
473                         if((! $mail) && (! $fsuggest) && (! $followup)) {
474                                 proc_run('php','include/delivery.php',$cmd,$item_id,$contact['id']);
475                                 if($interval)
476                                         @time_sleep_until(microtime(true) + (float) $interval);
477                                 continue;
478                         }
479
480                         $deliver_status = 0;
481
482                         logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest");
483
484                         switch($contact['network']) {
485                                 case NETWORK_DFRN:
486
487                                         // perform local delivery if we are on the same site
488
489                                         $basepath =  implode('/', array_slice(explode('/',$contact['url']),0,3));
490
491                                         if(link_compare($basepath,$a->get_baseurl())) {
492
493                                                 $nickname = basename($contact['url']);
494                                                 if($contact['issued-id'])
495                                                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
496                                                 else
497                                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
498
499                                                 $x = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
500                                                         `contact`.`pubkey` AS `cpubkey`, 
501                                                         `contact`.`prvkey` AS `cprvkey`, 
502                                                         `contact`.`thumb` AS `thumb`, 
503                                                         `contact`.`url` as `url`,
504                                                         `contact`.`name` as `senderName`,
505                                                         `user`.* 
506                                                         FROM `contact` 
507                                                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
508                                                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
509                                                         AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
510                                                         $sql_extra
511                                                         AND `user`.`account_expired` = 0 LIMIT 1",
512                                                         dbesc(NETWORK_DFRN),
513                                                         dbesc($nickname)
514                                                 );
515
516                                                 if(count($x)) {
517                                                         require_once('library/simplepie/simplepie.inc');
518                                                         logger('mod-delivery: local delivery');
519                                                         local_delivery($x[0],$atom);
520                                                         break;                                  
521                                                 }
522                                         }
523
524
525
526                                         logger('notifier: dfrndelivery: ' . $contact['name']);
527                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
528
529                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
530         
531                                         if($deliver_status == (-1)) {
532                                                 logger('notifier: delivery failed: queuing message');
533                                                 // queue message for redelivery
534                                                 add_to_queue($contact['id'],NETWORK_DFRN,$atom);
535                                         }
536                                         break;
537                                 case NETWORK_OSTATUS:
538
539                                         // Do not send to otatus if we are not configured to send to public networks
540                                         if($owner['prvnets'])
541                                                 break;
542                                         if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
543                                                 break;
544
545                                         if($followup && $contact['notify']) {
546                                                 logger('notifier: slapdelivery: ' . $contact['name']);
547                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
548
549                                                 if($deliver_status == (-1)) {
550                                                         // queue message for redelivery
551                                                         add_to_queue($contact['id'],NETWORK_OSTATUS,$slap);
552                                                 }
553                                         }
554                                         else {
555
556                                                 // only send salmon if public - e.g. if it's ok to notify
557                                                 // a public hub, it's ok to send a salmon
558
559                                                 if((count($slaps)) && ($public_message) && (! $expire)) {
560                                                         logger('notifier: slapdelivery: ' . $contact['name']);
561                                                         foreach($slaps as $slappy) {
562                                                                 if($contact['notify']) {
563                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
564                                                                         if($deliver_status == (-1)) {
565                                                                                 // queue message for redelivery
566                                                                                 add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
567                                                                         }
568                                                                 }
569                                                         }
570                                                 }
571                                         }
572                                         break;
573
574                                 case NETWORK_MAIL:
575                                                 
576                                         if(get_config('system','dfrn_only'))
577                                                 break;
578
579                                         // WARNING: does not currently convert to RFC2047 header encodings, etc.
580
581                                         $addr = $contact['addr'];
582                                         if(! strlen($addr))
583                                                 break;
584
585                                         if($cmd === 'wall-new' || $cmd === 'comment-new') {
586
587                                                 $it = null;
588                                                 if($cmd === 'wall-new') 
589                                                         $it = $items[0];
590                                                 else {
591                                                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", 
592                                                                 intval($argv[2]),
593                                                                 intval($uid)
594                                                         );
595                                                         if(count($r))
596                                                                 $it = $r[0];
597                                                 }
598                                                 if(! $it)
599                                                         break;
600                                                 
601
602
603                                                 $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
604                                                         intval($uid)
605                                                 );
606                                                 if(! count($local_user))
607                                                         break;
608                                                 
609                                                 $reply_to = '';
610                                                 $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
611                                                         intval($uid)
612                                                 );
613                                                 if($r1 && $r1[0]['reply_to'])
614                                                         $reply_to = $r1[0]['reply_to'];
615         
616                                                 $subject  = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
617                                                 $headers  = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
618
619                                                 if($reply_to)
620                                                         $headers .= 'Reply-to: ' . $reply_to . "\n";
621
622                                                 $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
623
624                                                 if($it['uri'] !== $it['parent-uri']) {
625                                                         $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
626                                                         if(! strlen($it['title'])) {
627                                                                 $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
628                                                                         dbesc($it['parent-uri'])
629                                                                 );
630                                                                 if(count($r)) {
631                                                                         $subtitle = $r[0]['title'];
632                                                                         if($subtitle) {
633                                                                                 if(strncasecmp($subtitle,'RE:',3))
634                                                                                         $subject = $subtitle;
635                                                                                 else
636                                                                                         $subject = 'Re: ' . $subtitle;
637                                                                         }
638                                                                 }
639                                                         }
640                                                 }
641
642                                                 $headers .= 'MIME-Version: 1.0' . "\n";
643                                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
644                                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
645                                                 $html    = prepare_body($it);
646                                                 $message = '<html><body>' . $html . '</body></html>';
647                                                 logger('notifier: email delivery to ' . $addr);
648                                                 mail($addr, $subject, $message, $headers);
649                                         }
650                                         break;
651                                 case NETWORK_DIASPORA:
652                                         require_once('include/diaspora.php');
653
654                                         if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')) || (! $normal_mode))
655                                                 break;
656
657                                         // special handling for followup to public post
658                                         // all other public posts processed as public batches further below
659
660                                         if($public_message) {
661                                                 if($followup)
662                                                         diaspora_send_followup($target_item,$owner,$contact, true);
663                                                 break;
664                                         }
665
666                                         if(! $contact['pubkey'])
667                                                 break;
668                                         
669                                         if($target_item['verb'] === ACTIVITY_DISLIKE) {
670                                                 // unsupported
671                                                 break;
672                                         }
673                                         elseif(($target_item['deleted']) && ($target_item['verb'] !== ACTIVITY_LIKE)) {
674                                                 // diaspora delete, 
675                                                 diaspora_send_retraction($target_item,$owner,$contact);
676                                                 break;
677                                         }
678                                         elseif($followup) {
679                                                 // send comments, likes and retractions of likes to owner to relay
680                                                 diaspora_send_followup($target_item,$owner,$contact);
681                                                 break;
682                                         }
683                                         elseif($target_item['parent'] != $target_item['id']) {
684                                                 // we are the relay - send comments, likes and unlikes to our conversants
685                                                 diaspora_send_relay($target_item,$owner,$contact);
686                                                 break;
687                                         }               
688                                         elseif(($top_level) && (! $walltowall)) {
689                                                 // currently no workable solution for sending walltowall
690                                                 diaspora_send_status($target_item,$owner,$contact);
691                                                 break;
692                                         }
693
694                                         break;
695
696                                 case NETWORK_FEED:
697                                 case NETWORK_FACEBOOK:
698                                         if(get_config('system','dfrn_only'))
699                                                 break;
700                                 default:
701                                         break;
702                         }
703                 }
704         }
705                 
706         // send additional slaps to mentioned remote tags (@foo@example.com)
707
708         if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
709                 if(! get_config('system','dfrn_only')) {
710                         foreach($url_recipients as $url) {
711                                 if($url) {
712                                         logger('notifier: urldelivery: ' . $url);
713                                         $deliver_status = slapper($owner,$url,$slap);
714                                         // TODO: redeliver/queue these items on failure, though there is no contact record
715                                 }
716                         }
717                 }
718         }
719
720
721         if($public_message) {
722
723                 $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s' 
724                         AND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ",
725                         dbesc(NETWORK_DIASPORA),
726                         intval($owner['uid']),
727                         intval(CONTACT_IS_SHARING)
728                 );
729                         
730                 $r2 = q("SELECT `id`, `name`,`network` FROM `contact` 
731                         WHERE `network` = '%s' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
732                         AND `rel` != %d order by rand() ",
733                         dbesc(NETWORK_DFRN),
734                         intval($owner['uid']),
735                         intval(CONTACT_IS_SHARING)
736                 );
737
738                 $r = array_merge($r2,$r1);
739
740                 if(count($r)) {
741                         logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
742
743                         // throw everything into the queue in case we get killed
744
745                         foreach($r as $rr) {
746                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
747                                         q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
748                                                 dbesc($cmd),
749                                                 intval($item_id),
750                                                 intval($rr['id'])
751                                         );
752                                 }
753                         }
754
755                         foreach($r as $rr) {
756
757                                 // except for Diaspora batch jobs
758                                 // Don't deliver to folks who have already been delivered to
759
760                                 if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
761                                         logger('notifier: already delivered id=' . $rr['id']);
762                                         continue;
763                                 }
764
765                                 if((! $mail) && (! $fsuggest) && (! $followup)) {
766                                         logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']); 
767                                         proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
768                                         if($interval)
769                                                 @time_sleep_until(microtime(true) + (float) $interval);
770                                 }
771                         }
772                 }
773
774
775                 if(strlen($hub)) {
776                         $hubs = explode(',', $hub);
777                         if(count($hubs)) {
778                                 foreach($hubs as $h) {
779                                         $h = trim($h);
780                                         if(! strlen($h))
781                                                 continue;
782                                         $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
783                                         post_url($h,$params);
784                                         logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
785                                         if(count($hubs) > 1)
786                                                 sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
787                                 }
788                         }
789                 }
790
791         }
792
793         if($normal_mode)
794                 call_hooks('notifier_normal',$target_item);
795
796         call_hooks('notifier_end',$target_item);
797
798         return;
799 }
800
801 if (array_search(__file__,get_included_files())===0){
802   notifier_run($argv,$argc);
803   killme();
804 }