--- rsvp.module	2005-09-20 11:47:15.683226296 -0700
+++ rsvp.module.invite_by_role	2005-09-20 11:48:27.317336256 -0700
@@ -591,7 +591,7 @@
 function rsvp_create_form($nid) {
   $form = form_textfield(t('RSVP Name'),'name', '', 40, 40, t('This is the name of your rsvp'));
   $form .= form_textarea(t('RSVP Message'), 'invite_text', '', 70, 5, t('This text will be sent to the people you invite'));
-  $form .= form_textarea(t('Invite List'), 'invite_list', '', 70, 5, t('Enter email addresses separated by commas'));
+  $form .= form_textarea(t('Invite List'), 'invite_list', '', 70, 5, t('Enter email addresses or role names separated by commas'));
   $form .= form_checkbox(t('Hide attendees'), 'blind', 1, 0, t('Prevent attendees from seeing who else is on the list'));
   $form .= form_checkbox(t('Allow list email'), 'list_email', 1, 0, t('Allow attendees to send messages to the people invited to your rsvp'));
   $form .= form_checkbox(t('Allow attendee invites'), 'allow_invite', 1, 0, t('Allow attendees to invite more people to the event'));
@@ -631,7 +631,7 @@
 function rsvp_attendee_form($rid) {
   global $user;
   if($user->uid) {
-    $form = form_textarea(t('Add Attendees'), 'invite_list', '', 70, 5, t('Enter email addresses separated by commas'));
+    $form = form_textarea(t('Add Attendees'), 'invite_list', '', 70, 5, t('Enter email addresses or role names separated by commas'));
     $form .= form_submit(t('Send Invites'));
     return form($form);
   }
@@ -931,14 +931,22 @@
 * @return boolean true if successful
 */
 function rsvp_set_invites($attendees, $rid) {
-  // strip whitespace
-  $attendees = strtolower(preg_replace('/\s+/', '', $attendees));
   // convert to array
   $attendees = explode(",", $attendees);
+
+  // if a role, include all users in that role
+  foreach ($attendees as $key => $role) {
+	if ($role_id = _rsvp_is_role(trim($role))) {
+	  unset($attendees[$key]);
+	  $attendees = array_merge($attendees, _rsvp_role_emails($role_id));
+	}
+  }
+
   // remove duplicates
   $attendees = array_unique($attendees);
 
   foreach($attendees as $key => $email) {
+	$email = trim($email); // strip whitespace
     if(valid_email_address($email) && $email != '') {
       if(!rsvp_attendee_exists($rid, $email)) {
         $user = _rsvp_check_user_email($email);
@@ -1296,4 +1304,27 @@
 
   return $status;
 }
+
+// is the given string the name of a role
+function _rsvp_is_role($name) {
+  static $roles = null;
+  if (!$roles) {
+	$result = db_query("SELECT name, rid FROM {role}");
+	while ($row = db_fetch_object($result)) {
+	  $roles[$row->name] = $row->rid;
+	}
+  }
+  return isset($roles[$name]) ? $roles[$name] : false;
+}
+
+// given a role (rid) return all email addresses
+function _rsvp_role_emails($rid) {
+  $items = array();
+  $result = db_query('SELECT mail FROM {users} u LEFT JOIN {users_roles} ur ON ur.uid=u.uid WHERE rid = %d', $rid);
+  while ($row = db_fetch_object($result)) {
+	$items[] = $row->mail;
+  }
+  return $items;
+}
+
 ?>
