--- modules/role_change_notify/role_change_notify.module	2009-07-10 21:13:22.317470000 -0700
+++ modules/role_change_notify/role_change_notify_new.module	2009-07-10 21:13:22.811265000 -0700
@@ -22,7 +22,14 @@ function role_change_notify_help($path, 
       drupal_set_message(t('None of these notifications will be sent unless you specify a valid site !email_address on the !site_information settings page.', array('!email_address' => '<strong>'. t('E-mail address') .'</strong>', '!site_information' => l(t('Site information'), 'admin/settings/site-information'))), 'error');
     }
     $output .= '<p>'. t('This page allows you to configure whether automatic emails should be set to users when a new role is assigned to them. You define if an email should be sent for each role type, and if so, you define what the subject and body of the resulting email will be.') .'</p>'
-    .'<p>'. t('For any of the settings below, you can use placeholders which will be substituted with the current values for the user, role, and site.') .' '. t('Available variables are:') .' %username, %uid, %role, %site, %uri, %uri_brief, %mailto, %date, %edit_uri, %login_uri.</p>';
+    .'<p>'. t('For any of the settings below, you can use placeholders which will be substituted with the current values for the user, role, and site.') .' ';
+	if (!module_exists('token')) {
+	  $output .= t('Available variables are:') . '[name], [uid], [site-name], [site-url], [uri_brief], [mail], [site-date], [login_uri], [account-edit], [role]. ';
+	  if (module_exists('profile')) {
+	    $output .= t('User profile variables are also available in the') . ' [profile_value] ' . t('format.');
+	  }
+	}
+	$output .= '</p>';
     break;
   }
   return $output;
@@ -104,7 +111,19 @@ function role_change_notify_settings_for
     '#rows' => 10,
     '#description' => '',
   );
+  if (module_exists('token')) {
+    $form['view']['token_help'] = array(
+      '#title' => t('Replacement patterns'),
+      '#type' => 'fieldset',
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+    );
 
+    $form['view']['token_help']['help'] = array(
+      '#value' => theme('token_help', 'user'),
+    );
+  }
   return system_settings_form($form);
 }
 
@@ -137,9 +156,18 @@ function role_change_notify_user($op, &$
     if (variable_get("role_change_notify_$roleid", FALSE)) {
       $role = $roles[$roleid];
       if (valid_email_address($user->mail) && valid_email_address($from)) {
-        $variables = _role_change_notify_get_variables($user, $role);
-        $subject = strtr(variable_get('role_change_notify_role_added_subject', role_change_notify_role_added_subject()), $variables);
-        $body = strtr(variable_get('role_change_notify_role_added_body', role_change_notify_role_added_body()), $variables);
+	    if(module_exists(profile)){
+		  profile_load_profile($user);  
+		}
+		$user->rolename = $role;
+		if(module_exists('token')){
+		  $subject = token_replace(variable_get('role_change_notify_role_added_subject', role_change_notify_role_added_subject()), $type = 'user', $user, $leading = '[', $trailing = ']');
+		  $body = token_replace(variable_get('role_change_notify_role_added_body', role_change_notify_role_added_body()), $type = 'user', $user, $leading = '[', $trailing = ']');
+		}else{
+		  $variables = _role_change_notify_get_variables($user, $role);
+		  $subject = strtr(variable_get('role_change_notify_role_added_subject', role_change_notify_role_added_subject()), $variables);
+		  $body = strtr(variable_get('role_change_notify_role_added_body', role_change_notify_role_added_body()), $variables);  
+		}
         $language = user_preferred_language($account);
         $context['from'] = $from;
         $context['subject'] = $subject;
@@ -151,8 +179,8 @@ function role_change_notify_user($op, &$
       }
     }
   }
-
 }
+
 /**
  * Implementation of hook_mail().
  *
@@ -189,17 +217,72 @@ function role_change_notify_mail($key, &
 function _role_change_notify_get_variables($user, $rolename) {
   global $base_url;
   $variables = array(
-    '%username' => $user->name,
-    '%uid' => $user->uid,
-    '%site' => variable_get('site_name', 'drupal'),
-    '%uri' => $base_url,
-    '%uri_brief' => drupal_substr($base_url, drupal_strlen('http://')),
-    '%mailto' => $user->mail,
-    '%date' => format_date(time()),
-    '%login_uri' => url('user', array('absolute' => TRUE)),
-    '%edit_uri' => url('user/'. $user->uid .'/edit', array('absolute' => TRUE)),
-    '%role' => $rolename,
+    '[name]' => $user->name,
+    '[uid]' => $user->uid,
+    '[site-name]' => variable_get('site_name', 'drupal'),
+    '[site-url]' => $base_url,
+    '[uri_brief]' => drupal_substr($base_url, drupal_strlen($isSSL ? 'https://' : 'http://')),
+    '[mail]' => $user->mail,
+    '[site-date]' => format_date(time()),
+    '[login_uri]' => url('user', array('absolute' => TRUE)),
+    '[account-edit]' => url('user/'. $user->uid .'/edit', array('absolute' => TRUE)),
+    '[role]' => $rolename,
   );
+  // If the profile module is present, create replacement patters for the values
+  if ($user->uid && module_exists('profile')) {
+    profile_load_profile($user);
+    $role_change_user_keys = array_keys(get_object_vars($user));
+    foreach($role_change_user_keys as $keys){
+      if(strpos($keys, 'profile_') === 0){
+        $variables['['.$keys.']'] = check_plain($user->$keys);
+      }
+    }
+  }
   return $variables;
 }
 
+/**
+ * Implementation of hook_token_values().
+ */
+function role_change_notify_token_values($type, $object = NULL) {
+  if ($type == 'user' && is_object($object)) {
+    $user = $object;
+	global $base_url;
+	$isSSL = ($_SERVER['HTTPS'] == 'on' ? TRUE : FALSE);
+	
+	$tokens = array(
+	  'uri_brief' => drupal_substr($base_url, drupal_strlen($isSSL ? 'https://' : 'http://')),
+      'login_uri' => url('user', array('absolute' => TRUE)),
+      'role' => $user->rolename,
+	);
+	
+	if (module_exists('profile')) {
+      $keys = array_keys(get_object_vars($user));
+      foreach($keys as $key){
+        if(strpos($key, 'profile_') === 0){
+          $tokens[$key] = check_plain($user->$key);
+        }
+      }
+    }
+  }
+  return $tokens;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function role_change_notify_token_list($type = 'profile') {
+  if ($type == 'user' || $type == 'all' || $type == 'profile') {
+    if (module_exists('profile')) {
+	  $result = db_query('SELECT title, name FROM profile_fields');
+	  while($row = db_fetch_array($result)){
+	    $tokens['profile'][$row['name']] = t($row['title']);
+	  }
+	}
+	$tokens['global']['uri_brief'] = t('Brief site URL');
+    $tokens['user']['login_uri'] = t('Login URL');
+    $tokens['user']['role'] = t('Name of the role being added');
+    
+    return $tokens;
+  }
+}
\ No newline at end of file
