imc.c
LOP/src/imc.c · 209.3 KB
From the archive LOP1.34.tgz
Download the archive « Back to the archive contents
/* IMC2 Freedom Client - Developed by Mud Domain.
*
* Copyright (C)2004 by Roger Libiez ( Samson )
* Contributions by Johnathan Walker ( Xorith ), Copyright (C)2004
* Additional contributions by Jesse Defer ( Garil ), Copyright (C)2004
* Additional contributions by Rogel, Copyright (C)2004
* Comments and suggestions welcome: imc@imc2.intermud.us
* License terms are available in the imc2freedom.license file.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
#include <time.h>
#ifdef WIN32
#include <io.h>
#undef EINTR
#undef EMFILE
#define EINTR WSAEINTR
#define EMFILE WSAEMFILE
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#define MAXHOSTNAMELEN 32
#else
#include <fnmatch.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <sys/types.h>
#endif
#include "h/sha256.h"
#include "h/mud.h"
#ifdef WEBSVR
#include "web.h"
#endif
void imc_savehistory( void );
int imchistorysave; /* history saving counter */
int imcwait; /* Reconnect timer */
int imcconnect_attempts; /* How many times have we tried to reconnect? */
unsigned long imc_sequencenumber; /* sequence# for outgoing packets */
bool imcpacketdebug = false;
time_t imcucache_clock; /* prune ucache stuff regularly */
void update_transfer( int type, int size );
void imclog( const char *format, ... ) __attribute__ ( ( format( printf, 1, 2 ) ) );
void imcbug( const char *format, ... ) __attribute__ ( ( format( printf, 1, 2 ) ) );
void imc_printf( CHAR_DATA * ch, const char *fmt, ... ) __attribute__ ( ( format( printf, 2, 3 ) ) );
void imcpager_printf( CHAR_DATA * ch, const char *fmt, ... ) __attribute__ ( ( format( printf, 2, 3 ) ) );
const char *imc_funcname( IMC_FUN * func );
IMC_FUN *imc_function( const char *func );
char *imc_send_social( CHAR_DATA *ch, char *argument, int telloption );
void imc_save_config( void );
void imc_save_channels( void );
const char *const imcperm_names[] =
{
"Notset", "None", "Mort", "Imm", "Admin", "Imp"
};
SITEINFO *this_imcmud;
IMC_CHANNEL *first_imc_channel, *last_imc_channel;
REMOTEINFO *first_rinfo, *last_rinfo;
IMC_BAN *first_imc_ban, *last_imc_ban;
IMCUCACHE_DATA *first_imcucache, *last_imcucache;
IMC_COLOR *first_imc_color, *last_imc_color;
IMC_CMD_DATA *first_imc_command, *last_imc_command;
IMC_HELP_DATA *first_imc_help, *last_imc_help;
IMC_PHANDLER *first_phandler, *last_phandler;
WHO_TEMPLATE *whot;
/*******************************************
* String buffering and logging functions. *
******************************************/
/*
* Got tired of the bug message caused by loading the who list for imc using fread_word s
* this is a copy of it without the bug messages since this is an expected deal
*/
char *imc_fread_word( FILE *fp )
{
static char word[MIL];
char *pword;
char cEnd;
do
{
if( feof( fp ) )
{
word[0] = '\0';
return word;
}
cEnd = getc( fp );
}
while( isspace( cEnd ) );
if( cEnd == '\'' || cEnd == '"' )
{
pword = word;
}
else
{
word[0] = cEnd;
pword = word + 1;
cEnd = ' ';
}
for( ; pword < word + MIL; pword++ )
{
if( feof( fp ) )
{
*pword = '\0';
return word;
}
*pword = getc( fp );
if( cEnd == ' ' ? isspace( *pword ) : *pword == cEnd )
{
if( cEnd == ' ' )
ungetc( *pword, fp );
*pword = '\0';
return word;
}
}
return NULL;
}
/* Generic log function which will route the log messages to the appropriate system logging function */
void imclog( const char *format, ... )
{
char buf[LGST];
va_list ap;
va_start( ap, format );
vsnprintf( buf, sizeof( buf ), format, ap );
va_end( ap );
to_channel( buf, "imclog", PERM_LOG );
fprintf( stderr, "%s :: IMC: %s\n", distime( current_time ), buf );
}
/* Generic bug logging function which will route the message to the appropriate function that handles bug logs */
void imcbug( const char *format, ... )
{
char buf[LGST];
va_list ap;
va_start( ap, format );
vsnprintf( buf, sizeof( buf ), format, ap );
va_end( ap );
to_channel( buf, "imcbug", PERM_LOG );
fprintf( stderr, "%s :: IMC ***BUG***: %s\n", distime( current_time ), buf );
}
/*
* Original Code from SW:FotE 1.1
* Reworked strrep function.
* Fixed a few glaring errors. It also won't overrun the bounds of a string.
* -- Xorith
*/
char *imcstrrep( const char *src, const char *sch, const char *rep )
{
int lensrc = strlen( src ), lensch = strlen( sch ), lenrep = strlen( rep ), x, y, in_p;
static char newsrc[LGST];
bool searching = false;
newsrc[0] = '\0';
for( x = 0, in_p = 0; x < lensrc; x++, in_p++ )
{
if( src[x] == sch[0] )
{
searching = true;
for( y = 0; y < lensch; y++ )
if( src[x + y] != sch[y] )
searching = false;
if( searching )
{
for( y = 0; y < lenrep; y++, in_p++ )
{
if( in_p == ( LGST - 1 ) )
{
newsrc[in_p] = '\0';
return newsrc;
}
if( src[x - 1] == sch[0] )
{
if( rep[0] == '\033' )
{
if( y < lensch )
{
if( y == 0 )
newsrc[in_p - 1] = sch[y];
else
newsrc[in_p] = sch[y];
}
else
y = lenrep;
}
else
{
if( y == 0 )
newsrc[in_p - 1] = rep[y];
newsrc[in_p] = rep[y];
}
}
else
newsrc[in_p] = rep[y];
}
x += lensch - 1;
in_p--;
searching = false;
continue;
}
}
if( in_p == ( LGST - 1 ) )
{
newsrc[in_p] = '\0';
return newsrc;
}
newsrc[in_p] = src[x];
}
newsrc[in_p] = '\0';
return newsrc;
}
char *imcone_argument( char *argument, char *arg_first )
{
char cEnd;
int count;
count = 0;
if( arg_first )
arg_first[0] = '\0';
if( !argument || argument[0] == '\0' )
return NULL;
while( isspace( *argument ) )
argument++;
cEnd = ' ';
if( *argument == '\'' || *argument == '"' )
cEnd = *argument++;
while( *argument != '\0' && ++count <= 255 )
{
if( *argument == cEnd )
{
argument++;
break;
}
if( arg_first )
*arg_first++ = *argument++;
else
argument++;
}
if( arg_first )
*arg_first = '\0';
while( isspace( *argument ) )
argument++;
return argument;
}
/********************************
* User level output functions. *
*******************************/
char *imc_strip_colors( const char *txt )
{
IMC_COLOR *color;
static char tbuf[LGST];
mudstrlcpy( tbuf, txt, sizeof( tbuf ) );
for( color = first_imc_color; color; color = color->next )
mudstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, "" ), sizeof( tbuf ) );
for( color = first_imc_color; color; color = color->next )
mudstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, "" ), sizeof( tbuf ) );
return tbuf;
}
/* Now tell me this isn't cleaner than the mess that was here before. -- Xorith */
/* Yes, Xorith it is. Now, how about this update? Much less hassle with no hardcoded table! -- Samson */
/* convert from imc color -> mud color */
char *color_itom( const char *txt, CHAR_DATA *ch )
{
IMC_COLOR *color;
static char tbuf[LGST];
if( !txt || *txt == '\0' )
return (char *)"";
if( IMCIS_SET( IMCFLAG( ch ), IMC_COLORFLAG ) )
{
mudstrlcpy( tbuf, txt, sizeof( tbuf ) );
for( color = first_imc_color; color; color = color->next )
mudstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, color->mudtag ), sizeof( tbuf ) );
}
else
mudstrlcpy( tbuf, imc_strip_colors( txt ), sizeof( tbuf ) );
return tbuf;
}
/* convert from mud color -> imc color */
char *color_mtoi( const char *txt )
{
IMC_COLOR *color;
static char tbuf[LGST];
if( !txt || *txt == '\0' )
return (char *)"";
mudstrlcpy( tbuf, txt, sizeof( tbuf ) );
for( color = first_imc_color; color; color = color->next )
mudstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, color->imctag ), sizeof( tbuf ) );
return tbuf;
}
/* Generic send_to_char type function to send to the proper code for each codebase */
void imc_to_char( const char *txt, CHAR_DATA *ch )
{
char buf[LGST * 2];
snprintf( buf, sizeof( buf ), "%s\033[0m", color_itom( txt, ch ) );
send_to_char( buf, ch );
}
/* Modified version of Smaug's ch_printf function */
void imc_printf( CHAR_DATA *ch, const char *fmt, ... )
{
char buf[LGST];
va_list args;
va_start( args, fmt );
vsnprintf( buf, sizeof( buf ), fmt, args );
va_end( args );
imc_to_char( buf, ch );
}
/* Generic send_to_pager type function to send to the proper code for each codebase */
void imc_to_pager( const char *txt, CHAR_DATA *ch )
{
char buf[LGST * 2];
snprintf( buf, sizeof( buf ), "%s\033[0m", color_itom( txt, ch ) );
send_to_pager( buf, ch );
}
/* Generic pager_printf type function */
void imcpager_printf( CHAR_DATA *ch, const char *fmt, ... )
{
char buf[LGST];
va_list args;
va_start( args, fmt );
vsnprintf( buf, sizeof( buf ), fmt, args );
va_end( args );
imc_to_pager( buf, ch );
}
/********************************
* Low level utility functions. *
********************************/
bool imcstr_prefix( const char *astr, const char *bstr )
{
if( !astr )
{
imcbug( "Strn_cmp: null astr." );
return true;
}
if( !bstr )
{
imcbug( "Strn_cmp: null bstr." );
return true;
}
for( ; *astr; astr++, bstr++ )
{
if( LOWER( *astr ) != LOWER( *bstr ) )
return true;
}
return false;
}
/* Returns an initial-capped string. */
char *imccapitalize( const char *str )
{
static char strcap[LGST];
int i;
for( i = 0; str[i] != '\0'; i++ )
strcap[i] = tolower( str[i] );
strcap[i] = '\0';
strcap[0] = toupper( strcap[0] );
return strcap;
}
/* Does the list have the member in it? */
bool imc_hasname( char *list, char *member )
{
if( !list || list[0] == '\0' )
return false;
if( !strstr( list, member ) )
return false;
return true;
}
/* Add a new member to the list, provided it's not already there */
void imc_addname( char **list, char *member )
{
char newlist[LGST];
if( imc_hasname( *list, member ) )
return;
if( !( *list ) || *list[0] == '\0' )
mudstrlcpy( newlist, member, sizeof( newlist ) );
else
snprintf( newlist, sizeof( newlist ), "%s %s", *list, member );
STRFREE( *list );
*list = STRALLOC( newlist );
}
void imc_fixname( char **list )
{
char newlist[LGST];
char *buf;
if( imc_hasname( *list, (char *)" " ) )
mudstrlcpy( newlist, imcstrrep( *list, " ", " " ), sizeof( newlist ) );
else if( *list && *list[0] != '\0' )
mudstrlcpy( newlist, *list, sizeof( newlist ) );
else
mudstrlcpy( newlist, "", sizeof( newlist ) );
buf = newlist;
if( buf && buf[0] != '\0' )
{
while( isspace( *buf ) )
buf++;
mudstrlcpy( newlist, buf, sizeof( newlist ) );
}
STRFREE( *list );
*list = STRALLOC( newlist );
}
/* Remove a member from a list, provided it's there. */
void imc_removename( char **list, char *member )
{
char newlist[LGST];
if( !imc_hasname( *list, member ) )
return;
mudstrlcpy( newlist, imcstrrep( *list, member, "" ), sizeof( newlist ) );
STRFREE( *list );
*list = STRALLOC( newlist );
imc_fixname( &*list );
}
char *imc_nameof( char *src )
{
static char name[SMST];
size_t x;
for( x = 0; x < strlen( src ); x++ )
{
if( src[x] == '@' )
break;
name[x] = src[x];
}
name[x] = '\0';
return name;
}
char *imc_mudof( char *src )
{
static char mud[SMST];
char *person;
if( !( person = strchr( src, '@' ) ) )
mudstrlcpy( mud, src, sizeof( mud ) );
else
mudstrlcpy( mud, person + 1, sizeof( mud ) );
return mud;
}
char *imc_channel_mudof( char *src )
{
static char mud[SMST];
size_t x;
for( x = 0; x < strlen( src ); x++ )
{
if( src[x] == ':' )
{
mud[x] = '\0';
break;
}
mud[x] = src[x];
}
return mud;
}
char *imc_channel_nameof( char *src )
{
static char name[SMST];
size_t x, y = 0;
bool colon = false;
for( x = 0; x < strlen( src ); x++ )
{
if( src[x] == ':' )
{
colon = true;
continue;
}
if( !colon )
continue;
name[y++] = src[x];
}
name[x] = '\0';
return name;
}
char *imc_makename( char *person, char *mud )
{
static char name[SMST];
snprintf( name, sizeof( name ), "%s@%s", person, mud );
return name;
}
char *escape_string( const char *src )
{
static char newstr[LGST];
size_t x, y = 0;
bool quote = false, endquote = false;
if( strchr( src, ' ' ) )
{
quote = true;
endquote = true;
}
for( x = 0; x < strlen( src ); x++ )
{
if( src[x] == '=' && quote )
{
newstr[y] = '=';
newstr[++y] = '"';
quote = false;
}
else if( src[x] == '\r' )
{
newstr[y] = '\\';
newstr[++y] = 'r';
}
else if( src[x] == '\n' )
{
newstr[y] = '\\';
newstr[++y] = 'n';
}
else if( src[x] == '\\' )
{
newstr[y] = '\\';
newstr[++y] = '\\';
}
else if( src[x] == '"' )
{
newstr[y] = '\\';
newstr[++y] = '"';
}
else
newstr[y] = src[x];
y++;
}
if( endquote )
newstr[y++] = '"';
newstr[y] = '\0';
return newstr;
}
/* Returns a CHAR_DATA class which matches the string */
CHAR_DATA *imc_find_user( char *name )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *vch = NULL;
for( d = first_descriptor; d; d = d->next )
{
if( ( vch = d->character ) && !strcasecmp( CH_IMCNAME( vch ), name )
&& d->connected == CON_PLAYING )
return vch;
}
return NULL;
}
char *imcgetname( char *from )
{
static char buf[SMST];
char *mud, *name;
mud = imc_mudof( from );
name = imc_nameof( from );
if( !strcasecmp( mud, this_imcmud->localname ) )
mudstrlcpy( buf, imc_nameof( name ), sizeof( buf ) );
else
mudstrlcpy( buf, from, sizeof( buf ) );
return buf;
}
/* check if a packet from a given source should be ignored */
bool imc_isbanned( char *who )
{
IMC_BAN *mud;
for( mud = first_imc_ban; mud; mud = mud->next )
{
if( !strcasecmp( mud->name, imc_mudof( who ) ) )
return true;
}
return false;
}
/* Beefed up to include wildcard ignores. */
bool imc_isignoring( CHAR_DATA * ch, const char *ignore )
{
IMC_IGNORE *temp;
/* Wildcard support thanks to Xorith */
for( temp = FIRST_IMCIGNORE( ch ); temp; temp = temp->next )
{
#ifndef WIN32
if( !fnmatch( temp->name, ignore, 0 ) )
#else
if( !str_prefix( temp->name, ignore ) )
#endif
return true;
}
return false;
}
/* There should only one of these..... */
void imc_delete_info( void )
{
STRFREE( this_imcmud->servername );
STRFREE( this_imcmud->rhost );
STRFREE( this_imcmud->network );
STRFREE( this_imcmud->clientpw );
STRFREE( this_imcmud->serverpw );
DISPOSE( this_imcmud->outbuf );
STRFREE( this_imcmud->localname );
STRFREE( this_imcmud->fullname );
STRFREE( this_imcmud->ihost );
STRFREE( this_imcmud->email );
STRFREE( this_imcmud->www );
STRFREE( this_imcmud->details );
STRFREE( this_imcmud->versionid );
STRFREE( this_imcmud->base );
DISPOSE( this_imcmud );
}
/* delete the info entry "p" */
void imc_delete_reminfo( REMOTEINFO * p )
{
UNLINK( p, first_rinfo, last_rinfo, next, prev );
STRFREE( p->name );
STRFREE( p->version );
STRFREE( p->network );
STRFREE( p->path );
STRFREE( p->url );
STRFREE( p->port );
STRFREE( p->host );
DISPOSE( p );
}
/* create a new info entry, insert into list */
void imc_new_reminfo( char *mud, char *version, char *netname, char *url, char *path )
{
REMOTEINFO *p, *mud_prev;
CREATE( p, REMOTEINFO, 1 );
p->name = STRALLOC( mud );
if( !url || url[0] == '\0' )
p->url = STRALLOC( "Unknown" );
else
p->url = STRALLOC( url );
if( !version || version[0] == '\0' )
p->version = STRALLOC( "Unknown" );
else
p->version = STRALLOC( version );
if( !netname || netname[0] == '\0' )
p->network = STRALLOC( this_imcmud->network );
else
p->network = STRALLOC( netname );
if( !path || path[0] == '\0' )
p->path = STRALLOC( "UNKNOWN" );
else
p->path = STRALLOC( path );
p->expired = false;
for( mud_prev = first_rinfo; mud_prev; mud_prev = mud_prev->next )
if( strcasecmp( mud_prev->name, mud ) >= 0 )
break;
if( !mud_prev )
LINK( p, first_rinfo, last_rinfo, next, prev );
else
INSERT( p, mud_prev, first_rinfo, next, prev );
}
/* find an info entry for "name" */
REMOTEINFO *imc_find_reminfo( char *name )
{
REMOTEINFO *p;
for( p = first_rinfo; p; p = p->next )
{
if( !strcasecmp( name, p->name ) )
return p;
}
return NULL;
}
bool check_mud( CHAR_DATA * ch, char *mud )
{
REMOTEINFO *r = imc_find_reminfo( mud );
if( !r )
{
imc_printf( ch, "~W%s ~cis not a valid mud name.\r\n", mud );
return false;
}
if( r->expired )
{
imc_printf( ch, "~W%s ~cis not connected right now.\r\n", r->name );
return false;
}
return true;
}
bool check_mudof( CHAR_DATA * ch, char *mud )
{
return check_mud( ch, imc_mudof( mud ) );
}
int get_imcpermvalue( const char *flag )
{
unsigned int x;
for( x = 0; x < ( sizeof( imcperm_names ) / sizeof( imcperm_names[0] ) ); x++ )
if( !strcasecmp( flag, imcperm_names[x] ) )
return x;
return -1;
}
bool imccheck_permissions( CHAR_DATA * ch, int checkvalue, int targetvalue, bool enforceequal )
{
if( checkvalue < 0 || checkvalue > IMCPERM_IMP )
{
imc_to_char( "Invalid permission setting.\r\n", ch );
return false;
}
if( checkvalue > IMCPERM( ch ) )
{
imc_to_char( "You can't set permissions higher than your own.\r\n", ch );
return false;
}
if( checkvalue == IMCPERM( ch ) && IMCPERM( ch ) != IMCPERM_IMP && enforceequal )
{
imc_to_char( "You can't set permissions equal to your own. Someone higher up must do this.\r\n", ch );
return false;
}
if( IMCPERM( ch ) < targetvalue )
{
imc_to_char( "You can't alter the permissions of someone or something above your own.\r\n", ch );
return false;
}
return true;
}
IMC_BAN *imc_newban( void )
{
IMC_BAN *ban;
CREATE( ban, IMC_BAN, 1 );
ban->name = NULL;
LINK( ban, first_imc_ban, last_imc_ban, next, prev );
return ban;
}
void imc_addban( char *what )
{
IMC_BAN *ban;
ban = imc_newban( );
ban->name = STRALLOC( what );
}
void imc_freeban( IMC_BAN * ban )
{
STRFREE( ban->name );
UNLINK( ban, first_imc_ban, last_imc_ban, next, prev );
DISPOSE( ban );
}
bool imc_delban( const char *what )
{
IMC_BAN *ban, *ban_next;
for( ban = first_imc_ban; ban; ban = ban_next )
{
ban_next = ban->next;
if( !strcasecmp( what, ban->name ) )
{
imc_freeban( ban );
return true;
}
}
return false;
}
IMC_CHANNEL *imc_findchannel( char *name )
{
IMC_CHANNEL *c;
for( c = first_imc_channel; c; c = c->next )
if( ( c->name && !strcasecmp( c->name, name ) ) || ( c->local_name && !strcasecmp( c->local_name, name ) ) )
return c;
return NULL;
}
void imc_freechan( IMC_CHANNEL *c )
{
int x;
if( !c )
{
imcbug( "%s: Freeing NULL channel!", __FUNCTION__ );
return;
}
UNLINK( c, first_imc_channel, last_imc_channel, next, prev );
STRFREE( c->name );
STRFREE( c->owner );
STRFREE( c->operators );
STRFREE( c->invited );
STRFREE( c->excluded );
STRFREE( c->local_name );
STRFREE( c->regformat );
STRFREE( c->emoteformat );
STRFREE( c->socformat );
for( x = 0; x < MAX_IMCHISTORY; x++ )
STRFREE( c->history[x] );
DISPOSE( c );
}
void imcformat_channel( CHAR_DATA *ch, IMC_CHANNEL *d, int format, bool all )
{
IMC_CHANNEL *c = NULL;
char buf[LGST];
if( all )
{
for( c = first_imc_channel; c; c = c->next )
{
if( !c->local_name || c->local_name[0] == '\0' )
continue;
if( format == 1 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~C%%s: ~c%%s", c->local_name );
STRFREE( c->regformat );
c->regformat = STRALLOC( buf );
}
if( format == 2 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~c%%s %%s", c->local_name );
STRFREE( c->emoteformat );
c->emoteformat = STRALLOC( buf );
}
if( format == 3 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~c%%s", c->local_name );
STRFREE( c->socformat );
c->socformat = STRALLOC( buf );
}
}
}
else
{
if( ch && ( !d->local_name || d->local_name[0] == '\0' ) )
{
imc_to_char( "This channel is not yet locally configured.\r\n", ch );
return;
}
if( format == 1 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~C%%s: ~c%%s", d->local_name );
STRFREE( d->regformat );
d->regformat = STRALLOC( buf );
}
if( format == 2 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~c%%s %%s", d->local_name );
STRFREE( d->emoteformat );
d->emoteformat = STRALLOC( buf );
}
if( format == 3 || format == 4 )
{
snprintf( buf, sizeof( buf ), "~R[~Y%s~R] ~c%%s", d->local_name );
STRFREE( d->socformat );
d->socformat = STRALLOC( buf );
}
}
imc_save_channels( );
}
void imc_new_channel( char *chan, char *owner, char *ops, char *invite, char *exclude, bool copen, int perm, char *lname )
{
IMC_CHANNEL *c;
if( !chan || chan[0] == '\0' )
{
imclog( "%s: NULL channel name received, skipping", __FUNCTION__ );
return;
}
if( !strchr( chan, ':' ) )
{
imclog( "%s: Improperly formatted channel name: %s", __FUNCTION__, chan );
return;
}
CREATE( c, IMC_CHANNEL, 1 );
c->name = STRALLOC( chan );
c->owner = STRALLOC( owner );
c->operators = STRALLOC( ops );
c->invited = STRALLOC( invite );
c->excluded = STRALLOC( exclude );
if( lname && lname[0] != '\0' )
c->local_name = STRALLOC( lname );
else
c->local_name = imc_channel_nameof( c->name );
c->level = perm;
c->refreshed = true;
c->open = copen;
LINK( c, first_imc_channel, last_imc_channel, next, prev );
imcformat_channel( NULL, c, 4, false );
}
/* Read to end of line into static buffer [Taken from Smaug's fread_line] */
char *imcfread_line( FILE *fp )
{
char line[LGST], *pline, c;
int ln;
pline = line;
line[0] = '\0';
ln = 0;
/* Skip blanks. Read first char. */
do
{
if( feof( fp ) )
{
imcbug( "%s: EOF encountered on read.", __FUNCTION__ );
mudstrlcpy( line, "", sizeof( line ) );
return STRALLOC( line );
}
c = getc( fp );
}
while( isspace( c ) );
ungetc( c, fp );
do
{
if( feof( fp ) )
{
imcbug( "%s: EOF encountered on read.", __FUNCTION__ );
*pline = '\0';
return STRALLOC( line );
}
c = getc( fp );
*pline++ = c;
ln++;
if( ln >= ( LGST - 1 ) )
{
imcbug( "%s: line too long", __FUNCTION__ );
break;
}
}
while( c != '\r' && c != '\n' );
do
{
c = getc( fp );
}
while( c == '\r' || c == '\n' );
ungetc( c, fp );
pline--;
*pline = '\0';
/* Since tildes generally aren't found at the end of lines, this seems workable. Will enable reading old configs. */
if( line[strlen( line ) - 1] == '~' )
line[strlen( line ) - 1] = '\0';
return STRALLOC( line );
}
/******************************************
* Packet handling and routing functions. *
******************************************/
void imc_register_packet_handler( const char *name, PACKET_FUN *func )
{
IMC_PHANDLER *ph;
for( ph = first_phandler; ph; ph = ph->next )
{
if( !strcasecmp( ph->name, name ) )
{
imclog( "Unable to register packet type %s. Another module has already registered it.", name );
return;
}
}
CREATE( ph, IMC_PHANDLER, 1 );
ph->name = STRALLOC( name );
ph->func = func;
LINK( ph, first_phandler, last_phandler, next, prev );
}
void imc_freepacket( IMC_PACKET *p )
{
IMC_PDATA *data, *data_next;
for( data = p->first_data; data; data = data_next )
{
data_next = data->next;
UNLINK( data, p->first_data, p->last_data, next, prev );
DISPOSE( data );
}
DISPOSE( p );
}
int find_next_esign( const char *string, int current )
{
bool quote = false;
if( string[current] == '=' )
current++;
for( ; string[current] != '\0'; current++ )
{
if( string[current] == '\\' && string[current + 1] == '"' )
{
current++;
continue;
}
if( string[current] == '"' )
quote = !quote;
if( string[current] == '=' && !quote )
break;
}
if( string[current] == '\0' )
return -1;
return current;
}
char *imc_getData( char *output, const char *key, const char *packet )
{
int current = 0;
unsigned int i = 0;
bool quote = false;
output[0] = '\0';
if( !packet || packet[0] == '\0' || !key || key[0] == '\0' )
{
imcbug( "%s: Invalid input", __FUNCTION__ );
return output;
}
while( ( current = find_next_esign( packet, current ) ) >= 0 )
{
if( strlen( key ) > ( unsigned int )current )
continue;
i = current - strlen( key );
if( strncasecmp( &packet[i], key, strlen( key ) ) == 0 )
break;
}
if( current < 0 )
return output;
current++;
if( packet[current] == '"' )
{
quote = true;
current++;
}
for( i = 0; packet[current] != '\0'; current++ )
{
if( packet[current] == '"' && quote )
break;
if( packet[current] == ' ' && !quote )
break;
if( packet[current] != '\\' )
{
output[i++] = packet[current];
continue;
}
current++;
if( packet[current] == 'r' )
output[i++] = '\r';
else if( packet[current] == 'n' )
output[i++] = '\n';
else if( packet[current] == '"' )
output[i++] = '"';
else if( packet[current] == '\\' )
output[i++] = '\\';
else
output[i++] = packet[current];
}
output[i] = '\0';
return output;
}
void imc_write_buffer( const char *txt )
{
char output[IMC_BUFF_SIZE];
size_t length;
/* This should never happen */
if( !this_imcmud || this_imcmud->desc < 1 )
{
imcbug( "%s: Configuration or socket is invalid!", __FUNCTION__ );
return;
}
/* This should never happen either */
if( !this_imcmud->outbuf )
{
imcbug( "%s: Output buffer has not been allocated!", __FUNCTION__ );
return;
}
snprintf( output, sizeof( output ), "%s\r\n", txt );
length = strlen( output );
/* Expand the buffer as needed. */
while( this_imcmud->outtop + length >= this_imcmud->outsize )
{
if( this_imcmud->outsize > 64000 )
{
/* empty buffer */
this_imcmud->outtop = 0;
imcbug( "Buffer overflow: %ld. Purging.", this_imcmud->outsize );
return;
}
this_imcmud->outsize *= 2;
RECREATE( this_imcmud->outbuf, char, this_imcmud->outsize );
}
/* Copy. */
strncpy( this_imcmud->outbuf + this_imcmud->outtop, output, length ); /* Leave this one alone! BAD THINGS(TM) will happen if you don't! */
this_imcmud->outtop += length;
this_imcmud->outbuf[this_imcmud->outtop] = '\0';
}
/* Convert a packet to text to then send to the buffer */
void imc_write_packet( IMC_PACKET *p )
{
IMC_PDATA *data;
char txt[IMC_BUFF_SIZE];
/* Assemble your buffer, and at the same time disassemble the packet struct to free the memory */
snprintf( txt, sizeof( txt ), "%s %lu %s %s %s", p->from, ++imc_sequencenumber, this_imcmud->localname, p->type, p->to );
for( data = p->first_data; data; data = data->next )
snprintf( txt + strlen( txt ), sizeof( txt ) - strlen( txt ), "%s", data->field );
imc_freepacket( p );
imc_write_buffer( txt );
}
void imc_addtopacket( IMC_PACKET *p, const char *fmt, ... )
{
IMC_PDATA *data;
char pkt[IMC_BUFF_SIZE];
va_list args;
va_start( args, fmt );
vsnprintf( pkt, sizeof( pkt ), fmt, args );
va_end( args );
CREATE( data, IMC_PDATA, 1 );
snprintf( data->field, sizeof( data->field ), " %s", escape_string( pkt ) );
LINK( data, p->first_data, p->last_data, next, prev );
}
IMC_PACKET *imc_newpacket( const char *from, const char *type, const char *to )
{
IMC_PACKET *p;
if( !type || type[0] == '\0' )
{
imcbug( "%s: Attempt to build packet with no type field.", __FUNCTION__ );
return NULL;
}
if( !from || from[0] == '\0' )
{
imcbug( "%s: Attempt to build %s packet with no from field.", __FUNCTION__, type );
return NULL;
}
if( !to || to[0] == '\0' )
{
imcbug( "%s: Attempt to build %s packet with no to field.", __FUNCTION__, type );
return NULL;
}
CREATE( p, IMC_PACKET, 1 );
snprintf( p->from, sizeof( p->from ), "%s@%s", from, this_imcmud->localname );
mudstrlcpy( p->type, type, sizeof( p->type ) );
mudstrlcpy( p->to, to, sizeof( p->to ) );
p->first_data = p->last_data = NULL;
return p;
}
void imc_update_tellhistory( CHAR_DATA *ch, const char *msg )
{
char new_msg[LGST];
struct tm *local = localtime( ¤t_time );
int x;
snprintf( new_msg, sizeof( new_msg ), "~R[%-2.2d:%-2.2d] %s", local->tm_hour, local->tm_min, msg );
for( x = 0; x < MAX_IMCTELLHISTORY; x++ )
{
if( IMCTELLHISTORY( ch, x ) == '\0' )
{
IMCTELLHISTORY( ch, x ) = STRALLOC( new_msg );
break;
}
if( x == MAX_IMCTELLHISTORY - 1 )
{
int i;
for( i = 1; i < MAX_IMCTELLHISTORY; i++ )
{
STRFREE( IMCTELLHISTORY( ch, i - 1 ) );
IMCTELLHISTORY( ch, i - 1 ) = STRALLOC( IMCTELLHISTORY( ch, i ) );
}
STRFREE( IMCTELLHISTORY( ch, x ) );
IMCTELLHISTORY( ch, x ) = STRALLOC( new_msg );
}
}
}
void imc_send_tell( const char *from, char *to, char *txt, int reply )
{
IMC_PACKET *p;
p = imc_newpacket( (char *)from, "tell", to );
imc_addtopacket( p, "text=%s", txt );
if( reply > 0 )
imc_addtopacket( p, "isreply=%d", reply );
imc_write_packet( p );
}
PFUN( imc_recv_tell )
{
CHAR_DATA *vic;
char txt[LGST], isreply[SMST], buf[LGST];
int reply;
imc_getData( txt, "text", packet );
imc_getData( isreply, "isreply", packet );
reply = atoi( isreply );
if( reply < 0 || reply > 2 )
reply = 0;
if( !( vic = imc_find_user( imc_nameof( q->to ) ) ) || IMCPERM( vic ) < IMCPERM_MORT )
{
snprintf( buf, sizeof( buf ), "No player named %s exists here.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
return;
}
if( strcasecmp( imc_nameof( q->from ), "ICE" ) )
{
if( IMCISINVIS( vic ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving tells.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( imc_isignoring( vic, q->from ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving tells.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( IMCIS_SET( IMCFLAG( vic ), IMC_TELL ) || IMCIS_SET( IMCFLAG( vic ), IMC_DENYTELL ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving tells.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( IMCAFK( vic ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is currently AFK. Try back later.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
STRFREE( IMC_RREPLY( vic ) );
STRFREE( IMC_RREPLY_NAME( vic ) );
IMC_RREPLY( vic ) = STRALLOC( q->from );
IMC_RREPLY_NAME( vic ) = STRALLOC( imcgetname( q->from ) );
}
}
/* Tell social */
if( reply == 2 )
snprintf( buf, sizeof( buf ), "~WImctell: ~c%s\r\n", txt );
else
snprintf( buf, sizeof( buf ), "~C%s ~cimctells you ~c'~W%s~c'~!\r\n", imcgetname( q->from ), txt );
imc_to_char( buf, vic );
imc_update_tellhistory( vic, buf );
}
PFUN( imc_recv_emote )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *ch;
char txt[LGST], lvl[SMST];
int level;
imc_getData( txt, "text", packet );
imc_getData( lvl, "level", packet );
level = get_imcpermvalue( lvl );
if( level < 0 || level > IMCPERM_IMP )
level = IMCPERM_IMM;
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( ch = d->character ) && IMCPERM( ch ) >= level )
imc_printf( ch, "~p[~GIMC~p] %s %s\r\n", imcgetname( q->from ), txt );
}
}
void update_imchistory( IMC_CHANNEL *channel, char *message )
{
char msg[LGST], buf[LGST];
struct tm *local;
int x;
if( !channel )
{
imcbug( "%s: NULL channel received!", __FUNCTION__ );
return;
}
if( !message || message[0] == '\0' )
{
imcbug( "%s: NULL message received!", __FUNCTION__ );
return;
}
mudstrlcpy( msg, message, sizeof( msg ) );
for( x = 0; x < MAX_IMCHISTORY; x++ )
{
if( !channel->history[x] )
{
local = localtime( ¤t_time );
snprintf( buf, sizeof( buf ), "~R[%-2.2d/%-2.2d %-2.2d:%-2.2d] ~G%s",
local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, msg );
channel->history[x] = STRALLOC( buf );
if( IMCIS_SET( channel->flags, IMCCHAN_LOG ) )
{
FILE *fp;
snprintf( buf, sizeof( buf ), "%s%s.log", IMC_DIR, channel->local_name );
if( !( fp = fopen( buf, "a" ) ) )
{
perror( buf );
imcbug( "Could not open file %s!", buf );
}
else
{
fprintf( fp, "%s\n", imc_strip_colors( channel->history[x] ) );
IMCFCLOSE( fp );
}
}
break;
}
if( x == MAX_IMCHISTORY - 1 )
{
int y;
for( y = 1; y < MAX_IMCHISTORY; y++ )
{
int z = y - 1;
if( channel->history[z] )
{
STRFREE( channel->history[z] );
channel->history[z] = STRALLOC( channel->history[y] );
}
}
local = localtime( ¤t_time );
snprintf( buf, sizeof( buf ), "~R[%-2.2d/%-2.2d %-2.2d:%-2.2d] ~G%s",
local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, msg );
STRFREE( channel->history[x] );
channel->history[x] = STRALLOC( buf );
if( IMCIS_SET( channel->flags, IMCCHAN_LOG ) )
{
FILE *fp;
snprintf( buf, sizeof( buf ), "%s%s.log", IMC_DIR, channel->local_name );
if( !( fp = fopen( buf, "a" ) ) )
{
perror( buf );
imcbug( "Could not open file %s!", buf );
}
else
{
fprintf( fp, "%s\n", imc_strip_colors( channel->history[x] ) );
IMCFCLOSE( fp );
}
}
}
}
if( ++imchistorysave >= 20 )
{
imc_savehistory( );
imchistorysave = 0;
}
}
void imc_display_channel( IMC_CHANNEL *c, const char *from, char *txt, int emote )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *ch;
char buf[LGST], name[SMST];
if( !c->local_name || c->local_name[0] == '\0' || !c->refreshed )
return;
if( emote < 2 )
snprintf( buf, sizeof( buf ), emote ? c->emoteformat : c->regformat, from, txt );
else if( strstr( txt, from ) )
snprintf( buf, sizeof( buf ), c->socformat, txt );
else
{
char tbuf[LGST];
snprintf( tbuf, sizeof( tbuf ), "%s sent: %s", from, txt );
snprintf( buf, sizeof( buf ), c->socformat, tbuf );
}
for( d = first_descriptor; d; d = d->next )
{
ch = d->character;
if( !ch || d->connected != CON_PLAYING )
continue;
if( is_npc( ch ) )
continue;
if( IMCPERM( ch ) < c->level || !imc_hasname( IMC_LISTEN( ch ), c->local_name ) )
continue;
if( !c->open )
{
snprintf( name, sizeof( name ), "%s@%s", CH_IMCNAME( ch ), this_imcmud->localname );
if( !imc_hasname( c->invited, name ) && strcasecmp( c->owner, name ) )
continue;
}
imc_printf( ch, "%s\r\n", buf );
}
update_imchistory( c, buf );
}
PFUN( imc_recv_pbroadcast )
{
IMC_CHANNEL *c;
char chan[SMST], txt[LGST], emote[SMST], sender[SMST];
int em;
imc_getData( chan, "channel", packet );
imc_getData( txt, "text", packet );
imc_getData( emote, "emote", packet );
imc_getData( sender, "realfrom", packet );
em = atoi( emote );
if( em < 0 || em > 2 )
em = 0;
if( ( c = imc_findchannel( chan ) ) )
imc_display_channel( c, sender, txt, em );
}
PFUN( imc_recv_broadcast )
{
IMC_CHANNEL *c;
char chan[SMST], txt[LGST], emote[SMST], sender[SMST];
int em;
imc_getData( chan, "channel", packet );
imc_getData( txt, "text", packet );
imc_getData( emote, "emote", packet );
imc_getData( sender, "sender", packet );
em = atoi( emote );
if( em < 0 || em > 2 )
em = 0;
if( !( c = imc_findchannel( chan ) ) )
return;
if( sender == NULL || sender[0] == '\0' )
imc_display_channel( c, q->from, txt, em );
else
imc_display_channel( c, sender, txt, em );
}
/* Send/recv private channel messages */
void imc_sendmessage( IMC_CHANNEL * c, char *name, char *text, int emote )
{
IMC_PACKET *p;
/* Private channel */
if( !c->open )
{
char to[SMST];
snprintf( to, sizeof( to ), "IMC@%s", imc_channel_mudof( c->name ) );
p = imc_newpacket( name, "ice-msg-p", to );
}
/* Public channel */
else
p = imc_newpacket( name, "ice-msg-b", "*@*" );
imc_addtopacket( p, "channel=%s", c->name );
imc_addtopacket( p, "text=%s", text );
imc_addtopacket( p, "emote=%d", emote );
imc_addtopacket( p, "%s", "echo=1" );
imc_write_packet( p );
}
PFUN( imc_recv_chanwhoreply )
{
IMC_CHANNEL *c;
CHAR_DATA *vic;
char chan[SMST], list[IMC_BUFF_SIZE];
imc_getData( chan, "channel", packet );
imc_getData( list, "list", packet );
if( !( c = imc_findchannel( chan ) ) )
return;
if( !( vic = imc_find_user( imc_nameof( q->to ) ) ) )
return;
imc_printf( vic, "~G%s", list );
}
PFUN( imc_recv_chanwho )
{
IMC_PACKET *p;
IMC_CHANNEL *c;
DESCRIPTOR_DATA *d;
CHAR_DATA *person;
char buf[IMC_BUFF_SIZE], lvl[SMST], channel[SMST], lname[SMST];
int level;
imc_getData( lvl, "level", packet );
level = get_imcpermvalue( lvl );
if( level < 0 || level > IMCPERM_IMP )
level = IMCPERM_ADMIN;
imc_getData( channel, "channel", packet );
imc_getData( lname, "lname", packet );
if( !( c = imc_findchannel( channel ) ) )
return;
if( !c->local_name )
snprintf( buf, sizeof( buf ), "Channel %s is not locally configured on %s\r\n", lname, this_imcmud->localname );
else if( c->level > level )
snprintf( buf, sizeof( buf ), "Channel %s is above your permission level on %s\r\n", lname, this_imcmud->localname );
else
{
int count = 0, col = 0;
snprintf( buf, sizeof( buf ), "The following people are listening to %s on %s:\r\r\n\n", lname,
this_imcmud->localname );
for( d = first_descriptor; d; d = d->next )
{
person = d->character;
if( !person )
continue;
if( IMCISINVIS( person ) )
continue;
if( !imc_hasname( IMC_LISTEN( person ), c->local_name ) )
continue;
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "%-15s", CH_IMCNAME( person ) );
count++;
if( ++col == 3 )
{
col = 0;
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "%s", "\r\n" );
}
}
if( col != 0 )
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "%s", "\r\n" );
/*
* Send no response to a broadcast request if nobody is listening.
*/
if( count == 0 && !strcasecmp( q->to, "*" ) )
return;
else if( count == 0 )
mudstrlcat( buf, "Nobody\r\n", sizeof( buf ) );
}
p = imc_newpacket( "*", "ice-chan-whoreply", q->from );
imc_addtopacket( p, "channel=%s", c->name );
imc_addtopacket( p, "list=%s", buf );
imc_write_packet( p );
}
void imc_sendnotify( CHAR_DATA * ch, char *chan, bool chon )
{
IMC_PACKET *p;
IMC_CHANNEL *channel;
if( !IMCIS_SET( IMCFLAG( ch ), IMC_NOTIFY ) )
return;
if( !( channel = imc_findchannel( chan ) ) )
return;
p = imc_newpacket( CH_IMCNAME( ch ), "channel-notify", "*@*" );
imc_addtopacket( p, "channel=%s", channel->name );
imc_addtopacket( p, "status=%d", chon );
imc_write_packet( p );
}
PFUN( imc_recv_channelnotify )
{
IMC_CHANNEL *c;
DESCRIPTOR_DATA *d;
CHAR_DATA *ch;
char buf[LGST];
char chan[SMST], cstat[SMST];
bool chon = false;
imc_getData( chan, "channel", packet );
imc_getData( cstat, "status", packet );
chon = atoi( cstat );
if( !( c = imc_findchannel( chan ) ) )
return;
if( !c->local_name || c->local_name[0] == '\0' )
return;
if( chon == true )
snprintf( buf, sizeof( buf ), c->emoteformat, q->from, "has joined the channel." );
else
snprintf( buf, sizeof( buf ), c->emoteformat, q->from, "has left the channel." );
for( d = first_descriptor; d; d = d->next )
{
ch = d->character;
if( !ch || d->connected != CON_PLAYING )
continue;
if( is_npc( ch ) )
continue;
if( IMCPERM( ch ) < c->level || !imc_hasname( IMC_LISTEN( ch ), c->local_name ) )
continue;
if( !IMCIS_SET( IMCFLAG( ch ), IMC_NOTIFY ) )
continue;
imc_printf( ch, "%s\r\n", buf );
}
}
char *imccenterline( const char *string, int length )
{
char stripped[300];
static char outbuf[400];
int amount;
mudstrlcpy( stripped, imc_strip_colors( string ), sizeof( stripped ) );
amount = length - strlen( stripped ); /* Determine amount to put in front of line */
if( amount < 1 )
amount = 1;
/* Justice, you are the String God! */
snprintf( outbuf, sizeof( outbuf ), "%*s%s%*s", ( amount / 2 ), "", string,
( ( amount / 2 ) * 2 ) == amount ? ( amount / 2 ) : ( ( amount / 2 ) + 1 ), "" );
return outbuf;
}
char *imcrankbuffer( CHAR_DATA *ch )
{
static char rbuf[SMST];
if( IMCPERM( ch ) >= IMCPERM_IMM )
{
mudstrlcpy( rbuf, "~YStaff", sizeof( rbuf ) );
if( CH_IMCRANK( ch ) && CH_IMCRANK( ch )[0] != '\0' )
snprintf( rbuf, sizeof( rbuf ), "~Y%s", color_mtoi( CH_IMCRANK( ch ) ) );
}
else
{
mudstrlcpy( rbuf, "~BPlayer", sizeof( rbuf ) );
if( CH_IMCRANK( ch ) && CH_IMCRANK( ch )[0] != '\0' )
snprintf( rbuf, sizeof( rbuf ), "~B%s", color_mtoi( CH_IMCRANK( ch ) ) );
}
return rbuf;
}
void imc_send_whoreply( char *to, char *txt )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "who-reply", to );
imc_addtopacket( p, "text=%s", txt );
imc_write_packet( p );
}
void imc_send_who( char *from, char *to, char *type )
{
IMC_PACKET *p;
p = imc_newpacket( from, "who", to );
imc_addtopacket( p, "type=%s", type );
imc_write_packet( p );
}
char *break_newlines( char *argument, char *arg_first )
{
char cEnd;
int count;
count = 0;
if( arg_first )
arg_first[0] = '\0';
if( !argument || argument[0] == '\0' )
return NULL;
while( isspace( *argument ) )
argument++;
cEnd = '\n';
if( *argument == '\'' || *argument == '"' )
cEnd = *argument++;
while( *argument != '\0' && ++count <= 255 )
{
if( *argument == cEnd )
{
argument++;
break;
}
if( arg_first )
*arg_first++ = *argument++;
else
argument++;
}
if( arg_first )
*arg_first = '\0';
while( isspace( *argument ) )
argument++;
return argument;
}
char *multiline_center( char *splitme )
{
static char newline[LGST];
char arg[SMST];
newline[0] = '\0';
while( 1 )
{
if( splitme[0] == '\0' )
break;
splitme = break_newlines( splitme, arg );
if( strstr( arg, "<center>" ) )
{
mudstrlcpy( arg, imcstrrep( arg, "<center>", "" ), sizeof( arg ) );
mudstrlcpy( arg, imccenterline( arg, 78 ), sizeof( arg ) );
}
mudstrlcat( newline, arg, sizeof( newline ) );
mudstrlcat( newline, "\n", sizeof( newline ) );
}
return newline;
}
char *process_who_head( int plrcount )
{
static char head[LGST];
char pcount[SMST];
mudstrlcpy( head, whot->head, sizeof( head ) );
snprintf( pcount, sizeof( pcount ), "%d", plrcount );
mudstrlcpy( head, imcstrrep( head, "<%plrcount%>", pcount ), sizeof( head ) );
mudstrlcpy( head, multiline_center( head ), sizeof( head ) );
return head;
}
char *process_who_tail( int plrcount )
{
static char tail[LGST];
char pcount[SMST];
mudstrlcpy( tail, whot->tail, sizeof( tail ) );
snprintf( pcount, sizeof( pcount ), "%d", plrcount );
mudstrlcpy( tail, imcstrrep( tail, "<%plrcount%>", pcount ), sizeof( tail ) );
mudstrlcpy( tail, multiline_center( tail ), sizeof( tail ) );
return tail;
}
char *process_plrline( char *plrrank, char *plrflags, char *plrname, char *plrtitle )
{
static char pline[LGST];
mudstrlcpy( pline, whot->immline, sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charrank%>", plrrank ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charflags%>", plrflags ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charname%>", plrname ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%chartitle%>", plrtitle ), sizeof( pline ) );
mudstrlcat( pline, "\n", sizeof( pline ) );
return pline;
}
char *process_immline( char *plrrank, char *plrflags, char *plrname, char *plrtitle )
{
static char pline[LGST];
mudstrlcpy( pline, whot->immline, sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charrank%>", plrrank ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charflags%>", plrflags ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%charname%>", plrname ), sizeof( pline ) );
mudstrlcpy( pline, imcstrrep( pline, "<%chartitle%>", plrtitle ), sizeof( pline ) );
mudstrlcat( pline, "\n", sizeof( pline ) );
return pline;
}
char *process_who_template( char *head, char *tail, char *plrlines, char *immlines, char *plrheader, char *immheader )
{
static char master[LGST];
mudstrlcpy( master, whot->master, sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%head%>", head ), sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%tail%>", tail ), sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%plrheader%>", plrheader ), sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%immheader%>", immheader ), sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%plrline%>", plrlines ), sizeof( master ) );
mudstrlcpy( master, imcstrrep( master, "<%immline%>", immlines ), sizeof( master ) );
return master;
}
char *imc_assemble_who( void )
{
CHAR_DATA *person;
DESCRIPTOR_DATA *d;
int pcount = 0;
bool plr = false;
char plrheader[SMST], immheader[SMST], rank[SMST], flags[SMST], name[SMST], title[SMST], plrline[SMST], immline[SMST];
char plrlines[LGST], immlines[LGST], head[LGST], tail[LGST];
static char master[LGST]; /* The final result that gets returned */
plrlines[0] = '\0';
immlines[0] = '\0';
plrheader[0] = '\0';
immheader[0] = '\0';
for( d = first_descriptor; d; d = d->next )
{
person = d->character;
if( person && d->connected == CON_PLAYING )
{
if( IMCPERM( person ) <= IMCPERM_NONE || IMCPERM( person ) >= IMCPERM_IMM )
continue;
if( IMCISINVIS( person ) )
continue;
++pcount;
if( !plr )
{
mudstrlcpy( plrheader, whot->plrheader, sizeof( plrheader ) );
plr = true;
}
mudstrlcpy( rank, imcrankbuffer( person ), sizeof( rank ) );
mudstrlcpy( flags, IMCAFK( person ) ? "AFK" : "---", sizeof( flags ) );
mudstrlcpy( name, CH_IMCNAME( person ), sizeof( name ) );
mudstrlcpy( title, color_mtoi( CH_IMCTITLE( person ) ), sizeof( title ) );
mudstrlcpy( plrline, process_plrline( rank, flags, name, title ), sizeof( plrline ) );
mudstrlcat( plrlines, plrline, sizeof( plrlines ) );
}
}
bool imm = false;
for( d = first_descriptor; d; d = d->next )
{
person = d->character;
if( person && d->connected == CON_PLAYING )
{
if( IMCPERM( person ) <= IMCPERM_NONE || IMCPERM( person ) < IMCPERM_IMM )
continue;
if( IMCISINVIS( person ) )
continue;
++pcount;
if( !imm )
{
mudstrlcpy( immheader, whot->immheader, sizeof( immheader ) );
imm = true;
}
mudstrlcpy( rank, imcrankbuffer( person ), sizeof( rank ) );
mudstrlcpy( flags, IMCAFK( person ) ? "AFK" : "---", sizeof( flags ) );
mudstrlcpy( name, CH_IMCNAME( person ), sizeof( name ) );
mudstrlcpy( title, color_mtoi( CH_IMCTITLE( person ) ), sizeof( title ) );
mudstrlcpy( immline, process_immline( rank, flags, name, title ), sizeof( immline ) );
mudstrlcat( immlines, immline, sizeof( immlines ) );
}
}
mudstrlcpy( head, process_who_head( pcount ), sizeof( head ) );
mudstrlcpy( tail, process_who_tail( pcount ), sizeof( tail ) );
mudstrlcpy( master, process_who_template( head, tail, plrlines, immlines, plrheader, immheader ), sizeof( master ) );
return master;
}
void imc_process_who( char *from )
{
char whoreply[IMC_BUFF_SIZE];
mudstrlcpy( whoreply, imc_assemble_who(), sizeof( whoreply ) );
imc_send_whoreply( from, whoreply );
}
/* Finger code */
void imc_process_finger( char *from, char *type )
{
CHAR_DATA *victim;
char buf[IMC_BUFF_SIZE], to[SMST];
if( !type || type[0] == '\0' )
return;
type = imcone_argument( type, to );
if( !( victim = imc_find_user( type ) ) )
{
imc_send_whoreply( from, (char *)"No such player is online.\r\n" );
return;
}
if( IMCISINVIS( victim ) || IMCPERM( victim ) < IMCPERM_MORT )
{
imc_send_whoreply( from, (char *)"No such player is online.\r\n" );
return;
}
snprintf( buf, sizeof( buf ), "\r\n~cPlayer Profile for ~W%s~c:\r\n"
"~W-------------------------------\r\n"
"~cStatus: ~W%s\r\n"
"~cPermission level: ~W%s\r\n"
"~cListening to channels [Names may not match your mud]: ~W%s\r\n",
CH_IMCNAME( victim ), ( IMCAFK( victim ) ? "AFK" : "Lurking about" ),
imcperm_names[IMCPERM( victim )],
( IMC_LISTEN( victim ) && IMC_LISTEN( victim )[0] != '\0' ) ? IMC_LISTEN( victim ) : "None" );
if( !IMCIS_SET( IMCFLAG( victim ), IMC_PRIVACY ) )
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~cEmail : ~W%s\r\n"
"~cHomepage: ~W%s\r\n"
"~cICQ : ~W%d\r\n"
"~cAIM : ~W%s\r\n"
"~cYahoo : ~W%s\r\n"
"~cMSN : ~W%s\r\n",
( IMC_EMAIL( victim ) && IMC_EMAIL( victim )[0] != '\0' ) ? IMC_EMAIL( victim ) : "None",
( IMC_HOMEPAGE( victim ) && IMC_HOMEPAGE( victim )[0] != '\0' ) ? IMC_HOMEPAGE( victim ) : "None",
IMC_ICQ( victim ),
( IMC_AIM( victim ) && IMC_AIM( victim )[0] != '\0' ) ? IMC_AIM( victim ) : "None",
( IMC_YAHOO( victim ) && IMC_YAHOO( victim )[0] != '\0' ) ? IMC_YAHOO( victim ) : "None",
( IMC_MSN( victim ) && IMC_MSN( victim )[0] != '\0' ) ? IMC_MSN( victim ) : "None" );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~W%s\r\n",
( IMC_COMMENT( victim ) && IMC_COMMENT( victim )[0] != '\0' ) ? IMC_COMMENT( victim ) : "" );
imc_send_whoreply( from, buf );
}
PFUN( imc_recv_who )
{
char type[SMST], buf[IMC_BUFF_SIZE];
imc_getData( type, "type", packet );
if( !strcasecmp( type, "who" ) )
{
imc_process_who( q->from );
return;
}
else if( strstr( type, "finger" ) )
{
imc_process_finger( q->from, type );
return;
}
else if( !strcasecmp( type, "info" ) )
{
snprintf( buf, sizeof( buf ), "\r\n~WMUD Name : ~c%s\r\n", this_imcmud->localname );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~WHost : ~c%s\r\n", this_imcmud->ihost );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~WAdmin Email : ~c%s\r\n", this_imcmud->email );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~WWebsite : ~c%s\r\n", this_imcmud->www );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~WIMC2 Version: ~c%s\r\n", this_imcmud->versionid );
snprintf( buf + strlen( buf ), sizeof( buf ) - strlen( buf ), "~WDetails : ~c%s\r\n", this_imcmud->details );
}
else
snprintf( buf, sizeof( buf ), "%s is not a valid option. Options are: who, finger, or info.\r\n", type );
imc_send_whoreply( q->from, buf );
}
PFUN( imc_recv_whoreply )
{
CHAR_DATA *vic;
char txt[IMC_BUFF_SIZE];
if( !( vic = imc_find_user( imc_nameof( q->to ) ) ) )
return;
imc_getData( txt, "text", packet );
imc_to_pager( txt, vic );
}
void imc_send_whoisreply( char *to, char *data )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "whois-reply", to );
imc_addtopacket( p, "text=%s", data );
imc_write_packet( p );
}
PFUN( imc_recv_whoisreply )
{
CHAR_DATA *vic;
char txt[LGST];
imc_getData( txt, "text", packet );
if( ( vic = imc_find_user( imc_nameof( q->to ) ) ) )
imc_to_char( txt, vic );
}
void imc_send_whois( char *from, char *user )
{
IMC_PACKET *p;
p = imc_newpacket( from, "whois", user );
imc_write_packet( p );
}
PFUN( imc_recv_whois )
{
CHAR_DATA *vic;
char buf[LGST];
if( ( vic = imc_find_user( imc_nameof( q->to ) ) ) && !IMCISINVIS( vic ) )
{
snprintf( buf, sizeof( buf ), "~RIMC Locate: ~Y%s@%s: ~cOnline.\r\n", CH_IMCNAME( vic ), this_imcmud->localname );
imc_send_whoisreply( q->from, buf );
}
}
PFUN( imc_recv_beep )
{
CHAR_DATA *vic = NULL;
char buf[LGST];
if( !( vic = imc_find_user( imc_nameof( q->to ) ) ) || IMCPERM( vic ) < IMCPERM_MORT )
{
snprintf( buf, sizeof( buf ), "No player named %s exists here.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
return;
}
if( IMCISINVIS( vic ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving beeps.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( imc_isignoring( vic, q->from ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving beeps.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( IMCIS_SET( IMCFLAG( vic ), IMC_BEEP ) || IMCIS_SET( IMCFLAG( vic ), IMC_DENYBEEP ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is not receiving beeps.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( IMCAFK( vic ) )
{
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
snprintf( buf, sizeof( buf ), "%s is currently AFK. Try back later.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
/* always display the true name here */
imc_printf( vic, "~c\a%s imcbeeps you.~!\r\n", q->from );
}
void imc_send_beep( char *from, char *to )
{
IMC_PACKET *p;
p = imc_newpacket( from, "beep", to );
imc_write_packet( p );
}
PFUN( imc_recv_isalive )
{
REMOTEINFO *r;
char version[SMST], netname[SMST], url[SMST], host[SMST], iport[SMST];
imc_getData( version, "versionid", packet );
imc_getData( netname, "networkname", packet );
imc_getData( url, "url", packet );
imc_getData( host, "host", packet );
imc_getData( iport, "port", packet );
if( !( r = imc_find_reminfo( imc_mudof( q->from ) ) ) )
{
imc_new_reminfo( imc_mudof( q->from ), version, netname, url, q->route );
return;
}
r->expired = false;
if( url != NULL && url[0] != '\0' )
{
STRFREE( r->url );
r->url = STRALLOC( url );
}
if( version != NULL && version[0] != '\0' )
{
STRFREE( r->version );
r->version = STRALLOC( version );
}
if( netname != NULL && netname[0] != '\0' )
{
STRFREE( r->network );
r->network = STRALLOC( netname );
}
if( q->route != NULL && q->route[0] != '\0' )
{
STRFREE( r->path );
r->path = STRALLOC( q->route );
}
if( host != NULL && host[0] != '\0' )
{
STRFREE( r->host );
r->host = STRALLOC( host );
}
if( iport != NULL && iport[0] != '\0' )
{
STRFREE( r->port );
r->port = STRALLOC( iport );
}
}
PFUN( imc_send_keepalive )
{
IMC_PACKET *p;
if( q )
p = imc_newpacket( "*", "is-alive", q->from );
else
p = imc_newpacket( "*", "is-alive", packet );
imc_addtopacket( p, "versionid=%s", this_imcmud->versionid );
imc_addtopacket( p, "url=%s", this_imcmud->www );
imc_addtopacket( p, "host=%s", this_imcmud->ihost );
imc_addtopacket( p, "port=%d", this_imcmud->iport );
imc_write_packet( p );
}
void imc_request_keepalive( void )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "keepalive-request", "*@*" );
imc_write_packet( p );
imc_send_keepalive( NULL, (char *)"*@*" );
}
void imc_firstrefresh( void )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "ice-refresh", "IMC@$" );
imc_write_packet( p );
}
PFUN( imc_recv_iceupdate )
{
IMC_CHANNEL *c;
char chan[SMST], owner[SMST], ops[SMST], invite[SMST], exclude[SMST], policy[SMST], level[SMST], lname[SMST];
int perm;
bool copen;
imc_getData( chan, "channel", packet );
imc_getData( owner, "owner", packet );
imc_getData( ops, "operators", packet );
imc_getData( invite, "invited", packet );
imc_getData( exclude, "excluded", packet );
imc_getData( policy, "policy", packet );
imc_getData( level, "level", packet );
imc_getData( lname, "localname", packet );
if( !strcasecmp( policy, "open" ) )
copen = true;
else
copen = false;
perm = get_imcpermvalue( level );
if( perm < 0 || perm > IMCPERM_IMP )
perm = IMCPERM_ADMIN;
if( !( c = imc_findchannel( chan ) ) )
{
imc_new_channel( chan, owner, ops, invite, exclude, copen, perm, lname );
return;
}
if( chan == NULL || chan[0] == '\0' )
{
imclog( "%s: NULL channel name received, skipping", __FUNCTION__ );
return;
}
STRFREE( c->name );
STRFREE( c->owner );
STRFREE( c->operators );
STRFREE( c->invited );
STRFREE( c->excluded );
c->name = STRALLOC( chan );
c->owner = STRALLOC( owner );
c->operators = STRALLOC( ops );
c->invited = STRALLOC( invite );
c->excluded = STRALLOC( exclude );
c->open = copen;
if( c->level == IMCPERM_NOTSET )
c->level = perm;
c->refreshed = true;
}
PFUN( imc_recv_icedestroy )
{
IMC_CHANNEL *c;
char chan[SMST];
imc_getData( chan, "channel", packet );
if( !( c = imc_findchannel( chan ) ) )
return;
imc_freechan( c );
imc_save_channels( );
}
int imctodikugender( int gender )
{
if( gender == 0 )
return SEX_MALE;
if( gender == 1 )
return SEX_FEMALE;
return SEX_NEUTRAL;
}
int dikutoimcgender( int gender )
{
if( gender == SEX_MALE )
return 0;
if( gender == SEX_FEMALE )
return 1;
return 2;
}
int imc_get_ucache_gender( const char *name )
{
IMCUCACHE_DATA *user;
for( user = first_imcucache; user; user = user->next )
{
if( !strcasecmp( user->name, name ) )
return user->gender;
}
/* -1 means you aren't in the list and need to be put there. */
return -1;
}
/* Saves the ucache info to disk because it would just be spamcity otherwise */
void imc_save_ucache( void )
{
FILE *fp;
IMCUCACHE_DATA *user;
if( !( fp = fopen( IMC_UCACHE_FILE, "w" ) ) )
{
imclog( "%s", "Couldn't write to IMC2 ucache file." );
return;
}
for( user = first_imcucache; user; user = user->next )
{
fprintf( fp, "%s", "#UCACHE\n" );
fprintf( fp, "Name %s\n", user->name );
fprintf( fp, "Sex %d\n", user->gender );
fprintf( fp, "Time %ld\n", ( long int )user->time );
fprintf( fp, "%s", "End\n\n" );
}
fprintf( fp, "%s", "#END\n" );
IMCFCLOSE( fp );
}
void imc_prune_ucache( void )
{
IMCUCACHE_DATA *ucache, *next_ucache;
for( ucache = first_imcucache; ucache; ucache = next_ucache )
{
next_ucache = ucache->next;
/* Info older than 30 days is removed since this person likely hasn't logged in at all */
if( ( current_time - ucache->time ) >= 2592000 )
{
STRFREE( ucache->name );
UNLINK( ucache, first_imcucache, last_imcucache, next, prev );
DISPOSE( ucache );
}
}
imc_save_ucache( );
}
/* Updates user info if they exist, adds them if they don't. */
void imc_ucache_update( const char *name, int gender )
{
IMCUCACHE_DATA *user;
for( user = first_imcucache; user; user = user->next )
{
if( !strcasecmp( user->name, name ) )
{
user->gender = gender;
user->time = current_time;
return;
}
}
CREATE( user, IMCUCACHE_DATA, 1 );
user->name = STRALLOC( ( char * )name );
user->gender = gender;
user->time = current_time;
LINK( user, first_imcucache, last_imcucache, next, prev );
imc_save_ucache( );
}
void imc_send_ucache_update( const char *visname, int gender )
{
IMC_PACKET *p;
p = imc_newpacket( visname, "user-cache", "*@*" );
imc_addtopacket( p, "gender=%d", gender );
imc_write_packet( p );
}
PFUN( imc_recv_ucache )
{
char gen[SMST];
int sex, gender;
imc_getData( gen, "gender", packet );
gender = atoi( gen );
sex = imc_get_ucache_gender( q->from );
if( sex == gender )
return;
imc_ucache_update( q->from, gender );
}
void imc_send_ucache_request( char *targetuser )
{
IMC_PACKET *p;
char to[SMST];
snprintf( to, sizeof( to ), "*@%s", imc_mudof( targetuser ) );
p = imc_newpacket( "*", "user-cache-request", to );
imc_addtopacket( p, "user=%s", targetuser );
imc_write_packet( p );
}
PFUN( imc_recv_ucache_request )
{
IMC_PACKET *p;
char to[SMST], user[SMST];
int gender;
imc_getData( user, "user", packet );
gender = imc_get_ucache_gender( user );
/* Gender of -1 means they aren't in the mud's ucache table. Don't waste the reply packet. */
if( gender == -1 )
return;
snprintf( to, sizeof( to ), "*@%s", imc_mudof( q->from ) );
p = imc_newpacket( "*", "user-cache-reply", to );
imc_addtopacket( p, "user=%s", user );
imc_addtopacket( p, "gender=%d", gender );
imc_write_packet( p );
}
PFUN( imc_recv_ucache_reply )
{
char user[SMST], gen[SMST];
int sex, gender;
imc_getData( user, "user", packet );
imc_getData( gen, "gender", packet );
gender = atoi( gen );
sex = imc_get_ucache_gender( user );
if( sex == gender )
return;
imc_ucache_update( user, gender );
}
PFUN( imc_recv_closenotify )
{
REMOTEINFO *r;
char host[SMST];
imc_getData( host, "host", packet );
if( ( r = imc_find_reminfo( host ) ) )
r->expired = true;
}
void imc_register_default_packets( void )
{
/* Once registered, these aren't cleared unless the mud is shut down */
if( first_phandler )
return;
imc_register_packet_handler( "keepalive-request", imc_send_keepalive );
imc_register_packet_handler( "is-alive", imc_recv_isalive );
imc_register_packet_handler( "ice-update", imc_recv_iceupdate );
imc_register_packet_handler( "ice-msg-r", imc_recv_pbroadcast );
imc_register_packet_handler( "ice-msg-b", imc_recv_broadcast );
imc_register_packet_handler( "user-cache", imc_recv_ucache );
imc_register_packet_handler( "user-cache-request", imc_recv_ucache_request );
imc_register_packet_handler( "user-cache-reply", imc_recv_ucache_reply );
imc_register_packet_handler( "tell", imc_recv_tell );
imc_register_packet_handler( "emote", imc_recv_emote );
imc_register_packet_handler( "ice-destroy", imc_recv_icedestroy );
imc_register_packet_handler( "who", imc_recv_who );
imc_register_packet_handler( "who-reply", imc_recv_whoreply );
imc_register_packet_handler( "whois", imc_recv_whois );
imc_register_packet_handler( "whois-reply", imc_recv_whoisreply );
imc_register_packet_handler( "beep", imc_recv_beep );
imc_register_packet_handler( "ice-chan-who", imc_recv_chanwho );
imc_register_packet_handler( "ice-chan-whoreply", imc_recv_chanwhoreply );
imc_register_packet_handler( "channel-notify", imc_recv_channelnotify );
imc_register_packet_handler( "close-notify", imc_recv_closenotify );
}
PACKET_FUN *pfun_lookup( const char *type )
{
IMC_PHANDLER *ph;
for( ph = first_phandler; ph; ph = ph->next )
if( !strcasecmp( type, ph->name ) )
return ph->func;
return NULL;
}
void imc_parse_packet( char *packet )
{
IMC_PACKET *p;
PACKET_FUN *pfun;
char arg[SMST];
unsigned long seq;
CREATE( p, IMC_PACKET, 1 );
packet = imcone_argument( packet, p->from );
packet = imcone_argument( packet, arg );
seq = atol( arg );
packet = imcone_argument( packet, p->route );
packet = imcone_argument( packet, p->type );
packet = imcone_argument( packet, p->to );
/* Banned muds are silently dropped - thanks to WynterNyght@IoG for noticing this was missing. */
if( imc_isbanned( p->from ) )
{
DISPOSE( p );
return;
}
pfun = pfun_lookup( p->type );
if( !pfun )
{
if( imcpacketdebug )
{
imclog( "PACKET: From %s, Seq %lu, Route %s, Type %s, To %s, EXTRA %s",
p->from, seq, p->route, p->type, p->to, packet );
imclog( "No packet handler function has been defined for %s", p->type );
}
DISPOSE( p );
return;
}
( *pfun ) ( p, packet );
this_imcmud->lttime = current_time;
/* This might seem slow, but we need to track muds who don't send is-alive packets */
if( !( imc_find_reminfo( imc_mudof( p->from ) ) ) )
imc_new_reminfo( imc_mudof( p->from ), (char *)"Unknown", this_imcmud->network, (char *)"Unknown", p->route );
DISPOSE( p );
}
void imc_finalize_connection( char *name, char *netname )
{
this_imcmud->state = IMC_ONLINE;
if( netname && netname[0] != '\0' )
{
STRFREE( this_imcmud->network );
this_imcmud->network = STRALLOC( netname );
}
STRFREE( this_imcmud->servername );
this_imcmud->servername = STRALLOC( name );
imclog( "Connected to %s. Network ID: %s", name, ( netname && netname[0] != '\0' ) ? netname : "Unknown" );
imcconnect_attempts = 0;
imc_request_keepalive( );
imc_firstrefresh( );
}
/* Handle an autosetup response from a supporting server - Samson 8-12-03 */
void imc_handle_autosetup( char *source, char *servername, char *cmd, char *txt, char *encrypt )
{
if( !strcasecmp( cmd, "reject" ) )
{
if( !strcasecmp( txt, "connected" ) )
{
imclog( "There is already a mud named %s connected to the network.", this_imcmud->localname );
imc_shutdown( false );
return;
}
if( !strcasecmp( txt, "private" ) )
{
imclog( "%s is a private server. Autosetup denied.", servername );
imc_shutdown( false );
return;
}
if( !strcasecmp( txt, "full" ) )
{
imclog( "%s has reached its connection limit. Autosetup denied.", servername );
imc_shutdown( false );
return;
}
if( !strcasecmp( txt, "ban" ) )
{
imclog( "%s has banned your connection. Autosetup denied.", servername );
imc_shutdown( false );
return;
}
imclog( "%s: Invalid 'reject' response. Autosetup failed.", servername );
imclog( "Data received: %s %s %s %s %s", source, servername, cmd, txt, encrypt );
imc_shutdown( false );
return;
}
if( !strcasecmp( cmd, "accept" ) )
{
imclog( "Autosetup completed successfully." );
if( encrypt && encrypt[0] != '\0' && !strcasecmp( encrypt, "SHA256-SET" ) )
{
imclog( "SHA-256 Authentication has been enabled." );
this_imcmud->sha256pass = true;
imc_save_config( );
}
imc_finalize_connection( servername, txt );
return;
}
imclog( "%s: Invalid autosetup response.", servername );
imclog( "Data received: %s %s %s %s %s", source, servername, cmd, txt, encrypt );
imc_shutdown( false );
}
bool imc_write_socket( void )
{
const char *ptr = this_imcmud->outbuf;
int nleft = this_imcmud->outtop, nwritten = 0;
if( nleft <= 0 )
return 1;
while( nleft > 0 )
{
if( ( nwritten = send( this_imcmud->desc, ptr, nleft, 0 ) ) <= 0 )
{
if( nwritten == -1 && errno == EAGAIN )
{
char *p2 = this_imcmud->outbuf;
ptr += nwritten;
while( *ptr != '\0' )
*p2++ = *ptr++;
*p2 = '\0';
this_imcmud->outtop = strlen( this_imcmud->outbuf );
return true;
}
if( nwritten < 0 )
imclog( "Write error on socket: %s", strerror( errno ) );
else
imclog( "%s", "Connection close detected on socket write." );
imc_shutdown( true );
return false;
}
nleft -= nwritten;
ptr += nwritten;
if( nwritten > 0 )
update_transfer( 6, nwritten );
}
if( imcpacketdebug )
{
imclog( "Packet Sent: %s", this_imcmud->outbuf );
imclog( "Bytes sent: %d", this_imcmud->outtop );
}
this_imcmud->outbuf[0] = '\0';
this_imcmud->outtop = 0;
return 1;
}
void imc_process_authentication( char *packet )
{
char command[SMST], rname[SMST], pw[SMST], version[SMST], netname[SMST], encrypt[SMST];
char response[LGST];
packet = imcone_argument( packet, command );
packet = imcone_argument( packet, rname );
packet = imcone_argument( packet, pw );
packet = imcone_argument( packet, version ); /* This is more or less ignored */
packet = imcone_argument( packet, netname );
packet = imcone_argument( packet, encrypt );
if( rname == NULL || rname[0] == '\0' )
{
imclog( "%s", "Incomplete authentication packet. Unable to connect." );
imc_shutdown( false );
return;
}
if( !strcasecmp( command, "SHA256-AUTH-INIT" ) )
{
char pwd[SMST];
char *cryptpwd;
long auth_value = 0;
if( pw == NULL || pw[0] == '\0' )
{
imclog( "SHA256 Authentication failure: No auth_value was returned by %s.", rname );
imc_shutdown( false );
return;
}
/* Lets encrypt this bastard now! */
auth_value = atol( pw );
snprintf( pwd, sizeof( pwd ), "%ld%s%s", auth_value, this_imcmud->clientpw, this_imcmud->serverpw );
cryptpwd = sha256_crypt( pwd );
snprintf( response, sizeof( response ), "SHA256-AUTH-RESP %s %s version=%d", this_imcmud->localname, cryptpwd, IMC_VERSION );
imc_write_buffer( response );
return;
}
/*
* SHA-256 response is pretty simple. If you blew the authentication, it happened on the server anyway.
* rname=servername pw=Networkname
*/
if( !strcasecmp( command, "SHA256-AUTH-APPR" ) )
{
imclog( "%s", "SHA-256 Authentication completed." );
imc_finalize_connection( rname, pw );
return;
}
/* The old way. Nice and icky, but still very much required for compatibility. */
if( !strcasecmp( command, "PW" ) )
{
if( strcasecmp( this_imcmud->serverpw, pw ) )
{
imclog( "%s sent an improper serverpassword.", rname );
imc_shutdown( false );
return;
}
imclog( "%s", "Standard Authentication completed." );
if( encrypt != NULL && encrypt[0] != '\0' && !strcasecmp( encrypt, "SHA256-SET" ) )
{
imclog( "SHA-256 Authentication has been enabled." );
this_imcmud->sha256pass = true;
imc_save_config( );
}
imc_finalize_connection( rname, netname );
return;
}
/*
* Should only be received from servers supporting this obviously
* arg1=autosetup name=servername pw=command version=response netname=SHA256-SET
*/
if( !strcasecmp( command, "autosetup" ) )
{
imc_handle_autosetup( command, rname, pw, version, netname );
return;
}
imclog( "Invalid authentication response received from %s!!", rname );
imclog( "Data received: %s %s %s %s %s", command, rname, pw, version, netname );
imc_shutdown( false );
}
/* Transfer one line from input buffer to input line. */
bool imc_read_buffer( void )
{
unsigned int i = 0, j = 0;
unsigned char ended = 0;
int k = 0;
if( this_imcmud->inbuf[0] == '\0' )
return 0;
k = strlen( this_imcmud->incomm );
if( k < 0 )
k = 0;
for( i = 0; this_imcmud->inbuf[i] != '\0'
&& this_imcmud->inbuf[i] != '\r' && this_imcmud->inbuf[i] != '\n' && i < IMC_BUFF_SIZE; i++ )
{
this_imcmud->incomm[k++] = this_imcmud->inbuf[i];
}
while( this_imcmud->inbuf[i] == '\r' || this_imcmud->inbuf[i] == '\n' )
{
ended = 1;
i++;
}
this_imcmud->incomm[k] = '\0';
while( ( this_imcmud->inbuf[j] = this_imcmud->inbuf[i + j] ) != '\0' )
j++;
this_imcmud->inbuf[j] = '\0';
return ended;
}
bool imc_read_socket( void )
{
unsigned int iStart, iErr;
bool begin = 1;
if( !this_imcmud || this_imcmud->desc < 1 )
return false;
iStart = strlen( this_imcmud->inbuf );
for( ;; )
{
int nRead;
nRead = recv( this_imcmud->desc, this_imcmud->inbuf + iStart, sizeof( this_imcmud->inbuf ) - 10 - iStart, 0 );
iErr = errno;
if( nRead > 0 )
{
iStart += nRead;
update_transfer( 5, nRead );
if( iStart >= sizeof( this_imcmud->inbuf ) - 10 )
break;
begin = 0;
}
else if( nRead == 0 && this_imcmud->state == IMC_ONLINE )
{
if( !begin )
break;
imclog( "%s", "Connection close detected on read of IMC2 socket." );
return false;
}
else if( iErr == EAGAIN )
break;
else
{
imclog( "%s: Descriptor error on #%d: %s", __FUNCTION__, this_imcmud->desc, strerror( iErr ) );
return false;
}
}
this_imcmud->inbuf[iStart] = '\0';
return true;
}
void imc_loop( void )
{
fd_set in_set, out_set;
struct timeval last_time, null_time;
gettimeofday( &last_time, NULL );
if( imcwait > 0 )
imcwait--;
/* Condition reached only if network shutdown after startup */
if( imcwait == 1 )
{
if( ++imcconnect_attempts > 3 )
{
if( this_imcmud->sha256pass )
{
imclog( "%s", "Unable to reconnect using SHA-256, trying standard authentication." );
this_imcmud->sha256pass = false;
imc_save_config( );
imcconnect_attempts = 0;
}
else
{
imcwait = -2;
imclog( "%s", "Unable to reestablish connection to server. Abandoning reconnect." );
return;
}
}
imc_startup( true, -1, false );
return;
}
if( this_imcmud->state == IMC_OFFLINE || this_imcmud->desc == -1 )
return;
/* If haven't received anything from server send a keep alive packet */
if( this_imcmud->lttime < ( current_time - ( 60 * URANGE( 1, this_imcmud->kltime, 100 ) ) ) )
{
imc_send_keepalive( NULL, (char *)"*@*" );
this_imcmud->lttime = current_time;
}
/* Will prune the cache once every 24hrs after bootup time */
if( imcucache_clock <= current_time )
{
imcucache_clock = current_time + 86400;
imc_prune_ucache( );
}
FD_ZERO( &in_set );
FD_ZERO( &out_set );
FD_SET( this_imcmud->desc, &in_set );
FD_SET( this_imcmud->desc, &out_set );
null_time.tv_sec = null_time.tv_usec = 0;
if( select( this_imcmud->desc + 1, &in_set, &out_set, NULL, &null_time ) < 0 )
{
perror( "imc_loop: select: poll" );
imc_shutdown( true );
return;
}
if( FD_ISSET( this_imcmud->desc, &in_set ) )
{
if( !imc_read_socket( ) )
{
if( this_imcmud->inbuf && this_imcmud->inbuf[0] != '\0' )
{
if( imc_read_buffer( ) )
{
if( !strcasecmp( this_imcmud->incomm, "SHA-256 authentication is required." ) )
{
imclog( "%s", "Unable to reconnect using standard authentication, trying SHA-256." );
this_imcmud->sha256pass = true;
imc_save_config();
}
else
imclog( "Last thing in incomm > %s", this_imcmud->incomm );
}
}
FD_CLR( this_imcmud->desc, &out_set );
imc_shutdown( true );
return;
}
while( imc_read_buffer( ) )
{
if( imcpacketdebug )
imclog( "Packet received: %s", this_imcmud->incomm );
switch( this_imcmud->state )
{
default:
case IMC_OFFLINE:
case IMC_AUTH1: /* Auth1 can only be set when still trying to contact the server */
break;
case IMC_AUTH2: /* Now you've contacted the server and need to process the authentication response */
imc_process_authentication( this_imcmud->incomm );
this_imcmud->incomm[0] = '\0';
break;
case IMC_ONLINE: /* You're up, pass the bastard off to the packet parser */
imc_parse_packet( this_imcmud->incomm );
this_imcmud->incomm[0] = '\0';
break;
}
}
}
if( this_imcmud->desc > 0 && this_imcmud->outtop > 0 && FD_ISSET( this_imcmud->desc, &out_set ) && !imc_write_socket( ) )
{
this_imcmud->outtop = 0;
imc_shutdown( true );
}
}
/************************************
* User login and logout functions. *
************************************/
void imc_adjust_perms( CHAR_DATA *ch )
{
if( !this_imcmud )
return;
/*
* Ugly hack to let the permission system adapt freely, but retains the ability to override that adaptation
* * in the event you need to restrict someone to a lower level, or grant someone a higher level. This of
* * course comes at the cost of forgetting you may have done so and caused the override flag to be set, but hey.
* * This isn't a perfect system and never will be. Samson 2-8-04.
*/
if( !IMCIS_SET( IMCFLAG( ch ), IMC_PERMOVERRIDE ) )
{
if( CH_IMCLEVEL( ch ) < this_imcmud->minlevel )
IMCPERM( ch ) = IMCPERM_NONE;
else if( CH_IMCLEVEL( ch ) >= this_imcmud->minlevel && CH_IMCLEVEL( ch ) < this_imcmud->immlevel )
IMCPERM( ch ) = IMCPERM_MORT;
else if( CH_IMCLEVEL( ch ) >= this_imcmud->immlevel && CH_IMCLEVEL( ch ) < this_imcmud->adminlevel )
IMCPERM( ch ) = IMCPERM_IMM;
else if( CH_IMCLEVEL( ch ) >= this_imcmud->adminlevel && CH_IMCLEVEL( ch ) < this_imcmud->implevel )
IMCPERM( ch ) = IMCPERM_ADMIN;
else if( CH_IMCLEVEL( ch ) >= this_imcmud->implevel )
IMCPERM( ch ) = IMCPERM_IMP;
}
}
void imc_char_login( CHAR_DATA *ch )
{
char buf[SMST];
int gender, sex;
if( !this_imcmud )
return;
imc_adjust_perms( ch );
if( this_imcmud->state != IMC_ONLINE )
{
if( IMCPERM( ch ) >= IMCPERM_IMM && imcwait == -2 )
imc_to_char( "~RThe IMC2 connection is down. Attempts to reconnect were abandoned due to excessive failures.\r\n", ch );
return;
}
if( IMCPERM( ch ) < IMCPERM_MORT )
return;
snprintf( buf, sizeof( buf ), "%s@%s", CH_IMCNAME( ch ), this_imcmud->localname );
gender = imc_get_ucache_gender( buf );
sex = dikutoimcgender( CH_IMCSEX( ch ) );
if( gender == sex )
return;
imc_ucache_update( buf, sex );
if( !IMCIS_SET( IMCFLAG( ch ), IMC_INVIS ) )
imc_send_ucache_update( CH_IMCNAME( ch ), sex );
}
bool imc_loadchar( CHAR_DATA *ch, FILE *fp, const char *word )
{
bool fMatch = false;
if( is_npc( ch ) )
return false;
if( IMCPERM( ch ) == IMCPERM_NOTSET )
imc_adjust_perms( ch );
switch( word[0] )
{
case 'I':
KEY( "IMCPerm", IMCPERM( ch ), fread_number( fp ) );
KEY( "IMCEmail", IMC_EMAIL( ch ), imcfread_line( fp ) );
KEY( "IMCAIM", IMC_AIM( ch ), imcfread_line( fp ) );
KEY( "IMCICQ", IMC_ICQ( ch ), fread_number( fp ) );
KEY( "IMCYahoo", IMC_YAHOO( ch ), imcfread_line( fp ) );
KEY( "IMCMSN", IMC_MSN( ch ), imcfread_line( fp ) );
KEY( "IMCHomepage", IMC_HOMEPAGE( ch ), imcfread_line( fp ) );
KEY( "IMCComment", IMC_COMMENT( ch ), imcfread_line( fp ) );
if( !strcasecmp( word, "IMCFlags" ) )
{
IMCFLAG( ch ) = fread_number( fp );
imc_char_login( ch );
fMatch = true;
break;
}
if( !strcasecmp( word, "IMClisten" ) )
{
IMC_LISTEN( ch ) = imcfread_line( fp );
if( IMC_LISTEN( ch ) && this_imcmud->state == IMC_ONLINE )
{
IMC_CHANNEL *channel = NULL;
char *channels = IMC_LISTEN( ch );
char arg[SMST];
while( 1 )
{
if( channels[0] == '\0' )
break;
channels = imcone_argument( channels, arg );
if( !( channel = imc_findchannel( arg ) ) )
imc_removename( &IMC_LISTEN( ch ), arg );
if( channel && IMCPERM( ch ) < channel->level )
imc_removename( &IMC_LISTEN( ch ), arg );
if( imc_hasname( IMC_LISTEN( ch ), arg ) )
imc_sendnotify( ch, arg, true );
}
}
fMatch = true;
break;
}
if( !strcasecmp( word, "IMCdeny" ) )
{
IMC_DENY( ch ) = imcfread_line( fp );
if( IMC_DENY( ch ) && this_imcmud->state == IMC_ONLINE )
{
IMC_CHANNEL *channel = NULL;
char *channels = IMC_DENY( ch );
char arg[SMST];
while( 1 )
{
if( channels[0] == '\0' )
break;
channels = imcone_argument( channels, arg );
if( !( channel = imc_findchannel( arg ) ) )
imc_removename( &IMC_DENY( ch ), arg );
if( channel && IMCPERM( ch ) < channel->level )
imc_removename( &IMC_DENY( ch ), arg );
}
}
fMatch = true;
break;
}
if( !strcasecmp( word, "IMCignore" ) )
{
IMC_IGNORE *temp;
CREATE( temp, IMC_IGNORE, 1 );
temp->name = imcfread_line( fp );
LINK( temp, FIRST_IMCIGNORE( ch ), LAST_IMCIGNORE( ch ), next, prev );
fMatch = true;
break;
}
break;
}
return fMatch;
}
void imc_savechar( CHAR_DATA *ch, FILE *fp )
{
IMC_IGNORE *temp;
if( is_npc( ch ) )
return;
fprintf( fp, "IMCPerm %d\n", IMCPERM( ch ) );
fprintf( fp, "IMCFlags %ld\n", ( long int )IMCFLAG( ch ) );
if( IMC_LISTEN( ch ) && IMC_LISTEN( ch )[0] != '\0' )
fprintf( fp, "IMCListen %s\n", IMC_LISTEN( ch ) );
if( IMC_DENY( ch ) && IMC_DENY( ch )[0] != '\0' )
fprintf( fp, "IMCDeny %s\n", IMC_DENY( ch ) );
if( IMC_EMAIL( ch ) && IMC_EMAIL( ch )[0] != '\0' )
fprintf( fp, "IMCEmail %s\n", IMC_EMAIL( ch ) );
if( IMC_HOMEPAGE( ch ) && IMC_HOMEPAGE( ch )[0] != '\0' )
fprintf( fp, "IMCHomepage %s\n", IMC_HOMEPAGE( ch ) );
if( IMC_ICQ( ch ) )
fprintf( fp, "IMCICQ %d\n", IMC_ICQ( ch ) );
if( IMC_AIM( ch ) && IMC_AIM( ch )[0] != '\0' )
fprintf( fp, "IMCAIM %s\n", IMC_AIM( ch ) );
if( IMC_YAHOO( ch ) && IMC_YAHOO( ch )[0] != '\0' )
fprintf( fp, "IMCYahoo %s\n", IMC_YAHOO( ch ) );
if( IMC_MSN( ch ) && IMC_MSN( ch )[0] != '\0' )
fprintf( fp, "IMCMSN %s\n", IMC_MSN( ch ) );
if( IMC_COMMENT( ch ) && IMC_COMMENT( ch )[0] != '\0' )
fprintf( fp, "IMCComment %s\n", IMC_COMMENT( ch ) );
for( temp = FIRST_IMCIGNORE( ch ); temp; temp = temp->next )
fprintf( fp, "IMCignore %s\n", temp->name );
}
void imc_freechardata( CHAR_DATA *ch )
{
IMC_IGNORE *ign, *ign_next;
int x;
if( is_npc( ch ) )
return;
if( !CH_IMCDATA( ch ) )
return;
for( ign = FIRST_IMCIGNORE( ch ); ign; ign = ign_next )
{
ign_next = ign->next;
STRFREE( ign->name );
UNLINK( ign, FIRST_IMCIGNORE( ch ), LAST_IMCIGNORE( ch ), next, prev );
DISPOSE( ign );
}
for( x = 0; x < MAX_IMCTELLHISTORY; x++ )
STRFREE( IMCTELLHISTORY( ch, x ) );
STRFREE( IMC_LISTEN( ch ) );
STRFREE( IMC_DENY( ch ) );
STRFREE( IMC_RREPLY( ch ) );
STRFREE( IMC_RREPLY_NAME( ch ) );
STRFREE( IMC_EMAIL( ch ) );
STRFREE( IMC_HOMEPAGE( ch ) );
STRFREE( IMC_AIM( ch ) );
STRFREE( IMC_YAHOO( ch ) );
STRFREE( IMC_MSN( ch ) );
STRFREE( IMC_COMMENT( ch ) );
DISPOSE( CH_IMCDATA( ch ) );
}
void imc_initchar( CHAR_DATA *ch )
{
if( is_npc( ch ) )
return;
CREATE( CH_IMCDATA( ch ), IMC_CHARDATA, 1 );
IMC_LISTEN( ch ) = NULL;
IMC_DENY( ch ) = NULL;
IMC_RREPLY( ch ) = NULL;
IMC_RREPLY_NAME( ch ) = NULL;
IMC_EMAIL( ch ) = NULL;
IMC_HOMEPAGE( ch ) = NULL;
IMC_AIM( ch ) = NULL;
IMC_YAHOO( ch ) = NULL;
IMC_MSN( ch ) = NULL;
IMC_COMMENT( ch ) = NULL;
IMCFLAG( ch ) = 0;
IMCSET_BIT( IMCFLAG( ch ), IMC_COLORFLAG );
FIRST_IMCIGNORE( ch ) = NULL;
LAST_IMCIGNORE( ch ) = NULL;
IMCPERM( ch ) = IMCPERM_NOTSET;
}
/*******************************************
* Network Startup and Shutdown functions. *
*******************************************/
void imc_loadhistory( void )
{
char filename[256];
FILE *tempfile;
IMC_CHANNEL *tempchan = NULL;
int x;
for( tempchan = first_imc_channel; tempchan; tempchan = tempchan->next )
{
if( !tempchan->local_name )
continue;
snprintf( filename, sizeof( filename ), "%s%s.hist", IMC_DIR, tempchan->local_name );
if( !( tempfile = fopen( filename, "r" ) ) )
continue;
for( x = 0; x < MAX_IMCHISTORY; x++ )
{
if( feof( tempfile ) )
tempchan->history[x] = NULL;
else
tempchan->history[x] = imcfread_line( tempfile );
}
IMCFCLOSE( tempfile );
// remove_file( filename ); /* Personaly I prefer always having a fairly good copy of imc history around */
}
}
void imc_savehistory( void )
{
char filename[256];
FILE *tempfile;
IMC_CHANNEL *tempchan = NULL;
int x;
for( tempchan = first_imc_channel; tempchan; tempchan = tempchan->next )
{
if( !tempchan->local_name )
continue;
if( !tempchan->history[0] )
continue;
snprintf( filename, sizeof( filename ), "%s%s.hist", IMC_DIR, tempchan->local_name );
if( !( tempfile = fopen( filename, "w" ) ) )
continue;
for( x = 0; x < MAX_IMCHISTORY; x++ )
{
if( tempchan->history[x] )
fprintf( tempfile, "%s\n", tempchan->history[x] );
}
IMCFCLOSE( tempfile );
}
}
void imc_save_channels( void )
{
IMC_CHANNEL *c;
FILE *fp;
if( !( fp = fopen( IMC_CHANNEL_FILE, "w" ) ) )
{
imcbug( "Can't write to %s", IMC_CHANNEL_FILE );
return;
}
for( c = first_imc_channel; c; c = c->next )
{
if( !c->local_name || c->local_name[0] == '\0' )
continue;
fprintf( fp, "%s", "#IMCCHAN\n" );
fprintf( fp, "ChanName %s\n", c->name );
fprintf( fp, "ChanLocal %s\n", c->local_name );
fprintf( fp, "ChanRegF %s\n", c->regformat );
fprintf( fp, "ChanEmoF %s\n", c->emoteformat );
fprintf( fp, "ChanSocF %s\n", c->socformat );
fprintf( fp, "ChanLevel %d\n", c->level );
fprintf( fp, "%s", "End\n\n" );
}
fprintf( fp, "%s", "#END\n" );
IMCFCLOSE( fp );
}
void imc_readchannel( IMC_CHANNEL *channel, FILE *fp )
{
const char *word;
bool fMatch;
for( ;; )
{
word = feof( fp ) ? "End" : fread_word( fp );
fMatch = false;
switch( word[0] )
{
case '*':
fMatch = true;
fread_to_eol( fp );
break;
case 'C':
KEY( "ChanName", channel->name, imcfread_line( fp ) );
KEY( "ChanLocal", channel->local_name, imcfread_line( fp ) );
KEY( "ChanRegF", channel->regformat, imcfread_line( fp ) );
KEY( "ChanEmoF", channel->emoteformat, imcfread_line( fp ) );
KEY( "ChanSocF", channel->socformat, imcfread_line( fp ) );
KEY( "ChanLevel", channel->level, fread_number( fp ) );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
{
/* Legacy support to convert channel permissions */
if( channel->level > IMCPERM_IMP )
{
/* The IMCPERM_NONE condition should realistically never happen.... */
if( channel->level < this_imcmud->minlevel )
channel->level = IMCPERM_NONE;
else if( channel->level >= this_imcmud->minlevel && channel->level < this_imcmud->immlevel )
channel->level = IMCPERM_MORT;
else if( channel->level >= this_imcmud->immlevel && channel->level < this_imcmud->adminlevel )
channel->level = IMCPERM_IMM;
else if( channel->level >= this_imcmud->adminlevel && channel->level < this_imcmud->implevel )
channel->level = IMCPERM_ADMIN;
else if( channel->level >= this_imcmud->implevel )
channel->level = IMCPERM_IMP;
}
}
return;
break;
}
if( !fMatch )
imcbug( "imc_readchannel: no match: %s", word );
}
}
void imc_loadchannels( void )
{
FILE *fp;
IMC_CHANNEL *channel;
first_imc_channel = NULL;
last_imc_channel = NULL;
imclog( "%s", "Loading channels..." );
if( !( fp = fopen( IMC_CHANNEL_FILE, "r" ) ) )
{
imcbug( "%s", "Can't open imc channel file" );
return;
}
for( ;; )
{
char letter;
char *word;
letter = fread_letter( fp );
if( letter == '*' )
{
fread_to_eol( fp );
continue;
}
if( letter != '#' )
{
imcbug( "%s", "imc_loadchannels: # not found." );
break;
}
word = fread_word( fp );
if( !strcasecmp( word, "IMCCHAN" ) )
{
int x;
CREATE( channel, IMC_CHANNEL, 1 );
imc_readchannel( channel, fp );
for( x = 0; x < MAX_IMCHISTORY; x++ )
channel->history[x] = NULL;
channel->refreshed = false; /* Prevents crash trying to use a bogus channel */
LINK( channel, first_imc_channel, last_imc_channel, next, prev );
imclog( "configured %s as %s", channel->name, channel->local_name );
continue;
}
else if( !strcasecmp( word, "END" ) )
break;
else
{
imcbug( "imc_loadchannels: bad section: %s.", word );
continue;
}
}
IMCFCLOSE( fp );
}
/* Save current mud-level ban list. Short, simple. */
void imc_savebans( void )
{
FILE *out;
IMC_BAN *ban;
if( !( out = fopen( IMC_BAN_FILE, "w" ) ) )
{
imcbug( "%s", "imc_savebans: error opening ban file for write" );
return;
}
fprintf( out, "%s", "#IGNORES\n" );
for( ban = first_imc_ban; ban; ban = ban->next )
fprintf( out, "%s\n", ban->name );
fprintf( out, "%s", "#END\n" );
IMCFCLOSE( out );
}
void imc_readbans( void )
{
FILE *inf;
char *word;
char temp[SMST];
imclog( "%s", "Loading ban list..." );
if( !( inf = fopen( IMC_BAN_FILE, "r" ) ) )
{
imcbug( "%s", "imc_readbans: couldn't open ban file" );
return;
}
word = fread_word( inf );
if( strcasecmp( word, "#IGNORES" ) )
{
imcbug( "%s", "imc_readbans: Corrupt file" );
IMCFCLOSE( inf );
return;
}
while( !feof( inf ) && !ferror( inf ) )
{
mudstrlcpy( temp, fread_word( inf ), sizeof( temp ) );
if( !strcasecmp( temp, "#END" ) )
{
IMCFCLOSE( inf );
return;
}
imc_addban( temp );
}
if( ferror( inf ) )
{
perror( "imc_readbans" );
IMCFCLOSE( inf );
return;
}
IMCFCLOSE( inf );
}
void imc_savecolor( void )
{
FILE *fp;
IMC_COLOR *color;
if( !( fp = fopen( IMC_COLOR_FILE, "w" ) ) )
{
imclog( "%s", "Couldn't write to IMC2 color file." );
return;
}
for( color = first_imc_color; color; color = color->next )
{
fprintf( fp, "%s", "#COLOR\n" );
fprintf( fp, "Name %s\n", color->name );
fprintf( fp, "Mudtag %s\n", color->mudtag );
fprintf( fp, "IMCtag %s\n", color->imctag );
fprintf( fp, "%s", "End\n\n" );
}
fprintf( fp, "%s", "#END\n" );
IMCFCLOSE( fp );
}
void imc_readcolor( IMC_COLOR *color, FILE *fp )
{
const char *word;
bool fMatch;
for( ;; )
{
word = feof( fp ) ? "End" : fread_word( fp );
fMatch = false;
switch( word[0] )
{
case '*':
fMatch = true;
fread_to_eol( fp );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
return;
break;
case 'I':
KEY( "IMCtag", color->imctag, imcfread_line( fp ) );
break;
case 'M':
KEY( "Mudtag", color->mudtag, imcfread_line( fp ) );
break;
case 'N':
KEY( "Name", color->name, imcfread_line( fp ) );
break;
}
if( !fMatch )
imcbug( "imc_readcolor: no match: %s", word );
}
}
void imc_load_color_table( void )
{
FILE *fp;
IMC_COLOR *color;
first_imc_color = last_imc_color = NULL;
imclog( "%s", "Loading IMC2 color table..." );
if( !( fp = fopen( IMC_COLOR_FILE, "r" ) ) )
{
imclog( "%s", "No color table found." );
return;
}
for( ;; )
{
char letter;
char *word;
letter = fread_letter( fp );
if( letter == '*' )
{
fread_to_eol( fp );
continue;
}
if( letter != '#' )
{
imcbug( "%s", "imc_load_color_table: # not found." );
break;
}
word = fread_word( fp );
if( !strcasecmp( word, "COLOR" ) )
{
CREATE( color, IMC_COLOR, 1 );
imc_readcolor( color, fp );
LINK( color, first_imc_color, last_imc_color, next, prev );
continue;
}
else if( !strcasecmp( word, "END" ) )
break;
else
{
imcbug( "imc_load_color_table: bad section: %s.", word );
continue;
}
}
IMCFCLOSE( fp );
}
void imc_savehelps( void )
{
FILE *fp;
IMC_HELP_DATA *help;
if( !( fp = fopen( IMC_HELP_FILE, "w" ) ) )
{
imclog( "%s", "Couldn't write to IMC2 help file." );
return;
}
for( help = first_imc_help; help; help = help->next )
{
fprintf( fp, "%s", "#HELP\n" );
fprintf( fp, "Name %s\n", help->name );
fprintf( fp, "Perm %s\n", imcperm_names[help->level] );
fprintf( fp, "Text %s¢\n", help->text );
fprintf( fp, "%s", "End\n\n" );
}
fprintf( fp, "%s", "#END\n" );
IMCFCLOSE( fp );
}
void imc_readhelp( IMC_HELP_DATA *help, FILE *fp )
{
const char *word;
char hbuf[LGST];
int permvalue;
bool fMatch;
for( ;; )
{
word = feof( fp ) ? "End" : fread_word( fp );
fMatch = false;
switch( word[0] )
{
case '*':
fMatch = true;
fread_to_eol( fp );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
return;
break;
case 'N':
KEY( "Name", help->name, imcfread_line( fp ) );
break;
case 'P':
if( !strcasecmp( word, "Perm" ) )
{
word = fread_word( fp );
permvalue = get_imcpermvalue( word );
if( permvalue < 0 || permvalue > IMCPERM_IMP )
{
imcbug( "imc_readhelp: Command %s loaded with invalid permission. Set to Imp.", help->name );
help->level = IMCPERM_IMP;
}
else
help->level = permvalue;
fMatch = true;
break;
}
break;
case 'T':
if( !strcasecmp( word, "Text" ) )
{
int num = 0;
while( ( hbuf[num] = fgetc( fp ) ) != EOF && hbuf[num] != '¢' && num < ( LGST - 2 ) )
num++;
hbuf[num] = '\0';
help->text = STRALLOC( hbuf );
fMatch = true;
break;
}
break;
}
if( !fMatch )
imcbug( "imc_readhelp: no match: %s", word );
}
}
void imc_load_helps( void )
{
FILE *fp;
IMC_HELP_DATA *help;
first_imc_help = last_imc_help = NULL;
imclog( "%s", "Loading IMC2 help file..." );
if( !( fp = fopen( IMC_HELP_FILE, "r" ) ) )
{
imclog( "%s", "No help file found." );
return;
}
for( ;; )
{
char letter;
char *word;
letter = fread_letter( fp );
if( letter == '*' )
{
fread_to_eol( fp );
continue;
}
if( letter != '#' )
{
imcbug( "%s", "imc_load_helps: # not found." );
break;
}
word = fread_word( fp );
if( !strcasecmp( word, "HELP" ) )
{
CREATE( help, IMC_HELP_DATA, 1 );
imc_readhelp( help, fp );
LINK( help, first_imc_help, last_imc_help, next, prev );
continue;
}
else if( !strcasecmp( word, "END" ) )
break;
else
{
imcbug( "imc_load_helps: bad section: %s.", word );
continue;
}
}
IMCFCLOSE( fp );
}
void imc_savecommands( void )
{
FILE *fp;
IMC_CMD_DATA *cmd;
IMC_ALIAS *alias;
if( !( fp = fopen( IMC_CMD_FILE, "w" ) ) )
{
imclog( "%s", "Couldn't write to IMC2 command file." );
return;
}
for( cmd = first_imc_command; cmd; cmd = cmd->next )
{
fprintf( fp, "%s", "#COMMAND\n" );
fprintf( fp, "Name %s\n", cmd->name );
if( cmd->function )
fprintf( fp, "Code %s\n", imc_funcname( cmd->function ) );
else
fprintf( fp, "%s", "Code NULL\n" );
fprintf( fp, "Perm %s\n", imcperm_names[cmd->level] );
fprintf( fp, "Connected %d\n", cmd->connected );
for( alias = cmd->first_alias; alias; alias = alias->next )
fprintf( fp, "Alias %s\n", alias->name );
fprintf( fp, "%s", "End\n\n" );
}
fprintf( fp, "%s", "#END\n" );
IMCFCLOSE( fp );
}
void imc_readcommand( IMC_CMD_DATA *cmd, FILE *fp )
{
IMC_ALIAS *alias;
const char *word;
int permvalue;
bool fMatch;
for( ;; )
{
word = feof( fp ) ? "End" : fread_word( fp );
fMatch = false;
switch( word[0] )
{
Showing the first 100 KB of 209.3 KB. Download the archive for the full file.