imc.c
cmud/src/imc.c · 186.2 KB
From the archive cmud.tar.gz
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.org
* License terms are available in the imc2freedom.license file.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
#include <fnmatch.h>
#include <time.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <sys/types.h>
#endif
#include "mud.h"
#include "sha256.h"
#define IMCKEY( literal, field, value ) \
if( !strcasecmp( word, literal ) ) \
{ \
field = value; \
fMatch = TRUE; \
break; \
}
extern int num_logins; /* Logins since reboot - external */
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 imc_time; /* Current clock time for the client */
void imclog( const char *format, ... ) __attribute__ ( ( format( printf, 1, 2 ) ) );
void imcbug( const char *format, ... ) __attribute__ ( ( format( printf, 1, 2 ) ) );
void imc_printf( D_MOBILE *ch, const char *fmt, ... ) __attribute__ ( ( format( printf, 2, 3 ) ) );
void imcpager_printf( D_MOBILE *ch, const char *fmt, ... ) __attribute__ ( ( format( printf, 2, 3 ) ) );
char *imc_funcname( IMC_FUN *func );
IMC_FUN *imc_function( const char *func );
char *imc_send_social( D_MOBILE *ch, char *argument, int telloption );
void imc_save_config( void );
void imc_save_channels( void );
char *const imcperm_names[] =
{
"Notset", "None", "Mort", "Imm", "Admin", "Imp"
};
SITEINFO *this_imcmud;
IMC_CHANNEL *first_imc_channel;
IMC_CHANNEL *last_imc_channel;
REMOTEINFO *first_rinfo;
REMOTEINFO *last_rinfo;
IMC_BAN *first_imc_ban;
IMC_BAN *last_imc_ban;
IMCUCACHE_DATA *first_imcucache;
IMCUCACHE_DATA *last_imcucache;
IMC_COLOR *first_imc_color;
IMC_COLOR *last_imc_color;
IMC_CMD_DATA *first_imc_command;
IMC_CMD_DATA *last_imc_command;
IMC_HELP_DATA *first_imc_help;
IMC_HELP_DATA *last_imc_help;
IMC_PHANDLER *first_phandler;
IMC_PHANDLER *last_phandler;
WHO_TEMPLATE *whot;
/*******************************************
* String buffering and logging functions. *
******************************************/
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*
* Renamed so it can play itself system independent.
* Samson 10-12-03
*/
size_t imcstrlcpy( char *dst, const char *src, size_t siz )
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if( n != 0 && --n != 0 )
{
do
{
if( ( *d++ = *s++ ) == 0 )
break;
}
while( --n != 0 );
}
/* Not enough room in dst, add NUL and traverse rest of src */
if( n == 0 )
{
if( siz != 0 )
*d = '\0'; /* NUL-terminate dst */
while( *s++ )
;
}
return( s - src - 1 ); /* count does not include NUL */
}
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(initial dst) + strlen(src); if retval >= siz,
* truncation occurred.
*
* Renamed so it can play itself system independent.
* Samson 10-12-03
*/
size_t imcstrlcat( char *dst, const char *src, size_t siz )
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while( n-- != 0 && *d != '\0' )
d++;
dlen = d - dst;
n = siz - dlen;
if( n == 0 )
return( dlen + strlen(s) );
while( *s != '\0' )
{
if( n != 1 )
{
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return( dlen + ( s - src ) ); /* count does not include NUL */
}
/* Generic log function which will route the log messages to the appropriate system logging function */
void imclog( const char *format, ... )
{
char buf[LGST], buf2[LGST];
char *strtime;
va_list ap;
va_start( ap, format );
vsnprintf( buf, LGST, format, ap );
va_end( ap );
snprintf( buf2, LGST, "IMC: %s", buf );
log_string(buf2);
strtime = ctime( &imc_time );
strtime[strlen(strtime)-1] = '\0';
fprintf( stderr, "%s :: %s\n", strtime, buf2 );
return;
}
/* 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], buf2[LGST];
char *strtime;
va_list ap;
va_start( ap, format );
vsnprintf( buf, LGST, format, ap );
va_end( ap );
snprintf( buf2, LGST, "***BUG*** IMC: %s", buf );
strtime = ctime( &imc_time );
strtime[strlen(strtime)-1] = '\0';
fprintf( stderr, "%s :: %s\n", strtime, buf2 );
return;
}
/*
Original Code from SW:FotE 1.1
Reworked strrep function.
Fixed a few glaring errors. It also will not 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];
imcstrlcpy( tbuf, txt, LGST );
for( color = first_imc_color; color; color = color->next )
imcstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, "" ), LGST );
for( color = first_imc_color; color; color = color->next )
imcstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, "" ), LGST );
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, D_MOBILE *ch )
{
IMC_COLOR *color;
static char tbuf[LGST];
if( !txt || *txt == '\0' )
return "";
if( IMCIS_SET( IMCFLAG(ch), IMC_COLORFLAG ) )
{
imcstrlcpy( tbuf, txt, LGST );
for( color = first_imc_color; color; color = color->next )
imcstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, color->mudtag ), LGST );
}
else
imcstrlcpy( tbuf, imc_strip_colors( txt ), LGST );
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 "";
imcstrlcpy( tbuf, txt, LGST );
for( color = first_imc_color; color; color = color->next )
imcstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, color->imctag ), LGST );
return tbuf;
}
/* Generic send_to_char type function to send to the proper code for each codebase */
void imc_to_char( const char *txt, D_MOBILE *ch )
{
char buf[LGST*2];
snprintf( buf, LGST*2, "%s\033[0m", color_itom( txt, ch ) );
text_to_mobile( ch, buf );
return;
}
/* Modified version of Smaug's ch_printf_color function */
void imc_printf( D_MOBILE *ch, const char *fmt, ... )
{
char buf[LGST];
va_list args;
va_start( args, fmt );
vsnprintf( buf, LGST, 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, D_MOBILE *ch )
{
char buf[LGST*2];
snprintf( buf, LGST*2, "%s\033[0m", color_itom( txt, ch ) );
text_to_mobile( ch, buf );
return;
}
/* Generic pager_printf type function */
void imcpager_printf( D_MOBILE *ch, const char *fmt, ... )
{
char buf[LGST];
va_list args;
va_start( args, fmt );
vsnprintf( buf, LGST, fmt, args );
va_end( args );
imc_to_pager( buf, ch );
return;
}
/********************************
* Low level utility functions. *
********************************/
/*
* 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' )
imcstrlcpy( newlist, member, LGST );
else
snprintf( newlist, LGST, "%s %s", *list, member );
IMCSTRFREE( *list );
*list = IMCSTRALLOC( newlist );
return;
}
/* 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;
imcstrlcpy( newlist, imcstrrep( *list, member, "" ), LGST );
IMCSTRFREE( *list );
*list = IMCSTRALLOC( newlist );
return;
}
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, '@' ) ) )
imcstrlcpy( mud, src, SMST );
else
imcstrlcpy( mud, person+1, SMST );
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, SMST, "%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] == '\n' )
{
newstr[y] = '\\';
newstr[++y] = 'n';
}
else if( src[x] == '\r' )
{
newstr[y] = '\\';
newstr[++y] = 'r';
}
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 D_MOBILE class which matches the string
*/
D_MOBILE *imc_find_user( char *name )
{
D_SOCKET *d;
ITERATOR *pIter;
D_MOBILE *vch = NULL;
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
if( ( vch = d->player ) != NULL && !strcasecmp( CH_IMCNAME(vch), name )
&& d->state == STATE_PLAYING )
{
FreeIterator( pIter );
return vch;
}
}
FreeIterator( pIter );
return NULL;
}
char *imcgetname( D_MOBILE *ch, char *from )
{
static char buf[SMST];
char *mud, *name;
mud = imc_mudof( from );
name = imc_nameof( from );
if( !strcasecmp( mud, this_imcmud->localname ) )
imcstrlcpy( buf, imc_nameof( name ), SMST );
else
imcstrlcpy( buf, from, SMST );
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( D_MOBILE *ch, const char *ignore )
{
IMC_IGNORE *temp;
/* Wildcard support thanks to Xorith */
for( temp = FIRST_IMCIGNORE(ch); temp; temp = temp->next )
{
if( !fnmatch( temp->name, ignore, 0 ) )
return TRUE;
}
return FALSE;
}
/* There should only one of these..... */
void imc_delete_info( void )
{
IMCSTRFREE( this_imcmud->servername );
IMCSTRFREE( this_imcmud->rhost );
IMCSTRFREE( this_imcmud->network );
IMCSTRFREE( this_imcmud->clientpw );
IMCSTRFREE( this_imcmud->serverpw );
IMCDISPOSE( this_imcmud->outbuf );
IMCSTRFREE( this_imcmud->localname );
IMCSTRFREE( this_imcmud->fullname );
IMCSTRFREE( this_imcmud->ihost );
IMCSTRFREE( this_imcmud->email );
IMCSTRFREE( this_imcmud->www );
IMCSTRFREE( this_imcmud->details );
IMCSTRFREE( this_imcmud->versionid );
IMCSTRFREE( this_imcmud->base );
IMCDISPOSE( this_imcmud );
}
/* delete the info entry "p" */
void imc_delete_reminfo( REMOTEINFO *p )
{
IMCUNLINK( p, first_rinfo, last_rinfo, next, prev );
IMCSTRFREE( p->name );
IMCSTRFREE( p->version );
IMCSTRFREE( p->network );
IMCSTRFREE( p->path );
IMCSTRFREE( p->url );
IMCSTRFREE( p->port );
IMCSTRFREE( p->host );
IMCDISPOSE( 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;
IMCCREATE( p, REMOTEINFO, 1 );
p->name = IMCSTRALLOC( mud );
if( !url || url[0] == '\0' )
p->url = IMCSTRALLOC( "Unknown" );
else
p->url = IMCSTRALLOC( url );
if( !version || version[0] == '\0' )
p->version = IMCSTRALLOC( "Unknown" );
else
p->version = IMCSTRALLOC( version );
if( !netname || netname[0] == '\0' )
p->network = IMCSTRALLOC( this_imcmud->network );
else
p->network = IMCSTRALLOC( netname );
if( !path || path[0] == '\0' )
p->path = IMCSTRALLOC( "UNKNOWN" );
else
p->path = IMCSTRALLOC( 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 )
IMCLINK( p, first_rinfo, last_rinfo, next, prev );
else
IMCINSERT( p, mud_prev, first_rinfo, next, prev );
return;
}
/* 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( D_MOBILE *ch, char *mud )
{
REMOTEINFO *r = imc_find_reminfo( mud );
if( !r )
{
imc_printf( ch, "~W%s ~cis not a valid mud name.\n\r", mud );
return FALSE;
}
if( r->expired )
{
imc_printf( ch, "~W%s ~cis not connected right now.\n\r", r->name );
return FALSE;
}
return TRUE;
}
bool check_mudof( D_MOBILE *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( D_MOBILE *ch, int checkvalue, int targetvalue, bool enforceequal )
{
if( checkvalue < 0 || checkvalue > IMCPERM_IMP )
{
imc_to_char( "Invalid permission setting.\n\r", ch );
return FALSE;
}
if( checkvalue > IMCPERM(ch) )
{
imc_to_char( "You cannot set permissions higher than your own.\n\r", ch );
return FALSE;
}
if( checkvalue == IMCPERM(ch) && IMCPERM(ch) != IMCPERM_IMP && enforceequal )
{
imc_to_char( "You cannot set permissions equal to your own. Someone higher up must do this.\n\r", ch );
return FALSE;
}
if( IMCPERM(ch) < targetvalue )
{
imc_to_char( "You cannot alter the permissions of someone or something above your own.\n\r", ch );
return FALSE;
}
return TRUE;
}
IMC_BAN *imc_newban( void )
{
IMC_BAN *ban;
IMCCREATE( ban, IMC_BAN, 1 );
ban->name = NULL;
IMCLINK( ban, first_imc_ban, last_imc_ban, next, prev );
return ban;
}
void imc_addban( char *what )
{
IMC_BAN *ban;
ban = imc_newban();
ban->name = IMCSTRALLOC( what );
}
void imc_freeban( IMC_BAN *ban )
{
IMCSTRFREE( ban->name );
IMCUNLINK( ban, first_imc_ban, last_imc_ban, next, prev );
IMCDISPOSE( 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", "imc_freechan: Freeing NULL channel!" );
return;
}
IMCUNLINK( c, first_imc_channel, last_imc_channel, next, prev );
IMCSTRFREE( c->name );
IMCSTRFREE( c->owner );
IMCSTRFREE( c->operators );
IMCSTRFREE( c->invited );
IMCSTRFREE( c->excluded );
IMCSTRFREE( c->local_name );
IMCSTRFREE( c->regformat );
IMCSTRFREE( c->emoteformat );
IMCSTRFREE( c->socformat );
for( x = 0; x < MAX_IMCHISTORY; x++ )
IMCSTRFREE( c->history[x] );
IMCDISPOSE( c );
return;
}
void imcformat_channel( D_MOBILE *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, LGST, "~R[~Y%s~R] ~C%%s: ~c%%s", c->local_name );
IMCSTRFREE( c->regformat );
c->regformat = IMCSTRALLOC( buf );
}
if( format == 2 || format == 4 )
{
snprintf( buf, LGST, "~R[~Y%s~R] ~c%%s %%s", c->local_name );
IMCSTRFREE( c->emoteformat );
c->emoteformat = IMCSTRALLOC( buf );
}
if( format == 3 || format == 4 )
{
snprintf( buf, LGST, "~R[~Y%s~R] ~c%%s", c->local_name );
IMCSTRFREE( c->socformat );
c->socformat = IMCSTRALLOC( buf );
}
}
}
else
{
if( ch && ( !d->local_name || d->local_name[0] == '\0' ) )
{
imc_to_char( "This channel is not yet locally configured.\n\r", ch );
return;
}
if( format == 1 || format == 4 )
{
snprintf( buf, LGST, "~R[~Y%s~R] ~C%%s: ~c%%s", d->local_name );
IMCSTRFREE( d->regformat );
d->regformat = IMCSTRALLOC( buf );
}
if( format == 2 || format == 4 )
{
snprintf( buf, LGST, "~R[~Y%s~R] ~c%%s %%s", d->local_name );
IMCSTRFREE( d->emoteformat );
d->emoteformat = IMCSTRALLOC( buf );
}
if( format == 3 || format == 4 )
{
snprintf( buf, LGST, "~R[~Y%s~R] ~c%%s", d->local_name );
IMCSTRFREE( d->socformat );
d->socformat = IMCSTRALLOC( buf );
}
}
imc_save_channels();
return;
}
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;
}
IMCCREATE( c, IMC_CHANNEL, 1 );
c->name = IMCSTRALLOC( chan );
c->owner = IMCSTRALLOC( owner );
c->operators = IMCSTRALLOC( ops );
c->invited = IMCSTRALLOC( invite );
c->excluded = IMCSTRALLOC( exclude );
if( lname && lname[0] != '\0' )
c->local_name = IMCSTRALLOC( lname );
else
c->local_name = imc_channel_nameof( c->name );
c->level = perm;
c->refreshed = TRUE;
c->open = copen;
IMCLINK( c, first_imc_channel, last_imc_channel, next, prev );
imcformat_channel( NULL, c, 4, FALSE );
return;
}
/*
* Read a number from a file. [Taken from Smaug's fread_number]
*/
int imcfread_number( FILE *fp )
{
int number;
bool sign;
char c;
do
{
if( feof(fp) )
{
imclog( "%s", "imcfread_number: EOF encountered on read." );
return 0;
}
c = getc( fp );
}
while( isspace(c) );
number = 0;
sign = FALSE;
if( c == '+' )
c = getc( fp );
else if ( c == '-' )
{
sign = TRUE;
c = getc( fp );
}
if( !isdigit(c) )
{
imclog( "imcfread_number: bad format. (%c)", c );
return 0;
}
while( isdigit(c) )
{
if( feof(fp) )
{
imclog( "%s", "imcfread_number: EOF encountered on read." );
return number;
}
number = number * 10 + c - '0';
c = getc( fp );
}
if( sign )
number = 0 - number;
if( c == '|' )
number += imcfread_number( fp );
else if( c != ' ' )
ungetc( c, fp );
return number;
}
/*
* Read to end of line into static buffer [Taken from Smaug's fread_line]
*/
char *imcfread_line( FILE *fp )
{
char line[LGST];
char *pline;
char c;
int ln;
pline = line;
line[0] = '\0';
ln = 0;
/*
* Skip blanks.
* Read first char.
*/
do
{
if( feof(fp) )
{
imcbug( "%s", "imcfread_line: EOF encountered on read." );
imcstrlcpy( line, "", LGST );
return IMCSTRALLOC( line );
}
c = getc( fp );
}
while( isspace(c) );
ungetc( c, fp );
do
{
if( feof(fp) )
{
imcbug( "%s", "imcfread_line: EOF encountered on read." );
*pline = '\0';
return IMCSTRALLOC( line );
}
c = getc( fp );
*pline++ = c; ln++;
if( ln >= (LGST - 1) )
{
imcbug( "%s", "imcfread_line: line too long" );
break;
}
}
while( c != '\n' && c != '\r' );
do
{
c = getc( fp );
}
while( c == '\n' || c == '\r' );
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 IMCSTRALLOC( line );
}
/*
* Read one word (into static buffer). [Taken from Smaug's fread_word]
*/
char *imcfread_word( FILE *fp )
{
static char word[SMST];
char *pword;
char cEnd;
do
{
if( feof(fp) )
{
imclog( "%s", "imcfread_word: EOF encountered on read." );
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 + SMST; pword++ )
{
if( feof(fp) )
{
imclog( "%s", "imcfread_word: EOF encountered on read." );
*pword = '\0';
return word;
}
*pword = getc( fp );
if( cEnd == ' ' ? isspace(*pword) : *pword == cEnd )
{
if( cEnd == ' ' )
ungetc( *pword, fp );
*pword = '\0';
return word;
}
}
imclog( "%s", "imcfread_word: word too long" );
return NULL;
}
/*
* Read to end of line (for comments). [Taken from Smaug's fread_to_eol]
*/
void imcfread_to_eol( FILE *fp )
{
char c;
do
{
if( feof(fp) )
{
imclog( "%s", "imcfread_to_eol: EOF encountered on read." );
return;
}
c = getc( fp );
}
while( c != '\n' && c != '\r' );
do
{
c = getc( fp );
}
while( c == '\n' || c == '\r' );
ungetc( c, fp );
return;
}
/*
* Read a letter from a file. [Taken from Smaug's fread_letter]
*/
char imcfread_letter( FILE *fp )
{
char c;
do
{
if( feof(fp) )
{
imclog( "%s", "imcfread_letter: EOF encountered on read." );
return '\0';
}
c = getc( fp );
}
while( isspace(c) );
return c;
}
/******************************************
* Packet handling and routing functions. *
******************************************/
void imc_register_packet_handler( 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;
}
}
IMCCREATE( ph, IMC_PHANDLER, 1 );
ph->name = IMCSTRALLOC(name);
ph->func = func;
IMCLINK( ph, first_phandler, last_phandler, next, prev );
return;
}
void imc_freepacket( IMC_PACKET *p )
{
IMC_PDATA *data, *data_next;
for( data = p->first_data; data; data = data_next )
{
data_next = data->next;
IMCUNLINK( data, p->first_data, p->last_data, next, prev );
IMCDISPOSE( data );
}
IMCDISPOSE( 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] == 'n' )
output[i++] = '\n';
else if( packet[current] == 'r' )
output[i++] = '\r';
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, IMC_BUFF_SIZE, "%s\n\r", 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;
IMCRECREATE( 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';
return;
}
/*
* 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, IMC_BUFF_SIZE, "%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), IMC_BUFF_SIZE-strlen(txt), "%s", data->field );
imc_freepacket( p );
imc_write_buffer( txt );
return;
}
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, IMC_BUFF_SIZE, fmt, args );
va_end( args );
IMCCREATE( data, IMC_PDATA, 1 );
snprintf( data->field, IMC_BUFF_SIZE, " %s", escape_string( pkt ) );
IMCLINK( data, p->first_data, p->last_data, next, prev );
return;
}
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;
}
IMCCREATE( p, IMC_PACKET, 1 );
snprintf( p->from, SMST, "%s@%s", from, this_imcmud->localname );
imcstrlcpy( p->type, type, SMST );
imcstrlcpy( p->to, to, SMST );
p->first_data = p->last_data = NULL;
return p;
}
void imc_update_tellhistory( D_MOBILE *ch, const char *msg )
{
char new_msg[LGST];
struct tm *local = localtime(&imc_time);
int x;
snprintf( new_msg, LGST, "~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) = IMCSTRALLOC( new_msg );
break;
}
if( x == MAX_IMCTELLHISTORY-1 )
{
int i;
for( i = 1; i < MAX_IMCTELLHISTORY; i++ )
{
IMCSTRFREE( IMCTELLHISTORY(ch,i-1) );
IMCTELLHISTORY(ch,i-1) = IMCSTRALLOC( IMCTELLHISTORY(ch,i) );
}
IMCSTRFREE( IMCTELLHISTORY(ch,x) );
IMCTELLHISTORY(ch,x) = IMCSTRALLOC( new_msg );
}
}
return;
}
void imc_send_tell( char *from, char *to, char *txt, int reply )
{
IMC_PACKET *p;
p = imc_newpacket( from, "tell", to );
imc_addtopacket( p, "text=%s", txt );
if( reply > 0 )
imc_addtopacket( p, "isreply=%d", reply );
imc_write_packet( p );
return;
}
PFUN( imc_recv_tell )
{
D_MOBILE *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, LGST, "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, LGST, "%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, LGST, "%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, LGST, "%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, LGST, "%s is currently AFK. Try back later.", q->to );
imc_send_tell( "*", q->from, buf, 1 );
}
return;
}
if( strcasecmp( imc_nameof( q->from ), "*" ) )
{
IMCSTRFREE( IMC_RREPLY(vic) );
IMCSTRFREE( IMC_RREPLY_NAME(vic) );
IMC_RREPLY(vic) = IMCSTRALLOC( q->from );
IMC_RREPLY_NAME(vic) = IMCSTRALLOC( imcgetname( vic, q->from ) );
}
}
/* Tell social */
if( reply == 2 || reply == 1 )
snprintf( buf, LGST, "~WImctell: ~c%s\n\r", txt );
else
snprintf( buf, LGST, "~C%s ~cimctells you ~c'~W%s~c'~!\n\r", imcgetname( vic, q->from ), txt );
imc_to_char( buf, vic );
imc_update_tellhistory( vic, buf );
return;
}
PFUN( imc_recv_emote )
{
D_SOCKET *d;
ITERATOR *pIter;
D_MOBILE *ch;
char txt[LGST], lvl[SMST];
int level;
imc_getData( txt, "text", packet );
imc_getData( lvl, "level", packet );
level = atoi( lvl );
if( level < 0 || level > IMCPERM_IMP )
level = IMCPERM_IMM;
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
if( d->state == STATE_PLAYING && ( ch = d->player ) != NULL
&& IMCPERM(ch) >= level )
imc_printf( ch, "~p[~GIMC~p] %s %s\n\r", imcgetname( ch, q->from ), txt );
}
FreeIterator( pIter );
return;
}
void update_imchistory( IMC_CHANNEL *channel, char *message )
{
char msg[LGST], buf[LGST];
struct tm *local;
int x;
if( !channel )
{
imcbug( "%s", "update_imchistory: NULL channel received!" );
return;
}
if( !message || message[0] == '\0' )
{
imcbug( "%s", "update_imchistory: NULL message received!" );
return;
}
imcstrlcpy( msg, message, LGST );
for( x = 0; x < MAX_IMCHISTORY; x++ )
{
if( channel->history[x] == NULL )
{
local = localtime( &imc_time );
snprintf( buf, LGST, "~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] = IMCSTRALLOC( buf );
if( IMCIS_SET( channel->flags, IMCCHAN_LOG ) )
{
FILE *fp;
snprintf( buf, LGST, "%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] != NULL )
{
IMCSTRFREE( channel->history[z] );
channel->history[z] = IMCSTRALLOC( channel->history[y] );
}
}
local = localtime( &imc_time );
snprintf( buf, LGST, "~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 );
IMCSTRFREE( channel->history[x] );
channel->history[x] = IMCSTRALLOC( buf );
if( IMCIS_SET( channel->flags, IMCCHAN_LOG ) )
{
FILE *fp;
snprintf( buf, LGST, "%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 );
}
}
}
}
return;
}
void imc_display_channel( IMC_CHANNEL *c, const char *from, char *txt, int emote )
{
D_SOCKET *d;
ITERATOR *pIter;
D_MOBILE *ch;
char buf[LGST], name[SMST];
if( !c->local_name || c->local_name[0] == '\0' || !c->refreshed )
return;
if( emote < 2 )
snprintf( buf, LGST, emote ? c->emoteformat : c->regformat, from, txt );
else
snprintf( buf, LGST, c->socformat, txt );
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
ch = d->player;
if( !ch || d->state != STATE_PLAYING )
continue;
if( IMCPERM(ch) < c->level || !imc_hasname( IMC_LISTEN(ch), c->local_name ) )
continue;
if( !c->open )
{
snprintf( name, SMST, "%s@%s", CH_IMCNAME(ch), this_imcmud->localname );
if( !imc_hasname( c->invited, name ) && strcasecmp( c->owner, name ) )
continue;
}
imc_printf( ch, "%s\n\r", buf );
}
FreeIterator( pIter );
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 ) ) )
return;
imc_display_channel( c, sender, txt, em );
return;
}
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 || sender[0] == '\0' )
imc_display_channel( c, q->from, txt, em );
else
imc_display_channel( c, sender, txt, em );
return;
}
/* 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, SMST, "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 );
return;
}
PFUN( imc_recv_chanwhoreply )
{
IMC_CHANNEL *c;
D_MOBILE *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 );
return;
}
PFUN( imc_recv_chanwho )
{
IMC_PACKET *p;
IMC_CHANNEL *c;
D_SOCKET *d;
ITERATOR *pIter;
D_MOBILE *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, IMC_BUFF_SIZE, "Channel %s is not locally configured on %s\n\r", lname, this_imcmud->localname );
else if( c->level > level )
snprintf( buf, IMC_BUFF_SIZE, "Channel %s is above your permission level on %s\n\r", lname, this_imcmud->localname );
else
{
int count = 0, col = 0;
snprintf( buf, IMC_BUFF_SIZE, "The following people are listening to %s on %s:\n\r\n\r", lname, this_imcmud->localname );
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
person = d->player;
if( !person )
continue;
if( IMCISINVIS(person) )
continue;
if( !imc_hasname( IMC_LISTEN(person), c->local_name ) )
continue;
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "%-15s", CH_IMCNAME(person) );
count++;
if( ++col % 3 == 0 )
{
col = 0;
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "%s", "\n\r" );
}
}
FreeIterator( pIter );
if( col != 0 )
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "%s", "\n\r" );
/* Send no response to a broadcast request if nobody is listening. */
if( count == 0 && !strcasecmp( q->to, "*" ) )
return;
else if( count == 0 )
imcstrlcat( buf, "Nobody\n\r", IMC_BUFF_SIZE );
}
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 );
return;
}
void imc_sendnotify( D_MOBILE *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 );
return;
}
PFUN( imc_recv_channelnotify )
{
IMC_CHANNEL *c;
D_SOCKET *d;
ITERATOR *pIter;
D_MOBILE *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, LGST, c->emoteformat, q->from, "has joined the channel." );
else
snprintf( buf, LGST, c->emoteformat, q->from, "has left the channel." );
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
ch = d->player;
if( !ch || d->state != STATE_PLAYING )
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\n\r", buf );
}
FreeIterator( pIter );
return;
}
char *imccenterline( const char *string, int length )
{
char stripped[300];
static char outbuf[400];
int amount;
imcstrlcpy( stripped, imc_strip_colors( string ), 300 );
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, 400, "%*s%s%*s", ( amount / 2 ), "", string,
( ( amount / 2 ) * 2 ) == amount ? ( amount / 2 ) : ( ( amount / 2 ) + 1 ), "" );
return outbuf;
}
char *imcrankbuffer( D_MOBILE *ch )
{
static char rbuf[SMST];
if( IMCPERM(ch) >= IMCPERM_IMM )
{
imcstrlcpy( rbuf, "~YStaff", SMST );
if( CH_IMCRANK(ch) && CH_IMCRANK(ch)[0] != '\0' )
snprintf( rbuf, SMST, "~Y%s", color_mtoi( CH_IMCRANK(ch) ) );
}
else
{
imcstrlcpy( rbuf, "~BPlayer", SMST );
if( CH_IMCRANK(ch) && CH_IMCRANK(ch)[0] != '\0' )
snprintf( rbuf, SMST, "~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 );
return;
}
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 );
return;
}
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>" ) )
{
imcstrlcpy( arg, imcstrrep( arg, "<center>", "" ), SMST );
imcstrlcpy( arg, imccenterline( arg, 78 ), SMST );
}
imcstrlcat( newline, arg, LGST );
imcstrlcat( newline, "\n", LGST );
}
return newline;
}
char *process_who_head( int plrcount )
{
static char head[LGST];
char pcount[SMST];
imcstrlcpy( head, whot->head, LGST );
snprintf( pcount, SMST, "%d", plrcount );
imcstrlcpy( head, imcstrrep( head, "<%plrcount%>", pcount ), LGST );
imcstrlcpy( head, multiline_center( head ), LGST );
return head;
}
char *process_who_tail( int plrcount )
{
static char tail[LGST];
char pcount[SMST];
imcstrlcpy( tail, whot->tail, LGST );
snprintf( pcount, SMST, "%d", plrcount );
imcstrlcpy( tail, imcstrrep( tail, "<%plrcount%>", pcount ), LGST );
imcstrlcpy( tail, multiline_center( tail ), LGST );
return tail;
}
char *process_plrline( char *plrrank, char *plrflags, char *plrname, char *plrtitle )
{
static char pline[LGST];
imcstrlcpy( pline, whot->immline, LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charrank%>", plrrank ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charflags%>", plrflags ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charname%>", plrname ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%chartitle%>", plrtitle ), LGST );
imcstrlcat( pline, "\n", LGST );
return pline;
}
char *process_immline( char *plrrank, char *plrflags, char *plrname, char *plrtitle )
{
static char pline[LGST];
imcstrlcpy( pline, whot->immline, LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charrank%>", plrrank ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charflags%>", plrflags ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%charname%>", plrname ), LGST );
imcstrlcpy( pline, imcstrrep( pline, "<%chartitle%>", plrtitle ), LGST );
imcstrlcat( pline, "\n", LGST );
return pline;
}
char *process_who_template( char *head, char *tail, char *plrlines, char *immlines, char *plrheader, char *immheader )
{
static char master[LGST];
imcstrlcpy( master, whot->master, LGST );
imcstrlcpy( master, imcstrrep( master, "<%head%>", head ), LGST );
imcstrlcpy( master, imcstrrep( master, "<%tail%>", tail ), LGST );
imcstrlcpy( master, imcstrrep( master, "<%plrheader%>", plrheader ), LGST );
imcstrlcpy( master, imcstrrep( master, "<%immheader%>", immheader ), LGST );
imcstrlcpy( master, imcstrrep( master, "<%plrline%>", plrlines ), LGST );
imcstrlcpy( master, imcstrrep( master, "<%immline%>", immlines ), LGST );
return master;
}
char *imc_assemble_who( void )
{
D_MOBILE *person;
D_SOCKET *d;
ITERATOR *pIter;
int pcount = 0;
bool plr = FALSE, imm = 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';
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
person = d->player;
if( person && d->state == STATE_PLAYING )
{
if( IMCPERM( person ) <= IMCPERM_NONE || IMCPERM( person ) >= IMCPERM_IMM )
continue;
if( IMCISINVIS( person ) )
continue;
++pcount;
if( !plr )
{
imcstrlcpy( plrheader, whot->plrheader, LGST );
plr = TRUE;
}
imcstrlcpy( rank, imcrankbuffer( person ), LGST );
if( IMCAFK( person ) )
imcstrlcpy( flags, "AFK", SMST );
else
imcstrlcpy( flags, "---", SMST );
imcstrlcpy( name, CH_IMCNAME( person ), SMST );
imcstrlcpy( title, color_mtoi( CH_IMCTITLE( person ) ), SMST );
imcstrlcpy( plrline, process_plrline( rank, flags, name, title ), SMST );
imcstrlcat( plrlines, plrline, LGST );
}
}
FreeIterator( pIter );
pIter = AllocIterator( dsock_list );
while( ( d = (D_SOCKET *)NextInList( pIter ) ) != NULL )
{
person = d->player;
if( person && d->state == STATE_PLAYING )
{
if( IMCPERM( person ) <= IMCPERM_NONE || IMCPERM( person ) < IMCPERM_IMM )
continue;
if( IMCISINVIS( person ) )
continue;
++pcount;
if( !imm )
{
imcstrlcpy( immheader, whot->immheader, SMST );
imm = TRUE;
}
imcstrlcpy( rank, imcrankbuffer( person ), SMST );
if( IMCAFK( person ) )
imcstrlcpy( flags, "AFK", SMST );
else
imcstrlcpy( flags, "---", SMST );
imcstrlcpy( name, CH_IMCNAME( person ), SMST );
imcstrlcpy( title, color_mtoi( CH_IMCTITLE( person ) ), SMST );
imcstrlcpy( immline, process_immline( rank, flags, name, title ), SMST );
imcstrlcat( immlines, immline, LGST );
}
}
FreeIterator( pIter );
imcstrlcpy( head, process_who_head( pcount ), LGST );
imcstrlcpy( tail, process_who_tail( pcount ), LGST );
imcstrlcpy( master, process_who_template( head, tail, plrlines, immlines, plrheader, immheader ), LGST );
return master;
}
void imc_process_who( char *from )
{
char whoreply[IMC_BUFF_SIZE];
imcstrlcpy( whoreply, imc_assemble_who(), IMC_BUFF_SIZE );
imc_send_whoreply( from, whoreply );
}
/* Finger code */
void imc_process_finger( char *from, char *type )
{
D_MOBILE *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, "No such player is online.\n\r" );
return;
}
if( IMCISINVIS(victim) || IMCPERM(victim) < IMCPERM_MORT )
{
imc_send_whoreply( from, "No such player is online.\n\r" );
return;
}
snprintf( buf, IMC_BUFF_SIZE,
"\n\r~cPlayer Profile for ~W%s~c:\n\r"
"~W-------------------------------\n\r"
"~cStatus: ~W%s\n\r"
"~cPermission level: ~W%s\n\r"
"~cListening to channels [Names may not match your mud]: ~W%s\n\r",
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), IMC_BUFF_SIZE-strlen(buf),
"~cEmail : ~W%s\n\r"
"~cHomepage: ~W%s\n\r"
"~cICQ : ~W%d\n\r"
"~cAIM : ~W%s\n\r"
"~cYahoo : ~W%s\n\r"
"~cMSN : ~W%s\n\r",
( 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), IMC_BUFF_SIZE-strlen(buf), "~W%s\n\r",
( 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, IMC_BUFF_SIZE, "\n\r~WMUD Name : ~c%s\n\r", this_imcmud->localname );
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "~WHost : ~c%s\n\r", this_imcmud->ihost );
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "~WAdmin Email : ~c%s\n\r", this_imcmud->email );
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "~WWebsite : ~c%s\n\r", this_imcmud->www );
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "~WIMC2 Version: ~c%s\n\r", this_imcmud->versionid );
snprintf( buf+strlen(buf), IMC_BUFF_SIZE-strlen(buf), "~WDetails : ~c%s\n\r", this_imcmud->details );
}
else
snprintf( buf, IMC_BUFF_SIZE, "%s is not a valid option. Options are: who, finger, or info.\n\r", type );
imc_send_whoreply( q->from, buf );
return;
}
PFUN( imc_recv_whoreply )
{
D_MOBILE *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 );
return;
}
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 );
return;
}
PFUN( imc_recv_whoisreply )
{
D_MOBILE *vic;
char txt[LGST];
imc_getData( txt, "text", packet );
if( ( vic = imc_find_user( imc_nameof( q->to ) ) ) != NULL )
imc_to_char( txt, vic );
return;
}
void imc_send_whois( char *from, char *user )
{
IMC_PACKET *p;
p = imc_newpacket( from, "whois", user );
imc_write_packet( p );
return;
}
PFUN( imc_recv_whois )
{
D_MOBILE *vic;
char buf[LGST];
if( ( vic = imc_find_user( imc_nameof( q->to ) ) ) != NULL && !IMCISINVIS(vic) )
{
snprintf( buf, LGST, "~RIMC Locate: ~Y%s@%s: ~cOnline.\n\r", CH_IMCNAME(vic), this_imcmud->localname );
imc_send_whoisreply( q->from, buf );
}
return;
}
PFUN( imc_recv_beep )
{
D_MOBILE *vic = NULL;
char buf[LGST];
if( !( vic = imc_find_user( imc_nameof( q->to ) ) ) || IMCPERM(vic) < IMCPERM_MORT )
{
snprintf( buf, LGST, "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, LGST, "%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, LGST, "%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, LGST, "%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, LGST, "%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.~!\n\r", q->from );
}
void imc_send_beep( char *from, char *to )
{
IMC_PACKET *p;
p = imc_newpacket( from, "beep", to );
imc_write_packet( p );
return;
}
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 );
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 && url[0] != '\0' )
{
IMCSTRFREE( r->url );
r->url = IMCSTRALLOC( url );
}
if( version && version[0] != '\0' )
{
IMCSTRFREE( r->version );
r->version = IMCSTRALLOC( version );
}
if( netname && netname[0] != '\0' )
{
IMCSTRFREE( r->network );
r->network = IMCSTRALLOC( netname );
}
if( q->route && q->route[0] != '\0' )
{
IMCSTRFREE( r->path );
r->path = IMCSTRALLOC( q->route );
}
if( host && host[0] != '\0' )
{
IMCSTRFREE( r->host );
r->host = IMCSTRALLOC( host );
}
if( iport && iport[0] != '\0' )
{
IMCSTRFREE( r->port );
r->port = IMCSTRALLOC( iport );
}
return;
}
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 );
return;
}
void imc_request_keepalive( void )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "keepalive-request", "*@*" );
imc_write_packet( p );
imc_send_keepalive( NULL, "*@*" );
return;
}
void imc_firstrefresh( void )
{
IMC_PACKET *p;
p = imc_newpacket( "*", "ice-refresh", "IMC@$" );
imc_write_packet( p );
return;
}
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 = atoi( 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 || chan[0] == '\0' )
{
imclog( "%s: NULL channel name received, skipping", __FUNCTION__ );
return;
}
IMCSTRFREE( c->name );
IMCSTRFREE( c->owner );
IMCSTRFREE( c->operators );
IMCSTRFREE( c->invited );
IMCSTRFREE( c->excluded );
c->name = IMCSTRALLOC( chan );
c->owner = IMCSTRALLOC( owner );
c->operators = IMCSTRALLOC( ops );
c->invited = IMCSTRALLOC( invite );
c->excluded = IMCSTRALLOC( exclude );
c->open = copen;
c->refreshed = TRUE;
return;
}
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();
}
PFUN( imc_recv_closenotify )
{
REMOTEINFO *r;
char host[SMST];
imc_getData( host, "host", packet );
if( !( r = imc_find_reminfo( host ) ) )
return;
r->expired = TRUE;
return;
}
void imc_register_default_packets( void )
{
/* Once registered, these are not 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( "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;
IMCCREATE( 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 ) )
{
IMCDISPOSE( 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 );
}
IMCDISPOSE( p );
return;
}
(*pfun)( p, packet );
IMCDISPOSE( p );
return;
}
void imc_finalize_connection( char *name, char *netname )
{
this_imcmud->state = IMC_ONLINE;
if( netname && netname[0] != '\0' )
{
IMCSTRFREE( this_imcmud->network );
this_imcmud->network = IMCSTRALLOC( netname );
}
IMCSTRFREE( this_imcmud->servername );
this_imcmud->servername = IMCSTRALLOC( name );
imclog( "Connected to %s. Network ID: %s", name, ( netname && netname[0] != '\0' ) ? netname : "Unknown" );
imcconnect_attempts = 0;
imc_request_keepalive();
imc_firstrefresh();
return;
}
/* 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( "SHA256 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 );
return;
}
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( 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 || 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 || pw[0] == '\0' )
{
imclog( "SHA-256 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, SMST, "%ld%s%s", auth_value, this_imcmud->clientpw, this_imcmud->serverpw );
cryptpwd = sha256_crypt( pwd );
snprintf( response, LGST, "SHA256-AUTH-RESP %s %s version=%d",
this_imcmud->localname, cryptpwd, IMC_VERSION );
imc_write_buffer( response );
return;
}
/* SHA256 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 && 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 );
return;
}
/*
* Transfer one line from input buffer to input line.
*/
bool imc_read_buffer( void )
{
unsigned int i = 0, j = 0, k = 0;
unsigned char ended = 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] != '\n' && this_imcmud->inbuf[i] != '\r'
&& i < IMC_BUFF_SIZE; i++ )
{
this_imcmud->incomm[k++] = this_imcmud->inbuf[i];
}
while( this_imcmud->inbuf[i] == '\n' || this_imcmud->inbuf[i] == '\r' )
{
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;
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;
if ( iStart >= sizeof( this_imcmud->inbuf ) - 10 )
break;
begin = 0;
}
else if ( nRead == 0 && this_imcmud->desc == 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 );
imc_time = (time_t) last_time.tv_sec;
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;
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() )
{
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 );
}
return;
}
/************************************
* User login and logout functions. *
************************************/
void imc_adjust_perms( D_MOBILE *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;
}
return;
}
void imc_char_login( D_MOBILE *ch )
{
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.\n\r", ch );
return;
}
return;
}
bool imc_loadchar( D_MOBILE *ch, FILE *fp, const char *word )
{
bool fMatch = FALSE;
if( IMCPERM(ch) == IMCPERM_NOTSET )
imc_adjust_perms( ch );
switch( word[0] )
{
case 'I':
IMCKEY( "IMCPerm", IMCPERM(ch), imcfread_number( fp ) );
IMCKEY( "IMCEmail", IMC_EMAIL(ch), imcfread_line( fp ) );
IMCKEY( "IMCAIM", IMC_AIM(ch), imcfread_line( fp ) );
IMCKEY( "IMCICQ", IMC_ICQ(ch), imcfread_number( fp ) );
IMCKEY( "IMCYahoo", IMC_YAHOO(ch), imcfread_line( fp ) );
IMCKEY( "IMCMSN", IMC_MSN(ch), imcfread_line( fp ) );
IMCKEY( "IMCHomepage", IMC_HOMEPAGE(ch), imcfread_line( fp ) );
IMCKEY( "IMCComment", IMC_COMMENT(ch), imcfread_line( fp ) );
if( !strcasecmp( word, "IMCFlags" ) )
{
IMCFLAG(ch) = imcfread_number( fp );
imc_char_login( ch );
fMatch = TRUE;
break;
}
if( !strcasecmp( word, "IMClisten" ) )
{
IMC_LISTEN(ch) = imcfread_line( fp );
if( IMC_LISTEN(ch) != NULL && 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) != NULL && 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;
IMCCREATE( temp, IMC_IGNORE, 1 );
temp->name = imcfread_line( fp );
IMCLINK( temp, FIRST_IMCIGNORE(ch), LAST_IMCIGNORE(ch), next, prev );
fMatch = TRUE;
break;
}
break;
}
return fMatch;
}
void imc_savechar( D_MOBILE *ch, FILE *fp )
{
IMC_IGNORE *temp;
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 );
return;
}
void imc_freechardata( D_MOBILE *ch )
{
IMC_IGNORE *ign, *ign_next;
int x;
if( CH_IMCDATA(ch) == NULL )
return;
for( ign = FIRST_IMCIGNORE(ch); ign; ign = ign_next )
{
ign_next = ign->next;
IMCSTRFREE( ign->name );
IMCUNLINK( ign, FIRST_IMCIGNORE(ch), LAST_IMCIGNORE(ch), next, prev );
IMCDISPOSE( ign );
}
for( x = 0; x < MAX_IMCTELLHISTORY; x++ )
IMCDISPOSE( IMCTELLHISTORY(ch,x) );
IMCSTRFREE( IMC_LISTEN(ch) );
IMCSTRFREE( IMC_DENY(ch) );
IMCSTRFREE( IMC_RREPLY(ch) );
IMCSTRFREE( IMC_RREPLY_NAME(ch) );
IMCSTRFREE( IMC_EMAIL(ch) );
IMCSTRFREE( IMC_HOMEPAGE(ch) );
IMCSTRFREE( IMC_AIM(ch) );
IMCSTRFREE( IMC_YAHOO(ch) );
IMCSTRFREE( IMC_MSN(ch) );
IMCSTRFREE( IMC_COMMENT(ch) );
IMCDISPOSE( CH_IMCDATA(ch) );
return;
}
void imc_initchar( D_MOBILE *ch )
{
IMCCREATE( 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;
return;
}
/*******************************************
* 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, 256, "%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 );
unlink( filename );
}
}
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, 256, "%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] != NULL )
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" : imcfread_word( fp );
fMatch = FALSE;
switch( word[0] )
{
case '*':
fMatch = TRUE;
imcfread_to_eol( fp );
break;
case 'C':
IMCKEY( "ChanName", channel->name, imcfread_line( fp ) );
IMCKEY( "ChanLocal", channel->local_name, imcfread_line( fp ) );
IMCKEY( "ChanRegF", channel->regformat, imcfread_line( fp ) );
IMCKEY( "ChanEmoF", channel->emoteformat, imcfread_line( fp ) );
IMCKEY( "ChanSocF", channel->socformat, imcfread_line( fp ) );
IMCKEY( "ChanLevel", channel->level, imcfread_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 = imcfread_letter( fp );
if( letter == '*' )
{
imcfread_to_eol( fp );
continue;
}
if( letter != '#' )
{
imcbug( "%s", "imc_loadchannels: # not found." );
break;
}
word = imcfread_word( fp );
if( !strcasecmp( word, "IMCCHAN" ) )
{
int x;
IMCCREATE( 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 */
IMCLINK( 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 );
return;
}
/* 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 );
return;
}
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 = imcfread_word( inf );
if( strcasecmp( word, "#IGNORES" ) )
{
imcbug( "%s", "imc_readbans: Corrupt file" );
IMCFCLOSE( inf );
return;
}
while( !feof( inf ) && !ferror( inf ) )
{
imcstrlcpy( temp, imcfread_word( inf ), SMST );
if( !strcasecmp( temp, "#END" ) )
{
IMCFCLOSE( inf );
return;
}
imc_addban( temp );
}
if( ferror( inf ) )
{
perror( "imc_readbans" );
IMCFCLOSE( inf );
return;
}
IMCFCLOSE( inf );
return;
}
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 );
return;
}
void imc_readcolor( IMC_COLOR *color, FILE *fp )
{
const char *word;
bool fMatch;
for ( ; ; )
{
word = feof( fp ) ? "End" : imcfread_word( fp );
fMatch = FALSE;
switch( word[0] )
{
case '*':
fMatch = TRUE;
imcfread_to_eol( fp );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
return;
break;
case 'I':
IMCKEY( "IMCtag", color->imctag, imcfread_line( fp ) );
break;
case 'M':
IMCKEY( "Mudtag", color->mudtag, imcfread_line( fp ) );
break;
case 'N':
IMCKEY( "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 = imcfread_letter( fp );
if ( letter == '*' )
{
imcfread_to_eol( fp );
continue;
}
if ( letter != '#' )
{
imcbug( "%s", "imc_load_color_table: # not found." );
break;
}
word = imcfread_word( fp );
if ( !strcasecmp( word, "COLOR" ) )
{
IMCCREATE( color, IMC_COLOR, 1 );
imc_readcolor( color, fp );
IMCLINK( 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 );
return;
}
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 );
return;
}
void imc_readhelp( IMC_HELP_DATA *help, FILE *fp )
{
const char *word;
char hbuf[LGST];
int permvalue;
bool fMatch;
for ( ; ; )
{
word = feof( fp ) ? "End" : imcfread_word( fp );
fMatch = FALSE;
switch( word[0] )
{
case '*':
fMatch = TRUE;
imcfread_to_eol( fp );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
return;
break;
case 'N':
IMCKEY( "Name", help->name, imcfread_line( fp ) );
break;
case 'P':
if( !strcasecmp( word, "Perm" ) )
{
word = imcfread_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 = IMCSTRALLOC( 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 = imcfread_letter( fp );
if ( letter == '*' )
{
imcfread_to_eol( fp );
continue;
}
if ( letter != '#' )
{
imcbug( "%s", "imc_load_helps: # not found." );
break;
}
word = imcfread_word( fp );
if( !strcasecmp( word, "HELP" ) )
{
IMCCREATE( help, IMC_HELP_DATA, 1 );
imc_readhelp( help, fp );
IMCLINK( 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 );
return;
}
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 != NULL )
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 );
return;
}
void imc_readcommand( IMC_CMD_DATA *cmd, FILE *fp )
{
IMC_ALIAS *alias;
const char *word;
int permvalue;
bool fMatch;
for ( ; ; )
{
word = feof( fp ) ? "End" : imcfread_word( fp );
fMatch = FALSE;
switch( word[0] )
{
case '*':
fMatch = TRUE;
imcfread_to_eol( fp );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
return;
break;
case 'A':
if( !strcasecmp( word, "Alias" ) )
{
IMCCREATE( alias, IMC_ALIAS, 1 );
alias->name = imcfread_line( fp );
IMCLINK( alias, cmd->first_alias, cmd->last_alias, next, prev );
fMatch = TRUE;
break;
}
break;
case 'C':
IMCKEY( "Connected", cmd->connected, imcfread_number( fp ) );
if( !strcasecmp( word, "Code" ) )
{
word = imcfread_word( fp );
cmd->function = imc_function( word );
if( cmd->function == NULL )
imcbug( "imc_readcommand: Command %s loaded with invalid function. Set to NULL.", cmd->name );
fMatch = TRUE;
break;
}
break;
case 'N':
IMCKEY( "Name", cmd->name, imcfread_line( fp ) );
break;
case 'P':
if( !strcasecmp( word, "Perm" ) )
{
word = imcfread_word( fp );
permvalue = get_imcpermvalue( word );
if( permvalue < 0 || permvalue > IMCPERM_IMP )
{
imcbug( "imc_readcommand: Command %s loaded with invalid permission. Set to Imp.", cmd->name );
cmd->level = IMCPERM_IMP;
}
else
cmd->level = permvalue;
fMatch = TRUE;
break;
}
break;
}
if( !fMatch )
imcbug( "imc_readcommand: no match: %s", word );
}
}
bool imc_load_commands( void )
{
FILE *fp;
IMC_CMD_DATA *cmd;
first_imc_command = last_imc_command = NULL;
imclog( "%s", "Loading IMC2 command table..." );
if( !( fp = fopen( IMC_CMD_FILE, "r" ) ) )
{
imclog( "%s", "No command table found." );
return FALSE;
}
for ( ; ; )
{
char letter;
char *word;
letter = imcfread_letter( fp );
if ( letter == '*' )
{
imcfread_to_eol( fp );
continue;
}
if ( letter != '#' )
{
imcbug( "%s", "imc_load_commands: # not found." );
break;
}
word = imcfread_word( fp );
if ( !strcasecmp( word, "COMMAND" ) )
{
IMCCREATE( cmd, IMC_CMD_DATA, 1 );
imc_readcommand( cmd, fp );
IMCLINK( cmd, first_imc_command, last_imc_command, next, prev );
continue;
}
else if( !strcasecmp( word, "END" ) )
break;
else
{
imcbug( "imc_load_commands: bad section: %s.", word );
continue;
}
}
IMCFCLOSE( fp );
return TRUE;
}
void imc_save_config( void )
{
FILE *fp;
if( !( fp = fopen( IMC_CONFIG_FILE, "w" ) ) )
{
imclog( "%s", "Couldn't write to config file." );
return;
}
fprintf( fp, "%s", "$IMCCONFIG\n\n" );
fprintf( fp, "# %s config file.\n", this_imcmud->versionid );
fprintf( fp, "%s", "# This file can now support the use of tildes in your strings.\n" );
fprintf( fp, "%s", "# This information can be edited online using the 'imcconfig' command.\n" );
fprintf( fp, "LocalName %s\n", this_imcmud->localname );
fprintf( fp, "Autoconnect %d\n", this_imcmud->autoconnect );
fprintf( fp, "MinPlayerLevel %d\n", this_imcmud->minlevel );
fprintf( fp, "MinImmLevel %d\n", this_imcmud->immlevel );
fprintf( fp, "AdminLevel %d\n", this_imcmud->adminlevel );
fprintf( fp, "Implevel %d\n", this_imcmud->implevel );
fprintf( fp, "InfoName %s\n", this_imcmud->fullname );
fprintf( fp, "InfoHost %s\n", this_imcmud->ihost );
fprintf( fp, "InfoPort %d\n", this_imcmud->iport );
fprintf( fp, "InfoEmail %s\n", this_imcmud->email );
fprintf( fp, "InfoWWW %s\n", this_imcmud->www );
fprintf( fp, "InfoBase %s\n", this_imcmud->base );
fprintf( fp, "InfoDetails %s\n\n", this_imcmud->details );
fprintf( fp, "%s", "# Your server connection information goes here.\n" );
fprintf( fp, "%s", "# This information should be available from the network you plan to join.\n" );
fprintf( fp, "ServerAddr %s\n", this_imcmud->rhost );
fprintf( fp, "ServerPort %d\n", this_imcmud->rport );
fprintf( fp, "ClientPwd %s\n", this_imcmud->clientpw );
fprintf( fp, "ServerPwd %s\n", this_imcmud->serverpw );
fprintf( fp, "#SHA256 auth: 0 = disabled, 1 = enabled\n" );
fprintf( fp, "SHA256 %d\n", this_imcmud->sha256 );
if( this_imcmud->sha256pass )
{
fprintf( fp, "%s", "#Your server is expecting SHA256 authentication now. Do not remove this line unless told to do so.\n" );
fprintf( fp, "SHA256Pwd %d\n", this_imcmud->sha256pass );
}
fprintf( fp, "%s", "End\n\n" );
fprintf( fp, "%s", "$END\n" );
IMCFCLOSE( fp );
return;
}
void imcfread_config_file( FILE *fin )
{
const char *word;
bool fMatch;
for( ;; )
{
word = feof( fin ) ? "end" : imcfread_word( fin );
fMatch = FALSE;
switch( word[0] )
{
case '#':
fMatch = TRUE;
imcfread_to_eol( fin );
break;
case 'A':
IMCKEY( "Autoconnect", this_imcmud->autoconnect, imcfread_number( fin ) );
IMCKEY( "AdminLevel", this_imcmud->adminlevel, imcfread_number( fin ) );
break;
case 'C':
IMCKEY( "ClientPwd", this_imcmud->clientpw, imcfread_line( fin ) );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
{
return;
}
break;
case 'I':
IMCKEY( "Implevel", this_imcmud->implevel, imcfread_number( fin ) );
IMCKEY( "InfoName", this_imcmud->fullname, imcfread_line( fin ) );
IMCKEY( "InfoShowing the first 100 KB of 186.2 KB. Download the archive for the full file.