Index: contrib/xmpp_xmlrpc/xmpp_xmlrpc.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/xmppframework/contrib/xmpp_xmlrpc/xmpp_xmlrpc.module,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 xmpp_xmlrpc.module --- contrib/xmpp_xmlrpc/xmpp_xmlrpc.module 7 Nov 2008 14:17:16 -0000 1.1.2.2 +++ contrib/xmpp_xmlrpc/xmpp_xmlrpc.module 18 Nov 2008 15:06:43 -0000 @@ -121,11 +121,35 @@ 'set_vcard' => 'xmpp_xmlrpc_set_vcard', 'get_roster' => 'xmpp_xmlrpc_get_roster', 'get_user_resources' => 'xmpp_xmlrpc_get_user_resources', + 'get_server_connection' => 'xmpp_xmlrpc_get_connection', + 'get_user_connection' => 'xmpp_xmlrpc_get_connection', + 'get_admin_connection' => 'xmpp_xmlrpc_get_connection', + 'release_connection' => 'xmpp_xmlrpc_release_connection', ); return $funcs[$op]; } /** + * Get connection parameters + * + * This is a connection-less method so we just pass around the connection parameters + */ +function xmpp_xmlrpc_get_connection() { + return array( + 'host' => XMPP_XMLRPC_URL, + 'port' => XMPP_XMLRPC_PORT, + 'query' => XMPP_XMLRPC_QUERY_STRING, + ); +} + +/** + * Foo function + */ +function xmpp_xmlrpc_release_connection() { + return TRUE; +} + +/** * Sending a message via XMLRPC interface * * @param $to @@ -137,15 +161,15 @@ * @param $subject * Subject of the message */ -function xmpp_xmlrpc_send_message($to, $type = 'chat', $body = null, $subject = null) { - return _xmpp_xmlrpc('send_message', array('from' => $to, 'to' => $to, 'subject' => $subject, 'body' => $body)); +function xmpp_xmlrpc_send_message($to, $type = 'chat', $body = null, $subject = null, $conn = NULL) { + return _xmpp_xmlrpc('send_message', array('from' => $to, 'to' => $to, 'subject' => $subject, 'body' => $body), $conn); } /** * Setting a users presence via XMLRPC interface * - * @param $user - * User Object we are setting the presence for + * @param $xmppaccount + * XMPP account we are setting the presence for * @param $type * Type of presence packet we are sending * @param $show @@ -153,15 +177,13 @@ * @param $status * Status message we will display to the users */ -function xmpp_xmlrpc_set_presence($user = NULL, $type = 'available', $show = 'available', $status = 'Available') { - if (!$user) { - global $user; - } +function xmpp_xmlrpc_set_presence($xmppaccount, $type = 'available', $show = 'available', $status = 'Available', $connection = NULL) { + // Return false if the user object does not have the jud and client information set - if (!isset($user->xmppclient['jid']) || !drupal_strlen($user->xmppclient['jid'])) { + if (!isset($xmppaccount['jid']) || !drupal_strlen($xmppaccount['jid'])) { return FALSE; } - return _xmpp_xmlrpc('set_presence', array('user' => $user->xmppclient['user_name'], 'server' => $user->xmppclient['server'], 'show' => $show, 'status' => $status, 'type' => $type)); + return _xmpp_xmlrpc('set_presence', array('user' => $xmppaccount['username'], 'server' => $xmppaccount['server'], 'show' => $show, 'status' => $status, 'type' => $type), $connection); } /** @@ -174,8 +196,8 @@ * @param $password * User Password */ -function xmpp_xmlrpc_delete_account($name, $host, $password) { - return _xmpp_xmlrpc('delete_account', array('user' => $user->name, 'host' => $host, 'password' => $password)); +function xmpp_xmlrpc_delete_account($name, $host, $password, $connection = NULL) { + return _xmpp_xmlrpc('delete_account', array('user' => $name, 'host' => $host, 'password' => $password), $connection); } /** @@ -190,8 +212,8 @@ * @param $title * The description name for the room */ -function xmpp_xmlrpc_create_muc($name, $service, $server, $title) { - $result = _xmpp_xmlrpc('create_muc_room', array('name' => $name, 'service' => $service, 'server' => $server)); +function xmpp_xmlrpc_create_muc($name, $service, $server, $title, $connection = NULL) { + $result = _xmpp_xmlrpc('create_muc_room', array('name' => $name, 'service' => $service, 'server' => $server), $connection); if ($result === FALSE) { watchdog('xmpp_xmlrpc', 'Errno: '. xmlrpc_errno() .' Message: '. xmlrpc_error_msg(), array(), WATCHDOG_ERROR); return $result; @@ -212,8 +234,8 @@ * @param $title * The description name for the room */ -function xmpp_xmlrpc_configure_muc($name, $service, $server, $title) { - return _xmpp_xmlrpc('muc_room_change_option', array('name' => $name, 'service' => $service, 'option' => 'title', 'value' => $title)); +function xmpp_xmlrpc_configure_muc($name, $service, $server, $title, $connection = NULL) { + return _xmpp_xmlrpc('muc_room_change_option', array('name' => $name, 'service' => $service, 'option' => 'title', 'value' => $title), $connection); } /** @@ -226,8 +248,8 @@ * @param $server * The server the muc will be created on */ -function xmpp_xmlrpc_delete_muc($name, $service, $server) { - return _xmpp_xmlrpc('delete_muc_room', array('name' => $name, 'service' => $service, 'server' => $server)); +function xmpp_xmlrpc_delete_muc($name, $service, $server, $connection = NULL) { + return _xmpp_xmlrpc('delete_muc_room', array('name' => $name, 'service' => $service, 'server' => $server), $connection); } /** @@ -248,8 +270,8 @@ * @param $subscription * Subscription i.e. both, to, from, none */ -function xmpp_xmlrpc_add_rosteritem($uname, $userver, $cname, $cserver, $nick, $group, $subscription) { - return _xmpp_xmlrpc('add_rosteritem', array('localuser' => $uname, 'localserver' => $userver, 'user' => $cname, 'server' => $cserver, 'nick' => $nick, 'group' => $group, 'subs' => $subscription)); +function xmpp_xmlrpc_add_rosteritem($uname, $userver, $cname, $cserver, $nick, $group, $subscription, $connection = NULL) { + return _xmpp_xmlrpc('add_rosteritem', array('localuser' => $uname, 'localserver' => $userver, 'user' => $cname, 'server' => $cserver, 'nick' => $nick, 'group' => $group, 'subs' => $subscription), $connection); } /** @@ -264,22 +286,20 @@ * @param $cserver * Contact server */ -function xmpp_xmlrpc_delete_rosteritem($uname, $userver, $cname, $cserver) { - return _xmpp_xmlrpc('delete_rosteritem', array('localuser' => $uname, 'localserver' => $userver, 'user' => $cname, 'server' => $cserver)); +function xmpp_xmlrpc_delete_rosteritem($uname, $userver, $cname, $cserver, $connection = NULL) { + return _xmpp_xmlrpc('delete_rosteritem', array('localuser' => $uname, 'localserver' => $userver, 'user' => $cname, 'server' => $cserver), $connection); } /** * Retrieving a user roster * - * @param $user - * User object + * @param $xmppuser + * XMPP User credentials */ -function xmpp_xmlrpc_get_roster($user = NULL) { - if (!$user) { - global $user; - } - $url = xmpp_xmlrpc_url(); - $result = xmlrpc($url, 'get_roster', array('user' => $user->xmppclient['user_name'], 'server' => $user->xmppclient['server'])); +function xmpp_xmlrpc_get_roster($xmppuser = NULL, $conn = NULL) { + + $url = xmpp_xmlrpc_url($conn); + $result = xmlrpc($url, 'get_roster', array('user' => $xmppuser['username'], 'server' => $xmppuser['server'])); if ($result === FALSE) { watchdog('xmpp_xmlrpc', 'Errno: '. xmlrpc_errno() .' Message: '. xmlrpc_error_msg(), array(), WATCHDOG_ERROR); return false; @@ -305,23 +325,24 @@ /** * Retrieving the number of online resources for a user * - * @param $user - * User object + * @param $account + * XMPP account */ -function xmpp_xmlrpc_get_user_resources($user = NULL) { - if (!$user) { - global $user; - } - return _xmpp_xmlrpc('num_resources', array('user' => $user->xmppclient['user_name'], 'host' => $user->xmppclient['server'])); +function xmpp_xmlrpc_get_user_resources($account, $connection = NULL) { + return _xmpp_xmlrpc('num_resources', array('user' => $account['username'], 'host' => $account['server']), $connection); } /** * Build xmlrpc url based off the module parameters + * + * Alternatively, if a connection is provided, will use the parameters for that connection */ -function xmpp_xmlrpc_url() { - $url = 'http://'. XMPP_XMLRPC_URL .':'. XMPP_XMLRPC_PORT .'/'; - if (strlen(XMPP_XMLRPC_QUERY_STRING) > 0) { - $url .= (strpos(XMPP_XMLRPC_QUERY_STRING, '/') == 0) ? substr(XMPP_XMLRPC_QUERY_STRING, 1) : XMPP_XMLRPC_QUERY_STRING; +function xmpp_xmlrpc_url($conn = NULL) { + list ($host, $port, $query_string) = $conn ? array($conn['host'], $conn['port'], $conn['query']) : xmpp_xmlrpc_get_connection(); + + $url = 'http://'. $host .':'. $port .'/'; + if (strlen($query_string) > 0) { + $url .= (strpos($query_string, '/') == 0) ? substr($query_string, 1) : $query_string; } return $url; } @@ -329,8 +350,8 @@ /** * Helper function. Make XMLRPC with error logging. */ -function _xmpp_xmlrpc($service, $params) { - $result = xmlrpc(xmpp_xmlrpc_url(), $service, $params); +function _xmpp_xmlrpc($service, $params, $connection = NULL) { + $result = xmlrpc(xmpp_xmlrpc_url($connection), $service, $params); if ($result === FALSE) { watchdog('xmpp_xmlrpc', 'Errno: '. xmlrpc_errno() .' Message: '. xmlrpc_error_msg(), array(), WATCHDOG_ERROR); return FALSE; Index: contrib/xmpp_api/xmpp_api.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/xmppframework/contrib/xmpp_api/xmpp_api.module,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 xmpp_api.module --- contrib/xmpp_api/xmpp_api.module 7 Nov 2008 14:17:14 -0000 1.1.2.2 +++ contrib/xmpp_api/xmpp_api.module 18 Nov 2008 15:06:43 -0000 @@ -137,11 +137,84 @@ 'set_vcard' => 'xmpp_api_set_vcard', 'get_roster' => 'xmpp_api_get_roster', 'get_user_resources' => 'xmpp_api_get_user_resources', + 'get_server_connection' => 'xmpp_api_get_server_connection', + 'get_admin_connection' => 'xmpp_api_get_server_connection', + 'get_user_connection' => 'xmpp_api_get_user_connection', + 'release_connection' => 'xmpp_api_release_connection', ); return $funcs[$op]; } /** + * Get connection object with this server credentials + */ +function xmpp_api_get_server_connection($params = array()) { + if (!strlen(XMPP_API_ADMINJID) || !strlen(XMPP_API_ADMINJID_PASSWORD)) { + watchdog('xmpp_api', 'Admin JID or Password is not set for server administration', array(), WATCHDOG_ERROR); + return false; + } + // connecting to the xmpp server + $params += array( + 'jid' => XMPP_API_ADMINJID, + 'password' => XMPP_API_ADMINJID_PASSWORD, + 'server' => XMPP_API_ADMINJID_SERVER, + 'timeout' => 30, + 'persistent' => FALSE, + ); + return _xmpp_api_get_connection($params); +} + +/** + * Get connection object with this server credentials + */ +function _xmpp_api_server_connection($conn = NULL) { + return $conn ? $conn : xmpp_api_get_server_connection(); +} + +/** + * Get connection with admin permissions + * + * In this case it will be the same as a generic server connection so this is just a wrapper + */ +function _xmpp_api_admin_connection($conn = NULL) { + return _xmpp_api_server_connection($conn); +} + + +/** + * Get connection object with some account credentials + * + * @param $xmppaccount + * User account, will default to the current user + */ +function xmpp_api_get_user_connection($xmppaccount) { + if ($conn = _xmpp_api_get_connection($xmppaccount)) { + return $conn; + } else { + return FALSE; + } +} + +/** + * Get connection object with some account credentials + * + * @param $account + * XMPP account + */ +function _xmpp_api_user_connection($conn, $account) { + return $conn ? $conn : xmpp_api_get_user_connection($account); +} + +/** + * Release connection and free up memory + */ +function xmpp_api_release_connection($conn) { + if ($conn) { + _xmpp_api_disconnect($conn); + } +} + +/** * Sending a message via XMPP * * @param $to @@ -153,19 +226,21 @@ * @param $subject * Subject of the message */ -function xmpp_api_send_message($to, $type = 'chat', $body = null, $subject = null) { +function xmpp_api_send_message($to, $type = 'chat', $body = null, $subject = null, $conn = NULL) { // connect to the server, send the message and disconnect from the server - $conn = _xmpp_api_connect(); - $conn->message($to, $body, $type, $subject); - _xmpp_api_disconnect($conn); - return TRUE; + if ($conn = _xmpp_api_server_connection($conn)) { + $conn->message($to, $body, $type, $subject); + return TRUE; + } else { + return FALSE; + } } /** * Function for setting the users presence in the system * - * @param $user - * User Object we wish to set presence for + * @param $account + * XMPP account data * @param $type * Type of presence available, unavailable * @param $show @@ -173,9 +248,13 @@ * @param $status * Status message for the user */ -function xmpp_api_set_presence($user = NULL, $type = 'available', $show = 'available', $status = 'Available') { - // Currently on a pure xmpp api this functionality cannot be performed - return TRUE; +function xmpp_api_set_presence($account, $type = 'available', $show = 'available', $status = 'Available', $conn = NULL) { + if ($conn = _xmpp_api_user_connection($conn, $account)) { + $conn->presence($status, $show, NULL, $type); + return TRUE; + } else { + return FALSE; + } } /** @@ -188,17 +267,12 @@ * @param $password * User Password */ -function xmpp_api_delete_account($name, $host, $password) { - // Verify if we have the admin jid and password information for the xmpp server - if (!strlen(XMPP_API_ADMINJID) || !strlen(XMPP_API_ADMINJID_PASSWORD)) { - watchdog('xmpp_api', 'Admin JID or Password is not set for server administration', array(), WATCHDOG_ERROR); +function xmpp_api_delete_account($name, $host, $password, $conn = NULL) { + if (!$conn = _xmpp_api_admin_connection($conn)) { return FALSE; } $jid = $name .'@'. $host; - // connecting to the server, deleting the roster item and disconnecting from the server - $conn = _xmpp_api_connect(XMPP_API_ADMINJID, XMPP_API_ADMINJID_PASSWORD); - // sending delete user form per XEP-0133 specification so we can retrieve the information $conn->deleteUserForm($host); $payload = $conn->processUntil('form_returned', XMPP_API_PROCESS_TIMEOUT); @@ -206,7 +280,6 @@ $packet = $payload[0][1]; // checking if the packet is an array or not if (!is_array($packet)) { - _xmpp_api_disconnect($conn); watchdog('xmpp_api', '@name does not have the correct privileges to perform server administration via ad-hoc commands', array('@name' => XMPP_API_ADMINJID), WATCHDOG_ERROR); return FALSE; } @@ -214,8 +287,7 @@ $packet['accountjids']['data'] = $jid; $conn->deleteUser($packet); $payload = $conn->processUntil('user_deleted', XMPP_API_PROCESS_TIMEOUT); - // disconnecting from the xmpp server - _xmpp_api_disconnect($conn); + // error occurred while setting the user name if ($payload[0][1] != 'result') { watchdog('xmpp_api', 'Error occurred while removing the user account', array(), WATCHDOG_ERROR); @@ -237,14 +309,13 @@ * @param $title * The description name for the room */ -function xmpp_api_create_muc($name, $service, $server, $title) { +function xmpp_api_create_muc($name, $service, $server, $title, $conn = NULL) { // implemented via XEP-0045 for the creation of the room - if (!strlen(XMPP_API_ADMINJID) || !strlen(XMPP_API_ADMINJID_PASSWORD)) { - watchdog('xmpp_api', 'Admin JID or Password is not set for server administration', array(), WATCHDOG_ERROR); + + // connecting to the xmpp server + if (!$conn = _xmpp_api_server_connection($conn)) { return FALSE; } - // connecting to the xmpp server - $conn = _xmpp_api_connect(XMPP_API_ADMINJID, XMPP_API_ADMINJID_PASSWORD); // setting the room including nickname we will utilize $room = $name .'@'. $service .'/xwchat-drupal'; @@ -282,7 +353,7 @@ $conn->createMucRoomFormSend($room, $packet); $payload = $conn->processUntil('muc_configured', XMPP_API_PROCESS_TIMEOUT); // disconnect from the xmpp server - _xmpp_api_disconnect($conn); + //_xmpp_api_disconnect($conn); if ($payload[0][1] != 'result') { watchdog('xmpp_api', 'Configuration options specified by the user are unacceptable to the room', array(), WATCHDOG_ERROR); @@ -295,14 +366,13 @@ /** * Configuring muc via XMPP */ -function xmpp_api_configure_muc($name, $service, $server, $title) { +function xmpp_api_configure_muc($name, $service, $server, $title, $conn = NULL) { // implemented via XEP-0045 for the creation of the room - if (!strlen(XMPP_API_ADMINJID) || !strlen(XMPP_API_ADMINJID_PASSWORD)) { - watchdog('xmpp_api', 'Admin JID or Password is not set for server administration', array(), WATCHDOG_ERROR); + + // connecting to the xmpp server + if (!$conn = _xmpp_api_server_connection($conn)) { return FALSE; } - // connecting to the xmpp server - $conn = _xmpp_api_connect(XMPP_API_ADMINJID, XMPP_API_ADMINJID_PASSWORD); // setting the room information regarding the muc $room = $name .'@'. $service; @@ -313,7 +383,6 @@ $packet = $payload[0][1]; // if we are not returned an array we know there was an error in the configuration if (!is_array($packet)) { - _xmpp_api_disconnect($conn); watchdog('xmpp_api', '@name does not have privileges to configure muc rooms', array('@name' => XMPP_API_ADMINJID), WATCHDOG_ERROR); return FALSE; } @@ -328,8 +397,6 @@ $conn->createMucRoomFormSend($room, $packet); $conn->processUntil('muc_configured', XMPP_API_PROCESS_TIMEOUT); - // disconnect from the xmpp server - _xmpp_api_disconnect($conn); if ($payload[0][1] != 'result') { watchdog('xmpp_api', 'Configuration options specified by the user are unacceptable to the room', array(), WATCHDOG_ERROR); return FALSE; @@ -349,21 +416,19 @@ * @param $server * The server the muc will be removed from */ -function xmpp_api_delete_muc($name, $service, $server) { +function xmpp_api_delete_muc($name, $service, $server, $conn = NULL) { // implemented via XEP-0045 for the removal of the room - if (!strlen(XMPP_API_ADMINJID) || !strlen(XMPP_API_ADMINJID_PASSWORD)) { - watchdog('xmpp_api', 'Admin JID or Password is not set for server administration', array(), WATCHDOG_ERROR); + + // connecting to the xmpp server + if (!$conn = _xmpp_api_server_connection($conn)) { return FALSE; } - // connecting to the xmpp server - $conn = _xmpp_api_connect(XMPP_API_ADMINJID, XMPP_API_ADMINJID_PASSWORD); // setting the room variable with full service information then sending delete packet $room = $name .'@'. $service; $conn->removeMucRoom($room, t('Removal via xmpp api drupal')); $payload = $conn->processUntil('muc_removed', XMPP_API_PROCESS_TIMEOUT); - // disconnect from the xmpp server - _xmpp_api_disconnect($conn); + if ($payload[0][1] != 'result') { watchdog('xmpp_api', 'MUC could not be removed from the xmpp server', array(), WATCHDOG_ERROR); return FALSE; @@ -395,24 +460,27 @@ watchdog('xmppframework', 'Contact name or server not received', array(), WATCHDOG_ERROR); return FALSE; } + + // Connecting to the xmpp server with user credentials + if (!$conn = _xmpp_api_user_connection($conn)) { + return FALSE; + } + // setting the jid of the contact we will add to our roster $jid = $cname .'@'.$cserver; if (!$nick) { $nick = $jid; } // connecting to the server, deleting the roster item and disconnecting from the server - $conn = _xmpp_api_connect(); $conn->presence(); // sending presence packet $conn->addRosterContact($jid, $nick, array($group)); $payload = $conn->processUntil('contact_added', XMPP_API_PROCESS_TIMEOUT); // doing error checking to make sure the contact was loaded if ($payload[0][1] != 'result') { watchdog('xmpp_api', 'Could not add user @jid to the roster, error occurred', array('@jid' => $jid), WATCHDOG_ERROR); - _xmpp_api_disconnect($conn); return FALSE; } // now send the subscribe presence otherwise we wont have any subscription $conn->presence(NULL, NULL, $jid, 'subscribe'); - _xmpp_api_disconnect($conn); return TRUE; } @@ -433,14 +501,19 @@ watchdog('xmppframework', 'Contact name or server not received', array(), WATCHDOG_ERROR); return FALSE; } + + // Connecting to the xmpp server with user credentials + if (!$conn = _xmpp_api_user_connection($conn)) { + return FALSE; + } + // setting the jid of the contact we will remove from our roster $jid = $cname .'@'. $cserver; - // connecting to the server, deleting the roster item and disconnecting from the server - $conn = _xmpp_api_connect(); + // deleting the roster item $conn->presence(); // sending presence packet $conn->deleteRosterContact($jid); $payload = $conn->processUntil('contact_removed', XMPP_API_PROCESS_TIMEOUT); - _xmpp_api_disconnect($conn); + // error checking to make sure the packet returned was not an error packet if ($payload[0][1] != 'result') { @@ -453,39 +526,42 @@ /** * Retrieve the users vcard via XMPP * - * @param $user - * If not null will retrieve this users account + * @param $account + * XMPP account */ -function xmpp_api_get_vcard($user = NULL) { - // check if the user has a username and password - $jid = ($user->xmppclient['jid']) ? $user->xmppclient['jid'] : NULL; - $pass = ($user->xmppclient['password']) ? __xmppclient_password($user->xmppclient['password'], TRUE) : NULL; - - $conn = _xmpp_api_connect($jid, $pass); - $conn->getVCard(); - $packet = $conn->processUntil('vcard_received', XMPP_API_PROCESS_TIMEOUT); - _xmpp_api_disconnect($conn); - if (!is_array($packet[0][1])) { - watchdog('xmpp_api', 'Could not retrieve users vcard from the xmpp server', array(), WATCHDOG_ERROR); - return FALSE; +function xmpp_api_get_vcard($account, $conn = NULL) { + // Get connection for this user + if ($conn = _xmpp_api_user_connection($conn, $account)) { + $conn->getVCard(); + $packet = $conn->processUntil('vcard_received', XMPP_API_PROCESS_TIMEOUT); + //_xmpp_api_disconnect($conn); + if (!is_array($packet[0][1])) { + watchdog('xmpp_api', 'Could not retrieve users vcard from the xmpp server', array(), WATCHDOG_ERROR); + return FALSE; + } + return $packet[0][1]; } - - return $packet[0][1]; } /** * Send the users vcard via XMPP + * + * @param $account + * XMPP account */ -function xmpp_api_set_vcard($vcard = array()) { - global $user; - $conn = _xmpp_api_connect(); +function xmpp_api_set_vcard($account, $vcard = array(), $conn = NULL) { + + // Connecting to the xmpp server with user credentials + if (!$conn = _xmpp_api_user_connection($conn, $account)) { + return FALSE; + } + $conn->sendVCard($vcard); $payload = $conn->processUntil('vcard_saved', XMPP_API_PROCESS_TIMEOUT); - _xmpp_api_disconnect($conn); // checking for errors that might have occurred if ($payload[0][1] != 'result') { - watchdog('xmpp_api', 'Could not set @name vcard, error occurred', array('@name' => $user->xmppclient['jid']), WATCHDOG_ERROR); + watchdog('xmpp_api', 'Could not set @name vcard, error occurred', array('@name' => $account['jid']), WATCHDOG_ERROR); return FALSE; } return TRUE; @@ -494,42 +570,33 @@ /** * Retrieve a users roster * - * @param $user - * User object + * @param $account + * XMPP account */ -function xmpp_api_get_roster($user = NULL) { - // check if the user has a username and password - $jid = ($user->xmppclient['jid']) ? $user->xmppclient['jid'] : NULL; - $pass = ($user->xmppclient['password']) ? __xmppclient_password($user->xmppclient['password'], TRUE) : NULL; - - $conn = _xmpp_api_connect($jid, $pass); - $conn->getRoster(); - $packet = $conn->processUntil('roster_received', XMPP_API_PROCESS_TIMEOUT); - _xmpp_api_disconnect($conn); - - // error checking to make sure we receive the roster and not an error - if (!is_array($packet[0][1])) { - watchdog('xmpp_api', 'Could not retrieve the users roster from the system', array(), WATCHDOG_ERROR); - return FALSE; +function xmpp_api_get_roster($account, $conn = NULL) { + // Check the connection, get a new one if not + if ($conn = _xmpp_api_user_connection($conn, $account)) { + $conn->getRoster(); + $packet = $conn->processUntil('roster_received', XMPP_API_PROCESS_TIMEOUT); + + // error checking to make sure we receive the roster and not an error + if (!is_array($packet[0][1])) { + watchdog('xmpp_api', 'Could not retrieve the users roster from the system', array(), WATCHDOG_ERROR); + return FALSE; + } + return $packet[0][1]; } - return $packet[0][1]; } /** * Retrieve the number of online resources for a user * - * @param $user - * User object + * @param $account + * XMPP account */ -function xmpp_api_get_user_resources($user = NULL) { - if (!$user) { - global $user; - } - // going based off the database information currently since need to write the ejabberd piece for it using pure api - if ($user->xmppclient['status'] && $user->xmppclient['status'] == 'available') { - return 1; - } - return 0; +function xmpp_api_get_user_resources($account = NULL) { + // We need to write the ejabberd piece for it using pure api + // In the meanwhile, it will be handled by the xmppframework } /*********************************************************************************************************** @@ -539,54 +606,62 @@ /** * Builds the XMPP Connection Object * - * @param $jid - * Optional admin jid - * @param $password - * Optional admin password + * @param $params + * Connection parameters. At the very least they should include: + * - jid and password + * or + * - username, password * * @return $conn * XMPP Connection Object + * */ -function _xmpp_api_build_conn($jid = NULL, $password = NULL) { - // if we received a username and password use them to log - if ($jid && $password) { - $pieces = explode('@', $jid); - $username = $pieces[0]; - $server = $pieces[1]; - $resource = 'drupal-server'; - } - else { - // no username or password received so use the global user - global $user; - $username = $user->xmppclient['user_name']; - $password = __xmppclient_password($user->xmppclient['password'], TRUE); - $server = $user->xmppclient['server']; - $resource = $user->xmppclient['resource']; - } - $conn = new XMPPHP_XMPP(XMPP_API_SERVER, XMPP_API_PORT, $username, $password, $resource, $server, $printlog=false, $loglevel=XMPPHP_Log::LEVEL_VERBOSE); - $conn->autoSubscribe(); - return $conn; +function _xmpp_api_build_connection($params) { + // when we are doing the connect make sure at this point we load the XMPPHP file + module_load_include('php', 'xmpp_api', 'vendor/XMPPHP/XMPP'); + + // if we received a username and password use them + if (!empty($params['jid']) && !empty($params['password'])) { + $pieces = explode('@', $params['jid']); + $params += array('username' => $pieces[0], 'server' => $pieces[1]); + } + + // Check minimum parameters and add defaults + if (!empty($params['username']) && !empty($params['password'])) { + $params += array('resource' => 'drupal-server', 'server' => NULL, 'printlog' => FALSE, 'loglevel' => XMPPHP_Log::LEVEL_VERBOSE); + $conn = new XMPPHP_XMPP(XMPP_API_SERVER, XMPP_API_PORT, $params['username'], $params['password'], $params['resource'], $params['server'], $params['printlog'], $params['loglevel']); + $conn->autoSubscribe(); + return $conn; + } else { + return FALSE; + } } /** * Connect to the XMPP server and return the connection * - * @param $jid - * Optional admin jid - * @param $password - * Optional admin password + * @param $params + * Connection parameters. At the very least they should include: + * - jid and password + * or + * - username, password * * @return $conn * XMPP Connection Object */ -function _xmpp_api_connect($jid = NULL, $password = NULL) { - // when we are doing the connect make sure at this point we load the XMPPHP file - module_load_include('php', 'xmpp_api', 'vendor/XMPPHP/XMPP'); - - $conn = _xmpp_api_build_conn($jid, $password); - $conn->connect(); - $conn->processUntil('session_start'); - return $conn; +function _xmpp_api_get_connection($params) { + // Add some defaults + $params += array( + 'timeout' => 30, + 'persistent' => FALSE, + ); + if ($conn = _xmpp_api_build_connection($params)) { + $conn->connect($params['timeout'], $params['persistent']); // Persistent + $conn->processUntil('session_start'); + return $conn; + } else { + watchdog('xmpp_api', 'Failed to get connection', array(), WATCHDOG_WARNING); + } } /** Index: xmppframework.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/xmppframework/xmppframework.module,v retrieving revision 1.1.2.4 diff -u -r1.1.2.4 xmppframework.module --- xmppframework.module 7 Nov 2008 14:17:14 -0000 1.1.2.4 +++ xmppframework.module 18 Nov 2008 15:06:42 -0000 @@ -117,15 +117,11 @@ * @param $subject * Subject of the message */ -function xmppframework_send_message($to, $type = 'chat', $body = null, $subject = null) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'send_message'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $to, $type, $body, $subject)) { - watchdog('xmppframework', 'Failed to send message to user %to', array('%to' => $to), WATCHDOG_ERROR); - } - } +function xmppframework_send_message($to, $type = 'chat', $body = null, $subject = null, $connection = NULL) { + if (_xmppframework_api_invoke('send_message', $to, $type, $body, $subject, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to send message to user %to', array('%to' => $to), WATCHDOG_ERROR); } } @@ -140,16 +136,31 @@ * @param $status * The status message for display */ -function xmppframework_set_presence($user, $type, $show, $status) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'set_presence'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $user, $type, $show, $status)) { - watchdog('xmppframework', 'Failed to set the xmpp presence for %to', array('%to' => $user->name), WATCHDOG_ERROR); - } - } - } +function xmppframework_set_user_presence($user, $type, $show, $status, $connection = NULL) { + if (_xmppframework_user_api_invoke('set_presence', $user, $type, $show, $status, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to set the xmpp presence for %to', array('%to' => $user->name), WATCHDOG_ERROR); + } +} + +/** + * + * @param $account + * XMPP account we are setting presence for + * @param $type + * Type, unavailable or empty + * @param $show + * The show status for the user + * @param $status + * The status message for display + */ +function xmppframework_set_presence($account, $type, $show, $status, $connection = NULL) { + if (_xmppframework_api_invoke('set_presence', $account, $type, $show, $status, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to set the xmpp presence for %to', array('%to' => $account['jid']), WATCHDOG_ERROR); + } } /** @@ -162,15 +173,11 @@ * @param $password * User Password */ -function xmppframework_delete_account($name, $host, $password) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'delete_account'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $name, $host, $password)) { - watchdog('xmppframework', 'Failed to delete %user account from server', array('%user' => $name), WATCHDOG_ERROR); - } - } +function xmppframework_delete_account($name, $host, $password, $connection = NULL) { + if (_xmppframework_api_invoke('delete_account', $name, $host, $password, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to delete %user account from server', array('%user' => $name), WATCHDOG_ERROR); } } @@ -184,16 +191,12 @@ * @param $title * The description name for the room */ -function xmppframework_create_muc($name, $service, $server, $title) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'create_muc'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $name, $service, $server, $title)) { - watchdog('xmppframework', 'Failed to create muc: %name', array('%name' => $name), WATCHDOG_ERROR); - } - } - } +function xmppframework_create_muc($name, $service, $server, $title, $connection = NULL) { + if (_xmppframework_api_invoke('create_muc', $name, $service, $server, $title, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to create muc: %name', array('%name' => $name), WATCHDOG_ERROR); + } } /** @@ -206,16 +209,12 @@ * @param $title * New title for the muc */ -function xmppframework_configure_muc($name, $service, $server, $title) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'configure_muc'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $name, $service, $server, $title)) { - watchdog('xmppframework', 'Failed to configure muc: %name', array('%name' => $name), WATCHDOG_ERROR); - } - } - } +function xmppframework_configure_muc($name, $service, $server, $title, $connection = NULL) { + if (_xmppframework_api_invoke('configure_muc', $name, $service, $server, $title, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to configure muc: %name', array('%name' => $name), WATCHDOG_ERROR); + } } /** @@ -226,16 +225,12 @@ * @param $server * The server the muc will be created on */ -function xmppframework_delete_muc($name, $service, $server) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'delete_muc'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $name, $service, $server)) { - watchdog('xmppframework', 'Failed to delete muc: %name', array('%name' => $name), WATCHDOG_ERROR); - } - } - } +function xmppframework_delete_muc($name, $service, $server, $connection = NULL) { + if (_xmppframework_api_invoke('delete_muc', $name, $service, $server, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to delete muc: %name', array('%name' => $name), WATCHDOG_ERROR); + } } /** @@ -254,16 +249,12 @@ * @param $subscription * Subscription i.e. both, to, from, none */ -function xmppframework_add_rosteritem($uname, $userver, $cname, $cserver, $nick, $group, $subscription) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'add_rosteritem'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $uname, $userver, $cname, $cserver, $nick, $group, $subscription)) { - watchdog('xmppframework', 'Failed to add %cname to %uname roster', array('%cname' => $cname, '%uname' => $uname), WATCHDOG_ERROR); - } - } - } +function xmppframework_add_rosteritem($uname, $userver, $cname, $cserver, $nick, $group, $subscription, $connection = NULL) { + if (_xmppframework_api_invoke('add_rosteritem', $uname, $userver, $cname, $cserver, $nick, $group, $subscription, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to add %cname to %uname roster', array('%cname' => $cname, '%uname' => $uname), WATCHDOG_ERROR); + } } /** @@ -276,16 +267,12 @@ * @param $cserver * Contact server */ -function xmppframework_delete_rosteritem($uname, $userver, $cname, $cserver) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'delete_rosteritem'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $uname, $userver, $cname, $cserver)) { - watchdog('xmppframework', 'Failed to delete %cname from %uname roster', array('%cname' => $cname, '%uname' => $uname), WATCHDOG_ERROR); - } - } - } +function xmppframework_delete_rosteritem($uname, $userver, $cname, $cserver, $connection = NULL) { + if (_xmppframework_api_invoke('delete_rosteritem', $uname, $userver, $cname, $cserver, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to delete %cname from %uname roster', array('%cname' => $cname, '%uname' => $uname), WATCHDOG_ERROR); + } } /** @@ -296,28 +283,22 @@ * * @return array with vcard information same structure as the array we send */ -function xmppframework_get_vcard($user = NULL) { - if (!$user) { - global $user; - } - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'get_vcard'); - if ($func && function_exists($func)) { - $data = call_user_func($func); - // if data is not an array then there was an error when trying to retrieve the users vcard - if (!is_array($data)) { - watchdog('xmppframework', 'Failed to retrieve %user vcard from server', array('%user' => $user->name), WATCHDOG_ERROR); - } - } +function xmppframework_get_user_vcard($user = NULL, $connection = NULL) { + $data = _xmppframework_user_api_invoke('get_vcard', $user, $connection); + // if data is not an array then there was an error when trying to retrieve the users vcard + if (!is_array($data)) { + watchdog('xmppframework', 'Failed to retrieve %user vcard from server', array('%user' => $user->name), WATCHDOG_ERROR); + return FALSE; + } else { return $data; - } - return false; + } } /** * Sets users vcard in the system * + * @param $user + * User to set the vcard for * @param $vcard * Array containing the vcard * @@ -329,16 +310,11 @@ * $vcard['bday'] = '02/12/2001'; * */ -function xmppframework_set_vcard($vcard = array()) { - global $user; - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'set_vcard'); - if ($func && function_exists($func)) { - if (!call_user_func($func, $vcard)) { - watchdog('xmppframework', 'Failed to set %user vcard', array('%user' => $user->name), WATCHDOG_ERROR); - } - } +function xmppframework_set_user_vcard($user = NULL, $vcard = array(), $connection = NULL) { + if (_xmppframework_api_invoke('set_vcard', $user, $vcard, $connection)) { + return TRUE; + } else { + watchdog('xmppframework', 'Failed to set %user vcard', array('%user' => $user->name), WATCHDOG_ERROR); } } @@ -348,17 +324,12 @@ * @param $user * User object */ -function xmppframework_get_roster($user = NULL) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'get_roster'); - if ($func && function_exists($func)) { - if (!($roster = call_user_func($func, $user))) { - watchdog('xmppframework', 'Failed to retrieve roster', array(), WATCHDOG_ERROR); - } - } +function xmppframework_get_user_roster($user = NULL, $connection = NULL) { + if ($roster = _xmppframework_user_api_invoke('get_roster', $user, $connection)) { + return $roster; + } else { + watchdog('xmppframework', 'Failed to retrieve roster', array(), WATCHDOG_ERROR); } - return $roster; } /** @@ -369,15 +340,124 @@ * */ function xmppframework_get_user_resources($user = NULL) { - $info = xmppframework_apis(XMPPFRAMEWORK_API); - if ($info && function_exists($info['operation'])) { - $func = call_user_func($info['operation'], 'get_user_resources'); - if ($func && function_exists($func)) { - $resources = call_user_func($func, $user); - if (!is_numeric($resources)) { - watchdog('xmppframework', 'Failed to retrieve the number of user resources', array(), WATCHDOG_ERROR); - } - } + $resources = _xmppframework_user_api_invoke('get_user_resources', $user); + if (is_numeric($resources)) { + return $resources; + } + elseif ($account = xmppframework_user_account($user)) { + // If the api doesn't provide it, default to the stored status for the user account + return !empty($account['status']) && $account['status'] == 'available' ? 1 : 0; + } + else { + watchdog('xmppframework', 'Failed to retrieve the number of user resources', array(), WATCHDOG_ERROR); + } +} + +/** + * Gets a connection using server credentials + */ +function xmppframework_get_server_connection($params = array()) { + $conn = _xmppframework_api_invoke('get_server_connection', $params); + if (!$conn) { + watchdog('xmppframework', 'Failed to get server connection', array(), WATCHDOG_WARNING); + } + return $conn; +} + +/** + * Gets a connection with admin credentials + */ +function xmppframework_get_admin_connection($params = array()) { + $conn = _xmppframework_api_invoke('get_admin_connection', $params); + if (!$conn) { + watchdog('xmppframework', 'Failed to get admin connection', array(), WATCHDOG_WARNING); + } + return $conn; +} + +/** + * Gets a connection using account or current user credentials + */ +function xmppframework_get_user_connection($account = NULL) { + if ($conn = _xmppframework_user_api_invoke('get_user_connection', $account)) { + return $conn; + } else { + watchdog('xmppframework', 'Failed to get user connection for %name', array('%name' => $account->name), WATCHDOG_WARNING, l($account->name, 'user/' . $account->uid)); + } +} + +/** + * Releases a connection + */ +function xmppframework_release_connection($conn) { + _xmppframework_api_invoke('release_connection', $conn); +} + +/** + * Translates user account into XMPP account credentials + * + * @param $account + * Drupal user account. If none, will default to current user + * @return array() + * XMPP account credentials, with these elements: + * - jid + * - username + * - password + * - server + * - resource + */ +function xmppframework_user_account($account = NULL) { + global $user; + + // This is the only place where we default to current user if account not passed + $account = $account ? $account : $user; + + // Atm we only have xmppclient account data + if (!empty($account->xmppclient)) { + $xmppaccount = $account->xmppclient; + // Some parameters need aditional mappings + $xmppaccount['username'] = $account->xmppclient['user_name']; + $xmppaccount['password'] = __xmppclient_password($account->xmppclient['password'], TRUE); + $xmppaccount['account'] = $account; // Add the full account here for further reference + return $xmppaccount; + } else { + return array(); + } +} + +/** + * Invoke API function for a user account + * + * This is a wrapper for _api_invoke() which takes a user account as first parameter + */ +function _xmppframework_user_api_invoke() { + global $user; + + $args = func_get_args(); + + // Convert the first parameter from user account into xmpp account. Will default to current user + $account = !empty($args[1]) ? $args[1] : $user; + if ($xmppaccount = xmppframework_user_account($account)) { + $args[1] = $xmppaccount; + return call_user_func_array('_xmppframework_api_invoke', $args); + } else { + watchdog('xmppframework', 'Cannot get XMPP account for user %name', array('%name' => $account->name), WATCHDOG_WARNING, l($account->name, 'user/' . $account->uid)); } - return $resources; } + +/** + * Invoke API function + */ +function _xmppframework_api_invoke() { + $args = func_get_args(); + $operation = array_shift($args); + $info = xmppframework_apis(XMPPFRAMEWORK_API); + if ($info && function_exists($info['operation']) && + ($function = call_user_func($info['operation'], $operation)) && function_exists($function)) + { + return call_user_func_array($function, $args); + } + else { + watchdog('xmppframework', 'Operation not supported by %api-name API: %operation', array('%api-name' => XMPPFRAMEWORK_API, '%operation' => $operation), WATCHDOG_WARNING); + } +} \ No newline at end of file