+/**
+ * broadcast a notice to all subscribers and reply recipients
+ *
+ * This function will send a notice to all subscribers on the local server
+ * who have Jabber addresses, and have Jabber notification enabled, and
+ * have this subscription enabled for Jabber. It also sends the notice to
+ * all recipients of @-replies who have Jabber addresses and Jabber notification
+ * enabled. This is really the heart of Jabber distribution in Laconica.
+ *
+ * @param Notice $notice The notice to broadcast
+ *
+ * @return boolean success flag
+ */
+
+function jabber_broadcast_notice($notice)
+{
+ if (!common_config('xmpp', 'enabled')) {
+ return true;
+ }
+ $profile = Profile::staticGet($notice->profile_id);
+
+ if (!$profile) {
+ common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
+ 'unknown profile ' . common_log_objstring($notice),
+ __FILE__);
+ return false;
+ }
+
+ $msg = jabber_format_notice($profile, $notice);
+ $entry = jabber_format_entry($profile, $notice);
+
+ $profile->free();
+ unset($profile);
+
+ $sent_to = array();
+
+ $conn = jabber_connect();
+
+ // First, get users to whom this is a direct reply
+ $user = new User();
+ $UT = common_config('db','type')=='pgsql'?'"user"':'user';
+ $user->query("SELECT $UT.id, $UT.jabber " .
+ "FROM $UT JOIN reply ON $UT.id = reply.profile_id " .
+ 'WHERE reply.notice_id = ' . $notice->id . ' ' .
+ "AND $UT.jabber is not null " .
+ "AND $UT.jabbernotify = 1 " .
+ "AND $UT.jabberreplies = 1 ");
+
+ while ($user->fetch()) {
+ common_log(LOG_INFO,
+ 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
+ __FILE__);
+ $conn->message($user->jabber, $msg, 'chat', null, $entry);
+ $conn->processTime(0);
+ $sent_to[$user->id] = 1;
+ }
+
+ $user->free();
+
+ // Now, get users subscribed to this profile
+
+ $user = new User();
+ $user->query("SELECT $UT.id, $UT.jabber " .
+ "FROM $UT JOIN subscription " .
+ "ON $UT.id = subscription.subscriber " .
+ 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
+ "AND $UT.jabber is not null " .
+ "AND $UT.jabbernotify = 1 " .
+ 'AND subscription.jabber = 1 ');
+
+ while ($user->fetch()) {
+ if (!array_key_exists($user->id, $sent_to)) {
+ common_log(LOG_INFO,
+ 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
+ __FILE__);
+ $conn->message($user->jabber, $msg, 'chat', null, $entry);
+ // To keep the incoming queue from filling up,
+ // we service it after each send.
+ $conn->processTime(0);
+ $sent_to[$user->id] = 1;
+ }
+ }
+
+ // Now, get users who have it in their inbox because of groups
+
+ $user = new User();
+ $user->query("SELECT $UT.id, $UT.jabber " .
+ "FROM $UT JOIN notice_inbox " .
+ "ON $UT.id = notice_inbox.user_id " .
+ 'WHERE notice_inbox.notice_id = ' . $notice->id . ' ' .
+ 'AND notice_inbox.source = 2 ' .
+ 'AND user.jabber is not null ' .
+ 'AND user.jabbernotify = 1 ');
+
+ while ($user->fetch()) {
+ if (!array_key_exists($user->id, $sent_to)) {
+ common_log(LOG_INFO,
+ 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
+ __FILE__);
+ $conn->message($user->jabber, $msg, 'chat', null, $entry);
+ // To keep the incoming queue from filling up,
+ // we service it after each send.
+ $conn->processTime(0);
+ $sent_to[$user->id] = 1;
+ }
+ }
+
+ $user->free();
+
+ return true;