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 = "Enables users to send messages to others."
+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,124 @@
+<?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> <message>". Messages will be delivered when the recipient makes any activity.', array('!botname' => variable_get('bot_nickname', 'BOTNAME')));
+  }
+}
+
+/**
+ * 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 = $from_query ? '' : "\s*${bot_name}[\:,-]\s*";
+  $to = $from_query ? $data->nick : $data->channel;
+
+  // Check for tells
+  if (preg_match("/^($addressed)tell (.*)$/i", $data->message, $matches)) {
+    list($recipient, $content) = explode(' ', $matches[2], 2);
+    // See if they're sending a factoid
+    if (module_exists('bot_factoid') && (substr($content, 0, 5) == 'about') && ($factoid = bot_factoid_load(substr($content, 6)))) {
+      bot_tell_save($data->nick, $recipient, time(), $factoid['result']);
+    }
+    else {
+      bot_tell_save($data->nick, $recipient, time(), $content);
+    }
+    $acknowledge = $from_query ? "!recipient will be sent the message." : "!nick: !recipient will be sent the message.";
+    bot_message($to, t($acknowledge, array('!nick' => $data->nick, '!recipient' => $recipient)));
+  }
+
+  // Check messages
+  $messages = bot_tell_load($data->nick);
+  if (!empty($messages)) {
+    $relay = $from_query ? "!from said: !contents (!time)" : "!nick: !from said: !contents (!time)";
+    // Display all the messages to the user
+    foreach ($messages as $message) {
+      bot_message($to, t($relay, array(
+        '!nick' => $data->nick,
+        '!from' => $message->author,
+        '!contents' => $message->contents,
+        '!time' => format_date($message->sent),
+      )));
+    }
+
+    // Remove the messages
+    bot_tell_delete($data->nick);
+  }
+}
+
+/**
+ * All responses are available via a query.
+ */
+function bot_tell_irc_msg_query($data) {
+  bot_tell_irc_msg_channel($data, TRUE);
+}
+
+/**
+ * Delete factoids for the given user.
+ *
+ * @param $recipient
+ *   The recipient of the message.
+ */
+function bot_tell_delete($recipient) {
+  return db_query("DELETE FROM {bot_tell} WHERE recipient LIKE '%s'", $recipient);
+}
+
+/**
+ * Retrieve factoids for the given user.
+ *
+ * @return
+ *    An array of all messages for the recipient.
+ */
+function bot_tell_load($recipient) {
+  $results = db_query("SELECT * FROM {bot_tell} WHERE recipient LIKE '%s'", $recipient);
+
+  // 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');
+}
