-function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
- $conn = jabber_connect();
-
- $to = htmlspecialchars($to);
- $status = htmlspecialchars($status);
- $out = "<presence";
- if($to) $out .= " to='$to'";
- if($type) $out .= " type='$type'";
- if($show == 'available' and !$status) {
- $out .= "/>";
- } else {
- $out .= ">";
- if($show && ($show != 'available')) $out .= "<show>$show</show>";
- if($status) $out .= "<status>$status</status>";
- $out .= "</presence>";
- }
- $conn->send($out);
+/**
+ * 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();
+ $user->query('SELECT user.id, user.jabber ' .
+ 'FROM user JOIN reply ON user.id = reply.profile_id ' .
+ 'WHERE reply.notice_id = ' . $notice->id . ' ' .
+ 'AND user.jabber is not null ' .
+ 'AND user.jabbernotify = 1 ' .
+ 'AND user.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 user.id, user.jabber ' .
+ 'FROM user JOIN subscription ' .
+ 'ON user.id = subscription.subscriber ' .
+ 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
+ 'AND user.jabber is not null ' .
+ 'AND user.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);
+ }
+ }
+
+ $user->free();
+
+ return true;