PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/laconica-0.5.0/lib/jabber.php

#
PHP | 289 lines | 226 code | 33 blank | 30 comment | 40 complexity | e6eb42d6e707b56787ad55930c769c82 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. * Laconica - a distributed open-source microblogging tool
  4. * Copyright (C) 2008, Controlez-Vous, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('LACONICA')) { exit(1); }
  20. require_once('XMPPHP/XMPP.php');
  21. # XXX: something of a hack to work around problems with the XMPPHP lib
  22. class Laconica_XMPP extends XMPPHP_XMPP {
  23. function messageplus($to, $body, $type = 'chat', $subject = null, $payload = null) {
  24. $to = htmlspecialchars($to);
  25. $body = htmlspecialchars($body);
  26. $subject = htmlspecialchars($subject);
  27. $jid = jabber_daemon_address();
  28. $out = "<message from='$jid' to='$to' type='$type'>";
  29. if($subject) $out .= "<subject>$subject</subject>";
  30. $out .= "<body>$body</body>";
  31. if($payload) $out .= $payload;
  32. $out .= "</message>";
  33. $cnt = strlen($out);
  34. common_log(LOG_DEBUG, "Sending $cnt chars to $to");
  35. $this->send($out);
  36. common_log(LOG_DEBUG, 'Done.');
  37. }
  38. }
  39. function jabber_valid_base_jid($jid) {
  40. # Cheap but effective
  41. return Validate::email($jid);
  42. }
  43. function jabber_normalize_jid($jid) {
  44. if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
  45. $node = $matches[1];
  46. $server = $matches[2];
  47. $resource = $matches[3];
  48. return strtolower($node.'@'.$server);
  49. } else {
  50. return NULL;
  51. }
  52. }
  53. function jabber_daemon_address() {
  54. return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
  55. }
  56. function jabber_connect($resource=NULL) {
  57. static $conn = NULL;
  58. if (!$conn) {
  59. $conn = new Laconica_XMPP(common_config('xmpp', 'host') ?
  60. common_config('xmpp', 'host') :
  61. common_config('xmpp', 'server'),
  62. common_config('xmpp', 'port'),
  63. common_config('xmpp', 'user'),
  64. common_config('xmpp', 'password'),
  65. ($resource) ? $resource :
  66. common_config('xmpp', 'resource'),
  67. common_config('xmpp', 'server'),
  68. common_config('xmpp', 'debug') ?
  69. true : false,
  70. common_config('xmpp', 'debug') ?
  71. XMPPHP_Log::LEVEL_VERBOSE : NULL
  72. );
  73. $conn->autoSubscribe();
  74. if (!$conn) {
  75. return false;
  76. }
  77. $conn->connect(true); # true = persistent connection
  78. if ($conn->isDisconnected()) {
  79. return false;
  80. }
  81. $conn->processUntil('session_start');
  82. }
  83. return $conn;
  84. }
  85. function jabber_send_notice($to, $notice) {
  86. $conn = jabber_connect();
  87. if (!$conn) {
  88. return false;
  89. }
  90. $profile = Profile::staticGet($notice->profile_id);
  91. if (!$profile) {
  92. common_log(LOG_WARNING, 'Refusing to send notice with ' .
  93. 'unknown profile ' . common_log_objstring($notice),
  94. __FILE__);
  95. return false;
  96. }
  97. $msg = jabber_format_notice($profile, $notice);
  98. $entry = jabber_format_entry($profile, $notice);
  99. $conn->messageplus($to, $msg, 'chat', NULL, $entry);
  100. return true;
  101. }
  102. # Extra stuff defined by Twitter, needed by twitter clients
  103. function jabber_format_entry($profile, $notice) {
  104. $noticeurl = common_local_url('shownotice',
  105. array('notice' => $notice->id));
  106. $msg = jabber_format_notice($profile, $notice);
  107. $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
  108. $entry .= "<source>\n";
  109. $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
  110. $entry .= "<link href='" . $profile->profileurl . "'/>\n";
  111. $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
  112. $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
  113. $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
  114. $entry .= "</source>\n";
  115. $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
  116. $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
  117. $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
  118. $entry .= "<id>". $notice->uri . "</id>\n";
  119. $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
  120. $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
  121. $entry .= "</entry>\n";
  122. $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
  123. $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
  124. $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
  125. $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
  126. $html .= "\n</body>\n";
  127. $html .= "\n</html>\n";
  128. $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
  129. $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
  130. $event .= "node='" . common_local_url('public') . "'>\n";
  131. $event .= "<item id='" . $notice->uri ."' />\n";
  132. $event .= "</items>\n";
  133. $event .= "</event>\n";
  134. # FIXME: include the pubsub event, too.
  135. return $html . $entry;
  136. # return $entry . "\n" . $event;
  137. }
  138. function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
  139. $conn = jabber_connect();
  140. if (!$conn) {
  141. return false;
  142. }
  143. $conn->message($to, $body, $type, $subject);
  144. return true;
  145. }
  146. function jabber_send_presence($status, $show='available', $to=Null) {
  147. $conn = jabber_connect();
  148. if (!$conn) {
  149. return false;
  150. }
  151. $conn->presence($status, $show, $to);
  152. return true;
  153. }
  154. function jabber_confirm_address($code, $nickname, $address) {
  155. $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
  156. 'has said that your Jabber ID belongs to them. ' .
  157. 'If that\'s true, you can confirm by clicking on this URL: ' .
  158. common_local_url('confirmaddress', array('code' => $code)) .
  159. ' . (If you cannot click it, copy-and-paste it into the ' .
  160. 'address bar of your browser). If that user isn\'t you, ' .
  161. 'or if you didn\'t request this confirmation, just ignore this message.';
  162. return jabber_send_message($address, $body);
  163. }
  164. function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
  165. $conn = jabber_connect();
  166. $to = htmlspecialchars($to);
  167. $status = htmlspecialchars($status);
  168. $out = "<presence";
  169. if($to) $out .= " to='$to'";
  170. if($type) $out .= " type='$type'";
  171. if($show == 'available' and !$status) {
  172. $out .= "/>";
  173. } else {
  174. $out .= ">";
  175. if($show && ($show != 'available')) $out .= "<show>$show</show>";
  176. if($status) $out .= "<status>$status</status>";
  177. $out .= "</presence>";
  178. }
  179. $conn->send($out);
  180. }
  181. function jabber_broadcast_notice($notice) {
  182. if (!common_config('xmpp', 'enabled')) {
  183. return true;
  184. }
  185. $profile = Profile::staticGet($notice->profile_id);
  186. if (!$profile) {
  187. common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
  188. 'unknown profile ' . common_log_objstring($notice),
  189. __FILE__);
  190. return false;
  191. }
  192. $sent_to = array();
  193. # First, get users who this is a direct reply to
  194. $reply = new Reply();
  195. $reply->notice_id = $notice->id;
  196. if ($reply->find()) {
  197. while ($reply->fetch()) {
  198. $user = User::staticGet($reply->profile_id);
  199. if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
  200. common_log(LOG_INFO,
  201. 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
  202. __FILE__);
  203. $success = jabber_send_notice($user->jabber, $notice);
  204. if ($success) {
  205. # Remember so we don't send twice
  206. $sent_to[$user->id] = true;
  207. } else {
  208. # XXX: Not sure, but I think that's the right thing to do
  209. common_log(LOG_WARNING,
  210. 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
  211. __FILE__);
  212. return false;
  213. }
  214. }
  215. }
  216. }
  217. # Now, get users subscribed to this profile
  218. # XXX: use a join here rather than looping through results
  219. $sub = new Subscription();
  220. $sub->subscribed = $notice->profile_id;
  221. if ($sub->find()) {
  222. while ($sub->fetch()) {
  223. $user = User::staticGet($sub->subscriber);
  224. if ($user && $user->jabber && $user->jabbernotify && !$sent_to[$user->id]) {
  225. common_log(LOG_INFO,
  226. 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
  227. __FILE__);
  228. $success = jabber_send_notice($user->jabber, $notice);
  229. if ($success) {
  230. $sent_to[$user->id] = true;
  231. } else {
  232. # XXX: Not sure, but I think that's the right thing to do
  233. common_log(LOG_WARNING,
  234. 'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
  235. __FILE__);
  236. return false;
  237. }
  238. }
  239. }
  240. }
  241. # Now, users who want everything
  242. $public = common_config('xmpp', 'public');
  243. # FIXME PRIV don't send out private messages here
  244. if ($public) {
  245. foreach ($public as $address) {
  246. common_log(LOG_INFO,
  247. 'Sending notice ' . $notice->id . ' to public listener ' . $address,
  248. __FILE__);
  249. jabber_send_notice($address, $notice);
  250. }
  251. }
  252. return true;
  253. }
  254. function jabber_format_notice(&$profile, &$notice) {
  255. return $profile->nickname . ': ' . $notice->content;
  256. }