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 13 Nov 2008 19:14:52 -0000 @@ -121,11 +121,34 @@ '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', + '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,8 +160,8 @@ * @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); } /** @@ -318,10 +341,12 @@ /** * Build xmlrpc url based off the module parameters */ -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) { + 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 +354,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 13 Nov 2008 19:14:45 -0000 @@ -137,11 +137,80 @@ '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_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 object with some account credentials + * + * @param $account + * User account, will default to the current user + */ +function xmpp_api_get_user_connection($account = NULL) { + global $user; + + $account = $account ? $account : $user; + + // Get connection with this user's credentials if possible, or none + if (!empty($account->xmppclient)) { + $params['username'] = $account->xmppclient['user_name']; + $params['password'] = __xmppclient_password($account->xmppclient['password'], TRUE); + $params['server'] = $account->xmppclient['server']; + $params['resource'] = $account->xmppclient['resource']; + return _xmpp_api_get_connection($params); + } +} + +/** + * Get connection object with some account credentials + * + * @param $account + * User account, will default to the current user + */ +function _xmpp_api_user_connection($conn = NULL, $account = NULL) { + 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,12 +222,13 @@ * @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); + //_xmpp_api_disconnect($conn); + return TRUE; + } } /** @@ -173,9 +243,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($user = NULL, $type = 'available', $show = 'available', $status = 'Available', $conn = NULL) { + if ($conn = _xmpp_api_user_connection($conn)) { + $conn->presence($status, $show, NULL, $type); + return TRUE; + } else { + return FALSE; + } } /** @@ -237,14 +311,11 @@ * @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); - return FALSE; - } + // connecting to the xmpp server - $conn = _xmpp_api_connect(XMPP_API_ADMINJID, XMPP_API_ADMINJID_PASSWORD); + $conn = _xmpp_server_connection($conn); // 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); @@ -456,21 +527,19 @@ * @param $user * If not null will retrieve this users 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($user = NULL, $conn = NULL) { + // Get connection for this user + if ($conn = _xmpp_api_user_connection($conn, $user)) { + $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]; } /** @@ -497,22 +566,20 @@ * @param $user * User object */ -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 = NULL, $conn = NULL) { + // Check the connection, get a new one if not + if ($conn = $conn ? $conn : xmpp_api_get_user_connection($account)) { + $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; + } + return $packet[0][1]; } - return $packet[0][1]; } /** @@ -521,12 +588,13 @@ * @param $user * User object */ -function xmpp_api_get_user_resources($user = NULL) { - if (!$user) { - global $user; - } +function xmpp_api_get_user_resources($account = NULL) { + global $user; + + $account = $account ? $account : $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') { + if ($account->xmppclient['status'] && $account->xmppclient['status'] == 'available') { return 1; } return 0; @@ -539,33 +607,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'; +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]); } - 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']; + + // 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 $params + * Connection parameters. At the very least they should include: + * - jid and password + * or + * - username, password + * + * @return $conn + * XMPP Connection Object + */ +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); } - $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; } /** @@ -590,6 +687,38 @@ } /** + * Builds the XMPP Connection Object + * + * @param $jid + * Optional admin jid + * @param $password + * Optional admin 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; +} + +/** * Disconnect from the xmpp server * * @param $conn 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 13 Nov 2008 19:14:44 -0000 @@ -117,12 +117,12 @@ * @param $subject * Subject of the message */ -function xmppframework_send_message($to, $type = 'chat', $body = null, $subject = null) { +function xmppframework_send_message($to, $type = 'chat', $body = null, $subject = null, $connection = 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)) { + if (!call_user_func($func, $to, $type, $body, $subject, $connection)) { watchdog('xmppframework', 'Failed to send message to user %to', array('%to' => $to), WATCHDOG_ERROR); } } @@ -140,12 +140,12 @@ * @param $status * The status message for display */ -function xmppframework_set_presence($user, $type, $show, $status) { +function xmppframework_set_presence($user, $type, $show, $status, $connection = NULL) { $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)) { + if (!call_user_func($func, $user, $type, $show, $status, $connection)) { watchdog('xmppframework', 'Failed to set the xmpp presence for %to', array('%to' => $user->name), WATCHDOG_ERROR); } } @@ -184,12 +184,12 @@ * @param $title * The description name for the room */ -function xmppframework_create_muc($name, $service, $server, $title) { +function xmppframework_create_muc($name, $service, $server, $title, $connection = NULL) { $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)) { + if (!call_user_func($func, $name, $service, $server, $title, $connection)) { watchdog('xmppframework', 'Failed to create muc: %name', array('%name' => $name), WATCHDOG_ERROR); } } @@ -296,15 +296,16 @@ * * @return array with vcard information same structure as the array we send */ -function xmppframework_get_vcard($user = NULL) { - if (!$user) { - global $user; - } +function xmppframework_get_vcard($account = NULL, $connection = NULL) { + global $user; + + $account = $account ? $account : $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); + $data = call_user_func($func, $account, $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); @@ -348,12 +349,12 @@ * @param $user * User object */ -function xmppframework_get_roster($user = NULL) { +function xmppframework_get_roster($user = NULL, $connection = 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))) { + if (!($roster = call_user_func($func, $user, $connection))) { watchdog('xmppframework', 'Failed to retrieve roster', array(), WATCHDOG_ERROR); } } @@ -381,3 +382,51 @@ } return $resources; } + +/** + * 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 using account or current user credentials + */ +function xmppframework_get_user_connection($account = NULL) { + global $user; + + $account = $account ? $account : $user; + $conn = _xmppframework_api_invoke('get_user_connection', $account); + if (!$conn) { + watchdog('xmppframework', 'Failed to get user connection', array(), WATCHDOG_WARNING); + } +} + +/** + * Releases a connection + */ +function xmppframework_release_connection($conn) { + _xmppframework_api_invoke('release_connection', $conn); +} + +/** + * 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