+/**
+ * sends a confirmation request to a JID
+ *
+ * @param string $code confirmation code for confirmation URL
+ * @param string $nickname nickname of confirming user
+ * @param string $address JID to send confirmation to
+ *
+ * @return boolean success flag
+ */
+
+function jabber_confirm_address($code, $nickname, $address)
+{
+ $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
+ 'has said that your Jabber ID belongs to them. ' .
+ 'If that\'s true, you can confirm by clicking on this URL: ' .
+ common_local_url('confirmaddress', array('code' => $code)) .
+ ' . (If you cannot click it, copy-and-paste it into the ' .
+ 'address bar of your browser). If that user isn\'t you, ' .
+ 'or if you didn\'t request this confirmation, just ignore this message.';
+
+ return jabber_send_message($address, $body);
+}
+
+/**
+ * sends a "special" presence stanza on the Jabber network
+ *
+ * @param string $type Type of presence
+ * @param string $to JID to send presence to
+ * @param string $show show value for presence
+ * @param string $status status value for presence
+ *
+ * @return boolean success flag
+ *
+ * @see jabber_send_presence()
+ */
+
+function jabber_special_presence($type, $to=null, $show=null, $status=null)
+{
+ // FIXME: why use this instead of jabber_send_presence()?
+ $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;
+}
+
+/**
+ * send 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) {
+ $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);
+
+ $conn = jabber_connect();
+
+ foreach ($public as $address) {
+ common_log(LOG_INFO,
+ 'Sending notice ' . $notice->id .
+ ' to public listener ' . $address,
+ __FILE__);
+ $conn->message($address, $msg, 'chat', null, $entry);
+ $conn->processTime(0);
+ }
+ $profile->free();
+ }
+
+ return true;
+}
+
+/**
+ * makes a plain-text formatted version of a notice, suitable for Jabber distribution
+ *
+ * @param Profile &$profile profile of the sending user
+ * @param Notice &$notice notice being sent
+ *
+ * @return string plain-text version of the notice, with user nickname prefixed
+ */
+
+function jabber_format_notice(&$profile, &$notice)
+{
+ return $profile->nickname . ': ' . $notice->content;