You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.1 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. <?php
  2. /**
  3. * @file Jabber.php
  4. * This file is part of MOVIM.
  5. *
  6. * @brief Wrapper around Jaxl to handle mid-level functionalities
  7. *
  8. * @author Etenil <etenil@etenilsrealm.nl>
  9. *
  10. * @version 1.0
  11. * @date 13 October 2010
  12. *
  13. * Copyright (C)2010 Movim Project
  14. *
  15. * See COPYING for licensing information.
  16. */
  17. // Jabber external component setting
  18. //define('JAXL_COMPONENT_HOST', 'component.'.JAXL_HOST_DOMAIN);
  19. //define('JAXL_COMPONENT_PASS', 'pass');
  20. define('JAXL_COMPONENT_PORT', 5559);
  21. define('JAXL_LOG_PATH', BASE_PATH . 'log/jaxl.log');
  22. define('JAXL_LOG_EVENT', true);
  23. define('JAXL_LOG_ROTATE', false);
  24. define('JAXL_BASE_PATH', LIB_PATH . 'Jaxl/');
  25. include(LIB_PATH . 'Jaxl/core/jaxl.class.php');
  26. class Jabber
  27. {
  28. private static $instance;
  29. private $jaxl;
  30. private $payload;
  31. /**
  32. * Firing up basic parts of jaxl and setting variables.
  33. */
  34. private function __construct($jid)
  35. {
  36. $userConf = Conf::getUserConf($jid);
  37. $serverConf = Conf::getServerConf();
  38. $sess = Session::start(APP_NAME);
  39. $sess->remove('jid'); // ???
  40. $this->jaxl = new JAXL(array(
  41. // User Configuration
  42. 'host' => $userConf['host'],
  43. 'domain' => isset($userConf['domain']) ? $userConf['domain'] : $userConf['host'],
  44. 'boshHost' => $userConf['boshHost'],
  45. 'boshSuffix' => $userConf['boshSuffix'],
  46. 'boshPort' => $userConf['boshPort'],
  47. // Server configuration
  48. 'boshCookieTTL' => $serverConf['boshCookieTTL'],
  49. 'boshCookiePath' => $serverConf['boshCookiePath'],
  50. 'boshCookieDomain' => $serverConf['boshCookieDomain'],
  51. 'boshCookieHTTPS' => $serverConf['boshCookieHTTPS'],
  52. 'boshCookieHTTPOnly' => $serverConf['boshCookieHTTPOnly'],
  53. 'logLevel' => $serverConf['logLevel'],
  54. 'boshOut'=>false,
  55. ));
  56. // Loading required XEPS
  57. $this->jaxl->requires(array(
  58. 'JAXL0030', // Service Discovery
  59. 'JAXL0054', // VCard
  60. 'JAXL0115', // Entity Capabilities
  61. 'JAXL0133', // Service Administration
  62. 'JAXL0085', // Chat State Notification
  63. 'JAXL0092', // Software Version
  64. 'JAXL0203', // Delayed Delivery
  65. 'JAXL0202', // Entity Time
  66. 'JAXL0206' // Jabber over Bosh
  67. ));
  68. // Defining call-backs
  69. $this->jaxl->addPlugin('jaxl_post_auth', array(&$this, 'postAuth'));
  70. $this->jaxl->addPlugin('jaxl_post_auth_failure', array(&$this, 'postAuthFailure'));
  71. //$this->jaxl->addPlugin('jaxl_post_roster_update', array(&$this, 'postRosterUpdate'));
  72. $this->jaxl->addPlugin('jaxl_post_disconnect', array(&$this, 'postDisconnect'));
  73. $this->jaxl->addPlugin('jaxl_get_iq', array(&$this, 'handle'));
  74. $this->jaxl->addPlugin('jaxl_get_auth_mech', array(&$this, 'postAuthMech'));
  75. $this->jaxl->addPlugin('jaxl_get_message', array(&$this, 'getMessage'));
  76. $this->jaxl->addPlugin('jaxl_get_presence', array(&$this, 'getPresence'));
  77. $this->jaxl->addPlugin('jaxl_get_bosh_curl_error', array(&$this, 'boshCurlError'));
  78. $this->jaxl->addplugin('jaxl_get_empty_body', array(&$this, 'getEmptyBody'));
  79. }
  80. public function getInstance($jid = false)
  81. {
  82. if(!is_object(self::$instance)) {
  83. if(!$jid) {
  84. $user = new User();
  85. if(!$user->isLogged()) {
  86. throw new MovimException(t("Error: User not logged in."));
  87. } else {
  88. $jid = $user->getLogin();
  89. if($jid = "")
  90. throw new MovimException(t("Error: JID not provided."));
  91. }
  92. } else {
  93. self::$instance = new Jabber($jid);
  94. }
  95. }
  96. return self::$instance;
  97. }
  98. /**
  99. * Logs in
  100. */
  101. public function login($jid, $pass)
  102. {
  103. if(!$this->checkJid($jid)) {
  104. throw new MovimException(t("Error: jid '%s' is incorrect", $jid));
  105. } else {
  106. $id = explode('@',$jid);
  107. $user = $id[0];
  108. $userConf = $id[1];
  109. $domain = $id[1];
  110. $this->jaxl->user = $user;
  111. $this->jaxl->pass = $pass;
  112. $this->jaxl->startCore('bosh');
  113. }
  114. self::setStatus(false, false);
  115. }
  116. public function postAuth() {
  117. //$this->jaxl->getRosterList();
  118. //$this->jaxl->getVCard();
  119. }
  120. public function postAuthFailure() {
  121. $this->jaxl->shutdown();
  122. throw new MovimException("Login error.");
  123. $user = new User();
  124. $user->desauth();
  125. }
  126. public function boshCurlError() {
  127. // $this->jaxl->shutdown();
  128. // throw new MovimException("Bosh connection error.");
  129. // $user = new User();
  130. // $user->desauth();
  131. }
  132. /*
  133. * Auth mechanism (default : MD5)
  134. */
  135. public function postAuthMech($mechanism) {$this->jaxl->auth('DIGEST-MD5');}
  136. /**
  137. * Logs out
  138. */
  139. public function logout()
  140. {
  141. define('JAXL_CURL_ASYNC', true);
  142. $this->jaxl->JAXL0206('endStream');
  143. }
  144. public function postDisconnect($data)
  145. {
  146. $evt = new Event();
  147. $evt->runEvent('postdisconnected', $data);
  148. }
  149. /**
  150. * Pings the server. This must be done regularly in order to keep the
  151. * session running.
  152. */
  153. public function pingServer()
  154. {
  155. define('JAXL_CURL_ASYNC', false);
  156. $this->jaxl->JAXL0206('ping');
  157. }
  158. public function getEmptyBody($payload) {
  159. $evt = new Event();
  160. // Oooooh, am I disconnected??
  161. if(preg_match('/condition=[\'"]item-not-found[\'"]/', $payload)) {
  162. $evt->runEvent('serverdisconnect', null);
  163. } else {
  164. $evt->runEvent('incomingemptybody', 'ping');
  165. }
  166. }
  167. /**
  168. * Envents handlers methods
  169. */
  170. public function handle($payload) {
  171. $evt = new Event();
  172. if(isset($payload['vCard'])) { // Holy mackerel, that's a vcard!
  173. if(!is_null($payload['from'])) {
  174. Cache::c("vcard".$payload["from"], $payload);
  175. $evt->runEvent('vcardreceived', $payload);
  176. } else {
  177. Cache::c("myvcard", $payload);
  178. $evt->runEvent('myvcardreceived', $payload);
  179. }
  180. } elseif($payload['queryXmlns'] == "jabber:iq:roster") {
  181. Cache::c("roster", $payload);
  182. $evt->runEvent('rosterreceived', $payload);
  183. } else {
  184. $evt->runEvent('none', var_export($payload, true));
  185. }
  186. }
  187. /*public function postRosterUpdate($payload) {
  188. $evt = new Event();
  189. $evt->runEvent('rosterreceived', $payload);
  190. }*/
  191. /* vCard methods
  192. * Ask for a vCard and handle it
  193. */
  194. public function getVCard($jid = false)
  195. {
  196. define('JAXL_CURL_ASYNC', true);
  197. $this->jaxl->JAXL0054('getVCard', $jid, $this->jaxl->jid, false);
  198. }
  199. /*
  200. * Incoming messages
  201. */
  202. public function getMessage($payloads) {
  203. foreach($payloads as $payload) {
  204. // reject offline message
  205. if($payload['offline'] != JAXL0203::$ns && $payload['type'] == 'chat') {
  206. $evt = new Event();
  207. if($payload['chatState'] == 'active' && $payload['body'] == NULL) {
  208. $evt->runEvent('incomeactive', $payload);
  209. }
  210. elseif($payload['chatState'] == 'composing') {
  211. $evt->runEvent('incomecomposing', $payload);
  212. }
  213. else {
  214. $evt->runEvent('incomemessage', $payload);
  215. }
  216. }
  217. }
  218. }
  219. /*
  220. * Incoming presences
  221. */
  222. public function getPresence($payloads) {
  223. foreach($payloads as $payload) {
  224. if($payload['type'] == '' || in_array($payload['type'], array('available', 'unavailable'))) {
  225. $evt = new Event();
  226. //Cache::c('presence' . $payload['type'], $payload);
  227. if($payload['type'] == 'unavailable') {
  228. if($payload['from'] == $this->jaxl->jid)
  229. $evt->runEvent('postdisconnected', $data);
  230. else
  231. $evt->runEvent('incomeoffline', $payload);
  232. }
  233. elseif($payload['show'] == 'away') {
  234. $evt->runEvent('incomeaway', $payload);
  235. }
  236. elseif($payload['show'] == 'dnd') {
  237. $evt->runEvent('incomednd', $payload);
  238. }
  239. else {
  240. $evt->runEvent('incomeonline', $payload);
  241. }
  242. }
  243. }
  244. }
  245. /**
  246. * Fetches the roster's list and calls the provided processing function on
  247. * roster's return.
  248. * @param callback is a function that is called when the roster is returned
  249. * by the server.
  250. */
  251. public function getRosterList()
  252. {
  253. define('JAXL_CURL_ASYNC', true);
  254. $this->jaxl->getRosterList();
  255. }
  256. /**
  257. * Sets the session's status.
  258. */
  259. public function setStatus($status, $show)
  260. {
  261. define('JAXL_CURL_ASYNC', true);
  262. $this->jaxl->setStatus($status, $show, 41, true);
  263. }
  264. private function checkJid($jid)
  265. {
  266. return true; /*
  267. preg_match('/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\(?:.[a-z]{2,5})?$/',
  268. $jid); */
  269. }
  270. /**
  271. * Sends a message.
  272. */
  273. public function sendMessage($addressee, $body)
  274. {
  275. define('JAXL_CURL_ASYNC', true);
  276. // Checking on the jid.
  277. if($this->checkJid($addressee)) {
  278. $this->jaxl->sendMessage($addressee, $body, false, 'chat');
  279. } else {
  280. throw new MovimException("Error: Incorrect JID `$addressee'");
  281. }
  282. }
  283. /**
  284. * Adds a contact to the roster.
  285. */
  286. public function addContact($jid, $contact, $alias)
  287. {
  288. if($this->checkJid($jid)) {
  289. $this->jaxl->subscribe($jid);
  290. $this->jaxl->addRoster($jid, $contact, $alias);
  291. } else {
  292. throw new MovimException("Error: Incorrect JID `$jid'");
  293. }
  294. }
  295. }
  296. ?>