/* Filename : emails.c
* Description : save your email-addresses
*
* written : 27-05-1998 - Gunner
* last modified : 27-05-1998 - Gunner
* HTML-Version : 04-02-2000 - Ghorwin
*/
#include <mudlib.h>
#include "/doc/crashcourse/defs.h"
inherit I_COMMAND;
#define HELPDIR "/doc/crashcourse/course08/help/"
#define COMMANDS ({ "list", "add", "remove", "clear", "query" })
/* This is where we keep the data */
mapping emails = ([]);
string
query_action()
{
return "emails";
}
string
short_help()
{
return "This will assist you in keeping email addresses of your friends.\n";
}
string
help()
{
return HELPDIR + "emails";
}
/*
* load_emails : Load the data from an objectfile(.o).
*/
int
load_emails()
{
string filename = source_file_name(this_object());
if (filename[-2..-1] == ".c")
filename = filename[0..-3];
if (file_size(filename+".o") <= 0 || !restore_object(filename))
return 0; // No file to load.
return 1; // We successfully loaded the objectfile.
}
/*
* save_emails : Save the data to an objectfile(.o).
*/
int save_emails()
{
string filename = source_file_name(this_object());
if (filename[-2..-1] == ".c")
filename = filename[0..-3];
if (filename[0..0] != "/")
filename = "/" + filename;
if (!save_object(filename,1))
return 0; // Error saving file. No access, perhaps? Or no directory?
return 1; // We successfully saved the objectfile.
}
// We should load in the datafile as soon as the file is created.
void create()
{
::create();
if (!load_emails() && this_player())
write("Error! Could not read the objectfile(emails).\n");
}
string usage()
{
return "Emails usage:\n"
"emails list - List all emails\n"
"emails add <email> <name> - Add a new entry\n"
"emails remove <email> - Remove an entry\n"
"emails clear - Remove ALL entries\n"
"emails query <name> - Find an email address\n";
}
static int
main(string arg)
{
string command, argument;
if (!arg)
NOT_FAIL(usage());
if (sscanf(arg,"%s %s",command,argument) < 2)
command = lower_case(arg);
else
command = lower_case(command);
if (member_array(command,COMMANDS) < 0)
NOT_FAIL(usage());
switch (command)
{
case "list":
if (sizeof(emails) < 1)
write("You have no email addresses registered.\n");
else
{
string email, name;
printf("%20s %s\n","Email","Name");
foreach(email,name in emails)
printf("%20s %s\n",email,name);
}
break;
case "add":
if (!argument)
write("You need to specify an argument.\n");
else
{
string email, name;
if (sscanf(argument,"%s %s",email,name) != 2)
write("You need to specify both an email address and a name.\n");
else
{
if (!undefinedp(emails[email]))
printf("Old: '%s' '%s'\nNew: '%s' '%s'\n",email,emails[email],
email,name);
else
printf("Email '%s' added as '%s'\n",email,name);
emails[email] = name;
if (!save_emails())
write("Error! Could not save emails database.\n");
}
}
break;
case "remove":
if (!argument)
write("You need to specify an argument.\n");
else
{
if (undefinedp(emails[argument]))
write("There is no such email address registered.\n");
else
{
printf("Email '%s' '%s' was removed.\n",argument,emails[argument]);
map_delete(emails,argument);
if (save_emails())
write("Error! Could not save emails database.\n");
}
}
break;
case "clear":
emails = ([]);
if (!save_emails())
write("Emails: Error saving emails.\n");
else
write("Emails: Cleared emails and saved empty database.\n");
break;
case "query":
if (!argument)
write("You need to specify an argument.\n");
else
{
string email, name;
int matches = 0;
argument = lower_case(argument); // Easier to find...
foreach(email, name in emails)
{
if (strsrch(lower_case(email),argument) >= 0 ||
strsrch(lower_case(name),argument) >= 0)
{
printf("%20s %s\n",email,name);
matches++;
}
}
if (matches == 0)
printf("Could not find any reference to '%s'.\n",argument);
else
printf("Found %d matches.\n",matches);
}
break;
}
return 1;
}