-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;
- }
- $sent_to = array();
- # First, get users who this is a direct reply to
- $reply = new Reply();
- $reply->notice_id = $notice->id;
- if ($reply->find()) {
- while ($reply->fetch()) {
- $user = User::staticGet($reply->profile_id);
- if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
- common_log(LOG_INFO,
- 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
- __FILE__);
- $success = jabber_send_notice($user->jabber, $notice);
- if ($success) {
- # Remember so we don't send twice
- $sent_to[$user->id] = true;
- } else {
- # XXX: Not sure, but I think that's the right thing to do
- common_log(LOG_WARNING,
- 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
- __FILE__);
- return false;
- }
- }
- }
- }
- # Now, get users subscribed to this profile
- # XXX: use a join here rather than looping through results
- $sub = new Subscription();
- $sub->subscribed = $notice->profile_id;
-
- if ($sub->find()) {
- while ($sub->fetch()) {
- $user = User::staticGet($sub->subscriber);
- if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) {
- common_log(LOG_INFO,
- 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
- __FILE__);
- $success = jabber_send_notice($user->jabber, $notice);
- if ($success) {
- $sent_to[$user->id] = true;
- } else {
- # XXX: Not sure, but I think that's the right thing to do
- common_log(LOG_WARNING,
- 'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
- __FILE__);
- return false;
- }
- }
- }
- }
-
- # Now, users who want everything
-
- $public = common_config('xmpp', 'public');
-
- # FIXME PRIV don't send out private messages here
-
- if ($public) {
- foreach ($public as $address) {
- common_log(LOG_INFO,
- 'Sending notice ' . $notice->id . ' to public listener ' . $address,
- __FILE__);
- jabber_send_notice($address, $notice);
- }
- }
-
- return true;
+/**
+ * Queue broadcast of 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 StatusNet.
+ *
+ * @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 true; // not recoverable; discard.
+ }
+
+ $msg = jabber_format_notice($profile, $notice);
+ $entry = jabber_format_entry($profile, $notice);
+
+ $profile->free();
+ unset($profile);
+
+ $sent_to = array();
+
+ $conn = jabber_proxy();
+
+ $ni = $notice->whoGets();
+
+ foreach ($ni as $user_id => $reason) {
+ $user = User::staticGet($user_id);
+ if (empty($user) ||
+ empty($user->jabber) ||
+ !$user->jabbernotify) {
+ // either not a local user, or just not found
+ continue;
+ }
+ switch ($reason) {
+ case NOTICE_INBOX_SOURCE_REPLY:
+ if (!$user->jabberreplies) {
+ continue 2;
+ }
+ break;
+ case NOTICE_INBOX_SOURCE_SUB:
+ $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
+ 'subscribed' => $notice->profile_id));
+ if (empty($sub) || !$sub->jabber) {
+ continue 2;
+ }
+ break;
+ case NOTICE_INBOX_SOURCE_GROUP:
+ break;
+ default:
+ throw new Exception(sprintf(_("Unknown inbox source %d."), $reason));
+ }
+
+ common_log(LOG_INFO,
+ 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
+ __FILE__);
+ $conn->message($user->jabber, $msg, 'chat', null, $entry);
+ }
+
+ return true;
+}
+
+/**
+ * Queue send of a notice to all public listeners
+ *
+ * For notices that are generated on the local system (by users), we can optionally
+ * forward them to remote listeners by XMPP.
+ *
+ * @param Notice $notice notice to broadcast
+ *
+ * @return boolean success flag
+ */
+
+function jabber_public_notice($notice)
+{
+ // Now, users who want everything
+
+ $public = common_config('xmpp', 'public');
+
+ // FIXME PRIV don't send out private messages here
+ // XXX: should we send out non-local messages if public,localonly
+ // = false? I think not
+
+ if ($public && $notice->is_local == Notice::LOCAL_PUBLIC) {
+ $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 true; // not recoverable; discard.
+ }
+
+ $msg = jabber_format_notice($profile, $notice);
+ $entry = jabber_format_entry($profile, $notice);
+
+ $conn = jabber_proxy();
+
+ foreach ($public as $address) {
+ common_log(LOG_INFO,
+ 'Sending notice ' . $notice->id .
+ ' to public listener ' . $address,
+ __FILE__);
+ $conn->message($address, $msg, 'chat', null, $entry);
+ }
+ $profile->free();
+ }
+
+ return true;