Index: bot_tell/bot_tell.info
===================================================================
RCS file: bot_tell/bot_tell.info
diff -N bot_tell/bot_tell.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ bot_tell/bot_tell.info	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,6 @@
+; $Id$
+name = Bot Tell
+package = Bot
+dependencies[] = bot
+description = "Allows messaging between users."
+core = 6.x
Index: bot_tell/bot_tell.module
===================================================================
RCS file: bot_tell/bot_tell.module
diff -N bot_tell/bot_tell.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ bot_tell/bot_tell.module	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,209 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Enables the sending and retrieving of messages.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
+function bot_tell_help($path, $arg) {
+  switch ($path) {
+    case 'irc:features':
+      return array(t('Tell'));
+    case 'irc:features#tell':
+      return t('Send messages with "BOTNAME: tell USERNAME Hello". Recieve your messages with "BOTNAME: messages?"');
+    case 'bot/tell':
+      return '<p>'. t('Browse all messages the bot will send.') .'</p>';
+  }
+}
+
+/**
+ * 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->author), 'class' => 'author'),
+      array('data' => check_plain($result->recipient), 'class' => 'recipient'),
+      array('data' => format_date($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);
+  dvm(bot_tell_load(NULL, NULL, NULL, 'RobLoach'));
+  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;
+
+  // Check for tells
+  if (preg_match("/^($addressed)tell (.*)$/i", $data->message, $matches)) {
+    $author = $data->nick;
+    list($recipient, $content) = explode(' ', $matches[2], 2);
+    bot_tell_save($author, $recipient, time(), $content);
+    bot_message($to, t("!nick: Okay.", array('!nick' => $data->nick)));
+  }
+
+  // Check messages
+  if (preg_match("/^($addressed)messages[?]+$/i", $data->message, $matches)) {
+    $messages = bot_tell_load(NULL, NULL, NULL, $data->nick);
+
+    // Check if the user had no messages
+    if (empty($messages)) {
+      bot_message($to, t("!nick: You have no messages.", array('!nick' => $data->nick)));
+    }
+    else {
+      // Display all the messages to the user
+      foreach ($messages as $message) {
+        bot_message($to, t("!to: (!time) !from: !contents", array(
+          '!to' => $data->nick,
+          '!from' => $message->author,
+          '!contents' => $message->contents,
+          '!time' => format_date($message->sent),
+        )));
+      }
+
+      // Remove messages
+      bot_tell_delete(NULL, NULL, NULL, $data->nick);
+    }
+  }
+}
+
+/**
+ * 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 $author
+ *   The author of the message.
+ * @param $sent
+ *   The timestamp of when the message was sent.
+ * @param $recipient
+ *   The recipient of the message.
+ */
+function bot_tell_delete($mid = NULL, $author = NULL, $sent = NULL, $recipient = NULL) {
+  if ($mid) {
+    return db_query("DELETE FROM {bot_tell} WHERE mid = %d", $mid);
+  }
+  $params = array(0 => 'DELETE FROM {bot_tell} WHERE 1 ');
+  if ($author) {
+    $params[0] .= "AND from LIKE '%s' ";
+    $params[] = $author;
+  }
+  if ($sent) {
+    $params[0] .= "AND sent = %d ";
+    $params[] = $sent;
+  }
+  if ($recipient) {
+    $params[0] .= "AND to LIKE '%s' ";
+    $params[] = $recipient;
+  }
+  if (count($params) > 1) {
+    return call_user_func_array('db_query', $params);
+  }
+}
+
+/**
+ * Retrieve factoids.
+ */
+function bot_tell_load($mid = NULL, $author = NULL, $sent = NULL, $recipient = 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 ($author) {
+      $params[0] .= "AND author LIKE '%s' ";
+      $params[] = $author;
+    }
+    if ($sent) {
+      $params[0] .= "AND sent = %d ";
+      $params[] = $sent;
+    }
+    if ($recipient) {
+      $params[0] .= "AND recipient LIKE '%s' ";
+      $params[] = $recipient;
+    }
+    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 $author
+ *   The author of the message.
+ * @param $recipient
+ *   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($author, $recipient, $sent, $contents, $mid = NULL) {
+  if ($mid) {
+    return db_query("UPDATE {bot_tell} SET author = '%s', recipient = '%s', sent = %d, contents = '%s' WHERE mid = %d", $author, $recipient, $sent, $contents, $mid);
+  }
+  return db_query("INSERT INTO {bot_tell} SET author = '%s', recipient = '%s', sent = %d, contents = '%s'", $author, $recipient, $sent, $contents);
+}
+
Index: bot_tell/bot_tell.install
===================================================================
RCS file: bot_tell/bot_tell.install
diff -N bot_tell/bot_tell.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ bot_tell/bot_tell.install	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,61 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_schema().
+ */
+function bot_tell_schema() {
+  $schema['bot_tell'] = array(
+    'description' => t('The single table necessary for message storage.'),
+    'fields' => array(
+      'mid' => array(
+        'description' => t('Primary Key: The message ID.'),
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'author' => array(
+        'default'     => '',
+        'description' => t('The author, or who wrote the message.'),
+        'length'      => 255,
+        'not null'    => TRUE,
+        'type'        => 'varchar',
+      ),
+      'recipient' => array(
+        'default'     => '',
+        'description' => t('The user who is to be delivered the message.'),
+        'length'      => 255,
+        'not null'    => TRUE,
+        'type'        => 'varchar',
+      ),
+      'contents' => array(
+        'default'     => '',
+        'description' => t('The contents, or what to be delivered.'),
+        'not null'    => TRUE,
+        'type'        => 'text',
+      ),
+      'sent' => array(
+        'type' => 'int',
+        'default' => 0,
+        'description' => t('Timestamp for when the message was sent.'),
+      ),
+    ),
+    'primary key' => array('mid'),
+  );
+
+  return $schema;
+}
+
+/**
+ * Implementation of hook_install().
+ */
+function bot_tell_install() {
+  drupal_install_schema('bot_tell');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function bot_tell_uninstall() {
+  drupal_uninstall_schema('bot_tell');
+}
