/*
*/
#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdarg.h>
#include "merc.h"
#include "quotes.h"
/*
* Globals
*/
char *current_date args ((void));
int nnum_quotes args ((void));
/*
* Local Functions
*/
int maxQuotes;
#define NULLSTR( str ) ( str == NULL || str[0] == '\0' )
NEWQUOTE_DATA *quotes_table;
char *qnshort_date (time_t time)
{
static char buf[20];
char tmp[20];
char *date;
if (time < 0)
{
time = current_time;
}
date = ctime (&time);
tmp[0] = date[4];
tmp[1] = date[5];
tmp[2] = date[6];
tmp[3] = '\0';
if (!str_cmp (tmp, "jan"))
{
buf[0] = '0';
buf[1] = '1';
}
else if (!str_cmp (tmp, "feb"))
{
buf[0] = '0';
buf[1] = '2';
}
else if (!str_cmp (tmp, "mar"))
{
buf[0] = '0';
buf[1] = '3';
}
else if (!str_cmp (tmp, "apr"))
{
buf[0] = '0';
buf[1] = '4';
}
else if (!str_cmp (tmp, "may"))
{
buf[0] = '0';
buf[1] = '5';
}
else if (!str_cmp (tmp, "jun"))
{
buf[0] = '0';
buf[1] = '6';
}
else if (!str_cmp (tmp, "jul"))
{
buf[0] = '0';
buf[1] = '7';
}
else if (!str_cmp (tmp, "aug"))
{
buf[0] = '0';
buf[1] = '8';
}
else if (!str_cmp (tmp, "sep"))
{
buf[0] = '0';
buf[1] = '9';
}
else if (!str_cmp (tmp, "oct"))
{
buf[0] = '1';
buf[1] = '0';
}
else if (!str_cmp (tmp, "nov"))
{
buf[0] = '1';
buf[1] = '1';
}
else if (!str_cmp (tmp, "dec"))
{
buf[0] = '1';
buf[1] = '2';
}
else
{
buf[3] = '9';
buf[4] = '9';
}
buf[2] = '/';
if (date[8] == ' ')
buf[3] = '0';
else
buf[3] = date[8];
buf[4] = date[9];
buf[5] = '/';
buf[6] = date[20];
buf[7] = date[21];
buf[8] = date[22];
buf[9] = date[23];
return buf;
}
void load_quotes (void)
{
FILE *fp;
int i;
if (!(fp = fopen (QUOTES_FILE, "r")))
{
bug ("Could not open Quotes File for reading.", 0);
return;
}
fscanf (fp, "%d\n", &maxQuotes);
/* Use malloc so we can realloc later on */
quotes_table = malloc (sizeof (NEWQUOTE_DATA) * (maxQuotes + 1));
for (i = 0; i < maxQuotes; i++)
{
quotes_table[i].quote = fread_string (fp);
quotes_table[i].coder = fread_string (fp);
quotes_table[i].date = fread_string (fp);
quotes_table[i].mudtime = fread_number (fp);
}
quotes_table[maxQuotes].coder = str_dup ("");
fclose (fp);
return; /* just return */
}
char *qncurrent_date ()
{
static char buf[128];
struct tm *t;
time_t nowtime;
nowtime = time (¤t_time);
t = localtime (&nowtime);
strftime (buf, 100, "%d/%m/%Y", t);
return buf;
}
void save_quotes (void)
{
FILE *fp;
int i;
if (!(fp = fopen (QUOTES_FILE, "w")))
{
perror (QUOTES_FILE);
return;
}
fprintf (fp, "%d\n", maxQuotes);
for (i = 0; i < maxQuotes; i++)
{
fprintf (fp, "%s~\n", quotes_table[i].quote);
fprintf (fp, "%s~\n", quotes_table[i].coder);
fprintf (fp, "%s~\n", quotes_table[i].date);
fprintf (fp, "%ld\n", (long)quotes_table[i].mudtime);
fprintf (fp, "\n");
}
fclose (fp);
return;
}
void delete_quote (int iQuote)
{
int i, j;
NEWQUOTE_DATA *new_table;
new_table = malloc (sizeof (NEWQUOTE_DATA) * maxQuotes);
if (!new_table)
return;
for (i = 0, j = 0; i < maxQuotes + 1; i++)
{
if (i != iQuote)
{
new_table[j] = quotes_table[i];
j++;
}
}
free (quotes_table);
quotes_table = new_table;
maxQuotes--;
save_quotes ();
return;
}
void do_naddquote (CHAR_DATA * ch, char *argument)
{
NEWQUOTE_DATA *new_table;
char buf[MSL];
if (IS_NPC (ch))
return;
if (argument[0] == '\0')
{
send_to_char ("Syntax: Addquote $QuoteString\n\r", ch);
send_to_char ("Type 'quotes' to view the list.#n\n\r", ch);
return;
}
maxQuotes++;
new_table =
realloc (quotes_table, sizeof (NEWQUOTE_DATA) * (maxQuotes + 1));
if (!new_table)
{ /* realloc failed */
send_to_char ("Memory allocation failed. Brace for impact.\n\r", ch);
return;
}
quotes_table = new_table;
quotes_table[maxQuotes - 1].quote = str_dup (argument);
quotes_table[maxQuotes - 1].coder = str_dup (ch->name);
quotes_table[maxQuotes - 1].date = str_dup (current_date ());
quotes_table[maxQuotes - 1].mudtime = current_time;
sprintf (buf, "#wYou added the following quote:\n\r #r%s\n\r",
argument);
stc (buf, ch);
sprintf (buf,
"#c%s #whas added a new quote, type '#yquotes#w' to see what it was",
ch->name);
do_info (NULL, buf);
save_quotes ();
return;
}
void do_qusave (CHAR_DATA * ch, char *argument)
{
char arg1[MAX_INPUT_LENGTH];
char arg2[MAX_INPUT_LENGTH];
char buf[MSL];
argument = one_argument (argument, arg1);
argument = one_argument (argument, arg2);
if (IS_NPC (ch))
return;
if (!ch->desc || NULLSTR (arg1))
{
stc ("Syntax: editquote load/save\n\r", ch);
stc ("Syntax: editquote delete (quote number)\n\r", ch);
stc ("Syntax: editquote rename (quote number)\n\r", ch);
return;
}
else if (!str_cmp (arg1, "load"))
{
load_quotes ();
send_to_char ("Quotes Loaded.\n\r", ch);
return;
}
else if (!str_cmp (arg1, "save"))
{
save_quotes ();
send_to_char ("Quotes Saved.\n\r", ch);
return;
}
else if (!str_cmp (arg1, "delete"))
{
int num;
if (NULLSTR (arg2) || !is_number (arg2))
{
send_to_char
("#wFor editquote delete, you must provide a quote number.#n\n\r",
ch);
send_to_char ("Syntax: editquote delete (quote number)\n\r", ch);
return;
}
num = atoi (arg2);
if (num < 0 || num > maxQuotes)
{
sprintf (buf, "Valid quotes are from 0 to %d.\n\r", maxQuotes);
stc (buf, ch);
return;
}
delete_quote (num);
send_to_char ("Quote deleted.\n\r", ch);
return;
}
else if (strcmp (arg1, "rename") == 0)
{
int num;
if (NULLSTR (arg2) || !is_number (arg2) || NULLSTR (argument))
{
send_to_char
("#yFor editquote rename, you must choose a quote number.#n\n\r",
ch);
send_to_char
("Syntax: editquote rename (quote number) 'New Quote'#n\n\r", ch);
return;
}
num = atoi (arg2);
if (num < 0 || num > maxQuotes)
{
char buf[MAX_STRING_LENGTH];
sprintf (buf, "Valid quotes are from 0 to %d.\n\r", maxQuotes);
send_to_char (buf, ch);
return;
}
quotes_table[num - 1].quote = str_dup (argument);
save_quotes ();
send_to_char ("Quote renamed.\n\r", ch);
return;
}
}
char *qnline_indent (char *text, int wBegin, int wMax)
{
static char buf[MAX_STRING_LENGTH];
char *ptr;
char *ptr2;
int count = 0;
bool stop = FALSE;
int wEnd=0;
buf[0] = '\0';
ptr = text;
ptr2 = buf;
while (!stop)
{
if (count == 0)
{
if (*ptr == '\0')
wEnd = wMax - wBegin;
else if (strlen (ptr) < (wMax - wBegin))
wEnd = wMax - wBegin;
else
{
int x = 0;
while (*(ptr + (wMax - wBegin - x)) != ' ')
x++;
wEnd = wMax - wBegin - (x - 1);
if (wEnd < 1)
wEnd = wMax - wBegin;
}
}
if (count == 0 && *ptr == ' ')
ptr++;
else if (++count != wEnd)
{
if ((*ptr2++ = *ptr++) == '\0')
stop = TRUE;
}
else if (*ptr == '\0')
{
stop = TRUE;
*ptr2 = '\0';
}
else
{
int k;
count = 0;
*ptr2++ = '\n';
*ptr2++ = '\r';
for (k = 0; k < wBegin; k++)
*ptr2++ = ' ';
}
}
return buf;
}
/*
int newquotes (void)
{
char *test;
char quote[100];
int newquotes;
int i;
i = 0;
sprintf (quote, "%ld", ch->pcdata->quoteplace);
newquotes = 0;
test = current_date ();
for (i = 0; i < maxQuotes; i++)
if (!str_cmp (test, quote))
newquotes++;
return newquotes;
}
*/
int nnum_quotes (void)
{
char *test;
int today;
int i;
i = 0;
test = current_date ();
today = 0;
for (i = 0; i < maxQuotes; i++)
if (!str_cmp (test, quotes_table[i].date))
today++;
return today;
}
void do_quotes (CHAR_DATA * ch, char *argument)
{
char arg[MIL], arg2[MIL], buf[MSL], *test;
int i, page = 0, maxpage = 0, today;
int display = 0;
argument = one_argument (argument, arg);
argument = one_argument (argument, arg2);
if (IS_NPC (ch))
return;
if (maxQuotes < 1)
return;
i = 0;
test = current_date ();
today = 0;
for (i = 0; i < maxQuotes; i++)
{
if (!str_cmp (test, quotes_table[i].date))
today++;
}
if (is_number (arg))
page = atoi (arg);
maxpage = (maxQuotes + 9) / 10;
if (page > 0)
{
if (page > maxpage)
{
sprintf (buf, "Show which page 1-%d\n\r", maxpage);
stc (buf, ch);
return;
}
page *= 10;
}
stc ("^1#wNUMBER #cWITNESS #yDATE #wQUOTE #n\n\r", ch);
stc ("#R--------------------------------------------------------------------------------#n\n\r", ch);
if (!str_cmp (arg, "search"))
{
int dsp = 0;
if (arg2[0] == '\0')
{
stc ("What do you want to search for??.\n\r", ch);
return;
}
for (i = 0; i < maxQuotes; i++)
{
if (!str_infix (arg2, quotes_table[i].quote))
// if (strstr(quotes_table[i].quote, arg2))
{
sprintf (buf, "#e[#w%4d#e] #c%-13s #y%10s #c- #w%-52s\n\r",
(++dsp),
quotes_table[i].coder,
quotes_table[i].date,
line_indent (quotes_table[i].quote, 34, 79));
stc (buf, ch);
}
}
if (dsp == 0)
stc ("There is NO match with what you have entered!.\n\r", ch);
stc ("#R--------------------------------------------------------------------------------#n\n\r", ch);
return;
}
for (i = 0; i < maxQuotes; i++)
{
if (page == 0
&& quotes_table[i].mudtime + (2 * 24L * 3600L) < current_time)
continue;
display++;
if (page > 0 && (page > 0 && (i < (page - 10) || i >= page)))
continue;
sprintf (buf, "#e[#w%4d#e] #c%-13s #y%10s #c- #w%-55s\n\r",
(i + 1),
quotes_table[i].coder,
quotes_table[i].date,
line_indent (quotes_table[i].quote, 34, 79));
stc (buf, ch);
}
ch->pcdata->quoteplace = quotes_table[i].mudtime;
stc ("#R--------------------------------------------------------------------------------#n\n\r", ch);
if (today > 0)
sprintf (buf,
"#wThere is a total of [#r%d#w] quotes of which [#c%d#w] %s been added today.#n",
maxQuotes, today, today > 1 ? "have" : "has");
else
sprintf (buf, "#wThere is a total of [#r%d#w] quotes.#n", maxQuotes);
cent_to_char (buf, ch);
stc ("#R--------------------------------------------------------------------------------#n\n\r", ch);
sprintf (buf, "#wTo see pages of quotes use: 'quotes <#c1#w-#r%d#w>'",
maxpage);
cent_to_char (buf, ch);
cent_to_char
("#wTo search all quotes for a quote use: 'quotes search <#cword#w>'#n",
ch);
stc ("#R--------------------------------------------------------------------------------#n\n\r", ch);
}