--- site_network.authentication.inc.bkp	2008-08-14 08:38:53.000000000 +0400
+++ site_network.authentication.inc	2008-08-14 08:38:23.000000000 +0400
@@ -12,15 +12,21 @@
  */
 function _site_network_login_validate(&$form, &$form_state) {
   if (variable_get('site_network_client', FALSE)) {
+    $name = trim($form_state['values']['name']);
     // The username must be split in two: the first part is the
     // username itself and the second is the server
-    $username = explode('@', $form_state['values']['name']);
-    $server   = $username[1];
-    $username = $username[0];
-
-    // If no server was specified, get the default option if its required
-    if (empty($server) and variable_get('site_network_client_server', '')) {
+    if ($server = strrchr($name, '@')) {
+      $username = substr($name, 0, strlen($name) - strlen($server));
+      $server = substr($server, 1);
+      if (variable_get('site_network_client_server_only', FALSE) && $server != variable_get('site_network_client_server', '')) {
+      	form_set_error('name', t('Server not allowed'));
+      	return ;
+      }
+    }
+    else {
+      // If no server was specified, get the default option
       $server = variable_get('site_network_client_server', '');
+      $username = $name;
     }
 
     // If a server exists, use it
@@ -34,12 +40,13 @@ function _site_network_login_validate(&$
         $form_state['#submit']  = array();
 
         // Check if the account already exists or its a new user
-        $account = user_external_load($aid);
+        $name = "$username@$server";
+        $account = user_external_load($name);
         if (isset($account->uid)) {
           user_external_login($account);
         }
         else {
-          user_external_login_register($form_state['values']['name'], 'site_network');
+          user_external_login_register($name, 'site_network');
         }
       }
       elseif (variable_get('site_network_client_server_only', FALSE)) {
