/[drupal]/contributions/modules/xmpp/xmpp.module
ViewVC logotype

Diff of /contributions/modules/xmpp/xmpp.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3 by mkhitrov, Wed Aug 15 17:16:20 2007 UTC revision 1.4 by mkhitrov, Mon Aug 20 03:17:29 2007 UTC
# Line 13  Line 13 
13   * Implementation of hook_help().   * Implementation of hook_help().
14   */   */
15  function xmpp_help($section) {  function xmpp_help($section) {
16          switch ($section) {    switch ($section) {
17                  case 'admin/help#xmpp':      case 'admin/help#xmpp':
18                          return '<p>' . t('        return '<p>' . t('
19                                  The XMPP module provides other Drupal components with a way to          The XMPP module provides other Drupal components with a way to
20                                  communicate over the extensible messaging and presence protocol (aka          communicate over the extensible messaging and presence protocol (aka
21                                  Jabber). Once enabled and configured, other modules will be able to send          Jabber). Once enabled and configured, other modules will be able to send
22                                  instant messages to site users.          instant messages to site users.
23                          ') . '</p>';        ') . '</p>';
24          }    }
25  }  }
26    
27  /**  /**
28   * Implementation of hook_perm().   * Implementation of hook_perm().
29   */   */
30  function xmpp_perm() {  function xmpp_perm() {
31          return array('administer xmpp');    return array('administer xmpp');
32  }  }
33    
34  /**  /**
35   * Implementation of hook_enable().   * Implementation of hook_enable().
36   */   */
37  function xmpp_enable() {  function xmpp_enable() {
38          $secret = sha1(uniqid(mt_rand(), true));    $secret = sha1(uniqid(mt_rand(), true));
39    variable_set('xmpp_server_secret', $secret);    variable_set('xmpp_server_secret', $secret);
40    variable_set('xmpp_client_auth_passwd', $secret);    variable_set('xmpp_client_auth_passwd', $secret);
41  }  }
# Line 44  function xmpp_enable() { Line 44  function xmpp_enable() {
44   * Implementation of hook_menu().   * Implementation of hook_menu().
45   */   */
46  function xmpp_menu($may_cache) {  function xmpp_menu($may_cache) {
47          $items = array();    $items = array();
48    
49          if ($may_cache) {    if ($may_cache) {
50                  $items[] = array(      $items[] = array(
51                          'path'               => 'admin/settings/xmpp',        'path'               => 'admin/settings/xmpp',
52                          'title'              => t('XMPP Settings'),        'title'              => t('XMPP Settings'),
53                          'description'        => t('Configure XMPP Module.'),        'description'        => t('Configure XMPP Module.'),
54                          'callback'           => 'drupal_get_form',        'callback'           => 'drupal_get_form',
55                          'callback arguments' => array('xmpp_client_settings'),        'callback arguments' => array('xmpp_client_settings'),
56                          'access'             => user_access('administer xmpp')        'access'             => user_access('administer xmpp')
57                  );      );
58    
59                  $items[] = array(      $items[] = array(
60                          'path'   => 'admin/settings/xmpp/client',        'path'   => 'admin/settings/xmpp/client',
61                          'title'  => t('Client'),        'title'  => t('Client'),
62                          'type'   => MENU_DEFAULT_LOCAL_TASK,        'type'   => MENU_DEFAULT_LOCAL_TASK,
63                          'access' => user_access('administer xmpp')        'access' => user_access('administer xmpp')
64                  );      );
65    
66                  $items[] = array(      $items[] = array(
67                          'path'               => 'admin/settings/xmpp/server',        'path'               => 'admin/settings/xmpp/server',
68                          'title'              => t('Server'),        'title'              => t('Server'),
69                          'callback'           => 'drupal_get_form',        'callback'           => 'drupal_get_form',
70                          'callback arguments' => array('xmpp_server_settings'),        'callback arguments' => array('xmpp_server_settings'),
71                          'type'               => MENU_LOCAL_TASK,        'type'               => MENU_LOCAL_TASK,
72                          'access'             => user_access('administer xmpp')        'access'             => user_access('administer xmpp')
73                  );      );
74          }    }
75          else    else {
76          {      $items[] = array(
77                  $items[] = array(        'path'     => 'xmpp/server',
78                          'path'     => 'xmpp/server',        'title'    => 'XMPP Server',
79                          'title'    => 'XMPP Server',        'callback' => 'xmpp_server',
80                          'callback' => 'xmpp_server',        'type'     => MENU_CALLBACK,
81                          'type'     => MENU_CALLBACK,        'access'   => variable_get('xmpp_server_enabled', false)
82                          'access'   => variable_get('xmpp_server_enabled', false)      );
83            );    }
         }  
84    
85          return $items;    return $items;
86  }  }
87    
88  /**  /**
89   * Implementation of hook_form_alter().   * Implementation of hook_form_alter().
90   */   */
91  function xmpp_form_alter($form_id, &$form) {  function xmpp_form_alter($form_id, &$form) {
92          if ($form_id == 'xmpp_client_settings')    if ($form_id == 'xmpp_client_settings')
93                  $form['#submit']['xmpp_client_settings_test'] = array();      $form['#submit']['xmpp_client_settings_test'] = array();
94  }  }
95    
96  /**  /**
97   * Client settings page.   * Client settings page.
98   */   */
99  function xmpp_client_settings() {  function xmpp_client_settings() {
100          $form            = array();    $form            = array();
101          $fields          = array();    $fields          = array();
102          $crypto_disabled = !extension_loaded('openssl');    $crypto_disabled = !extension_loaded('openssl');
103          $crypto_text     = ($crypto_disabled ? ' (Requires OpenSSL extension)' : '');    $crypto_text     = ($crypto_disabled ? ' (Requires OpenSSL extension)' : '');
104          $crypto          = ($crypto_disabled ? 'none' : 'tls');    $crypto          = ($crypto_disabled ? 'none' : 'tls');
105          $jid_format      = htmlspecialchars('<user>@<domain>[/<resource>]');    $jid_format      = htmlspecialchars('<user>@<domain>[/<resource>]');
106    
107          $query = db_query(    $sql   = 'SELECT fid, title FROM {profile_fields} WHERE type = \'textfield\'';
108                  'SELECT fid, title FROM {profile_fields} WHERE type = \'textfield\''    $query = db_query($sql);
109          );  
110      while ($record = db_fetch_array($query))
111          while ($record = db_fetch_array($query))      $fields[$record['fid']] = $record['title'];
112                  $fields[$record['fid']] = $record['title'];  
113      $transport_methods = array(
114          $transport_methods = array(      'tcp'    => t('External (TCP/IP)'),
115                  'tcp'    => t('External (TCP/IP)'),      'webtcp' => t('Built-in (WebTCP)')
116                  'webtcp' => t('Built-in (WebTCP)')    );
117          );  
118      $crypto_methods = array(
119          $crypto_methods = array(      'none' => t('None'),
120                  'none' => t('None'),      'tls'  => t('TLS'),
121                  'tls'  => t('TLS'),      'ssl'  => t('SSL')
122                  'ssl'  => t('SSL')    );
123          );  
124      //
125          //    // Basic settings
126          // Basic settings    //
127          //  
128      $form['client'] = array(
129          $form['client'] = array(      '#type'        => 'fieldset',
130                  '#type'        => 'fieldset',      '#title'       => t('Basic settings')
131                  '#title'       => t('Basic settings')    );
132          );  
133      $form['client']['xmpp_client_auth_jid'] = array(
134          $form['client']['xmpp_client_auth_jid'] = array(      '#type'          => 'textfield',
135                  '#type'          => 'textfield',      '#title'         => t('Username'),
136                  '#title'         => t('Username'),      '#description'   => t('JID to use for authentication ('.$jid_format.').'),
137                  '#description'   => t('JID to use for authentication ('.$jid_format.').'),      '#default_value' => variable_get('xmpp_client_auth_jid', ''),
138                  '#default_value' => variable_get('xmpp_client_auth_jid', ''),      '#required'      => true
139                  '#required'      => true    );
140          );  
141      $form['client']['xmpp_client_auth_passwd'] = array(
142          $form['client']['xmpp_client_auth_passwd'] = array(      '#type'          => 'password',
143                  '#type'          => 'password',      '#title'         => t('Password'),
144                  '#title'         => t('Password'),      '#description'   => t('Password to use for authentication. Leave this '.
145                  '#description'   => t('Password to use for authentication. Leave this '.                            'field blank to keep the old password or when using '.
146                                        'field blank to keep the old password or when using '.                            'the built-in server.')
147                                        'the built-in server.')    );
148          );  
149      $form['client']['xmpp_client_jid_field'] = array(
150          $form['client']['xmpp_client_jid_field'] = array(      '#type'          => 'select',
151                  '#type'          => 'select',      '#title'         => t('User JID Field'),
152                  '#title'         => t('User JID Field'),      '#description'   => t('Profile field which is used to store user JIDs. '.
153                  '#description'   => t('Profile field which is used to store user JIDs. '.                            'You should create this if it doesn\'t already '.
154                                        'You should create this if it doesn\'t already '.                            'exist.'),
155                                        'exist.'),      '#options'       => $fields,
156                  '#options'       => $fields,      '#default_value' => variable_get('xmpp_client_jid_field', ''),
157                  '#default_value' => variable_get('xmpp_client_jid_field', ''),      '#required'      => true
158                  '#required'      => true    );
159          );  
160      //
161          //    // Server type
162          // Server type    //
163          //  
164      $form['server_type'] = array(
165          $form['server_type'] = array(      '#type'  => 'fieldset',
166                  '#type'  => 'fieldset',      '#title' => t('Server type'),
167                  '#title' => t('Server type'),    );
168          );  
169      $form['server_type']['xmpp_client_transport'] = array(
170          $form['server_type']['xmpp_client_transport'] = array(      '#type'          => 'radios',
171                  '#type'          => 'radios',      '#title'         => t('Transport method'),
172                  '#title'         => t('Transport method'),      '#description'   => t('Server type and transport method to be used.'),
173                  '#description'   => t('Server type and transport method to be used.'),      '#options'       => $transport_methods,
174                  '#options'       => $transport_methods,      '#default_value' => variable_get('xmpp_client_transport', 'tcp'),
175                  '#default_value' => variable_get('xmpp_client_transport', 'tcp'),      '#required'      => true
176                  '#required'      => true    );
177          );  
178      //
179          //    // External (TCP/IP)
180          // External (TCP/IP)    //
181          //  
182      $form['external_tcp'] = array(
183          $form['external_tcp'] = array(      '#type'        => 'fieldset',
184                  '#type'        => 'fieldset',      '#title'       => t('External (TCP/IP)'),
185                  '#title'       => t('External (TCP/IP)'),      '#collapsible' => true,
186                  '#collapsible' => true,      '#collapsed'   => true
187                  '#collapsed'   => true    );
188          );  
189      $form['external_tcp']['xmpp_client_tcp_host'] = array(
190          $form['external_tcp']['xmpp_client_tcp_host'] = array(      '#type'          => 'textfield',
191                  '#type'          => 'textfield',      '#title'         => t('Host'),
192                  '#title'         => t('Host'),      '#description'   => t('Hostname or IP of the external server. This can '.
193                  '#description'   => t('Hostname or IP of the external server. This can '.                            'usually be determined automatically from the JID.'),
194                                        'usually be determined automatically from the JID.'),      '#default_value' => variable_get('xmpp_client_tcp_host', '')
195                  '#default_value' => variable_get('xmpp_client_tcp_host', '')    );
196          );  
197      $form['external_tcp']['xmpp_client_tcp_port'] = array(
198          $form['external_tcp']['xmpp_client_tcp_port'] = array(      '#type'          => 'textfield',
199                  '#type'          => 'textfield',      '#title'         => t('Port'),
200                  '#title'         => t('Port'),      '#description'   => t('Port on which to establish the connection. The '.
201                  '#description'   => t('Port on which to establish the connection. The '.                            'default is 5222, 5223 if SSL is used.'),
202                                        'default is 5222, 5223 if SSL is used.'),      '#default_value' => variable_get('xmpp_client_tcp_port', ''),
203                  '#default_value' => variable_get('xmpp_client_tcp_port', ''),      '#maxlength'     => 5
204                  '#maxlength'     => 5    );
205          );  
206      $form['external_tcp']['xmpp_client_tcp_use_dns'] = array(
207          $form['external_tcp']['xmpp_client_tcp_use_dns'] = array(      '#type'          => 'checkbox',
208                  '#type'          => 'checkbox',      '#title'         => t('Use DNS'),
209                  '#title'         => t('Use DNS'),      '#default_value' => variable_get('xmpp_client_tcp_use_dns', true),
210                  '#default_value' => variable_get('xmpp_client_tcp_use_dns', true),      '#description'   => t('Enable DNS SRV look-ups.')
211                  '#description'   => t('Enable DNS SRV look-ups.')    );
212          );  
213      $form['external_tcp']['xmpp_client_tcp_crypto'] = array(
214          $form['external_tcp']['xmpp_client_tcp_crypto'] = array(      '#type'          => 'radios',
215                  '#type'          => 'radios',      '#title'         => t('Encryption mechanism'. $crypto_text),
216                  '#title'         => t('Encryption mechanism'. $crypto_text),      '#description'   => t('Type of encryption to use (if available).'),
217                  '#description'   => t('Type of encryption to use (if available).'),      '#default_value' => variable_get('xmpp_client_tcp_crypto', $crypto),
218                  '#default_value' => variable_get('xmpp_client_tcp_crypto', $crypto),      '#options'       => $crypto_methods,
219                  '#options'       => $crypto_methods,      '#disabled'      => $crypto_disabled
220                  '#disabled'      => $crypto_disabled    );
221          );  
222      //
223          //    // Built-in (WebTCP)
224          // Built-in (WebTCP)    //
225          //  
226      $form['builtin_webtcp'] = array(
227          $form['builtin_webtcp'] = array(      '#type'        => 'fieldset',
228                  '#type'        => 'fieldset',      '#title'       => t('Built-in (WebTCP)'),
229                  '#title'       => t('Built-in (WebTCP)'),      '#collapsible' => true,
230                  '#collapsible' => true,      '#collapsed'   => true
231                  '#collapsed'   => true    );
232          );  
233      $form['builtin_webtcp']['xmpp_client_webtcp_url'] = array(
234          $form['builtin_webtcp']['xmpp_client_webtcp_url'] = array(      '#type'          => 'textfield',
235                  '#type'          => 'textfield',      '#title'         => t('URL'),
236                  '#title'         => t('URL'),      '#description'   => t('Location of the WebTCP server. Usually this is '.
237                  '#description'   => t('Location of the WebTCP server. Usually this is '.                            'http://[your_domain]/[path_to_drupal]/xmpp/server. '.
238                                        'http://[your_domain]/[path_to_drupal]/xmpp/server. '.                            'Do not add a trailing slash.'),
239                                        'Do not add a trailing slash.'),      '#default_value' => variable_get('xmpp_client_webtcp_url',
240                  '#default_value' => variable_get('xmpp_client_webtcp_url', '')                                       url('xmpp/server', null, null, true))
241          );    );
242    
243          //    //
244          // Test configuration    // Test configuration
245          //    //
246    
247          $form['test'] = array(    $form['test'] = array(
248                  '#type'  => 'fieldset',      '#type'  => 'fieldset',
249                  '#title' => t('Test configuration'),      '#title' => t('Test configuration'),
250          );    );
251    
252          $form['test']['xmpp_client_test_jid'] = array(    $form['test']['xmpp_client_test_jid'] = array(
253                  '#type'          => 'textfield',      '#type'          => 'textfield',
254                  '#title'         => t('Recipient'),      '#title'         => t('Recipient'),
255                  '#description'   => t('Send a test IM to the specified JID.')      '#description'   => t('Send a test IM to the specified JID.')
256          );    );
257    
258          return system_settings_form($form);    return system_settings_form($form);
259  }  }
260    
261  /**  /**
# Line 264  function xmpp_client_settings() { Line 263  function xmpp_client_settings() {
263   */   */
264  function xmpp_client_settings_validate($form_id, $values, $form) {  function xmpp_client_settings_validate($form_id, $values, $form) {
265    
266          if (!$values['xmpp_client_auth_passwd']) {    if (!$values['xmpp_client_auth_passwd']) {
267    
268                  if (!($passwd = variable_get('xmpp_client_auth_passwd', ''))) {      if (!($passwd = variable_get('xmpp_client_auth_passwd', ''))) {
269                          form_set_error('', t('Please enter the password.'));        form_set_error('', t('Please enter the password.'));
270                          return;        return;
271                  }      }
272    
273                  form_set_value($form['client']['xmpp_client_auth_passwd'], $passwd);      form_set_value($form['client']['xmpp_client_auth_passwd'], $passwd);
274          }    }
275    
276          $jid_regex = '/^[^@]+@[^\/]+(?:\/.+)?$/';    if (!xmpp_check_jid($values['xmpp_client_auth_jid'])) {
277        form_set_error('', t('Please enter a valid JID.'));
278          if (!preg_match($jid_regex, $values['xmpp_client_auth_jid'])) {      return;
279                  form_set_error('', t('Please enter a valid JID.'));    }
280                  return;  
281          }    if ($values['xmpp_client_test_jid']) {
282        if (!xmpp_check_jid($values['xmpp_client_test_jid'])) {
283          if ($form['xmpp_client_test_jid']) {        form_set_error('', t('Please enter a valid test JID.'));
284                  if (!preg_match($jid_regex, $values['xmpp_client_test_jid'])) {        return;
285                          form_set_error('', t('Please enter a valid test JID.'));      }
286                          return;    }
                 }  
         }  
287  }  }
288    
289  /**  /**
# Line 294  function xmpp_client_settings_validate($ Line 291  function xmpp_client_settings_validate($
291   */   */
292  function xmpp_client_settings_test($form_id, $form) {  function xmpp_client_settings_test($form_id, $form) {
293    
294          variable_set('xmpp_client_configured', true);    variable_set('xmpp_client_configured', true);
295    
296          if (!($test_jid = $form['xmpp_client_test_jid']))    if (!($test_jid = $form['xmpp_client_test_jid']))
297                  return;      return;
298    
299          if (!xmpp_message($test_jid, 'Hello, this is a test IM from Drupal :)')) {    if (!xmpp_message($test_jid, 'Hello, this is a test IM from Drupal :)')) {
300                  drupal_set_message('Error sending message: '. xmpp_last_error(), 'error');      drupal_set_message('Error sending message: '. xmpp_last_error(), 'error');
301                  return;      return;
302          }    }
303    
304          drupal_set_message('Test message sent.');    drupal_set_message('Test message sent.');
305  }  }
306    
307  /**  /**
308   * Server settings page.   * Server settings page.
309   */   */
310  function xmpp_server_settings() {  function xmpp_server_settings() {
311          $form = array();    $form = array();
312    
313          $form['server'] = array(    $form['server'] = array(
314                  '#type'  => 'fieldset',      '#type'  => 'fieldset',
315                  '#title' => t('Server configuration'),      '#title' => t('Server configuration'),
316          );    );
317    
318          $form['server']['xmpp_server_enabled'] = array(    $form['server']['xmpp_server_enabled'] = array(
319                  '#type'          => 'checkbox',      '#type'          => 'checkbox',
320                  '#title'         => t('Enable server'),      '#title'         => t('Enable server'),
321                  '#description'   => t('Turn the built-in server on or off.'),      '#description'   => t('Turn the built-in server on or off.'),
322                  '#default_value' => variable_get('xmpp_server_enabled', false)      '#default_value' => variable_get('xmpp_server_enabled', false)
323          );    );
324    
325          $form['server']['xmpp_server_stream_from'] = array(    $form['server']['xmpp_server_stream_from'] = array(
326                  '#type'          => 'textfield',      '#type'          => 'textfield',
327                  '#title'         => t('Host'),      '#title'         => t('Host'),
328                  '#description'   => t('Domain name hosted by this server.'),      '#description'   => t('Domain name hosted by this server.'),
329                  '#default_value' => variable_get('xmpp_server_stream_from', ''),      '#default_value' => variable_get('xmpp_server_stream_from', ''),
330                  '#required'      => true      '#required'      => true
331          );    );
332    
333          $form['server']['xmpp_server_secret'] = array(    $form['server']['xmpp_server_secret'] = array(
334                  '#type'          => 'textfield',      '#type'          => 'textfield',
335                  '#title'         => t('Secret'),      '#title'         => t('Secret'),
336                  '#description'   => t('Secret phrase needed to activate the server. This '.      '#description'   => t('Secret phrase needed to activate the server. This '.
337                                        'value needs to be set as the client\'s password '.                            'value needs to be set as the client\'s password '.
338                                        'in order for it to be able to access the server. '.                            'in order for it to be able to access the server. '.
339                                        'By default, server is protected with a random sha1 '.                            'By default, server is protected with a random sha1 '.
340                                        'hash string and you should only modify this field '.                            'hash string and you should only modify this field '.
341                                        'if you want other sites to use this server.'),                            'if you want other sites to use this server.'),
342                  '#default_value' => variable_get('xmpp_server_secret', ''),      '#default_value' => variable_get('xmpp_server_secret', ''),
343                  '#required'      => true      '#required'      => true
344          );    );
345    
346          $form['server']['xmpp_server_webtcp_port_start'] = array(    $form['server']['xmpp_server_webtcp_port_start'] = array(
347                  '#type'          => 'textfield',      '#type'          => 'textfield',
348                  '#title'         => t('Start port'),      '#title'         => t('Start port'),
349                  '#description'   => t('First port that can be used for incoming '.      '#description'   => t('First port that can be used for incoming '.
350                                        'connections. Must be greater than 5269.'),                            'connections. Must be greater than 5269.'),
351                  '#default_value' => variable_get('xmpp_server_webtcp_port_start', '48004'),      '#default_value' => variable_get('xmpp_server_webtcp_port_start', '48004'),
352                  '#maxlength'     => 5,      '#maxlength'     => 5,
353                  '#required'      => true      '#required'      => true
354          );    );
355    
356          $form['server']['xmpp_server_webtcp_port_end'] = array(    $form['server']['xmpp_server_webtcp_port_end'] = array(
357                  '#type'          => 'textfield',      '#type'          => 'textfield',
358                  '#title'         => t('End port'),      '#title'         => t('End port'),
359                  '#description'   => t('Last port that can be used for incoming '.      '#description'   => t('Last port that can be used for incoming '.
360                                        'connections. Must be greater than the start port.'),                            'connections. Must be greater than the start port.'),
361                  '#default_value' => variable_get('xmpp_server_webtcp_port_end', '48127'),      '#default_value' => variable_get('xmpp_server_webtcp_port_end', '48127'),
362                  '#maxlength'     => 5,      '#maxlength'     => 5,
363                  '#required'      => true      '#required'      => true
364          );    );
365    
366          return system_settings_form($form);    return system_settings_form($form);
367  }  }
368    
369  /**  /**
# Line 374  function xmpp_server_settings() { Line 371  function xmpp_server_settings() {
371   */   */
372  function xmpp_server($secret = '') {  function xmpp_server($secret = '') {
373    
374          if (!is_string($secret) || $secret !== variable_get('xmpp_server_secret', 0))    if (!is_string($secret) || $secret !== variable_get('xmpp_server_secret', 0))
375                  return drupal_access_denied();      return drupal_access_denied();
376    
377          require_once dirname(__FILE__).'/lib/server.inc';    require_once dirname(__FILE__).'/lib/server.inc';
378    
379          $conf = array(    $conf = array(
380                  'stream.from'       => variable_get('xmpp_server_stream_from', ''),      'stream.from'       => variable_get('xmpp_server_stream_from', ''),
381                  'webtcp.port.start' => variable_get('xmpp_server_webtcp_port_start', ''),      'webtcp.port.start' => variable_get('xmpp_server_webtcp_port_start', ''),
382                  'webtcp.port.end'   => variable_get('xmpp_server_webtcp_port_end', '')      'webtcp.port.end'   => variable_get('xmpp_server_webtcp_port_end', '')
383          );    );
384    
385          try    try {
386          {      $sess = XMPP_Server::instance()->listen($conf);
387                  $sess = XMPP_Server::instance()->listen($conf);  
388        while ($sess->ready)
389                  while ($sess->ready)        $sess->wait();
390                          $sess->wait();    }
391          }    catch (Exception $ex) {
392          catch (Exception $ex)      if (isset($sess))
393          {        $sess->handleException($ex);
394                  if (isset($sess))    }
                         $sess->handleException($ex);  
         }  
395  }  }
396    
397  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Line 407  function xmpp_server($secret = '') { Line 402  function xmpp_server($secret = '') {
402   * Get a connected XMPP_Session object.   * Get a connected XMPP_Session object.
403   */   */
404  function xmpp_get_session() {  function xmpp_get_session() {
405          static $sess  = null;    static $sess  = null;
406          static $first = true;    static $first = true;
407    
408          if ($first) {    if ($first) {
409    
410                  $first = false;      $first = false;
411    
412                  if (!variable_get('xmpp_client_configured', false))      if (!variable_get('xmpp_client_configured', false))
413                          return null;        return null;
414    
415                  require_once dirname(__FILE__).'/lib/client.inc';      require_once dirname(__FILE__).'/lib/client.inc';
416    
417                  $conf = array(      $conf = array(
418                          'auth.jid'    => variable_get('xmpp_client_auth_jid', ''),        'auth.jid'    => variable_get('xmpp_client_auth_jid', ''),
419                          'auth.passwd' => variable_get('xmpp_client_auth_passwd', ''),        'auth.passwd' => variable_get('xmpp_client_auth_passwd', ''),
420                          'transport'   => variable_get('xmpp_client_transport', '')        'transport'   => variable_get('xmpp_client_transport', '')
421                  );      );
422    
423                  if ($conf['transport'] == 'tcp') {      if ($conf['transport'] == 'tcp') {
424                          if ($host = variable_get('xmpp_client_tcp_host', ''))        if ($host = variable_get('xmpp_client_tcp_host', ''))
425                                  $conf['tcp.host'] = $host;          $conf['tcp.host'] = $host;
426    
427                          if ($port = variable_get('xmpp_client_tcp_port', ''))        if ($port = variable_get('xmpp_client_tcp_port', ''))
428                                  $conf['tcp.port'] = $port;          $conf['tcp.port'] = $port;
429    
430                          $conf['tcp.use_dns'] = variable_get('xmpp_client_tcp_use_dns', true);        $conf['tcp.use_dns'] = variable_get('xmpp_client_tcp_use_dns', true);
431                          $crypto = variable_get('xmpp_client_tcp_crypto', 'none');        $crypto = variable_get('xmpp_client_tcp_crypto', 'none');
432    
433                          if ($crypto == 'none')        if ($crypto == 'none')
434                                  $conf['tcp.use_tls'] = false;          $conf['tcp.use_tls'] = false;
435    
436                          if ($crypto == 'ssl')        if ($crypto == 'ssl')
437                                  $conf['tcp.use_ssl'] = true;          $conf['tcp.use_ssl'] = true;
438                  }      }
439                  else {      else {
440                          $conf['webtcp.url']  = variable_get('xmpp_client_webtcp_url', '');        $conf['webtcp.url']  = variable_get('xmpp_client_webtcp_url', '');
441                          $conf['webtcp.url'] .= '/'.variable_get('xmpp_client_auth_passwd', '');        $conf['webtcp.url'] .= '/'.variable_get('xmpp_client_auth_passwd', '');
442                  }      }
443    
444                  try {      try {
445                          $sess = XMPP_Client::instance()->connect($conf);        $sess = XMPP_Client::instance()->connect($conf);
446                          $sess->Presence->set(1);        $sess->Presence->set(1);
447                  }      }
448                  catch (Exception $ex) {      catch (Exception $ex) {
449                          $sess = null;        $sess = null;
450                          xmpp_last_error($ex->getMessage());        xmpp_last_error($ex->getMessage());
451                  }      }
452          }    }
453    
454          return ($sess && $sess->ready ? $sess : null);    return ($sess && $sess->ready ? $sess : null);
455  }  }
456    
457  /**  /**
# Line 464  function xmpp_get_session() { Line 459  function xmpp_get_session() {
459   */   */
460  function xmpp_get_jid($uid = null) {  function xmpp_get_jid($uid = null) {
461    
462          static $jid_regex = '/^[^@]+@[^\/]+(?:\/.+)?$/';    static $jid_regex = '/^[^@]+@[^\/]+(?:\/.+)?$/';
463          $fid = variable_get('xmpp_client_jid_field', '');    $fid = variable_get('xmpp_client_jid_field', '');
464    
465          if ($fid === '')    if ($fid === '')
466                  return (is_array($uid) ? array() : '');      return (is_array($uid) ? array() : '');
467    
468          if (is_null($uid)) {    if (is_null($uid)) {
469                  global $user;      global $user;
470                  $uid = $user->uid;      $uid = $user->uid;
471          }    }
472    
473          $match  = (is_array($uid) ? 'uid IN ('.implode(',', $uid).')' : 'uid='.$uid);    $match  = (is_array($uid) ? 'uid IN ('.implode(',', $uid).')' : 'uid='.$uid);
474          $result = array();    $result = array();
475      $sql    = "SELECT uid, value FROM {profile_values} WHERE $match AND fid=%d";
476          $query = db_query(    $query  = db_query($sql, $fid);
477                  "SELECT uid, value FROM {profile_values} WHERE $match AND fid=$fid"  
478          );    while ($record = db_fetch_array($query)) {
479        if (xmpp_check_jid($record['value']))
480          while ($record = db_fetch_array($query))        $result[$record['uid']] = $record['value'];
481          {    }
                 if (preg_match($jid_regex, $record['value']))  
                         $result[$record['uid']] = $record['value'];  
         }  
482    
483          return (is_array($uid) ? $result : ($result ? current($result) : ''));    return (is_array($uid) ? $result : ($result ? current($result) : ''));
484  }  }
485    
486  /**  /**
# Line 496  function xmpp_get_jid($uid = null) { Line 488  function xmpp_get_jid($uid = null) {
488   */   */
489  function xmpp_message($jid, $message) {  function xmpp_message($jid, $message) {
490    
491          if (!xmpp_presence_subscribe($jid))    if (!xmpp_presence_subscribe($jid))
492                  return false;      return false;
493    
494          if (!($sess = xmpp_get_session()))    if (!($sess = xmpp_get_session()))
495                  return false;      return false;
496    
497          try {    try {
498                  $sess->IM->send($jid, $message);      $sess->IM->send($jid, $message);
499                  return true;      return true;
500          }    }
501          catch (Exception $ex) {    catch (Exception $ex) {
502                  $sess->handleException($ex);      $sess->handleException($ex);
503                  xmpp_last_error($ex->getMessage());      xmpp_last_error($ex->getMessage());
504                  return false;      return false;
505          }    }
506  }  }
507    
508  /**  /**
# Line 518  function xmpp_message($jid, $message) { Line 510  function xmpp_message($jid, $message) {
510   */   */
511  function xmpp_presence_subscribe($jid) {  function xmpp_presence_subscribe($jid) {
512    
513          if (!($sess = xmpp_get_session()))    if (!xmpp_check_jid($jid) || !($sess = xmpp_get_session()))
514                  return false;      return false;
515    
516          list($local)  = explode('/', variable_get('xmpp_client_auth_jid', ''), 2);    list($local)  = explode('/', variable_get('xmpp_client_auth_jid', ''), 2);
517          list($remote) = explode('/', $jid, 2);    list($remote) = explode('/', $jid, 2);
518          $local        = db_escape_string($local);  
519          $remote       = db_escape_string($remote);    $sql   = 'SELECT * FROM {xmpp_presence} WHERE remote=\'%s\' AND local=\'%s\'';
520      $query = db_query($sql, $remote, $local);
521          $query = db_query(  
522                  "SELECT * FROM {xmpp_presence} WHERE remote='$remote' AND local='$local'"    if (db_fetch_array($query))
523          );      return true;
524    
525          if (db_fetch_array($query))    try {
526                  return true;      $sess->Presence->subscribe($remote);
527    
528          try {      $sql   = 'INSERT INTO {xmpp_presence} VALUES (\'%s\', \'%s\')';
529                  $sess->Presence->subscribe($remote);      $query = db_query($sql, $remote, $local);
530    
531                  $query = db_query(      return true;
532                          "INSERT INTO {xmpp_presence} VALUES ('$remote', '$local')"    }
533                  );    catch (Exception $ex) {
534        $sess->handleException($ex);
535                  return true;      xmpp_last_error($ex->getMessage());
536          }      return false;
537          catch (Exception $ex) {    }
                 $sess->handleException($ex);  
                 xmpp_last_error($ex->getMessage());  
                 return false;  
         }  
538  }  }
539    
540  /**  /**
# Line 554  function xmpp_presence_subscribe($jid) { Line 542  function xmpp_presence_subscribe($jid) {
542   */   */
543  function xmpp_start_buffer() {  function xmpp_start_buffer() {
544    
545          if (!($sess = xmpp_get_session()))    if (!($sess = xmpp_get_session()))
546                  return false;      return false;
547    
548          $sess->stream->autosend = false;    $sess->stream->autosend = false;
549          return true;    return true;
550  }  }
551    
552  /**  /**
# Line 566  function xmpp_start_buffer() { Line 554  function xmpp_start_buffer() {
554   */   */
555  function xmpp_end_buffer() {  function xmpp_end_buffer() {
556    
557          if (!($sess = xmpp_get_session()))    if (!($sess = xmpp_get_session()))
558                  return false;      return false;
559    
560      try {
561        $sess->stream->send();
562        return true;
563      }
564      catch (Exception $ex) {
565        $sess->handleException($ex);
566        xmpp_last_error($ex->getMessage());
567        return false;
568      }
569    }
570    
571          try {  /**
572                  $sess->stream->send();   * Check basic JID syntax.
573                  return true;   */
574          }  function xmpp_check_jid($jid) {
575          catch (Exception $ex) {    return preg_match('/^[^@]+@[^\/]+(?:\/.+)?$/', $jid);
                 $sess->handleException($ex);  
                 xmpp_last_error($ex->getMessage());  
                 return false;  
         }  
576  }  }
577    
578  /**  /**
579   * Get the last XMPP error message.   * Get the last XMPP error message.
580   */   */
581  function xmpp_last_error($set = null) {  function xmpp_last_error($set = null) {
582          static $error = '';    static $error = '';
583          return ($set ? $error = t($set) : $error);    return ($set ? $error = t($set) : $error);
584  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.3