MySQLMud/data/
MySQLMud/doc/
MySQLMud/log/
MySQLMud/players/
MySQLMud/www/images/
socketmud/data/
socketmud/doc/
socketmud/log/
socketmud/players/
/*
 * notes.c
 *
 * A simple diku-ish note system
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "mud.h"
#include "mysql.h"

/*
 * cmd_note()
 *
 * Primary command for note usage.
 */
void cmd_note(D_MOBILE * ch, char *arg)
{
    char arg1[MAX_BUFFER];
    char buf[MAX_BUFFER];

    arg = one_arg(arg, arg1);

    if (arg1[0] == '\0')
    {
        ptc(ch, "Syntax: note <action> <option>\n");
        ptc(ch, "Current Actions:\n");
        ptc(ch, "   list                - Displays last 15 messages.\n");
        ptc(ch, "   list <#>            - Displays notes from # to # + 15.\n");
        ptc(ch, "   read                - Read next unread message.\n");
        ptc(ch, "   read <#>            - Read message #.\n");
        ptc(ch, "   to <list>           - New note to. (Name, all, admin)\n");
        ptc(ch, "   subject <subject>   - New note subject.\n");
        ptc(ch, "   + <line of txt>     - Add a new line to note.\n");
        ptc(ch, "   show                - Shows current note in progress.\n");
        ptc(ch, "   clear               - Clears current note in progress.\n");
        ptc(ch, "   format              - Formats note properly.\n");
        ptc(ch, "   send / post         - Posts the note in progress.\n");
        return;
    }

    if (compares(arg1, "list"))
    {
        if (arg[0] == '\0')
        {
            note_list(ch, -1);
            return;
        }
        else
        {
            note_list(ch, atoi(arg));
            return;
        }
        return;
    }

    if (compares(arg1, "read"))
    {
        if (arg[0] == '\0')
        {
            note_read(ch, -1);
            return;
        }
        else
        {
            note_read(ch, atoi(arg));
            return;
        }
            
        return;
    }

    if (compares(arg1, "show"))
    {
        note_show(ch);
        return;
    }

    if (compares(arg1, "clear"))
    {
        ptc(ch, "Note cleared.\n");
        note_new(ch);
        return;
    }

    if (compares(arg1, "to"))
    {
        ch->to  = strdup(arg);
        ptc(ch, "Note is now to %s.\n", arg);
        return;
    }

    if (compares(arg1, "subject"))
    {
        ch->subject  = strdup(arg);
        ptc(ch, "Note subject is %s.\n", arg);
        return;
    }

   if(compares( arg1, "+" ))
   {
        strcpy(buf, ch->message);
        if(strlen(buf) + strlen(arg) >= 1048 )
        {
            ptc(ch, "Line too long.\n");
            return;
        }
        strcat(buf, arg);
        strcat(buf, "\n");
        free(ch->message);
        ch->message = strdup(buf);
        ptc(ch, "Line added.\n");
        return;
    }

    if (compares(arg1, "send") || compares(arg1, "post"))
    {
        note_send(ch);
        return;
    }
    else
    {
        cmd_note(ch, "");
        return;
    }
    return;
}

/*
 * note_list()
 *
 * Lists notes, takes argument from_num, which will
 * show next 15 notes starting from #.
 */
void note_list(D_MOBILE * ch, int from_num)
{
    char query[MAX_BUFFER];
    MYSQL_RES *result;

    /*
     * First, handle -1, which just shows LAST 15 messages.
     */
    if (from_num == -1)
    {
        ptc(ch, "Summary of newest 15 notes:\n");
        ptc(ch, "[%-2s] %-10s | %-25s\n", "id", "Sender", "Subject");
        snprintf(query, sizeof(query) +1, "SELECT * FROM `vand_notes` ORDER BY `id` DESC LIMIT 0 , 15");
        MySQLQuery(query);
        result = mysql_store_result(&mysqlconn);
        
        while ((row = mysql_fetch_row(result)))
        {
            ptc(ch, "[%-2s] %-10s | %-25s\n", row[NOTE_ID], row[NOTE_WHO], row[NOTE_SUBJECT]);
        }
        return;
    }
    else
    {
        ptc(ch, "Lazyness has precluded me from doing this part, requires 15+ Dummy notes..\n");
        return;
    }

    return;
}

/*
 * note_read()
 *
 * Reads next unread note, or specific number
 */
void note_read(D_MOBILE * ch, int num)
{
    char query[MAX_BUFFER];
    MYSQL_RES *result;
    int next = 0;

    /*
     * First, handle -1, which will show given number
     */
    if (num != -1)
    {
        snprintf(query, sizeof(query) +1, "SELECT * FROM `vand_notes` WHERE `id` = %d LIMIT 1", num);
        MySQLQuery(query);
        result = mysql_store_result(&mysqlconn);
        
        while ((row = mysql_fetch_row(result)))
        {
            ptc(ch, "[%-s]       %s\n" , row[NOTE_ID], strdup(row[NOTE_TIME]));
            ptc(ch, "Sender:    %s\n"   , strdup(row[NOTE_WHO]));
            ptc(ch, "To:        %s\n"   , strdup(row[NOTE_TO]));
            ptc(ch, "Subject:   %s\n"   , strdup(row[NOTE_SUBJECT]));
            ptc(ch, "---------------\n"); 
            ptc(ch, "%s\n"              , strdup(row[NOTE_MESSAGE]));
        }
        return;
    }
    /*
     * Else.. read next unread note.
     */
    else
    {
        next = ch->last_note;
        next++;
        snprintf(query, sizeof(query) +1, "SELECT * FROM `vand_notes` WHERE `id` = %d LIMIT 1", next);
        MySQLQuery(query);
        result = mysql_store_result(&mysqlconn);
        if( !mysql_num_rows( result ) )
        {
            ptc(ch, "No more unread notes.\n");
            return;
        }
        while ((row = mysql_fetch_row(result)))
        {
            ptc(ch, "[%-s]       %s\n" , row[NOTE_ID], strdup(row[NOTE_TIME]));
            ptc(ch, "Sender:    %s\n"   , strdup(row[NOTE_WHO]));
            ptc(ch, "To:        %s\n"   , strdup(row[NOTE_TO]));
            ptc(ch, "Subject:   %s\n"   , strdup(row[NOTE_SUBJECT]));
            ptc(ch, "---------------\n"); 
            ptc(ch, "%s\n"              , strdup(row[NOTE_MESSAGE]));
            ch->last_note   = atoi(row[NOTE_ID]);
        }
        return;
    }
}

/*
 * note_show()
 *
 * Shows current note in progress.
 */
void note_show(D_MOBILE * ch)
{
    ptc(ch, "Subject:   %s\n", ch->subject);
    ptc(ch, "To:        %s\n", ch->to);
    ptc(ch, "%s\n",            ch->message);
    return;
}

/*
 * note_new()
 *
 * Creates new note_data
 */
void note_new(D_MOBILE * ch)
{
   ch->to           = strdup( "" );
   ch->subject      = strdup( "" );
   ch->message      = strdup( "" );
   return;
}


/*
 * note_send()
 *
 * Attempts to send note in progress.
 * handles MySQL insert, and checks..
 */
void note_send(D_MOBILE * ch)
{
    char query[MAX_BUFFER];
    if (compares(ch->to, ""))
    {
        ptc(ch, "You must send the note to someone...\n");
        ptc(ch, "Use 'note to <people>' to add recipients.\n");
        return;
    }
    if (compares(ch->subject, ""))
    {
        ptc(ch, "There must be a subject.\n");
        ptc(ch, "Use 'note subject <subject>'.\n");
        return;
    }
    if (compares(ch->message, ""))
    {
        ptc(ch, "Only bad things huh? Your message must say something.\n");
        ptc(ch, "Use 'note + <line of txt>'\n");
        return;
    }

    /* ok checks done, sending note. */

    snprintf(query, sizeof(query) + 1, "INSERT INTO `vand_notes` ( `id` , `who` , `to` , `subject` , `message` , `web_message` , `time` ) VALUES ( NULL , '%s', '%s', '%s', '%s', '%s', CURRENT_TIMESTAMP);",
        ch->name, ch->to, ch->subject, ch->message, "Not done yet");
    MySQLQuery(query);
    ptc(ch, "Note posted.\n");
    note_new(ch);
    return;
}

/*
 * is_note_readable()
 *
 * Checks to see if note is viewable to ch
 *
 * Hmm, how the hell to do this...
 */
bool is_note_readable(D_MOBILE * ch, char * to_line)
{
    return 0;
}