imc.c
muddy/src/imc.c · 202.9 KB
From the archive astrum.tar.bz2
Download the archive « Back to the archive contents
/* $Id: imc.c,v 1.666 2004/09/20 10:49:49 shrike Exp $ */
/************************************************************************************
* Copyright 2004 Astrum Metaphora consortium *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
************************************************************************************/
/************************************************************************************
* IMC2 version 0.10 - an inter-mud communications protocol *
* Copyright (C) 1996 & 1997 Oliver Jowett <oliver@randomly.org> *
* *
* IMC2 Gold versions 1.00 though 2.00 are developed by MudWorld. *
* Copyright (C) 1999 - 2002 Haslage Net Electronics (Anthony R. Haslage) *
* *
* IMC2 MUD-Net version 3.10 is developed by Alsherok and Crimson Oracles *
* Copyright (C) 2002 Roger Libiez ( Samson ) *
* Additional code Copyright (C) 2002 Orion Elder *
* Registered with the United States Copyright Office *
* TX 5-555-584 *
* *
* IMC2 Continuum version 4.00 is developed by Rogel *
* Copyright (C) 2003 by Rogel, WhoStyles Copyright (C) 2003 by Kris Craig *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program (see the file COPYING); if not, write to the *
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* *
************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.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>
#if defined(IMCCIRCLE)
#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "comm.h"
#include "db.h"
#include "handler.h"
#include "interpreter.h"
#include "imc.h"
char log_buf[LSS];
#endif
#if defined(IMCSMAUG)
#include "mud.h"
#endif
#if defined(IMCROM) || defined(IMCMERC)
#include "merc.h"
#ifdef IMCROM
#include "tables.h"
#endif
#endif
#ifdef IMCACK
#include "ack.h"
#endif
#if defined(KEY)
#undef KEY
#endif
#define KEY( literal, field, value ) \
if ( !strcasecmp( word, literal ) ) \
{ \
field = value; \
fMatch = TRUE; \
break; \
}
static const char *wr_to;
static char wr_buf[IMC_DATA_LENGTH];
static int wr_sequence;
imc_statistics imc_stats;
time_t imc_now; /* current time */
time_t imc_boot; /* current time */
int imc_debug_on;
int imc_minplayerlevel; /* Minumum level IMC will see players at, defaults to 10 */
int imc_minimmlevel; /* Minimum level IMC sees someone as an immortal, defaults to IMCMAX_LEVEL */
int imc_minadminlevel; /* Minimum level IMC sees someone as an administrator, defaults to IMCMAX_LEVEL */
imc_siteinfo_struct imc_siteinfo;
imc_event *imc_event_list, *imc_event_free;
static int event_freecount;
static int memory_head; /* next entry in memory table to use, wrapping */
int imcwait; /* Reconnect timer */
unsigned long imc_sequencenumber; /* sequence# for outgoing packets */
const char *imc_name; /* our imc name */
static char pinger[100];
const char *imc_versionid;
HUBINFO *this_imcmud;
IMC_CHANNEL *first_imc_channel;
IMC_CHANNEL *last_imc_channel;
REMOTEINFO *first_rinfo;
REMOTEINFO *last_rinfo;
IMC_BLACKLIST *first_imc_mudblacklist;
IMC_BLACKLIST *last_imc_mudblacklist;
/* sequence memory */
_imc_memory imc_memory[IMC_MEMORY];
int (*imc_recv_chain)( PACKET *p, int bcast );
int (*imc_recv_hook)( PACKET *p, int bcast );
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 ) ) );
bool imc_str_prefix( const char *astr, const char *bstr )
{
if ( !astr || astr[0] == '\0' )
{
log( "imc_str_prefix: null astr." );
return TRUE;
}
if ( !bstr || bstr[0] == '\0' )
{
log( "imc_str_prefix: null bstr." );
return TRUE;
}
for ( ; *astr; astr++, bstr++ )
{
if ( tolower(*astr) != tolower(*bstr) )
return TRUE;
}
return FALSE;
}
unsigned char imc_whostyle; /* Sets the color pattern for IMC2 whos */
#define MAX_WHOSTYLE 4
const char * whostylename_table[MAX_WHOSTYLE] =
{ "Continuum Default", "Winter", "Fiery Bowels of Hell", "IMC2 Classic" };
/* Checks if a player's level is greater or equal to a permission level */
bool level_pass( CHAR_DATA *ch, unsigned char perm_level)
{
int level;
if(!ch)
return 1;
if( perm_level == PERM_PLAYER)
level = imc_minplayerlevel;
else if ( perm_level == PERM_IMMORTAL)
level = imc_minimmlevel;
else if ( perm_level == PERM_ADMIN)
level = imc_minadminlevel;
else
level = 0;
if ( level <= CH_IMCLEVEL(ch))
return 1;
else
return 0;
}
/* Finds the permission level for a player using level_pass and returns the permission level */
unsigned char get_permlevel( CHAR_DATA *ch)
{
if ( level_pass(ch, PERM_ADMIN))
return PERM_ADMIN;
if ( level_pass(ch, PERM_IMMORTAL))
return PERM_IMMORTAL;
if ( level_pass(ch, PERM_PLAYER))
return PERM_PLAYER;
return PERM_NONE;
}
/* Checks if a network function is able to be used by a player by checking deny and allow flags,
followed by a permission level check - preceeding all these checks is a non-npc check */
bool function_usable( CHAR_DATA *ch, long deny_flag, long allow_flag, unsigned char perm_level )
{
if (IS_NPC(ch))
return 0;
if ( IS_SET( IMC_PFLAGS(ch), deny_flag))
return 0;
if ( !level_pass( ch, perm_level) && !IS_SET( IMC_PFLAGS(ch), allow_flag ))
return 0;
return 1;
}
/*
* Error logging
*/
/* Generic log function which will route the log messages to the appropriate system logging function */
void imclog( const char *format, ... )
{
char buf[LSS];
va_list ap;
va_start( ap, format );
vsnprintf( buf, LSS, format, ap );
va_end( ap );
log( buf );
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[LSS];
va_list ap;
va_start( ap, format );
vsnprintf( buf, LSS, format, ap );
va_end( ap );
bug( buf, 0 );
return;
}
/* Generic substitute for write_to_buffer since not all codebases seem to have it */
void imcto_buffer( DESCRIPTOR_DATA *d, const char *txt, int length )
{
write_to_buffer( d, txt, 0 );
return;
}
/*
* Table is formatted with mud color code first, then ANSI code, then IMC color code.
* This should make it easier to modify local color codes.
*
* It is currently setup to support the AFKMUD mud color codes.
* You need to change the &, } and { tokens used in the table below to match what
* your mud uses to identify a color token with.
*
* & is the foreground text token.
* } is the blink text token.
* { is the background text token.
*
*/
imccolor_struct imccolor_table[] =
{
/* Color code symbols used in IMC must be carefully screened */
// First three characters in second column are used for mud color parsing tests.
// ~ is the color token used in the IMC mud standard.
// All {, }, and & must be replaced with your mud's color tokens to convert the table
// to your mud effectively. 'Doubling up' in this table is to escape color codes. If your
// mud does not do this, then make these single characters. Most muds do escape.
{ "&&", "&", "&" },
{ "{{", "{", "{" },
{ "}}", "}", "}" },
{ "~", "~","~~" },
{ "&-", "~", "~~" },
/* Foreground Standard Colors */
{ "{d", "\e[0;0;30m", "~x" }, // Black
{ "{r", "\e[0;0;31m", "~r" }, // Dark Red
{ "{g", "\e[0;0;32m", "~g" }, // Dark Green
{ "{y", "\e[0;0;33m", "~y" }, // Orange/Brown
{ "{b", "\e[0;0;34m", "~b" }, // Dark Blue
{ "{m", "\e[0;0;35m", "~p" }, // Purple/Magenta
{ "{m", "\e[0;0;35m", "~m" }, // Duplicated to support duplicate IMC code for color
{ "{c", "\e[0;0;36m", "~c" }, // Cyan
{ "{w", "\e[0;0;37m", "~w" }, // Grey
{ "{w", "\e[0;0;37m", "~d" }, // Duplicated to support triplicate IMC code for color
{ "{x", "\e[0;0;37m", "~!" }, // Triplicated to support IMC reset code for color (defaults to grey)
/* Foreground extended colors */
{ "{D", "\e[0;1;30m", "~D" }, // Dark Grey
{ "{R", "\e[0;1;31m", "~R" }, // Red
{ "{G", "\e[0;1;32m", "~G" }, // Green
{ "{Y", "\e[0;1;33m", "~Y" }, // Yellow
{ "{B", "\e[0;1;34m", "~B" }, // Blue
{ "{M", "\e[0;1;35m", "~M" }, // Pink
{ "{C", "\e[0;1;36m", "~C" }, // Light Blue
{ "{W", "\e[0;1;37m", "~W" }, // White
/* There are no IMC2 color codes for anything beyond this point - yet */
{ "{*", "", "" },
{ "{z", "", "" },
{NULL, NULL, NULL}
};
// Translates mud color codes into pure ANSI codes
char *tagtoansi( const char *txt, CHAR_DATA *ch )
{
int c, x, count = 0;
static char tbuf[LSS];
char code[3];
if( !txt || *txt == '\0' )
return "";
tbuf[0] = '\0';
for( count = 0; count < LSS; count++ )
{
if( *txt == '\0' )
break;
for (c=0; c < 3; c++ )
{
if (*txt == imccolor_table[c].mud[0])
break;
}
if ( c == 3)
{
tbuf[count] = *txt;
txt++;
continue;
}
code[0] = *txt;
txt++;
if( *txt == code[0] )
{
tbuf[count] = *txt;
txt++;
continue;
}
code[1] = *txt;
code[2] = '\0';
if ( !IS_SET(IMC_PFLAGS(ch), PSET_IMCPCOLOR) )
{
for( c = 0; imccolor_table[c].mud != NULL; c++ )
{
if( imccolor_table[c].mud[0] == code[0] && imccolor_table[c].mud[1] == code[1] )
{
for( x = 0; imccolor_table[c].ansi[x] != '\0'; x++ )
{
tbuf[count] = imccolor_table[c].ansi[x];
count++;
}
break;
}
}
}
count--;
txt++;
}
tbuf[count] = '\0';
return tbuf;
}
/* convert from imc color -> mud color */
char *color_itom( const char *s )
{
static char buf[IMC_DATA_LENGTH];
const char *current;
char *out;
int c, i, l=0;
int count;
for( current = s, out = buf, count = 0; *current && count < IMC_DATA_LENGTH; )
{
for (c=0; c < 4; c++ )
{
if (*current == imccolor_table[c].ansi[0])
break;
}
if( c != 4 )
{
for( i = 0; imccolor_table[i].mud != NULL; i++ )
{
l = strlen( imccolor_table[i].imc );
if( l && !strncmp( current, imccolor_table[i].imc, l ) )
break;
}
if( imccolor_table[i].mud != NULL ) /* match */
{
int len;
len = strlen( imccolor_table[i].mud );
count += len;
if( count >= IMC_DATA_LENGTH )
break;
strlcpy( out, imccolor_table[i].mud, IMC_DATA_LENGTH );
out += len;
current += l;
continue;
}
}
*out++ = *current++;
count++;
}
*out = 0;
return buf;
}
/* convert from mud color -> imc color */
char *color_mtoi( const char *s )
{
static char buf[IMC_DATA_LENGTH];
const char *current;
char *out;
int c, i, l = 0;
int count;
for( current = s, out = buf, count=0; *current && count < IMC_DATA_LENGTH; )
{
for (c=0; c < 4; c++ )
{
if (*current == imccolor_table[c].ansi[0])
break;
}
if (c != 4)
{
for( i = 0; imccolor_table[i].mud != NULL; i++ )
{
l = strlen( imccolor_table[i].mud );
if( l && !strncmp( current, imccolor_table[i].mud, l ) )
break;
}
if( imccolor_table[i].mud != NULL ) /* match */
{
int len;
len = strlen( imccolor_table[i].imc );
count += len;
if( count >= IMC_DATA_LENGTH )
break;
strlcpy( out, imccolor_table[i].imc, IMC_DATA_LENGTH );
out += len;
current += l;
continue;
}
}
*out++ = *current++;
count++;
}
*out = 0;
return buf;
}
// Retrieves a string length minus the color codes.
int imc_strlen_color( const char *str )
{
int c, i, length;
if( !str || str[0] == '\0' )
return 0;
for( length = i = 0 ; i < strlen ( str ) ; i++ )
{
for (c=0; c < 4; c++ )
{
if (str[i] == imccolor_table[c].ansi[0])
break;
}
if ( c == 4)
++length;
else
{
for (c=0; c < 4; c++)
{
if ( str[i] == imccolor_table[c].mud[0]
&& str[i+1] == imccolor_table[c].mud[1])
break;
}
if (c !=4)
length++;
else
--length;
}
}
return length;
}
// Creates a series of spaces that are equal in length to a chosen string
char *EmptyBlock( const char *argument, int leftsidespacing, int rightsidespacing )
{
static char buf[LSS];
int length = imc_strlen_color(argument) + leftsidespacing + rightsidespacing;
int i = 0;
if (!length)
return NULL;
for ( i = 0; i < length && i < LSS - 1; i++)
buf[i] = ' ';
buf[i] = '\0';
return buf;
}
// Inserts an alternating pattern of two symbols(possibly colors) into any text
char *Alternating_Symbols( const char *argument, const char *symbol1, const char *symbol2 )
{
static char buf[LSS];
bool start = TRUE;
int h = 0, i = 0, j = 0, k = 0;
if (!argument || argument[0] == '\0' || !symbol1 || symbol1[0] == '\0' || !symbol2 || symbol2[0] == '\0')
return NULL;
for ( i = 0; i < strlen(argument); i++)
{
if ( argument[i] == ' ' || argument[i] == '\n' || argument[i] == '\r')
{
if ( j + 1 >= LSS - 1)
break;
start = TRUE;
buf[j++] = argument[i];
continue;
}
if (start && j + strlen(symbol1) + 1 >= LSS - 1)
break;
else if ( j + strlen(symbol2) + 1 >= LSS - 1)
break;
if (start)
{
for (k = 0; k < strlen(symbol1); k++)
buf[j++] = symbol1[k];
}
else
{
for (h = 0; h < strlen(symbol2); h++)
buf[j++] = symbol2[h];
}
buf[j++] = argument[i];
start = !start;
}
buf[j] = '\0';
return buf;
}
// Centers any text, even text that includes color.
char *SetFill( const char *argument, int size, const char *filler)
{
int space, i = 0, j = 0, length = 0;
static char buf[LSS];
char startstring[LSS];
char endstring[LSS];
if (imc_strlen_color(filler) <= 0 )
return NULL;
space = (size - imc_strlen_color( argument )) / imc_strlen_color(filler);
length = space/2;
for ( ; length > 0; length--)
{
if ( j + strlen(filler) >= LSS - 1 )
break;
for ( i = 0; filler[i]; i++ )
startstring[j++] = filler[i];
}
startstring[j] = '\0';
if ( ((space/2) * 2) == space)
length = space/2;
else
length = (space/2) + 1;
for ( j = i = 0; length > 0; length--)
{
if ( j + strlen(filler) >= LSS - 1 )
break;
for ( i = 0; filler[i]; i++ )
endstring[j++] = filler[i];
}
endstring[j] = '\0';
snprintf( buf, LSS, "%s%s%s", startstring, argument, endstring );
return buf;
}
/* Modified version of Smaug's send_to_char_color function */
void imc_to_char( const char *txt, CHAR_DATA *ch )
{
char buf[LSS*3];
if( !ch )
{
imcbug( "%s", "imc_to_char: NULL ch!" );
return;
}
if( IS_NPC( ch ) )
return;
if( !ch->desc )
{
imcbug( "imc_to_char: NULL descriptor for %s!", CH_IMCNAME(ch) );
return;
}
if ( IS_SET(IMC_PFLAGS(ch), PSET_IMCPCOLOR) )
snprintf( buf, (LSS * 3), "%s", tagtoansi( txt, ch ));
else /* Default color changed to bright white. Remove the ansi color code in front if it bothers. */
snprintf( buf, (LSS * 3), "\e[0;1;37m%s", tagtoansi( txt, ch ));
imcto_buffer( ch->desc, buf, 0 );
if ( !IS_SET(IMC_PFLAGS(ch), PSET_IMCPCOLOR) )
imcto_buffer( ch->desc, "\e[0m", 0 ); /* Reset color to stop bleeding */
return;
}
/* Modified version of Smaug's ch_printf_color function */
void imc_printf( CHAR_DATA *ch, const char *fmt, ... )
{
char buf[LSS*2];
va_list args;
va_start( args, fmt );
vsnprintf( buf, LSS*2, 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 */
// May be changed later to make it more codebase independent
void imc_to_pager( const char *txt, CHAR_DATA *ch )
{
char buf[LSS*3];
/* Change default string color from grey to bright white */
/* Remove the ansi color code in front of the %s if it gets to annoying */
snprintf( buf, (LSS * 3), "\e[0;1;37m%s", tagtoansi( txt, ch ));
#if defined(IMCSMAUG)
send_to_pager_color( buf, ch );
#elif defined(IMCROM)
page_to_char( buf, ch );
#else
imc_to_char( buf, ch );
#endif
return;
}
/* Generic pager_printf type function */
void imcpager_printf( CHAR_DATA *ch, const char *fmt, ... )
{
char buf[LSS*2];
va_list args;
va_start( args, fmt );
vsnprintf( buf, LSS*2, fmt, args );
va_end( args );
imc_to_pager( buf, ch );
return;
}
/* free all the keys in "p" */
void imc_freedata( PACKET *p )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
if( p->key[i] )
IMCSTRFREE( p->key[i] );
if( p->value[i] )
IMCSTRFREE( p->value[i] );
}
return;
}
/*
* String manipulation functions, mostly exported
*/
/* lowercase what */
void imc_slower( char *what )
{
char *p = what;
while( *p )
{
*p = tolower( *p );
p++;
}
}
imc_event *imc_new_event( void )
{
imc_event *p;
if( imc_event_free )
{
p = imc_event_free;
imc_event_free = p->next;
event_freecount--;
}
else
IMCCREATE( p, imc_event, 1 );
p->when = 0;
p->callback = NULL;
p->data = NULL;
p->next = NULL;
p->timed = 0;
return p;
}
void imc_free_event( imc_event *p )
{
if( event_freecount > 10 ) /* pick a number, any number */
IMCDISPOSE( p );
else
{
p->next = imc_event_free;
imc_event_free = p;
event_freecount++;
}
}
void imc_run_events( time_t newtime )
{
imc_event *p;
void (*callback)(void *);
void *data;
while( imc_event_list )
{
p = imc_event_list;
if( p->when > newtime )
break;
imc_event_list = p->next;
callback = p->callback;
data = p->data;
imc_now = p->when;
imc_free_event( p );
if( callback )
(*callback)(data);
else
imcbug( "%s", "imc_run_events: NULL callback" );
}
imc_now = newtime;
}
void imc_add_event( int when, void (*callback)(void *), void *data, int timed )
{
imc_event *p, *scan, **last;
p = imc_new_event();
p->when = imc_now+when;
p->callback = callback;
p->data = data;
p->timed = timed;
for( last = &imc_event_list, scan = *last; scan; last = &scan->next, scan = *last )
if( scan->when >= p->when )
break;
p->next = scan;
*last = p;
}
void imc_cancel_event( void (*callback)(void *), void *data )
{
imc_event *p, *p_next, **last;
for( last = &imc_event_list, p = *last; p; p = p_next )
{
p_next = p->next;
if( (!callback) && p->data == data )
{
*last = p_next;
imc_free_event( p );
}
else if( (callback) && p->data == data )
{
*last = p_next;
imc_free_event( p );
}
else if( p->callback == callback && data == NULL )
{
*last = p_next;
imc_free_event( p );
}
else
last = &p->next;
}
}
/* get the value of "key" from "p"; if it isn't present, return "def" */
const char *imc_getkey( PACKET *p, const char *key, const char *def )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
if( p->key[i] && !strcasecmp( p->key[i], key ) )
return p->value[i];
return def;
}
/* identical to imc_getkey, except get the integer value of the key */
int imc_getkeyi( PACKET *p, const char *key, int def )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
if( p->key[i] && !strcasecmp( p->key[i], key ) )
return atoi( p->value[i] );
return def;
}
/* add "key=value" to "p" */
void imc_addkey( PACKET *p, const char *key, const char *value )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
if( p->key[i] && !strcasecmp( key, p->key[i] ) )
{
IMCSTRFREE( p->key[i] );
IMCSTRFREE( p->value[i] );
break;
}
}
if( !value )
return;
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
if( !p->key[i] )
{
p->key[i] = IMCSTRALLOC( key );
p->value[i] = IMCSTRALLOC( value );
return;
}
}
}
/* add "key=value" for an integer value */
void imc_addkeyi( PACKET *p, char *key, int value )
{
char temp[20];
snprintf( temp, 20, "%d", value );
imc_addkey( p, key, temp );
}
/* clone packet data */
void imc_clonedata( PACKET *p, PACKET *n )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
if( p->key[i] )
n->key[i] = IMCSTRALLOC( p->key[i] );
else
n->key[i] = NULL;
if( p->value[i] )
n->value[i] = IMCSTRALLOC( p->value[i] );
else
n->value[i] = NULL;
}
}
/* clear all keys in "p" */
void imc_initdata( PACKET *p )
{
int i;
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
p->key[i] = NULL;
p->value[i] = NULL;
}
}
/* checkrepeat: check for repeats in the memory table */
int checkrepeat( char *mud, unsigned long seq )
{
int i;
for( i = 0; i < IMC_MEMORY; i++ )
if( imc_memory[i].from && !strcasecmp( mud, imc_memory[i].from ) && seq == imc_memory[i].sequence )
return 1;
/* not a repeat, so log it */
if( imc_memory[memory_head].from )
IMCSTRFREE( imc_memory[memory_head].from );
imc_memory[memory_head].from = IMCSTRALLOC( mud );
imc_memory[memory_head].sequence = seq;
memory_head++;
if( memory_head == IMC_MEMORY )
memory_head = 0;
return 0;
}
/* return 'mud' from 'player@mud' */
char *imc_mudof( const char *fullname )
{
static char buf[IMC_MNAME_LENGTH];
char *where;
where = strchr( fullname, '@' );
if( !where )
strlcpy( buf, fullname, IMC_MNAME_LENGTH );
else
strlcpy( buf, where+1, IMC_MNAME_LENGTH );
return buf;
}
/* return 'player' from 'player@mud' */
char *imc_nameof( const char *fullname )
{
static char buf[IMC_PNAME_LENGTH];
char *where = buf;
int count = 0;
while( *fullname && *fullname != '@' && count < IMC_PNAME_LENGTH-1 )
*where++ = *fullname++, count++;
*where = 0;
return buf;
}
/* return 'player@mud' from 'player' and 'mud' */
char *imc_makename( const char *player, const char *mud )
{
static char buf[IMC_NAME_LENGTH];
buf[0] = '\0';
if ( !player || player[0] == '\0' )
return buf;
strlcpy( buf, player, IMC_PNAME_LENGTH );
if ( mud && mud[0] != '\0' )
{
strlcpy( buf + strlen(buf), "@", 2 );
strlcpy( buf + strlen(buf), mud, IMC_MNAME_LENGTH );
}
return buf;
}
/* imc_getarg: extract a single argument (with given max length) from
* argument to arg; if arg==NULL, just skip an arg, don't copy it out
*/
const char *imc_getarg( const char *argument, char *arg, int length )
{
static char buf[LSS];
int len = 0;
if (arg && (!argument || argument[0] == '\0'))
arg[0] = '\0';
while( *argument && isspace( *argument ) )
argument++;
if( arg )
while( *argument && !isspace( *argument ) && len < length-1 )
*arg++ = *argument++, len++;
else
while( *argument && !isspace( *argument ) )
argument++;
while( *argument && !isspace( *argument ) )
argument++;
while( *argument && isspace( *argument ) )
argument++;
if( arg )
*arg = '\0';
strlcpy( buf, argument, LSS);
return buf;
}
/* Check for a name in a list */
int imc_hasname( const char *list, const char *name )
{
const char *p;
char arg[IMC_NAME_LENGTH];
if( !list )
return( 0 );
p = imc_getarg( list, arg, IMC_NAME_LENGTH );
while( arg[0] )
{
if( !strcasecmp( name, arg ) )
return 1;
p = imc_getarg( p, arg, IMC_NAME_LENGTH );
}
return 0;
}
/* Add a name to a list */
void imc_addname( const char **list, const char *name )
{
char buf[IMC_DATA_LENGTH];
if( imc_hasname( *list, name ) )
return;
if( (*list) && (*list)[0] != '\0' )
snprintf( buf, IMC_DATA_LENGTH, "%s %s", *list, name );
else
strlcpy( buf, name, IMC_DATA_LENGTH );
if( *list )
IMCSTRFREE( *list );
*list = IMCSTRALLOC( buf );
}
/* Remove a name from a list */
void imc_removename( const char **list, const char *name )
{
char buf[1000];
char arg[IMC_NAME_LENGTH];
const char *p;
buf[0] = '\0';
p = imc_getarg( *list, arg, IMC_NAME_LENGTH );
while( arg[0] )
{
if( strcasecmp( arg, name ) )
{
if( buf[0] )
strlcat( buf, " ", 1000 );
strlcat( buf, arg, 1000 );
}
p = imc_getarg( p, arg, IMC_NAME_LENGTH );
}
if( *list )
IMCSTRFREE( *list );
*list = IMCSTRALLOC( buf );
}
/* escape2: escape " -> \", \ -> \\, CR -> \r, LF -> \n */
const char *escape2( const char *data )
{
static char buf[IMC_DATA_LENGTH];
char *p;
for( p = buf; *data && ( p-buf < IMC_DATA_LENGTH-1 ); data++, p++ )
{
if( *data == '\n' )
{
*p++ = '\\';
*p = 'n';
}
else if( *data == '\r' )
{
*p++ = '\\';
*p = 'r';
}
else if( *data == '\\' )
{
*p++ = '\\';
*p = '\\';
}
else if( *data == '"' )
{
*p++ = '\\';
*p = '"';
}
else
*p = *data;
}
*p = 0;
return buf;
}
/* printkeys: print key-value pairs, escaping values */
char *printkeys( PACKET *data )
{
static char buf[IMC_DATA_LENGTH];
char temp[IMC_DATA_LENGTH];
int len = 0;
int i;
buf[0] = '\0';
for( i = 0; i < IMC_MAX_KEYS; i++ )
{
if( !data->key[i] )
continue;
strlcpy( buf + len, data->key[i], IMC_DATA_LENGTH-len-1 );
strlcat( buf, "=", IMC_DATA_LENGTH );
len = strlen( buf );
if( !strchr( data->value[i], ' ' ) )
strlcpy( temp, escape2( data->value[i] ), IMC_DATA_LENGTH-1 );
else
{
temp[0] = '"';
strlcpy( temp+1, escape2( data->value[i] ), IMC_DATA_LENGTH-3 );
strlcat( temp, "\"", IMC_DATA_LENGTH - 2 );
}
strlcat( temp, " ", IMC_DATA_LENGTH );
strlcpy( buf + len, temp, IMC_DATA_LENGTH-len );
len = strlen( buf );
}
return buf;
}
/* parsekeys: extract keys from string */
void parsekeys( const char *string, PACKET *data )
{
const char *p1;
char *p2;
char k[IMC_DATA_LENGTH], v[IMC_DATA_LENGTH];
int quote;
p1 = string;
while( *p1 )
{
while( *p1 && isspace( *p1 ) )
p1++;
p2 = k;
while( *p1 && *p1 != '=' && p2-k < IMC_DATA_LENGTH-1 )
*p2++ = *p1++;
*p2=0;
if( !k[0] || !*p1 ) /* no more keys? */
break;
p1++; /* skip the '=' */
if( *p1 == '"' )
{
p1++;
quote = 1;
}
else
quote = 0;
p2 = v;
while( *p1 && ( !quote || *p1 != '"' ) && ( quote || *p1 != ' ' ) && p2-v < IMC_DATA_LENGTH+1 )
{
if( *p1 == '\\' )
{
switch( *(++p1) )
{
case '\\':
*p2++ = '\\';
break;
case 'n':
*p2++ = '\n';
break;
case 'r':
*p2++ = '\r';
break;
case '"':
*p2++ = '"';
break;
default:
*p2++ = *p1;
break;
}
if( *p1 )
p1++;
}
else
*p2++ = *p1++;
}
*p2 = 0;
if( !v[0] )
continue;
imc_addkey( data, k, v );
if( quote && *p1 )
p1++;
}
}
const char *generate2( PACKET *p )
{
static char temp[IMC_PACKET_LENGTH];
char newpath[IMC_PATH_LENGTH];
if( !p->type[0] || !p->i.from[0] || !p->i.to[0] )
{
imcbug( "%s", "generate2: bad packet!" );
imcbug( "type: %s from: %s to: %s", p->type, p->i.from, p->i.to );
imcbug( "path: %s data: %s", p->i.path, printkeys( p ) );
return NULL; /* catch bad packets here */
}
if( !p->i.path[0] )
strlcpy( newpath, imc_name, IMC_PATH_LENGTH );
else
snprintf( newpath, IMC_PATH_LENGTH, "%s!%s", p->i.path, imc_name );
snprintf( temp, IMC_PACKET_LENGTH, "%s %lu %s %s %s %s",
p->i.from, p->i.sequence, newpath, p->type, p->i.to, printkeys( p ) );
return temp;
}
PACKET *interpret2( const char *argument )
{
char seq[20];
static PACKET out;
imc_initdata( &out );
argument = imc_getarg( argument, out.i.from, IMC_NAME_LENGTH );
argument = imc_getarg( argument, seq, 20 );
argument = imc_getarg( argument, out.i.path, IMC_PATH_LENGTH );
argument = imc_getarg( argument, out.type, IMC_TYPE_LENGTH );
argument = imc_getarg( argument, out.i.to, IMC_NAME_LENGTH );
if( !out.i.from[0] || !seq[0] || !out.i.path[0] || !out.type[0] || !out.i.to[0] )
{
imcbug( "%s", "interpret2: bad packet received, discarding" );
return NULL;
}
parsekeys( argument, &out );
out.i.sequence = strtoul( seq, NULL, 10 );
return &out;
}
_imc_vinfo imc_vinfo[] =
{
{ 0, NULL, NULL },
{ 1, NULL, NULL },
{ 2, generate2, interpret2 }
};
/* return 1 if 'name' is a part of 'path' (internal) */
int inpath( char *path, char *name )
{
char buf[IMC_MNAME_LENGTH+3];
char tempn[IMC_MNAME_LENGTH], tempp[IMC_PATH_LENGTH];
strlcpy( tempn, name, IMC_MNAME_LENGTH );
strlcpy( tempp, path, IMC_PATH_LENGTH );
imc_slower( tempn );
imc_slower( tempp );
if( !strcasecmp( tempp, tempn ) )
return 1;
snprintf( buf, IMC_MNAME_LENGTH+3, "%s!", tempn );
if( !strncmp( tempp, buf, strlen( buf ) ) )
return 1;
snprintf( buf, IMC_MNAME_LENGTH+3, "!%s", tempn );
if( strlen(buf) < strlen(tempp) && !strcasecmp( tempp + strlen(tempp) - strlen(buf), buf ) )
return 1;
snprintf( buf, IMC_MNAME_LENGTH+3, "!%s!", tempn );
if( strstr( tempp, buf ) )
return 1;
return 0;
}
/* find an info entry for "name" */
REMOTEINFO *imc_find_reminfo( const char *name, int type )
{
REMOTEINFO *p;
for( p = first_rinfo; p; p = p->next )
{
if( !strcasecmp( name, p->name ) )
return p;
}
return NULL;
}
/* return 'b' from 'a!b!c!d!e' */
const char * imc_hubinpath( const char *path )
{
const char *separator, *where;
static char buf[IMC_NAME_LENGTH];
int i = 0;
if ( !path || path[0] == '\0')
return NULL;
if ( (separator = strchr( path, '!' ) ) == NULL )
return path;
where = separator + 1;
while ( *where != '!' && *where != '\0' )
{
where++;
i++;
}
i++;
strlcpy( buf, separator + 1, i );
return buf;
}
/* return 'a' from 'a!b!c!d!e' */
char *imc_firstinpath( char *path )
{
static char buf[IMC_NAME_LENGTH];
char *p;
for( p = buf; *path && *path != '!'; *p++ = *path++ )
;
*p = 0;
return buf;
}
/* create a new info entry, insert into list */
REMOTEINFO *imc_new_reminfo( const char *mud )
{
REMOTEINFO *p, *mud_prev;
IMCCREATE( p, REMOTEINFO, 1 );
p->name = IMCSTRALLOC( mud );
p->netname = NULL;
p->version = NULL;
p->path = NULL;
p->top_sequence = 0;
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 p;
}
/* update our routing table based on a packet received with path "path" */
void updateroutes( char *path )
{
REMOTEINFO *p;
char *sender, *temp;
/* loop through each item in the path, and update routes to there */
temp = path;
while( temp && temp[0] != '\0' )
{
sender = imc_firstinpath( temp );
if( strcasecmp( sender, imc_name ) )
{
/* not from us */
/* check if its in the list already */
p = imc_find_reminfo( sender, 1 );
if( !p ) /* not in list yet, create a new entry */
{
p = imc_new_reminfo( sender );
p->netname = IMCSTRALLOC( "unknown" );
p->version = IMCSTRALLOC( "unknown" );
}
else
{ /* already in list, update the entry */
p->expired = FALSE;
}
}
/* get the next item in the path */
temp = strchr( temp, '!' );
if( temp )
temp++; /* skip to just after the next '!' */
}
}
/* check if a packet from a given source is blacklisted */
bool imc_isblacklisted( char *who )
{
IMC_BLACKLIST *mud;
for( mud = first_imc_mudblacklist; mud; mud = mud->next )
{
if( !strcasecmp( mud->name, imc_mudof( who ) ) )
return TRUE;
}
return FALSE;
}
static struct
{
const char *name;
long allow_flag;
long deny_flag;
long pset_flag;
int minlevel;
} imc_functions[]=
{
{ "IMC-Status", ALLOW_IMCSTATUS, DENY_IMCSTATUS, PSET_IMCSTATUS, PERM_IMMORTAL },
{ "IMC-Chan-FYI", ALLOW_IMCCHANFYI, DENY_IMCCHANFYI, PSET_IMCCHANFYI, PERM_IMMORTAL },
{ "imcptell", ALLOW_NOTSUPPORTED, DENY_IMCPTELL, PSET_IMCPTELL, PERM_PLAYER },
{ "imcpbeep", ALLOW_NOTSUPPORTED, DENY_IMCPBEEP, PSET_IMCPBEEP, PERM_PLAYER },
{ "imcpvisible", ALLOW_IMCPVISIBLE, DENY_IMCPVISIBLE, PSET_IMCPVISIBLE, PERM_PLAYER },
{ "imcpcolor", ALLOW_NOTSUPPORTED, DENY_NOTSUPPORTED, PSET_IMCPCOLOR, PERM_PLAYER }
};
#define numfunctions ( sizeof( imc_functions ) / sizeof( imc_functions[0] ) )
imc_char_data *imc_getdata( CHAR_DATA *ch )
{
imc_char_data *d;
IMCCREATE( d, imc_char_data, 1 );
if( !ch ) /* fake system character */
{
d->perm_level = PERM_NONE;
strlcpy( d->name, "*", IMC_NAME_LENGTH );
return d;
}
d->perm_level = get_permlevel(ch);
strlcpy( d->name, CH_IMCNAME(ch), IMC_NAME_LENGTH );
return d;
}
void receive_oldchannel( imc_char_data *from, int number, const char *argument, int emote )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *victim;
char buf[IMC_DATA_LENGTH], str[IMC_DATA_LENGTH];
int chan;
if( number != 2 && number != 15 )
return;
if ( number == 2 )
{
chan = 0;
if (!emote)
snprintf( buf, IMC_DATA_LENGTH, "~c[~CIMC-Status~c] ~C%s ~cannounces '%s'\n\r",
from->name, argument);
else
snprintf( buf, IMC_DATA_LENGTH, "~c[~CIMC-Status~c] ~C%s ~c%s\n\r", from->name, argument );
}
else
{
chan = 1;
if (!emote)
snprintf( buf, IMC_DATA_LENGTH, "~c[~CIMC-Chan-FYI~c] ~C%s ~cannounces '%s'\n\r",
from->name, argument );
else
snprintf( buf, IMC_DATA_LENGTH, "~c[~CIMC-Chan-FYI~c] ~C%s ~c%s\n\r", from->name, argument );
}
snprintf( str, IMC_DATA_LENGTH, "%s", color_itom(buf));
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& !IS_SET( IMC_PFLAGS( victim ), imc_functions[chan].pset_flag )
&& function_usable( victim, imc_functions[chan].deny_flag, imc_functions[chan].allow_flag,
imc_functions[chan].minlevel ) )
{
imc_to_char( str, victim );
}
}
}
void imc_recv_chat( imc_char_data *from, int channel, const char *argument )
{
receive_oldchannel( from, channel, argument, 0 );
}
void imc_recv_emote( imc_char_data *from, int channel, const char *argument )
{
receive_oldchannel( from, channel, argument, 1 );
}
/* convert back from 'd' to 'p' */
void setdata( PACKET *p, imc_char_data *d )
{
imc_initdata( p );
if( !d )
{
strlcpy( p->from, "*", IMC_NAME_LENGTH );
imc_addkeyi( p, "level", -1 );
return;
}
strlcpy( p->from, d->name, IMC_NAME_LENGTH );
imc_addkeyi( p, "level", d->perm_level );
}
/* send a tell to a remote player */
void imc_send_tell( imc_char_data *from, char *to, char *argument, int isreply )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
if( !strcasecmp( imc_mudof( to ), "*" ) )
return; /* don't let them do this */
setdata( &out, from );
strlcpy( out.to, to, IMC_NAME_LENGTH );
strlcpy( out.type, "tell", IMC_TYPE_LENGTH );
imc_addkey( &out, "text", argument );
if( isreply )
imc_addkeyi( &out, "isreply", isreply );
imc_send( &out );
imc_freedata( &out );
}
/* send a 'you have been blacklisted' response */
void imc_sendblacklistresponse( char *to )
{
char buf[IMC_DATA_LENGTH];
if( strcasecmp( imc_nameof( to ), "*" ) )
{
snprintf( buf, IMC_DATA_LENGTH, "%s is blocking packets from your mud.", imc_name );
imc_send_tell( NULL, to, buf, 1 );
}
}
void imc_recv_imcptell( imc_char_data *from, char *to, const char *argument, int isreply )
{
DESCRIPTOR_DATA *d ;
CHAR_DATA *victim = 0 ;
IMC_BLACKLIST *entry ;
char buf[IMC_DATA_LENGTH] ;
char tstring[LSS] ;
if( !strcasecmp( to, "*" ) ) /* drop messages to system */
return;
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& IS_IMCVISIBLE(victim) && !strcasecmp( to, CH_IMCNAME(victim) ))
break;
}
if(!d)
{
snprintf( buf, IMC_DATA_LENGTH, "%s is not here.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
if( !function_usable(victim, DENY_IMCPTELL, ALLOW_IMCPTELL, PERM_PLAYER)
|| IS_SET(IMC_PFLAGS(victim), PSET_IMCPTELL))
{
if( strcasecmp( imc_nameof( from->name ), "*" ) )
{
snprintf( buf, IMC_DATA_LENGTH, "%s is not receiving tells.", to );
imc_send_tell( NULL, from->name, buf, 1 );
}
return;
}
for( entry = FIRST_IMCBLACKLIST(victim); entry; entry = entry->next )
{
if(!strcasecmp( from->name, entry->name ) || !strcasecmp( imc_mudof(from->name), entry->name))
break;
}
if (entry)
{
snprintf( buf, IMC_DATA_LENGTH, "%s has blacklisted you.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
if( IS_SET(IMC_PFLAGS(victim), PSET_IMCPAFK) )
{
snprintf( buf, IMC_DATA_LENGTH, "%s is AFK.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
if( strcasecmp( imc_nameof( from->name ), "*" ) ) /* not a system message */
{
if( IMC_RREPLY(victim) )
IMCSTRFREE( IMC_RREPLY(victim) );
IMC_RREPLY(victim) = IMCSTRALLOC( from->name );
}
strlcpy( tstring, color_itom("~C%s ~cimcptells you ~c'~W%s~c'\n\r" ), LSS);
snprintf( buf, IMC_DATA_LENGTH, tstring,
from->name, color_itom(argument) );
imc_to_char( buf, victim );
return;
}
void imc_recv_whoreply( char *to, const char *text, int sequence, int inlen )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *victim;
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& !strcasecmp( to, CH_IMCNAME(victim) ) )
{
imc_to_pager( color_itom(text), victim );
return;
}
}
}
char *channel_mudof( const char *fullname )
{
static char buf[IMC_PNAME_LENGTH];
char *where = buf;
int count=0;
while( *fullname && *fullname != ':' && count < IMC_PNAME_LENGTH-1 )
*where++ = *fullname++, count++;
*where = 0;
return buf;
}
/* see if someone can talk on a channel - lots of string stuff here! */
bool imc_audible( IMC_CHANNEL *c, const char *who )
{
if( !c->refreshed ) // Crash fix
return FALSE;
/* checking a mud? */
if( !strchr( who, '@' ) )
{
char arg[IMC_DATA_LENGTH];
const char *p;
int invited = 0;
/* added compress policy - shogar - 1/29/2000 */
if( c->policy != CHAN_PRIVATE && c->policy != CHAN_CPRIVATE )
return TRUE;
if( !strcasecmp( channel_mudof( c->name ) , imc_name ) )
return TRUE;
/* Private channel - can only hear if someone with the right mud name is there. */
p = imc_getarg( c->invited, arg, IMC_NAME_LENGTH );
while( arg[0] )
{
if( !strcasecmp( who, arg ) || !strcasecmp( who, imc_mudof( arg ) ) )
{
invited = 1;
break;
}
p = imc_getarg( p, arg, IMC_NAME_LENGTH );
}
if( !invited )
return FALSE;
p = imc_getarg( c->excluded, arg, IMC_NAME_LENGTH );
while( arg[0] )
{
if( !strcasecmp( who, arg ) || !strcasecmp( who, imc_mudof( arg ) ) )
return FALSE;
p = imc_getarg( p, arg, IMC_NAME_LENGTH );
}
return TRUE;
}
/* owners and operators always can */
if( !strcasecmp( c->owner, who ) || imc_hasname( c->operators, who ) )
return TRUE;
/* IMC locally can use any channel */
if( !strcasecmp( imc_nameof( who ), "IMC" ) && !strcasecmp( imc_mudof( who ), imc_name ) )
return FALSE;
/* added compress copen policy - shogar - 1/29/2000 */
if( c->policy == CHAN_OPEN || c->policy == CHAN_COPEN )
{
/* open policy. default yes. override with excludes, then invites */
if( ( imc_hasname( c->excluded, who ) || imc_hasname( c->excluded, imc_mudof( who ) ) )
&& !imc_hasname( c->invited, who ) && !imc_hasname( c->invited, imc_mudof( who ) ) )
return FALSE;
else
return TRUE;
}
/* closed or private. default no, override with invites, then excludes */
if( ( imc_hasname( c->invited, who ) || imc_hasname( c->invited, imc_mudof( who ) ) )
&& !imc_hasname( c->excluded, who ) && !imc_hasname( c->excluded, imc_mudof( who ) ) )
return TRUE;
else
return FALSE;
}
void update_imchistory( IMC_CHANNEL *channel, char *message )
{
char msg[LSS], buf[LSS];
struct tm *local;
time_t t;
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;
}
strlcpy( msg, message, LSS );
for( x = 0; x < MAX_IMCHISTORY; x++ )
{
if( channel->history[x] == NULL )
{
t = time( NULL );
local = localtime( &t );
snprintf( buf, LSS, "~R[%-2.2d/%-2.2d %-2.2d:%-2.2d] ~G%s\n\r",
local->tm_mon+1, local->tm_mday, local->tm_hour, local->tm_min, color_mtoi(msg) );
channel->history[x] = IMCSTRALLOC( color_itom(buf) );
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] );
}
}
t = time( NULL );
local = localtime( &t );
snprintf( buf, LSS, "~R[%-2.2d/%-2.2d %-2.2d:%-2.2d] ~G%s\n\r",
local->tm_mon+1, local->tm_mday, local->tm_hour, local->tm_min, color_mtoi(msg) );
IMCSTRFREE( channel->history[x] );
channel->history[x] = IMCSTRALLOC( color_itom(buf) );
}
}
return;
}
void imc_showchannel( IMC_CHANNEL *c, const char *from, const char *txt, int emote )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *ch;
char buf[LSS];
if( !c->local_name || c->local_name[0] == '\0' )
return;
if( emote < 2 )
snprintf( buf, LSS, emote ? c->emoteformat : c->regformat, from, color_itom(txt));
else
snprintf( buf, LSS, c->socformat, color_itom( txt ) );
for( d = first_descriptor; d; d = d->next )
{
if( !d->character )
continue;
ch = d->character;
if( get_permlevel(ch) < c->perm_level
|| !imc_audible( c, imc_makename( CH_IMCNAME(ch), imc_name ) )
|| !imc_hasname( IMC_CSUBSCRIBED(ch), c->local_name ) )
continue;
imc_printf( ch, "%s\n\r", buf );
}
update_imchistory( c, buf );
}
void imc_sendmessage( IMC_CHANNEL *c, const char *name, char *text, int emote )
{
PACKET out;
strlcpy( out.from, name, IMC_NAME_LENGTH );
imc_initdata( &out );
imc_addkey( &out, "channel", c->name );
imc_addkey( &out, "text", text );
imc_addkeyi( &out, "emote", emote );
/* send a message out on a channel */
if( c->policy == CHAN_PRIVATE || c->policy == CHAN_CPRIVATE )
{
strlcpy( out.type, "ice-msg-p", IMC_TYPE_LENGTH );
snprintf( out.to, IMC_NAME_LENGTH, "IMC@%s", channel_mudof( c->name ) );
}
else
{
/* broadcast */
strlcpy( out.type, "ice-msg-b", IMC_TYPE_LENGTH );
strlcpy( out.to, "*@*", IMC_NAME_LENGTH );
}
imc_send( &out );
imc_freedata( &out );
}
/* respond to a who request with the given data */
void imc_send_whoreply( const char *to, char *data, int sequence )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
if( !strcasecmp( imc_mudof( to ), "*" ) )
return; /* don't let them do this */
imc_initdata( &out );
strlcpy( out.to, to, IMC_NAME_LENGTH );
strlcpy( out.type, "who-reply", IMC_TYPE_LENGTH );
strlcpy( out.from, "*", IMC_NAME_LENGTH );
if( sequence != -1 )
imc_addkeyi( &out, "sequence", sequence );
imc_addkey( &out, "text", data );
imc_send( &out );
imc_freedata( &out );
}
void imc_whoreply_start( const char *to )
{
wr_sequence = 0;
wr_to = IMCSTRALLOC( to );
wr_buf[0] = '\0';
}
void imc_whoreply_add( char *text )
{
/* give a bit of a margin for error here */
if( strlen( wr_to ) + strlen( text ) >= IMC_DATA_LENGTH-500 )
{
imc_send_whoreply( wr_to, wr_buf, wr_sequence );
wr_sequence++;
strlcpy( wr_buf, text, IMC_DATA_LENGTH );
return;
}
strlcat( wr_buf, text, IMC_DATA_LENGTH );
}
void imc_whoreply_end( void )
{
imc_send_whoreply( wr_to, wr_buf, -( wr_sequence + 1 ) );
IMCSTRFREE( wr_to );
wr_buf[0] = '\0';
}
// Displays the person's rank or permission level status
char *imcrankbuffer( CHAR_DATA *ch )
{
static char rbuf[LSS];
char imclevel_color;
unsigned char perm_level;
char rankcolor_code[4] = { 'g', 'G', 'C', 'Y'};
perm_level = get_permlevel(ch);
imclevel_color = rankcolor_code[perm_level];
if( CH_IMCRANK(ch) && CH_IMCRANK(ch)[0] != '\0' )
{
snprintf( rbuf, LSS, "~%c%s", rankcolor_code[perm_level], color_mtoi(CH_IMCRANK(ch)));
return rbuf;
}
if ( perm_level < PERM_IMMORTAL )
snprintf( rbuf, LSS, "~%cPlayer", rankcolor_code[perm_level] );
else if ( perm_level >= PERM_ADMIN )
snprintf( rbuf, LSS, "~%cAdministrator", rankcolor_code[perm_level] );
else
snprintf( rbuf, LSS, "~%cImmortal", rankcolor_code[perm_level] );
return rbuf;
}
/* expanded for minimal mud-specific code. I really don't want to replicate
* stock in-game who displays here, since it's one of the most commonly
* changed pieces of code. shrug.
*/
void process_imcplist( imc_char_data *from, const char *argument )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *person;
char buf[LSS], personbuf[LSS], tailbuf[LSS], rank[LSS], buf2[LSS];
char stats[20];
int pcount = 0, i = 0;
imc_whoreply_start( from->name );
imc_whoreply_add( "\n\r" );
switch( imc_whostyle )
{
case 1: // The Winter Who Style by Kris
snprintf( buf, LSS, " Players on %s ", imc_siteinfo.name );
imc_whoreply_add( SetFill( EmptyBlock( buf, 0, 1 ), 77, "~W _" ) );
imc_whoreply_add( "\n\r");
snprintf( buf2, LSS, "%s~C|", Alternating_Symbols( buf, "~W", "~C" ) );
imc_whoreply_add( "~C|" );
imc_whoreply_add( SetFill( buf2, 77, "~B*~C|" ) );
imc_whoreply_add( "\n\r");
if( imc_siteinfo.port > 0 )
snprintf( buf, LSS, "~W**** ~wtelnet://%s:%d ~W****",
imc_siteinfo.host, imc_siteinfo.port );
else
snprintf( buf, LSS, "~W**** ~wtelnet://%s ~W****", imc_siteinfo.host );
imc_whoreply_add( SetFill( buf, 78, " ") );
imc_whoreply_add( "\n\r\n\r" );
break;
case 2: // The Fiery Bowels of Hell Who Style by Kris
snprintf( buf, LSS, "~YPlayers on %s", imc_siteinfo.name );
imc_whoreply_add( SetFill( buf, 78, " " ));
imc_whoreply_add( "\n\r" );
imc_whoreply_add( SetFill(
"~R ( ~r, ~R) ) ( ~r, ~R) ) ( ~r, ~R) ) ",
78, " " ) );
imc_whoreply_add( "\n\r" );
imc_whoreply_add( SetFill(
"~R ( ~r,~R(~r. ~R( (~r,`~R) ) ( ~r,~R(~r. ~R( (~r,`~R) ) ( ~r,~R(~r. ~R( (~r,`~R) ) ( ~r,~R(~r. ~R",
78, " " ) );
imc_whoreply_add( "\n\r" );
imc_whoreply_add( SetFill(
"~R )~r' ~R(~r' ~R(~r'~R) ( ) ) (~r.~R) )~r' ~R(~r' ~R(~r'~R) ( ) ) (~r.~R) } )~r' ~R(~r' ~R(~r'~R) ( ) ) (~r.~R) )~r' ~R(~r' ~R",
78, " " ) );
imc_whoreply_add( "\n\r" );
imc_whoreply_add( SetFill(
"~R (~r' ~R)~r,~R)~r. ~B_~R)~B_ ~R( (~r,~R)~r' ~R) (~r' ~R)~r,~R)~r. ~B_~R)~B_ ~R( (~r,~R)~r' ~R) (~r' ~R)~r,~R)~r. ~B_~R)~B_ ~R( (~r,~R)~r' ~R) (~r' ~R)~r,~R)~r. ~R",
78, " " ) );
imc_whoreply_add( "\n\r" );
if( imc_siteinfo.port > 0 )
snprintf( buf, LSS, "~Y---> ~ytelnet://%s:%d ~Y<---",
imc_siteinfo.host, imc_siteinfo.port );
else
snprintf( buf, LSS, "~Y---> ~ytelnet://%s ~Y<---", imc_siteinfo.host );
imc_whoreply_add( SetFill( buf, 78, " ") );
imc_whoreply_add( "\n\r\n\r" );
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
snprintf( buf, LSS, "~R-=[ ~WPlayers on %s ~R]=-", imc_siteinfo.name );
imc_whoreply_add( SetFill( buf, 78, " ") );
imc_whoreply_add( "\n\r" );
if( imc_siteinfo.port > 0 )
snprintf( buf, LSS, "~Y-=[ ~Wtelnet://%s:%d ~Y]=-", imc_siteinfo.host, imc_siteinfo.port );
else
snprintf( buf, LSS, "~Y-=[ telnet://%s ]=-", imc_siteinfo.host );
imc_whoreply_add( SetFill( buf, 78, " ") );
imc_whoreply_add( "\n\r" );
break;
default: // IMC2 4.00 Continuum Default Who Style by Kris
snprintf( buf, LSS, " Players on %s ", imc_siteinfo.name );
imc_whoreply_add( SetFill( EmptyBlock( buf, 0, 1 ), 77, "~C _" ) );
imc_whoreply_add( "\n\r");
snprintf( buf2, LSS, "%s~G|", Alternating_Symbols( buf, "~Y", "~C" ) );
imc_whoreply_add( "~G|" );
imc_whoreply_add( SetFill( buf2, 77, "~cx~G|" ) );
imc_whoreply_add( "\n\r");
if( imc_siteinfo.port > 0 )
snprintf( buf, LSS, "~C**** ~ctelnet://%s:%d ~C****",
imc_siteinfo.host, imc_siteinfo.port );
else
snprintf( buf, LSS, "~C**** ~ctelnet://%s ~C****", imc_siteinfo.host );
imc_whoreply_add( SetFill( buf, 78, " ") );
imc_whoreply_add( "\n\r\n\r" );
break;
}
i = 0;
/* See if there are any visible immortals online to even display players header. */
for( d = first_descriptor; d; d = d->next )
if( d->character && (d->connected == CON_PLAYING) && level_pass( d->character, PERM_IMMORTAL)
&& IS_IMCVISIBLE(d->character))
i++;
if( i > 0 )
{
switch( imc_whostyle )
{
case 1: // The Winter Who Style by Kris
imc_whoreply_add( SetFill( "~CAdmins ~Wand ~CImmortals", 78, " ") );
imc_whoreply_add( "\n\r~W");
imc_whoreply_add( SetFill( "", 78, "-" ) );
break;
case 2: // The Fiery Bowels of Hell Who Style by Kris
imc_whoreply_add( SetFill( "~RAdmins~Y----~rand~Y----~RImmortals", 78, "~Y-") );
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
imc_whoreply_add( SetFill( "-=[ ~WImmortals ~R]=-", 78, "~R-") );
break;
default: // IMC2 4.00 Continuum Default Who Style by Kris
imc_whoreply_add( SetFill( "~CAdmins ~cand ~CImmortals", 78, " ") );
imc_whoreply_add( "\n\r~Y");
imc_whoreply_add( SetFill( "", 78, "-" ) );
break;
}
imc_whoreply_add( "\n\r\n\r" );
for( d = first_descriptor; d; d = d->next )
{
if( d->character && d->connected == CON_PLAYING )
{
person = d->character;
if ( !level_pass(d->character, PERM_IMMORTAL ) || !IS_IMCVISIBLE(person))
continue;
pcount++;
strlcpy( rank, SetFill( imcrankbuffer(person), 22, " ") , LSS );
if ( IS_SET(IMC_PFLAGS(person), PSET_IMCPAFK) )
strlcpy( stats, "[AFK]", 20 );
else
strlcpy( stats, "[---]", 20 );
switch( imc_whostyle )
{
case 2: // The Fiery Bowels of Hell Who Style by Kris
snprintf( personbuf, LSS, "~Y%s ~y%s ~Y%s~y%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
snprintf( personbuf, LSS, "%s ~D%s ~G%s%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
case 1: // The Winter Who Style by Kris
default: // IMC2 4.00 Continuum Default Who Style by Kris
snprintf( personbuf, LSS, "~C%s ~w%s ~C%s~c%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
}
imc_whoreply_add( personbuf );
}
}
}
i = 0;
/* See if there are any visible players online to even display players header. */
for( d = first_descriptor; d; d = d->next )
if( d->character && (d->connected == CON_PLAYING) && !level_pass( d->character, PERM_IMMORTAL)
&& IS_IMCVISIBLE(d->character))
i++;
if( i > 0 )
{
switch( imc_whostyle )
{
case 1: // The Winter Who Style by Kris
imc_whoreply_add( "\n\r\n\r" );
imc_whoreply_add( SetFill( " ~GMortal Players ", 78, "~W*") );
break;
case 2: // The Fiery Bowels of Hell Who Style by Kris
imc_whoreply_add( "\n\r\n\r" );
imc_whoreply_add( SetFill( " ~RMortal Players ", 78, "~Y*") );
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
imc_whoreply_add( SetFill( "-=[ ~WPlayers ~B]=-", 78, "~B-") );
break;
default: // IMC2 4.00 Continuum Default Who Style by Kris
imc_whoreply_add( "\n\r\n\r" );
imc_whoreply_add( SetFill( " ~GMortal Players ", 78, "~Y*") );
break;
};
imc_whoreply_add( "\n\r\n\r" );
for( d = first_descriptor; d; d = d->next )
{
if( d->character && d->connected == CON_PLAYING )
{
person = d->character;
if( level_pass(d->character, PERM_IMMORTAL ) || !IS_IMCVISIBLE(person) )
continue;
pcount++;
strlcpy( rank, SetFill( imcrankbuffer(person), 22, " "), LSS );
if ( IS_SET(IMC_PFLAGS(person), PSET_IMCPAFK) )
strlcpy( stats, "[AFK]", 20 );
else
strlcpy( stats, "[---]", 20 );
switch( imc_whostyle )
{
case 2: // The Fiery Bowels of Hell Who Style by Kris
snprintf( personbuf, LSS, "~R%s ~r%s ~R%s~r%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
snprintf( personbuf, LSS, "%s ~D%s ~G%s%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
case 1: // The Winter Who Style by Kris
default: // IMC2 4.00 Continuum Default Who Style by Kris
snprintf( personbuf, LSS, "~G%s ~w%s ~G%s~g%s\n\r", rank, stats,
CH_IMCNAME(person), color_mtoi(CH_IMCTITLE(person)));
break;
}
personbuf[LSS] = '\0';
imc_whoreply_add( personbuf );
}
}
}
switch( imc_whostyle )
{
case 1: // The Winter Who Style by Kris
snprintf( tailbuf, LSS, "\n\r\n\r~W** ~C%d ~cPlayer%s ~W**-", pcount, pcount == 1 ? "" : "s" );
imc_whoreply_add( tailbuf );
snprintf( tailbuf, LSS, "~W** ~GHomepage: ~g%s ~W**-** ~C%3d ~cMax Since Reboot ~W**\n\r",
imc_siteinfo.www, IMCMAXPLAYERS );
imc_whoreply_add( tailbuf );
break;
case 2: // The Fiery Bowels of Hell Who Style by Kris
snprintf( tailbuf, LSS, "\n\r\n\r~R** ~Y%d ~yPlayer%s ~R**-", pcount, pcount == 1 ? "" : "s" );
imc_whoreply_add( tailbuf );
snprintf( tailbuf, LSS, "~R** ~YHomepage: ~y%s ~R**-** ~Y%3d ~yMax Since Reboot ~R**\n\r",
imc_siteinfo.www, IMCMAXPLAYERS );
imc_whoreply_add( tailbuf );
break;
case 3: // IMC2 Classic Who Style - Slight altered from older versions of the IMC2 client
snprintf( tailbuf, LSS, "\n\r~Y[~W%d Player%s~Y] ", pcount, pcount == 1 ? "" : "s" );
imc_whoreply_add( tailbuf );
snprintf( tailbuf, LSS, "~Y[~WHomepage: %s~Y] [~W%3d Max Since Reboot~Y]\n\r",
imc_siteinfo.www, IMCMAXPLAYERS );
imc_whoreply_add( tailbuf );
break;
default: // IMC2 4.00 Continuum Default Who Style by Kris
snprintf( tailbuf, LSS, "\n\r\n\r~Y** ~C%d ~cPlayer%s ~Y**-", pcount, pcount == 1 ? "" : "s" );
imc_whoreply_add( tailbuf );
snprintf( tailbuf, LSS, "~Y** ~GHomepage: ~g%s ~Y**-** ~C%3d ~cMax Since Reboot ~Y**\n\r",
imc_siteinfo.www, IMCMAXPLAYERS );
imc_whoreply_add( tailbuf );
break;
}
imc_whoreply_end();
}
/* Displays generic information about a player. */
void process_imcpinfo( imc_char_data *from, const char *to )
{
DESCRIPTOR_DATA *d ;
CHAR_DATA *victim = 0 ;
char buf[LSS], permissions[LSS], functions[LSS];
unsigned char i = 0 ;
static char * const male_female [] = { "Neither", "Male", "Female" };
if ( !to || to[0] == '\0')
return;
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& IS_IMCVISIBLE(victim) && !strcasecmp( to, CH_IMCNAME(victim) ) )
break;
}
if (!d)
{
imc_send_whoreply( from->name, "That player is either currently offline or not visible.\n\r", -1);
return;
}
i = get_permlevel(victim);
if ( i == PERM_NONE )
strlcpy( permissions, "Ineligible for IMC", LSS );
else if ( i == PERM_PLAYER )
strlcpy( permissions, "Player", LSS );
else if ( i == PERM_IMMORTAL )
strlcpy( permissions, "Immortal", LSS );
else
strlcpy( permissions, "Administrator", LSS);
functions[0] = '\0';
for ( i = 0; i < numfunctions; i++)
{
if ( function_usable( victim, imc_functions[i].deny_flag, imc_functions[i].allow_flag,
imc_functions[i].minlevel) && !IS_SET( IMC_PFLAGS(victim), imc_functions[i].pset_flag))
snprintf( functions + strlen(functions), LSS, "%s ", imc_functions[i].name);
}
snprintf( buf, LSS, "~cPlayer Information\n\r"
"~W------------------\n\r"
"~cName: ~W%s\n\r"
"~cSex: ~W%s\n\r"
"~cPermission Level: ~W%s\n\r"
"~cSubscribed Channels: ~W%s\n\r"
"~cActive Network Functions: ~W%s\n\r",
CH_IMCNAME(victim), male_female[URANGE(0, CH_IMCSEX(victim), 2)], permissions,
(IMC_CSUBSCRIBED(victim) && IMC_CSUBSCRIBED(victim)[0] != '\0' ? IMC_CSUBSCRIBED(victim) : "None"),
(functions && functions[0] != '\0' ? functions : ""));
imc_send_whoreply( from->name, buf, -1 );
return;
}
/* connect to hub */
bool imc_connect_to( void )
{
int desc;
struct sockaddr_in sa;
char buf[IMC_DATA_LENGTH];
int r;
if( !this_imcmud )
{
imcbug( "%s", "No connection data loaded" );
return FALSE;
}
if( this_imcmud->desc > 0 )
{
imcbug( "%s", "Already connected" );
return FALSE;
}
imclog( "Connecting to %s", this_imcmud->hubname );
/* warning: this blocks. It would be better to farm the query out to
* another process, but that is difficult to do without lots of changes
* to the core mud code. You may want to change this code if you have an
* existing resolver process running.
*/
if( ( sa.sin_addr.s_addr = inet_addr( this_imcmud->host ) ) == -1UL )
{
struct hostent *hostinfo;
if( !( hostinfo = gethostbyname( this_imcmud->host ) ) )
{
imcbug( "%s", "imc_connect: couldn't resolve hostname" );
return FALSE;
}
sa.sin_addr.s_addr = *(unsigned long *) hostinfo->h_addr;
}
sa.sin_port = htons( this_imcmud->port );
sa.sin_family = AF_INET;
desc = socket( AF_INET, SOCK_STREAM, 0 );
if( desc < 0 )
{
perror( "socket" );
return FALSE;
}
r = fcntl( desc, F_GETFL, 0 );
if( r < 0 || fcntl( desc, F_SETFL, O_NONBLOCK | r ) < 0 )
{
perror( "imc_connect: fcntl" );
close( desc );
return FALSE;
}
if( connect( desc, (struct sockaddr *)&sa, sizeof(sa) ) < 0 )
{
if( errno != EINPROGRESS )
{
perror( "connect" );
close( desc );
return FALSE;
}
}
this_imcmud->state = CONN_SENDCLIENTPWD;
this_imcmud->desc = desc;
this_imcmud->insize = IMC_MINBUF;
IMCCREATE( this_imcmud->inbuf, char, this_imcmud->insize );
this_imcmud->outsize = IMC_MINBUF;
IMCCREATE( this_imcmud->outbuf, char, this_imcmud->outsize );
this_imcmud->inbuf[0] = this_imcmud->outbuf[0] = '\0';
this_imcmud->newoutput = 0;
/* NetName connection authentication by Kris */
snprintf( buf, IMC_DATA_LENGTH, "PW %s %s version=%d %s", imc_name, this_imcmud->clientpw,
IMC_VERSION, imc_siteinfo.netname );
do_imcsend( buf );
return TRUE;
}
char *imc_list( void )
{
static char buf[IMC_DATA_LENGTH];
REMOTEINFO *p;
int count = 1;
snprintf( buf, IMC_DATA_LENGTH, "~WActive muds on IMC:\n\r"
"~c%-15.15s ~C%-35.35s ~g%-10.10s ~G%-10.10s"
"\n\r\n\r~c%-15.15s ~C%-35.35s ~g%-10.10s ~G%-10.10s",
"Name", "IMC Version", "Network", "Hub",
imc_siteinfo.name, imc_versionid, imc_siteinfo.netname, this_imcmud->hubname );
for( p = first_rinfo; p; p = p->next, count++ )
{
snprintf( buf + strlen(buf), IMC_DATA_LENGTH - strlen(buf),
"\n\r~c%-15.15s ~C%-35.35s ~g%-10.10s ~G%-10.10s",
p->name, p->version, p->netname, p->expired ? "~Rexpired" : imc_hubinpath(p->path));
}
snprintf( buf + strlen( buf ), IMC_DATA_LENGTH - strlen(buf),
"\n\r~W%d muds on IMC found.\n\r", count );
return buf;
}
// Formats a number into a string, with numbers seperated by commas
// Merely eye-candy, but why not? -- Kratas
char *comma( int number )
{
char buf[200];
static char buf_comma[200];
int x, x_pcomma, ex_amount;
strlcpy( buf_comma, "", 200 );
snprintf( buf, 200, "%d", number );
ex_amount = strlen( buf ) % 3;
for( x = 0, x_pcomma = 0 ; x < strlen( buf ) ; x++, x_pcomma++ )
{
if( (x-ex_amount) % 3 == 0 && x > 0 )
{
buf_comma[x_pcomma] = ',';
x_pcomma++;
buf_comma[x_pcomma] = buf[x];
}
else
buf_comma[x_pcomma] = buf[x];
}
buf_comma[x_pcomma] = '\0';
return buf_comma;
}
/* get some IMC stats, return a string describing them */
char *imc_getstats( char *choice )
{
static char buf[IMC_DATA_LENGTH];
int evcount = 0;
imc_event *ev = imc_event_list;
long secondspast;
if ( !choice || choice[0] == '\0' || !strcasecmp( choice, "network" ) )
{
while( ev )
{
evcount++;
ev = ev->next;
}
if( (imc_now - imc_stats.start) > 0)
secondspast = imc_now - imc_stats.start;
else
secondspast = 1;
snprintf( buf, IMC_DATA_LENGTH,
"~WGeneral IMC Statistics\n\r"
"~cReceived packets : ~C%ld\n\r"
"~cReceived bytes : ~C%s (%ld/second)\n\r"
"~cTransmitted packets: ~C%ld\n\r"
"~cTransmitted bytes : ~C%s (%ld/second)\n\r"
"~cMaximum packet size: ~C%d\n\r"
"~cPending events : ~C%d\n\r"
"~cSequence drops : ~C%d\n\r"
"~cDebug : ~C%s\n\r"
"~cLast IMC Boot : ~C%s\n\r",
imc_stats.rx_pkts, comma( imc_stats.rx_bytes), imc_stats.rx_bytes / secondspast,
imc_stats.tx_pkts, comma( imc_stats.tx_bytes), imc_stats.tx_bytes / secondspast,
imc_stats.max_pkt, evcount, imc_stats.sequence_drops, imc_debug_on ? "Yes" : "No",
ctime( &imc_boot ) );
return buf;
}
if (!strcasecmp( choice, "general"))
{
snprintf( buf, IMC_DATA_LENGTH,
"~WSite Information:\n\r"
"~cName ~W:~C %s\n\r"
"~cIMC Version ~W:~C %s\n\r"
"~cAddress ~W:~C telnet://%s:%d\n\r"
"~cWebpage ~W:~C http://%s\n\r"
"~cAdmin Email ~W:~C %s\n\r"
"~cDetails ~W:~C %s\n\r",
imc_siteinfo.name, imc_versionid, imc_siteinfo.host, imc_siteinfo.port, imc_siteinfo.www,
imc_siteinfo.email, imc_siteinfo.details );
return buf;
}
return "Bad invocation of imc_getstats.";
}
void imc_recv_who( imc_char_data *from, const char *type )
{
char arg[LSS];
if ( !type || type[0] == '\0')
return;
type = imc_getarg( type, arg, LSS );
/* ... and lets send 'em the data! */
if( !strcasecmp( arg, "who" ) )
process_imcplist( from, type );
else if( !strcasecmp( arg, "finger" ) )
process_imcpinfo( from, type );
else if( !strcasecmp( arg, "info" ) )
imc_send_whoreply( from->name, imc_getstats("general"), -1 );
else if( !strcasecmp( arg, "list" ) )
imc_send_whoreply( from->name, imc_list(), -1 );
else if( !strcasecmp( arg, "istats" ) )
imc_send_whoreply( from->name, imc_getstats("network"), -1 );
else if( !strcasecmp( arg, "options" ) || !strcasecmp( arg, "services" ) || !strcasecmp( arg, "help" ) )
imc_send_whoreply( from->name,
"~WAvailable imcminfo types:\n\r"
"~chelp ~W- ~Cthis list\n\r"
"~cwho ~W- ~Cwho listing\n\r"
"~cinfo ~W- ~Cmud information\n\r"
"~clist ~W- ~Cactive IMC connections\n\r"
"~cistats ~W- ~Cnetwork traffic statistics\n\r"
"~cfinger xxx ~W- ~Cfinger player xxx\n\r",
-1 );
else
imc_send_whoreply( from->name, "Sorry, no information is available of that type.\n\r", -1 );
return;
}
/* respond with a whois-reply */
void imc_send_whoisreply( char *to, char *data )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
if( !strcasecmp( imc_mudof( to ), "*" ) )
return; /* don't let them do this */
imc_initdata( &out );
strlcpy( out.to, to, IMC_NAME_LENGTH );
strlcpy( out.type, "whois-reply", IMC_TYPE_LENGTH );
strlcpy( out.from, "*", IMC_NAME_LENGTH );
imc_addkey( &out, "text", data );
imc_send( &out );
imc_freedata( &out );
}
void imc_recv_whois( imc_char_data *from, char *to )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *victim;
char buf[LSS];
/* This circumvents the Validator packets that were used to deter people from being in an invisible
state from IMC2 3.00 to 3.10. They were easily circumvented and so removed from the IMC2 client
code. */
if ( !imc_str_prefix ( "Validator", from->name ) )
{
snprintf( buf, LSS, "imcpfind %s : %s@%s is online.\n\r", to, to, imc_name );
imc_send_whoisreply( from->name, buf );
return;
}
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& IS_IMCVISIBLE(victim)
&& ( !strcasecmp( to, CH_IMCNAME(victim) )
|| ( strlen(to) > 3 && !imc_str_prefix( to, CH_IMCNAME(victim) ) ) ) )
{
snprintf( buf, LSS, "imcpfind %s : %s@%s is online.\n\r", to, CH_IMCNAME(victim), imc_name );
imc_send_whoisreply( from->name, buf );
}
}
}
void imc_recv_whoisreply( char *to, const char *text )
{
DESCRIPTOR_DATA *d;
CHAR_DATA *victim;
for( d = first_descriptor; d; d = d->next )
{
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& !strcasecmp( (char *)to, CH_IMCNAME(victim)) )
{
imc_to_char( color_itom(text), victim );
return;
}
}
}
void imc_recv_imcpbeep( imc_char_data *from, char *to )
{
DESCRIPTOR_DATA *d ;
CHAR_DATA *victim = 0 ;
IMC_BLACKLIST *entry ;
char buf[IMC_DATA_LENGTH] ;
if( !strcasecmp( to, "*" ) ) /* drop messages to system */
return;
for( d = first_descriptor; d; d = d->next )
if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL
&& IS_IMCVISIBLE(victim) && !strcasecmp( to, CH_IMCNAME(victim) ))
break;
if(!d)
{
snprintf( buf, IMC_DATA_LENGTH, "%s is not here.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
if( !function_usable(victim, DENY_IMCPBEEP, ALLOW_IMCPBEEP, PERM_PLAYER)
|| IS_SET(IMC_PFLAGS(victim), PSET_IMCPBEEP))
{
if( strcasecmp( imc_nameof( from->name ), "*" ) )
{
snprintf( buf, IMC_DATA_LENGTH, "%s is not receiving beeps.", to );
imc_send_tell( NULL, from->name, buf, 1 );
}
return;
}
for( entry = FIRST_IMCBLACKLIST(victim); entry; entry = entry->next )
if( !strcasecmp( from->name, entry->name ) || !strcasecmp( imc_mudof(from->name), entry->name))
break;
if (entry)
{
snprintf( buf, IMC_DATA_LENGTH, "%s has blacklisted you.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
if ( IS_SET(IMC_PFLAGS(victim), PSET_IMCPAFK) )
{
snprintf( buf, IMC_DATA_LENGTH, "%s is AFK.", to );
imc_send_tell( NULL, from->name, buf, 1 );
return;
}
/* always display the true name here */
snprintf( buf, IMC_DATA_LENGTH, color_itom("~c\a%s imcpbeeps you.\n\r"), from->name );
imc_to_char( buf, victim );
return;
}
/* called when a keepalive has been received */
void imc_recv_keepalive( char *from, const char *version, const char *networkname )
{
REMOTEINFO *p;
if( !strcasecmp( from, imc_name ) )
return;
/* this should never fail, imc.c should create an entry if one doesn't exist (in the path update code) */
p = imc_find_reminfo( from, 0 );
if( !p ) /* boggle */
return;
if( strcasecmp( version, p->version ) ) /* remote version has changed? */
{
IMCSTRFREE( p->version ); /* if so, update it */
p->version = IMCSTRALLOC( version );
}
if( strcasecmp( networkname, p->netname ) )
{
IMCSTRFREE( p->netname );
p->netname = IMCSTRALLOC( networkname );
}
return;
}
/* send a traceroute request */
void imc_send_traceroute( const char *to, const char *playername )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
imc_initdata( &out );
strlcpy( out.type, "ping", IMC_TYPE_LENGTH );
strlcpy( out.from, playername, IMC_NAME_LENGTH );
strlcpy( out.to, "*@", IMC_MNAME_LENGTH - 2 );
strlcpy( out.to+2, to, IMC_MNAME_LENGTH-2 );
imc_send( &out );
imc_freedata( &out );
}
/* send a traceroute packet back */
void imc_send_traceroutereply( char *to, char *path )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
imc_initdata( &out );
strlcpy( out.type, "ping-reply", IMC_TYPE_LENGTH );
strlcpy( out.from, "*", IMC_NAME_LENGTH );
strlcpy( out.to, to, IMC_MNAME_LENGTH );
imc_addkey( &out, "path", path );
imc_send( &out );
imc_freedata( &out );
}
/* called when a traceroute is received */
void imc_recv_traceroute( char *from, char *path )
{
/* return the traceroute */
imc_send_traceroutereply( from, path );
}
/* Changes a route's ! to -> for a more understandable display */
char *display_route( const char *route)
{
static char buf[LSS];
int i = 0;
if (!route || *route == '\0')
return NULL;
while( i < LSS - 1 && *route != '\0' )
{
if ( *route == '!' )
{
buf[i++] = '-';
buf[i++] = '>';
route++;
}
else
buf[i++] = *route++;
}
buf[i] = '\0';
return buf;
}
/* called when a traceroute reply is received */
void imc_recv_traceroutereply( char *from, char *to, const char *pathto, char *pathfrom )
{
char incoming[LSS], outgoing[LSS];
DESCRIPTOR_DATA *d = NULL;
CHAR_DATA *ch = NULL;
for( d = first_descriptor; d; d = d->next )
if( d->connected == CON_PLAYING && (ch = d->original ? d->original : d->character) != NULL
&& ( !strcasecmp( CH_IMCNAME(ch), pinger) || !strcasecmp( CH_IMCNAME(ch), to ) ) )
break;
pinger[0] = '\0';
if(!d)
return;
if ( pathfrom && pathfrom[0] != '\0' )
snprintf( incoming, LSS, "%s->%s", display_route(pathfrom), imc_name );
if ( pathto && pathto[0] != '\0' )
snprintf( outgoing, LSS, "%s->%s", display_route(pathto), from );
imc_printf( ch, color_itom(
"~WTraceroute Information for %s\n\r"
"~cSend Path ~W: ~C%s\n\r"
"~cReturn Path ~W: ~C%s\n\r\n\r"),
from, outgoing, incoming );
return;
}
/* send a keepalive to everyone */
void imc_send_keepalive( void )
{
PACKET out;
if( !this_imcmud || this_imcmud->state < CONN_COMPLETE )
return;
imc_initdata( &out );
strlcpy( out.type, "is-alive", IMC_TYPE_LENGTH );
strlcpy( out.from, "*", IMC_NAME_LENGTH );
strlcpy( out.to, "*@*", IMC_NAME_LENGTH );
imc_addkey( &out, "versionid", imc_versionid );
imc_addkey( &out, "networkname", imc_siteinfo.netname );
imc_send( &out );
imc_freedata( &out );
}
void getdata( PACKET *p, imc_char_data *d )
{
strlcpy( d->name, p->from, IMC_NAME_LENGTH );
d->perm_level = imc_getkeyi( p, "level", 0 );
return;
}
/* Commands called by the interface layer */
/* handle a packet destined for us, or a broadcast */
void imc_recv( PACKET *p )
{
imc_char_data d;
int bcast;
REMOTEINFO *i;
bcast = !strcasecmp( imc_mudof( p->i.to ), "*" ) ? 1 : 0;
getdata( p, &d );
i = imc_find_reminfo( imc_mudof( p->from ), 0 );
if( i )
{
if( i->path )
IMCSTRFREE( i->path );
i->path = IMCSTRALLOC( p->i.path );
i->expired = FALSE;
}
/* chat: message to a channel (broadcast) */
if( !strcasecmp( p->type, "chat" ) && !imc_isblacklisted( p->from ) )
imc_recv_chat( &d, imc_getkeyi( p, "channel", 0 ), imc_getkey( p, "text", "" ) );
/* emote: emote to a channel (broadcast) */
else if( !strcasecmp( p->type, "emote" ) && !imc_isblacklisted( p->from ) )
imc_recv_emote( &d, imc_getkeyi( p, "channel", 0 ), imc_getkey( p, "text", "" ) );
/* tell: tell a player here something */
else if( !strcasecmp( p->type, "tell" ) )
{
if( imc_isblacklisted( p->from ) )
imc_sendblacklistresponse( p->from );
else
imc_recv_imcptell( &d, p->to, imc_getkey( p, "text", "" ), imc_getkeyi( p, "isreply", 0 ) );
}
/* who-reply: receive a who response */
else if( !strcasecmp( p->type, "who-reply" ) || !strcasecmp( p->type, "wHo-reply" ) )
imc_recv_whoreply( p->to, imc_getkey( p, "text", "" ), imc_getkeyi( p, "sequence", -1 ), -1 );
/* who: receive a who request */
else if( !strcasecmp( p->type, "who" ) || !strcasecmp( p->type, "wHo" ) )
{
if( imc_isblacklisted( p->from ) )
imc_sendblacklistresponse( p->from );
else
imc_recv_who( &d, imc_getkey( p, "type", "who" ) );
}
/* whois-reply: receive a whois response */
else if( !strcasecmp( p->type, "whois-reply" ) )
imc_recv_whoisreply( p->to, imc_getkey( p, "text", "" ) );
/* whois: receive a whois request */
else if( !strcasecmp( p->type, "whois" ) )
imc_recv_whois( &d, p->to );
/* beep: beep a player */
else if( !strcasecmp( p->type, "beep" ) )
{
if( imc_isblacklisted( p->from ) )
imc_sendblacklistresponse( p->from );
else
imc_recv_imcpbeep( &d, p->to );
}
/* is-alive: receive a keepalive (broadcast) */
else if( !strcasecmp( p->type, "is-alive" ) )
imc_recv_keepalive( imc_mudof( p->from ), imc_getkey( p, "versionid", "unknown" ),
imc_getkey( p, "networkname", "unknown") );
/* traceroute: receive a send path */
else if( !strcasecmp( p->type, "ping" ) )
imc_recv_traceroute( p->from, p->i.path );
/* traceroute-reply: receive a send path and a return path */
else if( !strcasecmp( p->type, "ping-reply" ) )
imc_recv_traceroutereply( imc_mudof( p->from ), imc_nameof(p->to), imc_getkey( p, "path", NULL ),
p->i.path );
/* handle keepalive requests - shogar */
else if( !strcasecmp( p->type, "keepalive-request" ) )
imc_send_keepalive();
/* expire closed hubs - shogar */
else if( !strcasecmp( p->type, "close-notify" ) )
{
REMOTEINFO *r;
char fake[90];
r = imc_find_reminfo( imc_getkey( p, "host", "unknown" ), 0 );
if( r )
{
r->expired = TRUE;
for( r = first_rinfo; r; r = r->next )
{
char *sf;
snprintf( fake, 90, "!%s", imc_getkey( p, "host", "___unknown " ) );
if( r->name && r->path && ( sf = strstr( r->path, fake ) ) && sf
&& ( *( sf + strlen( fake ) ) == '!' || *( sf + strlen( fake ) ) == 0 ) )
{
r->expired = TRUE;
imc_send_traceroute( r->name, "*");
}
}
}
}
/* call catch-all fn if present */
else
{
PACKET out;
if( imc_recv_hook )
if( (*imc_recv_hook)( p, bcast ) )
return;
if( bcast || !strcasecmp( p->type, "reject" ) )
return;
/* reject packet */
strlcpy( out.type, "reject", IMC_TYPE_LENGTH );
strlcpy( out.to, p->from, IMC_NAME_LENGTH );
strlcpy( out.from, p->to, IMC_NAME_LENGTH );
imc_clonedata( p, &out );
imc_addkey( &out, "old-type", p->type );
imc_send( &out );
imc_freedata( &out );
}
}
/* send a packet to a mud using the right version */
void do_send_packet( PACKET *p )
{
const char *output;
output = ( *imc_vinfo[IMC_VERSION].generate )( p );
if( output )
{
imc_stats.tx_pkts++;
if( strlen( output ) > imc_stats.max_pkt )
imc_stats.max_pkt = strlen( output );
do_imcsend( output );
}
}
void imc_send( PACKET *p )
{
if( !this_imcmud || this_imcmud->state == CONN_NONE )
{
imcbug( "%s", "imc_send when not active!" );
return;
}
/* initialize packet fields that the caller shouldn't/doesn't set */
p->i.path[0] = '\0';
p->i.sequence = imc_sequencenumber++;
if( !imc_sequencenumber )
imc_sequencenumber++;
strlcpy( p->i.to, p->to, IMC_NAME_LENGTH );
strlcpy( p->i.from, p->from, IMC_NAME_LENGTH );
strlcat( p->i.from, "@", IMC_NAME_LENGTH - strlen(p->i.from ) );
strlcpy( p->i.from + strlen( p->i.from ), imc_name, IMC_NAME_LENGTH - strlen( p->i.from ) );
do_send_packet( p );
}
/* 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->netname );
imc_cancel_event( NULL, p );
IMCDISPOSE( p );
}
/* send a keepalive, and queue the next keepalive event */
void ev_keepalive( void *data )
{
imc_send_keepalive();
}
/* send a keepalive request to everyone - shogar */
void imc_request_keepalive( void )
{
PACKET out;
imc_initdata( &out );
strlcpy( out.type, "keepalive-request", IMC_TYPE_LENGTH );
strlcpy( out.from, "*", IMC_TYPE_LENGTH );
strlcpy( out.to, "*@*", IMC_TYPE_LENGTH );
imc_addkey( &out, "versionid", imc_versionid );
imc_send( &out );
imc_freedata( &out );
}
void ev_request_keepalive( void *data )
{
imc_request_keepalive();
}
/* put a line onto descriptors output buffer */
void do_imcsend( const char *line )
{
int len;
char *newbuf;
int newsize = this_imcmud->outsize;
if( this_imcmud->state == CONN_NONE )
return;
if( !this_imcmud->outbuf[0] )
this_imcmud->newoutput = 1;
len = strlen( this_imcmud->outbuf ) + strlen( line ) + 3;
if( len > this_imcmud->outsize )
{
while( newsize < len )
newsize *= 2;
IMCCREATE( newbuf, char, newsize );
strlcpy( newbuf, this_imcmud->outbuf, newsize );
IMCDISPOSE( this_imcmud->outbuf );
this_imcmud->outbuf = newbuf;
this_imcmud->outsize = newsize;
}
if( len < this_imcmud->outsize/2 && len >= IMC_MINBUF )
{
newsize = this_imcmud->outsize/2;
IMCCREATE( newbuf, char, newsize );
strlcpy( newbuf, this_imcmud->outbuf, newsize );
IMCDISPOSE( this_imcmud->outbuf );
this_imcmud->outbuf = newbuf;
this_imcmud->outsize = newsize;
}
strlcat( this_imcmud->outbuf, line, newsize );
if ( imc_debug_on )
imclog(line);
strlcat( this_imcmud->outbuf, "\n\r", newsize );
}
/* read waiting data from descriptor.
* read to a temp buffer to avoid repeated allocations
*/
void do_imcread( void )
{
int size;
int r;
char temp[IMC_MAXBUF];
char *newbuf;
int newsize = 0;
r = read( this_imcmud->desc, temp, IMC_MAXBUF-1 );
if( !r || ( r < 0 && errno != EAGAIN && errno != EWOULDBLOCK ) )
{
if( r < 0 ) /* read error */
imclog( "Read error on connection to %s", this_imcmud->hubname );
else /* socket was closed */
imclog( "EOF encountered on connection to %s", this_imcmud->hubname );
imc_shutdown( TRUE );
return;
}
if( r < 0 ) /* EAGAIN error */
return;
temp[r] = '\0';
size = strlen( this_imcmud->inbuf ) + r + 1;
if( size >= this_imcmud->insize )
{
newsize = this_imcmud->insize;
while( newsize < size )
newsize *= 2;
IMCCREATE( newbuf, char, newsize );
strlcpy( newbuf, this_imcmud->inbuf, newsize );
IMCDISPOSE( this_imcmud->inbuf );
this_imcmud->inbuf = newbuf;
this_imcmud->insize = newsize;
}
if( size < this_imcmud->insize/2 && size >= IMC_MINBUF )
{
newsize = this_imcmud->insize;
newsize /= 2;
IMCCREATE( newbuf, char, newsize );
strlcpy( newbuf, this_imcmud->inbuf, newsize );
IMCDISPOSE( this_imcmud->inbuf );
this_imcmud->inbuf = newbuf;
this_imcmud->insize = newsize;
}
strlcat( this_imcmud->inbuf, temp, this_imcmud->insize );
if (imc_debug_on)
imclog(temp);
imc_stats.rx_bytes += r;
}
/* write to descriptor */
void do_imcwrite( void )
{
int size, w;
if( this_imcmud->state == CONN_SENDCLIENTPWD )
{
/* Wait for server password */
this_imcmud->state = CONN_WAITSERVERPWD;
return;
}
size = strlen( this_imcmud->outbuf );
if( !size ) /* nothing to write */
return;
w = write( this_imcmud->desc, this_imcmud->outbuf, size );
if( !w || ( w < 0 && errno != EAGAIN && errno != EWOULDBLOCK ) )
{
if( w < 0 ) /* write error */
imcbug( "Write error on connection to %s", this_imcmud->hubname );
else /* socket was closed */
imcbug( "EOF encountered on connection to %s", this_imcmud->hubname );
imc_shutdown( TRUE );
return;
}
if( w < 0 ) /* EAGAIN */
return;
/* throw away data we wrote */
strlcpy( this_imcmud->outbuf, this_imcmud->outbuf + w, this_imcmud->outsize );
imc_stats.tx_bytes += w;
}
/* try to read a line from the input buffer, NULL if none ready
* all lines are \n\r terminated in theory, but take other combinations
*/
char *imcgetline( char *buffer )
{
int i;
static char buf[IMC_PACKET_LENGTH];
/* copy until \n, \r, end of buffer, or out of space */
for( i = 0; buffer[i] && buffer[i] != '\n' && buffer[i] != '\r' && i+1 < IMC_PACKET_LENGTH; i++ )
buf[i] = buffer[i];
/* end of buffer and we haven't hit the maximum line length */
if( !buffer[i] && i+1 < IMC_PACKET_LENGTH )
{
buf[0] = '\0';
return NULL; /* so no line available */
}
/* terminate return string */
buf[i] = '\0';
/* strip off extra control codes */
while( buffer[i] && ( buffer[i] == '\n' || buffer[i] == '\r' ) )
i++;
/* remove the line from the input buffer */
strlcpy( buffer, buffer+i, this_imcmud->insize );
return buf;
}
/* handle a password response from a server - for connection negotiation. DON'T REMOVE THIS */
void serverpassword( const char *argument )
{
char arg1[3], name[IMC_MNAME_LENGTH], pw[IMC_PW_LENGTH], version[20];
int rversion;
argument = imc_getarg( argument, arg1, 4 ); /* has to be PW */
argument = imc_getarg( argument, name, IMC_MNAME_LENGTH );
argument = imc_getarg( argument, pw, IMC_PW_LENGTH );
argument = imc_getarg( argument, version, 20 );
if( strcasecmp( arg1, "PW" ) )
{
imclog( "%s: non-PW password packet", this_imcmud->hubname );
imc_shutdown( FALSE );
return;
}
if( !this_imcmud || strcasecmp( this_imcmud->serverpw, pw ) )
{
imclog( "%s", "Password failure for hub!" );
imc_shutdown( FALSE );
return;
}
this_imcmud->state = CONN_COMPLETE;
/* check for a version string (assume version 0 if not present) */
if( sscanf( version, "version=%d", &rversion ) != 1 )
rversion = 0;
/* check for generator/interpreter */
if( !imc_vinfo[rversion].generate || !imc_vinfo[rversion].interpret )
{
imclog( "%s: Unsupported version %d", this_imcmud->hubname, rversion );
imc_shutdown( FALSE );
return;
}
imclog( "%s: Connected (version %d)", this_imcmud->hubname, rversion );
}
/* start up IMC */
bool imc_startup_network( void )
{
imclog( "%s", "IMC2 Network Initializing" );
imc_stats.start = imc_now;
imc_stats.rx_pkts = 0;
imc_stats.tx_pkts = 0;
imc_stats.rx_bytes = 0;
imc_stats.tx_bytes = 0;
imc_stats.sequence_drops = 0;
/* Connect to Hub */
if( !imc_connect_to() )
return FALSE;
imc_add_event( 20, ev_keepalive, NULL, 1 );
/* fill my imcmlist please - shogar */
imc_add_event( 30, ev_request_keepalive, NULL, 1 );
return TRUE;
}
void imc_freechardata( CHAR_DATA *ch )
{
if( IS_NPC(ch) )
return;
if( IMC_CSUBSCRIBED(ch) )
IMCSTRFREE( IMC_CSUBSCRIBED(ch) );
if( IMC_RREPLY(ch) )
IMCSTRFREE( IMC_RREPLY(ch) );
#ifndef IMCCIRCLE
IMCDISPOSE( CH_IMCDATA(ch) );
#endif
return;
}
void imc_initchar( CHAR_DATA *ch )
{
if( IS_NPC(ch) )
return;
#if !defined (IMCCIRCLE)
IMCCREATE( ch->pcdata->imcchardata, IMC_CHARDATA, 1 );
#endif
IMC_CSUBSCRIBED(ch) = NULL;
IMC_RREPLY(ch) = NULL;
IMC_PFLAGS(ch) = 0;
FIRST_IMCBLACKLIST(ch) = NULL;
LAST_IMCBLACKLIST(ch) = NULL;
return;
}
/*
* Read a string from file fp using IMCSTRALLOC [Taken from Smaug's fread_string]
*/
const char *imcfread_string( FILE *fp )
{
char buf[LSS];
char *plast;
char c;
int ln;
plast = buf;
buf[0] = '\0';
ln = 0;
/*
* Skip blanks.
* Read first char.
*/
do
{
if ( feof(fp) )
{
imcbug( "%s", "imcfread_string: EOF encountered on read." );
return IMCSTRALLOC( "" );
}
c = getc( fp );
}
while ( isspace(c) );
if ( ( *plast++ = c ) == '~' )
return IMCSTRALLOC( "" );
for ( ;; )
{
if ( ln >= (LSS - 1) )
{
imcbug( "%s", "imcfread_string: string too long" );
*plast = '\0';
return IMCSTRALLOC( buf );
}
switch ( *plast = getc( fp ) )
{
default:
plast++; ln++;
break;
case EOF:
imcbug( "%s", "imcfread_string: EOF" );
*plast = '\0';
return IMCSTRALLOC( buf );
break;
case '\n':
plast++; ln++;
*plast++ = '\r'; ln++;
break;
case '\r':
break;
case '~':
*plast = '\0';
return IMCSTRALLOC( buf );
}
}
}
/*
* 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 )
{
static char line[LSS];
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." );
strlcpy( line, "", LSS );
return 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 line;
}
c = getc( fp );
*pline++ = c; ln++;
if ( ln >= (LSS - 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 = '\0';
return line;
}
/*
* Read one word (into static buffer). [Taken from Smaug's fread_word]
*/
char *imcfread_word( FILE *fp )
{
static char word[MSS];
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 + MSS; 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 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;
}
/*
* 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;
}
bool imc_loadchar( CHAR_DATA *ch, FILE *fp, char *word )
{
if( IS_NPC(ch) )
return FALSE;
if ( feof(fp))
return FALSE;
if( !strcasecmp( word, "IMCPFlags" ) )
{
IMC_PFLAGS(ch) = imcfread_number(fp);
return TRUE;
}
if ( !strcasecmp( word, "IMCCSubscribed"))
{
IMC_CSUBSCRIBED(ch) = imcfread_string(fp);
return TRUE;
}
if( !strcasecmp( word, "IMCBlacklist" ) )
{
IMC_BLACKLIST *temp;
IMCCREATE( temp, IMC_BLACKLIST, 1 );
temp->name = imcfread_string( fp );
IMCLINK( temp, FIRST_IMCBLACKLIST(ch), LAST_IMCBLACKLIST(ch), next, prev );
return TRUE;
}
return FALSE;
}
void imc_savechar( CHAR_DATA *ch, FILE *fp )
{
IMC_BLACKLIST *temp;
if( IS_NPC(ch) )
return;
if( IMC_PFLAGS(ch) )
fprintf( fp, "IMCPFlags %ld\n", IMC_PFLAGS(ch) );
if( IMC_CSUBSCRIBED(ch) )
fprintf( fp, "IMCCSubscribed %s~\n", IMC_CSUBSCRIBED(ch) );
for( temp = FIRST_IMCBLACKLIST(ch); temp; temp = temp->next )
fprintf( fp, "IMCBlacklist %s~\n", temp->name );
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':
KEY( "Autoconnect", this_imcmud->autoconnect, imcfread_number( fin ) );
KEY( "AdminLevel", imc_minadminlevel, imcfread_number( fin ) );
break;
case 'C':
KEY( "ClientPwd", this_imcmud->clientpw, imcfread_string( fin ) );
break;
case 'D':
KEY( "Debug", imc_debug_on, imcfread_number( fin ) );
break;
case 'E':
if( !strcasecmp( word, "End" ) )
{
return;
}
break;
case 'H':
KEY( "HubAddr", this_imcmud->host, imcfread_string( fin ) );
KEY( "HubName", this_imcmud->hubname, imcfread_string( fin ) );
KEY( "HubPort", this_imcmud->port, imcfread_number( fin ) );
break;
case 'I':
KEY( "InfoName", imc_siteinfo.name, imcfread_string( fin ) );
KEY( "InfoHost", imc_siteinfo.host, imcfread_string( fin ) );
KEY( "InfoPort", imc_siteinfo.port, imcfread_number( fin ) );
KEY( "InfoEmail", imc_siteinfo.email, imcfread_string( fin ) );
KEY( "InfoWWW", imc_siteinfo.www, imcfread_string( fin ) );
KEY( "InfoDetails", imc_siteinfo.details, imcfread_string( fin ) );
KEY( "InfoBase", imc_siteinfo.base, imcfread_string( fin ) );
break;
case 'L':
KEY( "LocalName", imc_name, imcfread_string( fin ) );
break;
case 'M':
KEY( "MinPlayerLevel", imc_minplayerlevel, imcfread_number( fin ) );
KEY( "MinImmLevel", imc_minimmlevel, imcfread_number( fin ) );
break;
case 'N':
KEY( "NetworkName", imc_siteinfo.netname, imcfread_string( fin ) );
case 'S':
KEY( "ServerPwd", this_imcmud->serverpw, imcfread_string( fin ) );
break;
case 'W':
KEY( "WhoStyle", imc_whostyle, imcfread_number( fin ) );
break;
}
if( !fMatch )
imcbug( "imcfread_config_file: Bad keyword: %s\n\r", word );
}
}
/* There should only one of these..... */
void imc_delete_info( void )
{
if( this_imcmud->hubname )
IMCSTRFREE( this_imcmud->hubname );
if( this_imcmud->host )
IMCSTRFREE( this_imcmud->host );
if( this_imcmud->clientpw )
IMCSTRFREE( this_imcmud->clientpw );
if( this_imcmud->serverpw )
IMCSTRFREE( this_imcmud->serverpw );
imc_cancel_event( NULL, this_imcmud );
IMCDISPOSE( this_imcmud );
}
bool imc_read_config( void )
{
FILE *fin;
char cbase[MSS];
if( this_imcmud != NULL )
imc_delete_info();
this_imcmud = NULL;
imclog( "%s", "Loading IMC2 network data..." );
if( ( fin = dfopen(IMC_PATH, IMC_CONFIG_FILE, "r" ) ) == NULL )
{
imclog( "%s", "Can't open configuration file" );
imclog( "%s", "Network configuration aborted." );
return FALSE;
}
for( ; ; )
{
char letter;
char *word;
letter = imcfread_letter( fin );
if( letter == '#' )
{
imcfread_to_eol( fin );
continue;
}
if( letter != '$' )
{
imcbug( "%s", "imc_read_confiShowing the first 100 KB of 202.9 KB. Download the archive for the full file.