--- includes/og.admin.inc	Wed Nov 19 00:23:49 2008
+++ includes/og.admin.inc	Thu May 21 15:46:13 2009
@@ -185,5 +185,19 @@
     '#default_value' => _og_mail_text('og_new_admin_body')
   );
 
+  $form['og_settings']['invite'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Invitations'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE
+  );
+
+  $form['og_settings']['invite']['og_email_max'] = array('#type' => 'textfield',
+    '#title' => t('Maximum # of OG Invitations via email at one time'),
+    '#default_value' => variable_get('og_email_max', 10),
+    '#size' => 4,
+    '#maxlength' => 4,
+    '#description' => t('The number of emails you may enter at one time for inviting new users to join the site and get added to the group when they join'),
+  );
   return system_settings_form($form);
 }
--- og.install	Fri May 15 13:28:02 2009
+++ og.install	Fri May 22 14:55:28 2009
@@ -143,6 +143,24 @@
     ),
     'primary key'       => array('nid', 'group_nid'),
   );
+  $schema['og_invite'] = array(
+    'description'     => 'Pending OG Invitations',
+    'fields'          => array(
+        'mail' => array(
+        'type' => 'varchar',
+        'length' => 64,
+        'not null' => TRUE,
+        'description' => "Invited email address.",
+      ),
+      'group_nid'       => array(
+        'description'     => "The group's {node}.nid.",
+        'type'            => 'int',
+        'size'            => 'normal',
+        'not null'        => TRUE,
+      ),
+    ),
+    'primary key'       => array('mail', 'group_nid'),
+  );
 
   return $schema;
 }
--- og.module	Tue May 19 14:52:47 2009
+++ og.module	Fri May 22 15:17:00 2009
@@ -857,6 +857,9 @@
     '#title' => t('Email addresses or usernames'),  
     '#description' => t('Enter up to %max email addresses or usernames. Separate multiple addresses by commas or new lines. Each person will receive an invitation message from you.', array('%max' => $max))
   );
+  if (og_is_group_admin($node)) {
+    $form['mails']['#description'] = t('Enter up to %max email addresses or usernames. Separate multiple addresses by commas or new lines.<br />Existing users will be added silently, the rest will receive an invitation message from you and be added once they create an account.', array('%max' => $max));
+  }  
   $form['pmessage'] = array(
     '#type' => 'textarea', 
     '#title' => t('Personal message'), 
@@ -882,6 +885,9 @@
   elseif (in_array($user->mail, $emails)) {
     form_set_error('mails', t('You may not invite yourself - @self.', array('@self' => $user->mail)));
   }
+  elseif (in_array($user->name, $emails)) {
+    form_set_error('mails', t('You may not invite yourself - @self.', array('@self' => $user->name)));
+  }
   else {
     $valid_emails = array();
     $bad = array();
@@ -927,10 +933,37 @@
   
   global $user;
   $from = $user->mail;
+  $sentmails = 0;
   foreach ($emails as $mail) {
+    if (og_is_group_admin($node)) {
+      $account = user_load(array('mail' => $mail));
+      if ($account && $account->mail) {
+          og_save_subscription($node->nid, $account->uid, array('is_active' => 1));        
+          drupal_set_message(t('@name was added to the group and no invitation was sent to them.', array('@name' => $account->name)));
+      }
+      else {
+        $sql = "SELECT COUNT(*) FROM {og_invite} WHERE mail = '%s' AND group_nid = %d";
+        $cnt = db_result(db_query($sql, $mail, $node->nid));
+        if ($cnt) {
+          drupal_set_message(t('@email was already on the invitation list for this group, but will be re-invited.', array('@email' => $mail)));
+        }
+        else {
+          $record = array('mail' => $mail, 'group_nid' => $node->nid);
+          $result = drupal_write_record('og_invite', $record);
+        }
+        $variables['!group_url'] = url('user/register', array('absolute' => TRUE));
     drupal_mail('og', 'invite_user', $mail, $GLOBALS['language'], $variables, $from);
+        $sentmails ++;
+      }
+    }
+    else {
+      drupal_mail('og', 'invite_user', $mail, $GLOBALS['language'], $variables, $from);
+      $sentmails ++;
+    }
+  }
+  if ($sentmails) {
+    drupal_set_message(format_plural($sentmails, '1 invitation sent.', '@count invitations sent.'));
   }
-  drupal_set_message(format_plural(count($emails), '1 invitation sent.', '@count invitations sent.'));
 }
 
 /**
@@ -1174,6 +1207,7 @@
     '#description' => t('Add one or more usernames in order to associate users with this group. Multiple usernames should be separated by a comma.'),
     '#element_validate' => array('og_add_users_og_names_validate'),
   );
+  $form['og_names']['#description'] .= '<br />' . t('If you wish to use emails in addition to usernames, <a href="@url">invite</a> will accept a mix of both.', array( '@url' => url('og/invite/'. $group_node->nid)));
   $form['op'] = array('#type' => 'submit', '#value' => t('Add users'));
   $form['gid'] = array('#type' => 'value', '#value' => $group_node->nid);
   return $form;
@@ -2256,6 +2290,7 @@
         );
         return $form;
       }
+      break;
     case 'insert':
       if (isset($edit['og_register'])) {
         foreach (array_keys(array_filter($edit['og_register'])) as $gid) {
@@ -2265,6 +2300,16 @@
           }
         }  
       }
+      $sql = "SELECT group_nid FROM {og_invite} WHERE mail = '%s'";
+      $result = db_query($sql, $account->mail);
+      while ($group = db_fetch_array($result)) {
+        $gid = $group['group_nid'];
+        $groupnode = node_load($gid);
+        og_save_subscription($gid, $account->uid, array('is_active' => 1));
+        drupal_set_message(t('You are now a member of the %group.', array('%group' => $groupnode->title))); 
+      }
+      $sql = "DELETE FROM {og_invite} WHERE mail = '%s'";
+      $result = db_query($sql, $account->mail);
       break;
     case 'delete':
       $sql = 'DELETE FROM {og_uid} WHERE uid=%d';
@@ -2510,7 +2555,7 @@
     if ($subscription == 'active' || user_access('administer nodes')) {
       $links = module_invoke_all('og_create_links', $node);
       // We want to open this up for OG_INVITE_ONLY but we need to handle invitation workflow much better. See http://drupal.org/node/170332
-      if ($node->og_selective < OG_INVITE_ONLY) {
+      if ($node->og_selective < OG_INVITE_ONLY  || og_is_group_admin($node)) {
         $links['invite'] = l(t('Invite friend'), "og/invite/$node->nid");
       }
       $links['subscribers'] = $txt;
