.', array('!url' => url('bot/tell', array('absolute' => TRUE)))); case 'bot/tell': return '

'. t('Browse all messages the bot knows about.') .'

'; } } /** * Implementation of hook_menu(). */ function bot_tell_menu() { $items['bot/tell'] = array( 'access arguments' => array('access content'), 'description' => "Browse all the messages.", 'page callback' => 'bot_tell_overview', 'title' => 'Tells', ); return $items; } /** * Browse all the tells the bot knows about. */ function bot_tell_overview() { $output = NULL; $headers = array(t('From'), t('To'), t('Sent'), t('Contents')); $rows = array(); // storage of gimme-gimme awesomeness. wheedoggy! $results = pager_query('SELECT * FROM {bot_tell} ORDER BY sent', 25); while ($result = db_fetch_object($results)) { $rows[] = array( array('data' => check_plain($result->from), 'class' => 'from'), array('data' => check_plain($result->to), 'class' => 'to'), array('data' => check_plain($result->sent), 'class' => 'sent'), array('data' => check_plain($result->contents), 'class' => 'contents') ); } $output .= theme('table', $headers, $rows, array('id' => 'tell')); $output .= theme('pager', NULL, 25, 0); return $output; } /** * Listen for conversation directed at, or about, the bot. * * @param $data * The regular $data object prepared by the IRC library. * @param $from_query * Boolean; whether this was a queried request. */ function bot_tell_irc_msg_channel($data, $from_query = FALSE) { $bot_name = variable_get('bot_nickname', 'bot_module'); $addressed = "\s*${bot_name}[\:,-]\s*"; // bot mentioned? $to = $from_query ? $data->nick : $data->channel; bot_message($data->message); // look for factoids to answer. // @todo if addressed, give response if no known factoid. /*if (preg_match("/^($addressed)?(.*)[!\?]+$/i", $data->message, $matches)) { // "BOTNAME?" fails as a factoid. this fixes it. $subject = $matches[2] ? $matches[2] : $matches[1]; if ($factoid = bot_factoid_load($subject, $data)) { if (preg_match("/^\/me /", $factoid['result'])) { bot_action($to, str_replace('/me ', '', $factoid['result'])); } else { bot_message($to, $factoid['result']); } } } // @todo allow subject searching and return list of subjects that match? // @todo allow "tell about " private messaging? // look for factoids to delete. if (preg_match("/^($addressed)forget( about)? (.*)$/i", $data->message, $matches)) { bot_factoid_delete($matches[3]); // good night, sweetheart. baaah dum dum dum. it's time to GoOoOooOOoo. bot_message($to, t("!nick: I've forgotten about !subject.", array('!nick' => $data->nick, '!subject' => $matches[3]))); } // @todo delete message should be customizable. this one sucks. // look for factoids to learn. if (bot_factoid_save($data->message)) { bot_message($to, t("!nick: Okay.", array('!nick' => $data->nick))); // @todo success message should be customizable. "Okay." sucks. }*/ } /** * All responses are available via a query. */ function bot_tell_irc_msg_query($data) { bot_tell_irc_msg_channel($data, TRUE); } /** * Delete a factoid. * * @param $mid * The message ID. Enter NULL if it is unknown. * @param $from * The author of the message. * @param $sent * The timestamp of when the message was sent. * @param $to * The recipient of the message. */ function bot_tell_delete($mid = NULL, $from = NULL, $sent = NULL, $to = NULL) { if ($mid) { return db_query("DELETE FROM {bot_tell} WHERE mid = %d", $mid); } $params = array(0 => 'DELETE FROM {bot_tell} WHERE 1 '); if ($from) { $params[0] .= "AND from LIKE '%s' "; $params[] = $from; } if ($sent) { $params[0] .= "AND sent = %d "; $params[] = $sent; } if ($to) { $params[0] .= "AND to LIKE '%s' "; $params[] = $to; } if (count($params) > 1) { return call_user_func_array('db_query', $params); } } /** * Retrieve factoids. */ function bot_tell_load($mid = NULL, $from = NULL, $sent = NULL, $to = NULL, $contents = NULL) { // Create the query $result = NULL; if ($mid) { $results = db_query("SELECT * FROM {bot_tell} WHERE mid = %d", $mid); } else { $params = array(0 => 'SELECT * FROM {bot_tell} WHERE 1 '); if ($from) { $params[0] .= "AND from LIKE '%s' "; $params[] = $from; } if ($sent) { $params[0] .= "AND sent = %d "; $params[] = $sent; } if ($to) { $params[0] .= "AND to LIKE '%s' "; $params[] = $to; } if ($contents) { $params[0] .= "AND contents LIKE '%s' "; $params[] = $contents; } if (count($params) > 1) { $results = call_user_func_array('db_query', $params); } } // Construct the messages $messages = array(); while ($result = db_fetch_object($results)) { $messages[] = $result; } return $messages; } /** * Creates a factoid. * * We expect a raw string right from the IRC user - we'll handle all the * checks for prefacing and "no," and "also", etc. Learning factoids * will only happen if the bot name has been used. * * @param $from * The author of the message. * @param $to * The recipient of the message. * @param $sent * The timestamp of the message. * @param $contents * The contents of the message. * @param $mid * The message ID, if we are updating a message instead of inserting a new one. */ function bot_tell_save($from, $to, $sent, $contents, $mid = NULL) { if ($mid) { return db_query("UPDATE {bot_tell} SET from = '%s', to = '%s', sent = %d, content = '%s') WHERE mid = %d", $from, $to, $sent, $contents, $mid); } return db_query("INSERT INTO {bot_tell} (from, to, sent, content) VALUES ('%s', '%s', %d, '%s')", $from, $to, $sent, $contents); }