Index: bot.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/bot/bot.module,v retrieving revision 1.9.2.9.2.5 diff -u -r1.9.2.9.2.5 bot.module --- bot.module 26 Apr 2008 01:32:30 -0000 1.9.2.9.2.5 +++ bot.module 21 Jun 2008 00:50:43 -0000 @@ -22,7 +22,7 @@ * Implementation of hook_perm(). */ function bot_perm() { - return array('administer bot'); + return array('administer bot', 'set own irc credentials'); } /** @@ -47,6 +47,29 @@ } /** + * Implementation of hook_user(). + */ +function bot_auth_user($op, $edit, &$account, $category = NULL) { + if ($op == 'form' && $category == 'account') { + global $user; + $form['bot'] = array( + '#type' => 'fieldset', + '#title' => t('IRC credentials'), + '#collapsible' => TRUE, + '#weight' => 4, + '#access' => ((user_access('set own irc credentials') && ($account->uid == $user->uid)) || user_access('administer bot')), + ); + $form['bot']['bot_hostname'] = array( + '#type' => 'textfield', + '#title' => t('Hostname'), + '#default_value' => empty($edit['bot_hostname']) ? '' : $edit['bot_hostname'], + '#description' => t('The hostname associated with your IRC account (Example: drupal.org/user/123456/view).') + ); + return $form; + } +} + +/** * Run an IRC-only crontab every five minutes. * * Here, we clear out some of the Drupal caches that are normally @@ -196,3 +219,42 @@ return system_settings_form($form); } + +/** + * Check a given user's credentials. + * + * Looks up the IRC hostname, and compares what is provided on the website. + * + * $user = bot_authenticate($data); + * if (user_access('ahem', $user)) { + * // Do something + * } + * + * @param $data + * The regular $data object prepared by the IRC library. + * @param $reset + * Whether or not we should reload the cached user data + * @return + * The original data object, along with its associated uid. + */ +function bot_authenticate($data, $reset = FALSE) { + static $users = array(); + // Cache the users so that we don't hit the database for every action + if (isset($data->nick)) { + // See if we need to reset the cache + if (!isset($users[$data->nick]) || $reset) { + // Load the user and see if they have the correct hostname information + $user = user_load(array('name' => $data->nick)); + if (!empty($user->bot_hostname) && isset($data->host) && ($data->host == $user->bot_hostname)) { + // Cache the user for later use + $users[$data->nick] = $data; + $users[$data->nick]->uid = $user->uid; + } + } + } + else { + return NULL; + } + // Return the user object if the user is still the same authenticated user + return ($users[$data->nick]->host == $data->host) ? $users[$data->nick] : NULL; +}