/
rogue24b3/
rogue24b3/data/
/***************************************************************************\
[*]    ___    ____   ____   __   __  ____ [*]   ROGUE: ROM With Attitude  [*]
[*]   /#/ )  /#/  ) /#/  ) /#/  /#/ /#/   [*]    All rights reserved      [*]
[*]  /#/ <  /#/  / /#/ _  /#/  /#/ /#/--  [*]   Copyright(C) 2000-2001    [*]
[*] /#/   \(#(__/ (#(__/ (#(__/#/ (#(___  [*] Kenneth Conley (Mendanbar)  [*]
[*]  Expression of Digital Creativity..   [*]  scmud@mad.scientist.com    [*]
[-]---------------------------------------+-+-----------------------------[-]
[*] File: webutils.c                                                      [*]
[*] Usage: Creating html files to go in a web directory and viewed online [*]
\***************************************************************************/

/***************************************************************************
 *	Rogue 2.4 is copyright 2000-2001 Kenneth Conley                    *
 *	Rogue has been brought to you by the Rogue consortium              *
 *	    Kenneth Conley (scmud@mad.scientist.com)                       *
 *	By using this code, you have agreed to follow the terms of the     *
 *	Rogue license, in the file Rogue24/doc/rogue.license               *
 ***************************************************************************/

/***************************************************************************
 * Web Utility code written by Mendanbar of Rogue MUD, rogue.dyn.dhs.org   *
 * 3033. Copyright (c) 2001 Kenneth Conley, All Rights Reserved. Use of    *
 * this code is permitted as long as these credits are put in the files    *
 * if you use this or a modified version of the code. If you have any      *
 * questions, comments, or bug fixes email me: scmud@mad.scientist.com     *
 * This code was designed to generate a custom who list for web viewers.   *
 * Color conversion code is a hack of Lope's color modified for the web.   *
 * Web Utility v1.0. Please do not remove this notice from this file.      *
 * - Mendanbar the Implementor, Creator, and Coder of Rogue MUD            *
 ***************************************************************************/

#include "merc.h"

void	web_update	args( ( void ) );
void	gen_who_html	args( ( void ) );
void	gen_wiz_html	args( ( void ) );
bool	web_can_see	args( ( CHAR_DATA *ch ) );
void	webcolorconv	args( ( char *buffer, const char *txt ) );

extern	int		boot_high;
extern	char *	const	wiz_titles[][2];
extern	WIZLIST_DATA	*wiz_list;

#define WHO_HTML	"../who.html"
#define WIZ_HTML	"../wizlist.html"
#define TELNET_ADDR	"telnet://rogue.dyn.dhs.org:3033/"


// Assumes that the viewer is level 0, with no detection
bool web_can_see(CHAR_DATA *ch) {
    if (IS_NPC(ch) || !ch)
	return FALSE;

    if (ch->invis_level)
	return FALSE;

    if (ch->incog_level)
	return FALSE;

    if (AFF_FLAGGED(ch, AFF_INVISIBLE))
	return FALSE;

    return TRUE;
}

void web_update( void ) {
    gen_who_html();

/* I figure wizlist only needs to be generated
 * when the wizlist file is saved. - Mendanbar
 *
    gen_wiz_html();
    mudlogf(NRM, LVL_STAFF, FALSE, "UPDATE: Generating %s file.", WIZ_HTML);
*/
    return;
}

void gen_who_html( void ) {
    FILE *fp;
    DESCRIPTOR_DATA *d;
    time_t ct = time(0);
    char *time_s = asctime(localtime(&ct));
    char	buf[MAX_STRING_LENGTH],
		staff_buf[MAX_STRING_LENGTH],
		player_buf[MAX_STRING_LENGTH],
		who_buf[MAX_STRING_LENGTH*4];
    SInt32	staff = 0, players = 0, sort = 0;

    fclose(fpReserve);
    if (!(fp = fopen(WHO_HTML, "w"))) {
	mudlogf(BRF, LVL_STAFF, TRUE, "SYSERR: gen_who_html() unable to write to %s file.", WHO_HTML);
	return;
    }

    *(time_s + strlen(time_s) -1) = '\0';
    fprintf(fp, "<HTML><HEAD>\n");
    fprintf(fp, "<TITLE>Rogue MUD who list for %s</TITLE></HEAD>\n", time_s);
    fprintf(fp, "<!--\n");
    fprintf(fp, " File generated automatically by Rogue MUD every minute.\n");
    fprintf(fp, "                 rogue.dyn.dhs.org 3033\n");
    fprintf(fp, "-->\n");
    fprintf(fp, "<BODY BGCOLOR=\"#000000\" TEXT=silver LINK=white VLINK=white ALINK=silver>\n");
    fprintf(fp, "<PRE>\n");

    strcpy(staff_buf,	"Staff currently online\n"
			"----------------------\n");
    strcpy(player_buf,	"Players currently online\n"
			"------------------------\n");

    for (sort = MAX_LEVEL; sort >= 0; --sort) {
	for (d = descriptor_list; d != NULL; d = d->next) {
		CHAR_DATA *wch;
		const char *pclass;

		if (STATE(d) != CON_PLAYING)	continue;
		if (!(wch = Original(d)))	continue;
		if (wch->level != sort)		continue;
		if (!web_can_see(wch))		continue;

		pclass = class_table[wch->Class].who_name;

		if (wch->level >= LEVEL_IMMORTAL)
			pclass = get_cat(GET_CATEGORY(wch));

		*buf = '\0';
		if (IS_STAFF(wch)) {
			sprintf(buf, "`y %s`y %s%s%s`n",
			pclass, wch->name, wch->lname, wch->pcdata->title);
		staff++;
		} else if (IS_REAL_HERO(wch)) {
			sprintf(buf, "[%3d %3s %3s] %s%s%s`n",
			wch->hero_level,
			pc_race_table[wch->race].who_name,
			"`YHRO`n", wch->name, wch->lname,
			wch->pcdata->title);
		players++;
		} else {
			sprintf(buf, "[%3d %3s %3s] %s%s%s`n",
			wch->level,
			pc_race_table[wch->race].who_name,
			pclass, wch->name, wch->lname, wch->pcdata->title);
		players++;
		}
		if (IS_SET(wch->act, PLR_KILLER)) strcat(buf, " `R(KILLER)`n");
		if (IS_SET(wch->act, PLR_THIEF))  strcat(buf, " `B(THIEF)`n");
		if (is_clan(wch))
			sprintf(buf + strlen(buf), " <%s>",
				clan_table[wch->clan].who_name);
		if (IS_SET(wch->comm, COMM_AFK)) strcat(buf, " `c[AFK]`n");
		strcat(buf, "`n\n");

		if (IS_STAFF(wch))	strcat(staff_buf, buf);
		else			strcat(player_buf, buf);
	}
    }
    *buf = '\0';
    if (staff) {
	strcat(buf, staff_buf);
	strcat(buf, "\n");
    }
    if (players) {
	strcat(buf, player_buf);
	strcat(buf, "\n");
    }
    if ((staff + players) == 0)
	strcat(buf, "No staff or players are currently visible to you.\n");
    if (staff)
	sprintf(buf+strlen(buf), "There %s %d visible staff%s",
	(staff == 1 ? "is" : "are"), staff, players ? " and there" : ".");
    if (players)
	sprintf(buf+strlen(buf), "%s %s %d visible player%s.",
	staff ? "" : "There", (players == 1 ? "is" : "are"), players, (players == 1 ? "" : "s"));
    strcat(buf, "\n");
    if ((staff + players) > boot_high)
	boot_high = staff+players;
    sprintf(buf+strlen(buf), "There is a boot time high of %d player%s.\n", boot_high, (boot_high == 1 ? "" : "s"));
    sprintf(buf+strlen(buf), "Rogue MUD: `y%s`n</a>\n", TELNET_ADDR);
    webcolorconv(who_buf, buf);

    fprintf(fp, who_buf);
    fprintf(fp, "</PRE><BR><BR><BR>\n");
    fprintf(fp, "</BODY></HTML>\n");
    fclose(fp);
    fpReserve = fopen(NULL_FILE, "r");
    return;
}

void gen_wiz_html( void ) {
    FILE *fp;
    bool found;
    WIZLIST_DATA *pWiz;
    time_t ct = time(0);
    int level, amount, length;
    char *time_s = asctime(localtime(&ct));
    char	buf[MAX_STRING_LENGTH],
		buf2[MAX_STRING_LENGTH],
		wiz_buf[MAX_STRING_LENGTH*2];

    fclose(fpReserve);
    if (!(fp = fopen(WIZ_HTML, "w"))) {
	mudlogf(BRF, LVL_STAFF, TRUE, "SYSERR: gen_wiz_html() unable to write to %s file.", WIZ_HTML);
	return;
    }

    *(time_s + strlen(time_s) -1) = '\0';
    fprintf(fp, "<HTML><HEAD>\n");
    fprintf(fp, "<TITLE>Rogue MUD wizlist for %s</TITLE></HEAD>\n", time_s);
    fprintf(fp, "<!--\n");
    fprintf(fp, " File generated automatically by Rogue MUD.\n");
    fprintf(fp, "           rogue.dyn.dhs.org 3033\n");
    fprintf(fp, "-->\n");
    fprintf(fp, "<BODY BGCOLOR=\"#000000\" TEXT=silver LINK=white VLINK=white ALINK=silver>\n");
    fprintf(fp, "<PRE>\n");

    if (wiz_list == NULL) {
	sprintf(buf, "No immortals are listed at this time.\n");
    } else {
	sprintf(buf, "The staff of <A HREF=\"%s\">Rogue MUD</A>\n", TELNET_ADDR);
	strcat(buf, "**********************\n\n");

	for (level = MAX_LEVEL; level >= LEVEL_IMMORTAL; level--) {
	    amount = 0;
	    found  = FALSE;

	    for (pWiz = wiz_list; pWiz != NULL; pWiz = pWiz->next) {
		if (pWiz->level == level) {
		    amount++;
		    found = TRUE;
		}
	    }

	    if (!found)
		continue;

	    sprintf(buf2, "%-12.12s:   ",
	    wiz_titles[MAX_LEVEL-level][(amount == 1) ? 0 : 1]);
	    strcat(buf, buf2);

	    length = 1;
	    for (pWiz = wiz_list; pWiz != NULL; pWiz = pWiz->next) {
		if (pWiz->level == level) {
		    if (((length % 3) == 1) && (length != 1)) {
			sprintf(buf2, "%15s ", " ");
			strcat(buf, buf2);
		    }
		    sprintf(buf2, "%-12.12s%s", pWiz->name,
			!(length % 3) ? "\n" : " ");
		    strcat(buf, buf2);
		    length++;
		}
	    }
	    if (--length % 3)	strcat(buf, "\n");
	    strcat(buf, "\n");
	}
    }
    webcolorconv(wiz_buf, buf);

    fprintf(fp, wiz_buf);
    fprintf(fp, "Note: If you are not on this list contact Admin.\n");
    fprintf(fp, "</PRE><BR><BR><BR>\n");
    fprintf(fp, "</BODY></HTML>\n");
    fclose(fp);
    fpReserve = fopen(NULL_FILE, "r");
    mudlogf(CMP, LVL_STAFF, TRUE, "UPDATE: Generating %s file.", WIZ_HTML);
    return;
}

/***********************************************************************
 * I used the basic 16 colors using silver as the normal "white" color *
 * which looks a little gray. I have an old HTML 3.2 book that has a   *
 * color table with hex codes and name codes. The name codes seemed to *
 * fit just as well as making my own hex codes and look just like the  *
 * mud color codes so what the hell? - Mendanbar                       *
 ***********************************************************************/
int webcolor(char type, char *string) {
    bool fColor = TRUE;
    char *p = '\0',
	 txt[MAX_INPUT_LENGTH],
	 code[MAX_INPUT_LENGTH];

    switch (type) {
	default:
		sprintf(code, "silver");
		break;
	case 'n':
		sprintf(code, "silver");
		break;
	case 'b':
		sprintf(code, "navy");
		break;
	case 'c':
		sprintf(code, "teal");
		break;
	case 'g':
		sprintf(code, "green");
		break;
	case 'm':
		sprintf(code, "purple");
		break;
	case 'r':
		sprintf(code, "maroon");
		break;
	case 'w':
		sprintf(code, "silver");
		break;
	case 'y':
		sprintf(code, "olive");
		break;
	case 'B':
		sprintf(code, "blue");
		break;
	case 'C':
		sprintf(code, "cyan");
		break;
	case 'G':
		sprintf(code, "lime");
		break;
	case 'M':
		sprintf(code, "magenta");
		break;
	case 'R':
		sprintf(code, "red");
		break;
	case 'W':
		sprintf(code, "white");
		break;
	case 'Y':
		sprintf(code, "yellow");
		break;
	case 'k':
		sprintf(code, "black");
		break;
	case 'K':
		sprintf(code, "gray");
		break;
	case '-':
		sprintf(code, "~");
		fColor = FALSE;
		break;
	case '`':
		sprintf(code, "`");
		fColor = FALSE;
		break;
    }

    if (fColor)
	sprintf(txt, "<FONT COLOR=\"%s\">", code);
    else
	sprintf(txt, "%s", code);

    p = txt;
    while (*p != '\0') {
	*string = *p++;
	*++string = '\0';
    }
    return (strlen(txt));
}

/* Hack of Lope's colourconv() in comm.c
 * - Mendanbar
 */
void webcolorconv(char *buffer, const char *txt) {
    int		skip = 0;
    const char	*point;

    if (txt) {
	for (point = txt; *point; point++) {
		if (*point == '`') {
			point++;
			skip = webcolor(*point, buffer);
			while (skip-- > 0)
				++buffer;
			continue;
		}
		*buffer = *point;
		*++buffer = '\0';
	}
	*buffer = '\0';
    }
    return;
}