imc.c

src/imc.c · 222.5 KB

From the archive ack.zip

Download the archive   « Back to the archive contents

/*
 * IMC2 AntiFreeze Client - Developed by Mud Domain.
 * This code is certified as 100% LGPL compatible.
 * All aspects have been verified against the core components
 * contained in Oliver's relicensed code. No components licensed
 * under the GPL remain.
 *
 * All additions Copyright (C)2002-2004 Roger Libiez ( Samson )
 * Contributions by Xorith, Copyright (C)2004
 * Contributions by Orion Elder, Copyright (C)2003
 * Comments and suggestions welcome: imc@imc2.org
 * Registered with the United States Copyright Office: TX 5-962-561
 *
 * We would like to thank the following people for their hard work on
 * versions 1.00 Gold through 3.00 Mud-Net even though 
 * their contributions no longer remain:
 *
 * Scion Altera, Shogar, Kratas, Tagith, Noplex,
 * Senir, Trax and Ntanel StormBlade.
 *
 * This work is derived from:
 * IMC2 version 0.10 - an inter-mud communications protocol
 * Copyright (C) 1996 & 1997 Oliver Jowett <oliver@randomly.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/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>
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <sys/types.h>
#endif
#include "md5.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"
   time_t current_time;
#endif

#if defined(IMCSMAUG) || defined(IMCCHRONICLES)
   #include "mud.h"
#ifdef IMCCHRONICLES
   #include "factions.h"
   #include "files.h"
#endif
#endif
#if defined(IMCROM) || defined(IMCMERC) || defined(IMCUENVY) || defined(IMC1STMUD)
   #include "merc.h"
   #ifdef IMCROM
      #include "tables.h"
   #endif
#endif
#ifdef IMCACK
   #include "ack.h"
#endif

#define IMCKEY( literal, field, value ) \
if( !strcasecmp( word, literal ) )      \
{                                       \
   field = value;                       \
   fMatch = TRUE;                       \
   break;                               \
}

static char *wr_to;
static char wr_buf[IMC_DATA_LENGTH];
static int wr_sequence;
int imc_active;                     /* Connection state */
time_t imc_now;                     /* current time */
time_t imc_boot;                    /* start time */
int imcwait;                        /* Reconnect timer */
int imcconnect_attempts;            /* How many times have we tried to reconnect? */
unsigned long imc_sequencenumber;	/* sequence# for outgoing packets */
bool imcpacketdebug = FALSE;
time_t imcucache_clock;             /* prune ucache stuff regularly */
static char lastping[IMC_MNAME_LENGTH];
static char pinger[100];
char *IMC_VERSIONID;

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;

imc_statistics imc_stats;

_imc_memory imc_memory[IMC_MEMORY]; /* sequence memory */
static int memory_head; /* next entry in memory table to use, wrapping */

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 ) ) );
char *imc_funcname( IMC_FUN *func );
IMC_FUN *imc_function( const char *func );
char *imc_send_social( CHAR_DATA *ch, char *argument, int telloption );
void imc_save_config( void );
void imc_save_channels( void );

char *const imcperm_names[] =
{
   "Notset", "None", "Mort", "Imm", "Admin", "Imp"
};

static struct 
{
    int number;
    const char *name;
    const char *chatstr;
    const char *emotestr;
    int perm;
    char *to;
} imc_channels[]=
{
    {
        2,
        "imcinfo",
        /* why didn't I think of this? :) */
        "~p[~GIMCinfo~p] %s announces '%s'\n\r",
        "~p[~GIMCinfo~p] %s %s\n\r",
        IMCPERM_IMM,
        "*",
    },

    {
        15,
        "imc",
        "~p[~GIMC~p] %s %s\n\r",
        "~p[~GIMC~p] %s %s\n\r",
        IMCPERM_IMM,
        "*"
    }
};

#define numchannels ( sizeof( imc_channels ) / sizeof( imc_channels[0] ) )
/*******************************************
 * 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 */
}

char *imc_crypt( const char *pwd )
{
   md5_state_t state;
   md5_byte_t digest[16];
   static char passwd[16];
   unsigned int x;

   md5_init( &state );
   md5_append( &state, (const md5_byte_t *)pwd, strlen( pwd ) );
   md5_finish( &state, digest );

   imcstrlcpy( passwd, (const char *)digest, 16 );

   /* The listed exceptions below will fubar the MD5 authentication packets, so change them */
   for( x = 0; x < strlen( passwd ); x++ )
   {
      if( passwd[x] == '\n' )
         passwd[x] = 'n';
      if( passwd[x] == '\r' )
         passwd[x] = 'r';
      if( passwd[x] == '\t' )
         passwd[x] = 't';
      if( passwd[x] == ' ' )
         passwd[x] = 's';
      if( (int)passwd[x] == 11 )
         passwd[x] = 'x';
      if( (int)passwd[x] == 12 )
         passwd[x] = 'X';
   }
   return( passwd );
}

/* 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];
   va_list ap;

   va_start( ap, format );
   vsnprintf( buf, LGST, format, ap );
   va_end( ap );

   snprintf( buf2, LGST, "IMC: %s", buf );

#ifndef IMCCIRCLE
   log_string( buf2 );
#else
   basic_mud_log( "%s", buf2 );
#endif
   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];
   va_list ap;

   va_start( ap, format );
   vsnprintf( buf, LGST, format, ap );
   va_end( ap );

   snprintf( buf2, LGST, "IMC: %s", buf );

#if defined(IMCSMAUG) || defined(IMCCHRONICLES)
   bug( "%s", buf2 );
#elif defined(IMCCIRCLE)
   basic_mud_log( "%s", buf2 );
#else
   bug( buf2, 0 );
#endif
   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[IMC_DATA_LENGTH];
   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 == ( IMC_DATA_LENGTH - 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 == ( IMC_DATA_LENGTH - 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;

   while( isspace(*argument) )
      argument++;

   cEnd = ' ';
   if( *argument == '\'' || *argument == '"' )
      cEnd = *argument++;

   while( *argument != '\0' || ++count >= 255 )
   {
      if( *argument == cEnd )
	{
         argument++;
	   break;
      }
      *arg_first = (*argument);
      arg_first++;
      argument++;
   }
   *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[IMC_DATA_LENGTH];

   imcstrlcpy( tbuf, txt, IMC_DATA_LENGTH );
   for( color = first_imc_color; color; color = color->next )
      imcstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, "" ), IMC_DATA_LENGTH );

   for( color = first_imc_color; color; color = color->next )
      imcstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, "" ), IMC_DATA_LENGTH );
   return tbuf;
}

/* Now tell me this isn't cleaner than the mess that was here before. -- Xorith */
/* Yes, Xorith it is. Now, how about this update? Much less hassle with no hardcoded table! -- Samson */
/* convert from imc color -> mud color */
char *color_itom( const char *txt, CHAR_DATA *ch )
{
   IMC_COLOR *color;
   static char tbuf[IMC_DATA_LENGTH];

   if( !txt || *txt == '\0' )
      return "";

   if( IMCIS_SET( IMCFLAG(ch), IMC_COLORFLAG ) )
   {
      imcstrlcpy( tbuf, txt, IMC_DATA_LENGTH );
      for( color = first_imc_color; color; color = color->next )
         imcstrlcpy( tbuf, imcstrrep( tbuf, color->imctag, color->mudtag ), IMC_DATA_LENGTH );
   }
   else
      imcstrlcpy( tbuf, imc_strip_colors( txt ), IMC_DATA_LENGTH );

   return tbuf;
}

/* convert from mud color -> imc color */
char *color_mtoi( const char *txt )
{
   IMC_COLOR *color;
   static char tbuf[IMC_DATA_LENGTH];

   if( !txt || *txt == '\0' )
      return "";

   imcstrlcpy( tbuf, txt, IMC_DATA_LENGTH );
   for( color = first_imc_color; color; color = color->next )
      imcstrlcpy( tbuf, imcstrrep( tbuf, color->mudtag, color->imctag ), IMC_DATA_LENGTH );

   return tbuf;
}

/* Generic send_to_char type function to send to the proper code for each codebase */
void imc_to_char( const char *txt, CHAR_DATA *ch )
{
   char buf[LGST*2];

   snprintf( buf, LGST*2, "%s\033[0m", color_itom( txt, ch ) );
#if defined(IMCSMAUG)
   send_to_char_color( buf, ch );
#elif defined(IMCCIRCLE)
   #if _CIRCLEMUD < CIRCLEMUD_VERSION(3,0,21)
      send_to_char( buf, ch );
   #else
      send_to_char( ch, "%s", buf );
   #endif
#else
   send_to_char( buf, ch );
#endif
   return;
}

/* Modified version of Smaug's ch_printf_color function */
void imc_printf( CHAR_DATA *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, CHAR_DATA *ch )
{
   char buf[LGST*2];

   snprintf( buf, LGST*2, "%s\033[0m", color_itom( txt, ch ) );
#if defined(IMCSMAUG) || defined(IMCCHRONICLES)
   send_to_pager_color( buf, ch );
#elif defined(IMCROM)
   page_to_char( buf, ch );
#elif defined(IMC1STMUD)
   sendpage( ch, buf2 );
#else
   imc_to_char( buf, ch );
#endif
   return;
}

/* Generic pager_printf type function */
void imcpager_printf( CHAR_DATA *ch, const char *fmt, ... )
{
   char buf[LGST];
   va_list args;
 
   va_start( args, fmt );
   vsnprintf( buf, LGST, fmt, args );
   va_end( args );

   imc_to_pager( buf, ch );
   return;
}

/********************************
 * Low level utility functions. *
 ********************************/

bool imcstr_prefix( const char *astr, const char *bstr )
{
   if( !astr )
   {
      imcbug( "Strn_cmp: null astr." );
      return TRUE;
   }

   if( !bstr )
   {
      imcbug( "Strn_cmp: null bstr." );
      return TRUE;
   }

   for( ; *astr; astr++, bstr++ )
   {
      if( LOWER(*astr) != LOWER(*bstr) )
         return TRUE;
   }
   return FALSE;
}

/*
 * Returns an initial-capped string.
 */
char *imccapitalize( const char *str )
{
   static char strcap[LGST];
   int i;

   for( i = 0; str[i] != '\0'; i++ )
	strcap[i] = tolower( str[i] );
   strcap[i] = '\0';
   strcap[0] = toupper( strcap[0] );
   return strcap;
}

/*  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
 */
char *imc_getarg( char *argument, char *arg, int length )
{
   int len = 0;

   if( !argument || argument[0] == '\0' )
   {
      if( arg )
         arg[0] = '\0';

      return argument;
   }

   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';

   return argument;
}

/* Check for a name in a list */
bool imc_hasname( char *list, char *name )
{
   char *p;
   char arg[IMC_NAME_LENGTH];

   if( !list )
      return FALSE;

   p = imc_getarg( list, arg, IMC_NAME_LENGTH );
   while( arg[0] )
   {
      if( !strcasecmp( name, arg ) )
         return TRUE;
      p = imc_getarg( p, arg, IMC_NAME_LENGTH );
   }
   return FALSE;
}

/* Add a name to a list */
void imc_addname( char **list, 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
      imcstrlcpy( buf, name, IMC_DATA_LENGTH );

   IMCSTRFREE( *list );
   *list = IMCSTRALLOC( buf );
}

/* Remove a name from a list */
void imc_removename( char **list, char *name )
{
   char buf[1000];
   char arg[IMC_NAME_LENGTH];
   char *p;
  
   buf[0] = '\0';
   p = imc_getarg( *list, arg, IMC_NAME_LENGTH );
   while( arg[0] )
   {
      if( strcasecmp( arg, name ) )
      {
         if( buf[0] )
	      imcstrlcat( buf, " ", 1000 );
         imcstrlcat( buf, arg, 1000 );
      }
      p = imc_getarg( p, arg, IMC_NAME_LENGTH );
   }
   IMCSTRFREE( *list );
   *list = IMCSTRALLOC( buf );
}

/* escape2: escape " -> \", \ -> \\, CR -> \r, LF -> \n */
char *escape2( 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;
}

/* free all the keys in "p" */
void imc_freedata( PACKET *p )
{
   int i;

   for( i = 0; i < IMC_MAX_KEYS; i++ )
   {
      IMCSTRFREE( p->key[i] );
      IMCSTRFREE( p->value[i] );
   }
   return;
}

/* get the value of "key" from "p"; if it isn't present, return "def" */
char *imc_getkey( PACKET *p, char *key, 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, 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, char *key, 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;
   }
}

/* 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;
      imcstrlcpy( buf + len, data->key[i], IMC_DATA_LENGTH-len-1 );
      imcstrlcat( buf, "=", IMC_DATA_LENGTH );
      len = strlen( buf );

      if( !strchr( data->value[i], ' ' ) )
         imcstrlcpy( temp, escape2( data->value[i] ), IMC_DATA_LENGTH-1 );
      else
      {
         temp[0] = '"';
         imcstrlcpy( temp+1, escape2( data->value[i] ), IMC_DATA_LENGTH-3 );
         imcstrlcat( temp, "\"", IMC_DATA_LENGTH );
      }
      imcstrlcat( temp, " ", IMC_DATA_LENGTH );
      imcstrlcpy( 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++;
   }
}

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] )
      imcstrlcpy( newpath, this_imcmud->localname, IMC_PATH_LENGTH );
   else
      snprintf( newpath, IMC_PATH_LENGTH, "%s!%s", p->i.path, this_imcmud->localname );

   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( 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" );
      imcbug( "Type: %s From: %s To: %s", out.type, out.i.from, out.i.to );
      imcbug( "Path: %s Seq: %s", out.i.path, seq );
      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 }
};

/* 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 */
   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 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];

   imcstrlcpy( tempn, name, IMC_MNAME_LENGTH );
   imcstrlcpy( tempp, path, IMC_PATH_LENGTH );

   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;
}

/* return 'e' from 'a!b!c!d!e' */
char *imc_lastinpath( char *path )
{
   const char *where;
   static char buf[IMC_NAME_LENGTH];

   where = path + strlen(path)-1;
   while( *where != '!' && where >= path )
      where--;

   imcstrlcpy( buf, where+1, IMC_NAME_LENGTH );
   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;
}

/* convert from the char data in 'p' to an internal representation in 'd' */
void getdata( PACKET *p, imc_char_data *d )
{
   imcstrlcpy( d->name, p->from, IMC_NAME_LENGTH );
   d->level = imc_getkeyi( p, "level", 0 );
}

/* convert back from 'd' to 'p' */
void setdata( PACKET *p, imc_char_data *d )
{
   imc_initdata( p );

   if( !d )
   {
      imcstrlcpy( p->from, "*", IMC_NAME_LENGTH );
      imc_addkeyi( p, "level", -1 );
      return;
   }

   imcstrlcpy( p->from, d->name, IMC_NAME_LENGTH );
   imc_addkeyi( p, "level", d->level );
}

imc_char_data *imc_getdata( CHAR_DATA *ch )
{
   imc_char_data *d;

   IMCCREATE( d, imc_char_data, 1 );
   if( !ch ) /* fake system character */
   {
      d->level = IMCPERM_NONE;
      imcstrlcpy( d->name, "*", IMC_NAME_LENGTH );
      return d;
   }

   d->level = IMCPERM(ch);
   imcstrlcpy( d->name, CH_IMCNAME(ch), IMC_NAME_LENGTH );

   return d;
}

/*
 * Returns a CHAR_DATA structure which matches the string
 */
CHAR_DATA *imc_find_user( char *name ) 
{
   DESCRIPTOR_DATA *d;
   CHAR_DATA *vch = NULL;

   for ( d = first_descriptor; d; d = d->next ) 
   {
	if( ( vch = d->character ? d->character : d->original ) != NULL && !strcasecmp( CH_IMCNAME(vch), name )
         && d->connected == CON_PLAYING ) 
	   return vch;
    }
    return NULL;
}

/* return 'player' from 'player@mud' */
char *imc_nameof( 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 'mud' from 'player@mud' */
char *imc_mudof( char *fullname )
{
   static char buf[IMC_MNAME_LENGTH];
   char *where;

   where = strchr( fullname, '@' );
   if( !where )
      imcstrlcpy( buf, fullname, IMC_MNAME_LENGTH );
   else
      imcstrlcpy( buf, where+1, IMC_MNAME_LENGTH );

   return buf;
}

char *channel_mudof( 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( char *player, char *mud, bool social )
{
   static char buf[IMC_NAME_LENGTH];

   imcstrlcpy( buf, player, IMC_PNAME_LENGTH );

   /* The social code gets wierd if "mud" isn't specified, so bypass it if it's not */
   if( mud && mud[0] != '\0' && !social )
   {
      imcstrlcat( buf, "@", IMC_NAME_LENGTH );
      imcstrlcpy( buf + strlen(buf), mud, IMC_MNAME_LENGTH );
   }
   return buf;
}

char *getname( CHAR_DATA *ch, imc_char_data *vict )
{
   static char buf[IMC_NAME_LENGTH];
   char *mud, *name;
   imc_char_data *chdata = imc_getdata( ch );

   mud = imc_mudof( vict->name );
   name = imc_nameof( vict->name );

   if( !strcasecmp( mud, this_imcmud->localname ) )
	snprintf( buf, IMC_NAME_LENGTH, "%s", imc_nameof( name ) );
   else
      imcstrlcpy( buf, vict->name, IMC_NAME_LENGTH );
   IMCDISPOSE( chdata );
   return buf;
}

/* check if a packet from a given source should be ignored */
bool imc_isbanned( char *who )
{
   IMC_BAN *mud;

   for( mud = first_imc_ban; mud; mud = mud->next )
   {
	if( !strcasecmp( mud->name, imc_mudof( who ) ) )
	   return TRUE;
   }
   return FALSE;
}

/* Beefed up to include wildcard ignores. */
bool imc_isignoring( CHAR_DATA *ch, const char *ignore )
{
   IMC_IGNORE *temp;

   /* Wildcard support thanks to Xorith */
   for( temp = FIRST_IMCIGNORE(ch); temp; temp = temp->next )
   {
	if( !fnmatch( temp->name, ignore, 0 ) )
	   return TRUE;
   }
   return FALSE;
}

/* need exactly 2 %s's, and no other format specifiers */
bool verify_format( const char *fmt, short sneed )
{
   const char *c;
   int i = 0;

   c = fmt;
   while( ( c = strchr( c, '%' ) ) != NULL )
   {
      if( *( c + 1 ) == '%' )  /* %% */
      {
         c += 2;
         continue;
      }
    
      if( *( c + 1 ) != 's' )  /* not %s */
         return FALSE;

      c++;
      i++;
   }
   if( i != sneed )
      return FALSE;

   return TRUE;
}

/* There should only one of these..... */
void imc_delete_info( void )
{
   int i;

   for( i = 0; i < IMC_MEMORY; i++ )
      IMCSTRFREE( imc_memory[i].from );

   IMCSTRFREE( this_imcmud->routername );
   IMCSTRFREE( this_imcmud->rhost );
   IMCSTRFREE( this_imcmud->network );
   IMCSTRFREE( this_imcmud->clientpw );
   IMCSTRFREE( this_imcmud->serverpw );
   IMCDISPOSE( this_imcmud->outbuf );
   IMCDISPOSE( this_imcmud->inbuf );
   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->base );
   IMCDISPOSE( this_imcmud );
   IMCSTRFREE( IMC_VERSIONID );
}

/* 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 );
   IMCDISPOSE( p );
}

/* create a new info entry, insert into list */
REMOTEINFO *imc_new_reminfo( char *mud )
{
   REMOTEINFO *p, *mud_prev;

   IMCCREATE( p, REMOTEINFO, 1 );

   p->name    = IMCSTRALLOC( mud );
   p->url     = IMCSTRALLOC( "Unknown" );
   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;
}

/* find an info entry for "name" */
REMOTEINFO *imc_find_reminfo( char *name )
{
   REMOTEINFO *p;

   for( p = first_rinfo; p; p = p->next )
   {
      if( !strcasecmp( name, p->name ) )
         return p;
   }
   return NULL;
}

bool check_mud( CHAR_DATA *ch, char *mud )
{
   REMOTEINFO *r = imc_find_reminfo( mud );

   if( !r )
   {
      imc_printf( ch, "~W%s ~cis not a valid mud name.\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( CHAR_DATA *ch, char *mud )
{
   return check_mud( ch, imc_mudof( mud ) );
}

int get_imcpermvalue( const char *flag )
{
   unsigned int x;

   for( x = 0; x < (sizeof(imcperm_names) / sizeof(imcperm_names[0])); x++ )
      if( !strcasecmp(flag, imcperm_names[x]) )
         return x;
   return -1;
}

bool imccheck_permissions( CHAR_DATA *ch, int checkvalue, int targetvalue, bool enforceequal )
{
   if( checkvalue < 0 || checkvalue > IMCPERM_IMP )
   {
      imc_to_char( "Invalid permission setting.\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( 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( !strcasecmp( c->name, name ) )
         return c;
   return NULL;
}

IMC_CHANNEL *imc_findlchannel( char *name )
{
   IMC_CHANNEL *c;

   for( c = first_imc_channel; c; c = c->next )
      if( c->local_name && !strcasecmp( c->local_name, name ) )
         return c;
  return NULL;
}

void imc_freechan( IMC_CHANNEL *c )
{
   int x;

   if( !c ) /* How? */
   {
	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 );
   IMCSTRFREE( c->dlname );

   for( x = 0; x < MAX_IMCHISTORY; x++ )
	IMCSTRFREE( c->history[x] );
   IMCDISPOSE( c );
   return;
}

void imc_shutdownchannels( void )
{
   IMC_CHANNEL *ic, *icnext;

   for( ic = first_imc_channel; ic; ic = icnext )
   {
	icnext = ic->next;

	imc_freechan( ic );
   }
}

/*
 * 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 fline[LGST];
   char *pline;
   char c;
   int ln;

   pline = fline;
   fline[0] = '\0';
   ln = 0;

   /*
    * Skip blanks.
    * Read first char.
    */
   do
   {
      if( feof(fp) )
      {
         imcbug( "%s", "imcfread_line: EOF encountered on read." );
         imcstrlcpy( fline, "", LGST );
         return IMCSTRALLOC( fline );
      }
	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( fline );
      }
      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( fline[strlen(fline)-1] == '~' )
      fline[strlen(fline)-1] = '\0';
   return IMCSTRALLOC( fline );
}

/*
 * 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, void (*func)( imc_char_data *from, PACKET *p ) )
{
   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;
}

/* put a line onto descriptors output buffer */
void do_imcsend( char *fline )
{
   int len;
   char *newbuf;
   int newsize = this_imcmud->outsize;

   if( this_imcmud->state == IMC_CLOSED )
      return;

   if( !this_imcmud->outbuf[0] )
      this_imcmud->newoutput = 1;

   len = strlen( this_imcmud->outbuf ) + strlen( fline ) + 3;

   if( len > this_imcmud->outsize )
   {
      while( newsize < len )
         newsize *= 2;

	IMCCREATE( newbuf, char, newsize );
      imcstrlcpy( 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 );
      imcstrlcpy( newbuf, this_imcmud->outbuf, newsize );
      IMCDISPOSE( this_imcmud->outbuf );
      this_imcmud->outbuf = newbuf;
      this_imcmud->outsize = newsize;
   }
   imcstrlcat( this_imcmud->outbuf, fline, this_imcmud->outsize );
   imcstrlcat( this_imcmud->outbuf, "\n\r", this_imcmud->outsize );
}

/* send a packet to a mud using the right version */
void do_send_packet( PACKET *p )
{
   char *output;

   output = ( *imc_vinfo[IMC_VERSION].generate )( p );

   if( output )
   {
      imc_stats.tx_pkts++;
      if( strlen( output ) > (unsigned int)imc_stats.max_pkt )
         imc_stats.max_pkt = strlen( output );
      do_imcsend( output );
   }
}

void imc_send( PACKET *p )
{
   if( imc_active < IA_UP )
   {
      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++;

   if( !strcasecmp( p->type, "remote-admin" ) && this_imcmud->md5pass == TRUE )
   {
      char pwd[IMC_DATA_LENGTH];
      char *cryptpwd;

      snprintf( pwd, IMC_DATA_LENGTH, "%ld%s", p->i.sequence, imc_getkey( p, "hash", "none" ) );
      cryptpwd = imc_crypt( pwd );
      imc_addkey( p, "hash", cryptpwd );
   }

   imcstrlcpy( p->i.to, p->to, IMC_NAME_LENGTH );
   snprintf( p->i.from, IMC_NAME_LENGTH, "%s@%s", p->from, this_imcmud->localname );
   do_send_packet( 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, this_imcmud->localname ) )
      {
         /* not from us */
         /* check if its in the list already */
         p = imc_find_reminfo( sender );
         if( !p ) /* not in list yet, create a new entry */
         {
	      p = imc_new_reminfo( sender );

	      p->version = IMCSTRALLOC( "unknown" );
            p->network = IMCSTRALLOC( "unknown" );
         }
         /* already in list, update the entry */
         else
	      p->expired = FALSE;
      }
      /* get the next item in the path */
      temp = strchr( temp, '!' );
      if( temp )
         temp++;			/* skip to just after the next '!' */
   }
}

void do_imcchannel( imc_char_data *from, int number, char *argument, int emote )
{
   DESCRIPTOR_DATA *d;
   CHAR_DATA *victim;
   char str[LGST], buf[LGST];
   unsigned int chan;

   for( chan = 0; chan < numchannels; chan++ )
      if( imc_channels[chan].number == number )
         break;

   if( chan == numchannels )
      return;

   imcstrlcpy( str, emote ? imc_channels[chan].emotestr : imc_channels[chan].chatstr, LGST );

   for( d = first_descriptor; d; d = d->next )
   {
      if( d->connected == CON_PLAYING && ( victim = d->original ? d->original : d->character ) != NULL 
	 && IMCPERM(victim) >= imc_channels[chan].perm )
      {
         snprintf( buf, LGST, str, getname( victim, from ), argument );
         imc_to_char( buf, victim );
      }
   }
}

/* send a tell to a remote player */
void imc_send_tell( imc_char_data *from, char *to, char *argument, int isreply )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( !strcasecmp( imc_mudof( to ), "*" ) )
      return; /* don't let them do this */

   setdata( &out, from );

   imcstrlcpy( out.to, to, IMC_NAME_LENGTH );
   imcstrlcpy( out.type, "tell", IMC_TYPE_LENGTH );
   imc_addkey( &out, "text", argument );
   if( isreply )
      imc_addkeyi( &out, "isreply", isreply );

   imc_send( &out );
   imc_freedata( &out );
}

void imc_update_tellhistory( CHAR_DATA *ch, const char *msg )
{
   char new_msg[LGST];
   time_t t = time( NULL );
   struct tm *local = localtime(&t);
   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_recv_tell( imc_char_data *from, char *to, char *argument, int isreply )
{
   CHAR_DATA *victim = NULL;
   char buf[IMC_DATA_LENGTH];

   if( !strcasecmp( to, "*" ) ) /* ignore messages to system */
      return;

   victim = imc_find_user( to );

   if( victim )
   {
      if( IMCPERM(victim) < IMCPERM_MORT )
         return;

      if( strcasecmp( imc_nameof( from->name ), "ICE" ) )
      {
         if( IMCISINVIS(victim) )
         {
            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;
         }

         if( imc_isignoring( victim, from->name ) )
         {
            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;
         }

         if( IMCIS_SET( IMCFLAG(victim), IMC_TELL ) || IMCIS_SET( IMCFLAG(victim), IMC_DENYTELL ) )
         {
            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;
         }

         if( IMCAFK(victim) )
         {
            if( strcasecmp( imc_nameof( from->name ), "*" ) )
            {
	         snprintf( buf, IMC_DATA_LENGTH, "%s is currently AFK. Try back later.", to );
	         imc_send_tell( NULL, from->name, buf, 1 );
            }
            return;
         }

         if( strcasecmp( imc_nameof( from->name ), "*" ) )     /* not a system message */
         {
	      IMCSTRFREE( IMC_RREPLY(victim) );
	      IMCSTRFREE( IMC_RREPLY_NAME(victim) );
            IMC_RREPLY(victim) = IMCSTRALLOC( from->name );
            IMC_RREPLY_NAME(victim) = IMCSTRALLOC( getname( victim, from ) );
         }
      }
      /* Tell social */
      if( isreply == 2 )
         snprintf( buf, IMC_DATA_LENGTH, "~WImctell: ~c%s\n\r", argument );
      else
	   snprintf( buf, IMC_DATA_LENGTH, "~C%s ~cimctells you ~c'~W%s~c'~!\n\r", getname( victim, from ), argument );
      imc_to_char( buf, victim );
      imc_update_tellhistory( victim, buf );
   }
   else
   {
      snprintf( buf, IMC_DATA_LENGTH, "No player named %s exists here.", to );
      imc_send_tell( NULL, from->name, buf, 1 );
   }
}

void imc_recv_whoreply( char *to, char *text, int sequence )
{
   CHAR_DATA *victim;

   if( !( victim = imc_find_user( to ) ) )
      return;

   imc_to_pager( text, victim );
   return;
}

void update_imchistory( IMC_CHANNEL *channel, char *message )
{
   char msg[LGST], buf[LGST];
   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;
   }

   imcstrlcpy( msg, message, LGST );
   for( x = 0; x < MAX_IMCHISTORY; x++ )
   {
      if( channel->history[x] == NULL )
      {
         t = time( NULL );
         local = localtime( &t );
         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] );
            }
         }

         t = time( NULL );
         local = localtime( &t );
         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_showchannel( IMC_CHANNEL *c, char *from, char *txt, int emote )
{
   DESCRIPTOR_DATA *d;
   CHAR_DATA *ch;
   char buf[LGST], name[SMST];

   if( !c->local_name || c->local_name[0] == '\0' || !c->refreshed )
      return;
  
   if( emote < 2 )
      snprintf( buf, LGST, emote ? c->emoteformat : c->regformat, from, txt );
   else
      snprintf( buf, LGST, c->socformat, txt );
 
   for( d = first_descriptor; d; d = d->next )
   {
      ch = d->original ? d->original : d->character;

	if( !ch || d->connected != CON_PLAYING )
	   continue;

      /* Freaking stupid PC_DATA crap! */
      if( IS_NPC(ch) )
         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 );
  }
  update_imchistory( c, buf );
}

void imc_sendmessage( IMC_CHANNEL *c, char *name, char *text, int emote )
{
   PACKET out;

   imcstrlcpy( 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 );
   imc_addkeyi( &out, "echo", 1 );

   /* send a message out on a channel */
   if( !c->open )
   {
      imcstrlcpy( out.type, "ice-msg-p", IMC_TYPE_LENGTH );
      snprintf( out.to, IMC_NAME_LENGTH, "IMC@%s", channel_mudof( c->name ) );
   }
   else
   {
      /* broadcast */
      imcstrlcpy( out.type, "ice-msg-b", IMC_TYPE_LENGTH );
      imcstrlcpy( out.to, "*@*", IMC_NAME_LENGTH );
   }
   imc_send( &out );
   imc_freedata( &out );
}

/* respond to a who request with the given data */
void imc_send_whoreply( char *to, char *data, int sequence )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( !strcasecmp( imc_mudof( to ), "*" ) )
      return; /* don't let them do this */

   imc_initdata( &out );

   imcstrlcpy( out.to, to, IMC_NAME_LENGTH );

   imcstrlcpy( out.type, "who-reply", IMC_TYPE_LENGTH );
   imcstrlcpy( 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( 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++;
      imcstrlcpy( wr_buf, text, IMC_DATA_LENGTH );
      return;
   }
   imcstrlcat( 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';
}

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( CHAR_DATA *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;
}

/* 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_rwho( imc_char_data *from, char *argument )
{
   DESCRIPTOR_DATA *d;
   CHAR_DATA *person;
   char buf[LGST], whobuf[LGST], personbuf[LGST], tailbuf[LGST], rank[LGST], rankout[LGST];
   char stats[SMST];
   int pcount = 0, xx, yy;

   imc_whoreply_start( from->name );
   imc_whoreply_add( "\n\r" );

   snprintf( whobuf, LGST, "~R-=[ ~WPlayers on %s ~R]=-", this_imcmud->fullname );
   imcstrlcpy( buf, imccenterline( whobuf, 78 ), LGST );
   imcstrlcat( buf, "\n\r", LGST );
   imc_whoreply_add( buf );
    
   if( this_imcmud->iport > 0 )
      snprintf( whobuf, LGST, "~Y-=[ ~Wtelnet://%s:%d ~Y]=-", this_imcmud->ihost, this_imcmud->iport );
   else
      snprintf( whobuf, LGST, "~Y-=[ telnet://%s ]=-", this_imcmud->ihost );

   imcstrlcpy( buf, imccenterline( whobuf, 78 ), LGST );
   imcstrlcat( buf, "\n\r\n\r", LGST );
   imc_whoreply_add( buf );

   xx = 0;
   for( d = first_descriptor; d; d = d->next )
   {
      person = d->original ? d->original : d->character;
	if( person && d->connected == CON_PLAYING ) 
	{
	   if( IMCPERM(person) <= IMCPERM_NONE || IMCPERM(person) >= IMCPERM_IMM )
            continue;

	   if( IMCISINVIS( person ) )
	      continue;

         pcount++;

         if( xx == 0 )
            imc_whoreply_add( "~B--------------------------------=[ ~WPlayers ~B]=---------------------------------\n\r\n\r" );

	   imcstrlcpy( rank, imcrankbuffer( person ), LGST );
	   imcstrlcpy( rankout, imccenterline( rank, 20 ), LGST );

         imcstrlcpy( stats, "~z[", SMST );
         if( IMCAFK( person ) )
            imcstrlcat( stats, "AFK", SMST );
         else
            imcstrlcat( stats, "---", SMST );
         imcstrlcat( stats, "]~G", SMST );
	   snprintf( personbuf, LGST, "%s %s %s%s\n\r", rankout, stats, CH_IMCNAME(person), CH_IMCTITLE(person) );
	   imc_whoreply_add( color_mtoi( personbuf ) );
         xx++;
	}
   }

   yy = 0;
   for( d = first_descriptor; d; d = d->next )
   {
      person = d->original ? d->original : d->character;
	if( person && d->connected == CON_PLAYING ) 
      {
	   if( IMCPERM(person) <= IMCPERM_NONE || IMCPERM(person) < IMCPERM_IMM )
		continue;

	   if( IMCISINVIS( person ) )
		continue;

         pcount++;

         if( yy == 0 )
	      imc_whoreply_add( "\n\r~R-------------------------------=[ ~WImmortals ~R]=--------------------------------\n\r\n\r" );

	   imcstrlcpy( rank, imcrankbuffer( person ), LGST );
	   imcstrlcpy( rankout, imccenterline( rank, 20 ), LGST );

         imcstrlcpy( stats, "~z[", SMST );
         if( IMCAFK( person ) )
            imcstrlcat( stats, "AFK", SMST );
         else
            imcstrlcat( stats, "---", SMST );
         imcstrlcat( stats, "]~G", SMST );

	   snprintf( personbuf, LGST, "%s %s %s%s\n\r", rankout, stats, CH_IMCNAME(person), CH_IMCTITLE(person) );
	   imc_whoreply_add( color_mtoi( personbuf ) );
         yy++;
      }
   }

   snprintf( tailbuf, LGST, "\n\r~Y[~W%d Player%s~Y] ", pcount, pcount == 1 ? "" : "s" );
   imc_whoreply_add( tailbuf );

   snprintf( tailbuf, LGST, "~Y[~WHomepage: %s~Y] [~W%3d Player%s Online~Y]\n\r",
      this_imcmud->www, xx+yy, ( xx+yy == 1 ) ? "" : "s" );
   imc_whoreply_add( tailbuf );

   imc_whoreply_end();
}

void process_rfinger( imc_char_data *from, char *to )
{
   CHAR_DATA *victim;
   char buf[IMC_DATA_LENGTH];

   if( !to || to[0] == '\0' )
      return;

   if( !( victim = imc_find_user( to ) ) )
   {
      imc_send_whoreply( from->name, "No such player is online.\n\r", -1 );
      return;
   }

   if( IMCISINVIS(victim) || IMCPERM(victim) < IMCPERM_MORT )
   {
      imc_send_whoreply( from->name, "No such player is online.\n\r", -1 );
      return;
   }

   snprintf( buf, IMC_DATA_LENGTH,
      "\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_DATA_LENGTH-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_DATA_LENGTH-strlen(buf), "~W%s\n\r",
      ( IMC_COMMENT(victim) && IMC_COMMENT(victim)[0] != '\0' ) ? IMC_COMMENT(victim) : "" );

   imc_send_whoreply( from->name, buf, -1 );
}

void imc_recv_who( imc_char_data *from, char *type )
{
   char arg[SMST];
   char output[LGST];

   type = imc_getarg( type, arg, SMST );

   if( !strcasecmp( arg, "who" ) )
   {
      process_rwho( from, type );
      return;
   }
   else if( !strcasecmp( arg, "finger" ) )
   {
      process_rfinger( from, type );
      return;
   }
   else if( !strcasecmp( arg, "info" ) )
      snprintf( output, LGST,
         "~!Site Information --\n\r"
         "~cSite Name      ~W: ~!%s\n\r"
         "~cSite Host      ~W: ~!%s\n\r"
         "~cAdmin Email    ~W: ~!%s\n\r"
         "~cAdmin MUDMail  ~W: ~!Disabled\n\r"
         "~cWeb Site       ~W: ~!%s\n\r"
         "IMC Information --\n\r"
         "~cIMC Version    ~W: ~!%s\n\r"
         "~cIMC Details    ~W: ~!%s\n\r",
         this_imcmud->fullname, this_imcmud->ihost, this_imcmud->email, this_imcmud->www, IMC_VERSIONID, this_imcmud->details );
   else
      snprintf( output, LGST, "%s is not a valid option. Options are: who, finger, or info.\n\r", type );

   imc_send_whoreply( from->name, output, -1 );
}

/* respond with a whois-reply */
void imc_send_whoisreply( char *to, char *data )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( !strcasecmp( imc_mudof( to ), "*" ) )
      return; /* don't let them do this */

   imc_initdata( &out );

   imcstrlcpy( out.to, to, IMC_NAME_LENGTH );
   imcstrlcpy( out.type, "whois-reply", IMC_TYPE_LENGTH );
   imcstrlcpy( 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 )
{
   CHAR_DATA *victim;
   char buf[LGST];

   if( ( victim = imc_find_user( to ) ) != NULL && !IMCISINVIS(victim) )
   {
      snprintf( buf, LGST, "~RIMC Locate: ~Y%s@%s: ~cOnline.\n\r", CH_IMCNAME(victim), this_imcmud->localname );
      imc_send_whoisreply( from->name, buf );
   }
   return;
}

void imc_recv_whoisreply( char *to, char *text )
{
   CHAR_DATA *victim;

   if( ( victim = imc_find_user( to ) ) != NULL )
      imc_to_char( text, victim );
   return;
}

void imc_recv_beep( imc_char_data *from, char *to )
{
   CHAR_DATA *victim = NULL;
   char buf[IMC_DATA_LENGTH];

   if( !strcasecmp( to, "*" ) ) /* ignore messages to system */
      return;

   victim = imc_find_user( to );

   if( victim )
   {
      if( IMCPERM(victim) < IMCPERM_MORT )
         return;

      if( IMCISINVIS(victim) )
      {
         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;
      }

      if( imc_isignoring( victim, from->name ) )
      {
         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;
      }

      if( IMCIS_SET( IMCFLAG(victim), IMC_BEEP ) || IMCIS_SET( IMCFLAG(victim), IMC_DENYBEEP ) )
      {
         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;
      }

      if( IMCAFK(victim) )
      {
         if( strcasecmp( imc_nameof( from->name ), "*" ) )
         {
	      snprintf( buf, IMC_DATA_LENGTH, "%s is currently AFK. Try back later.", to );
	      imc_send_tell( NULL, from->name, buf, 1 );
         }
         return;
      }
    
      /* always display the true name here */
      snprintf( buf, IMC_DATA_LENGTH, "~c\a%s imcbeeps you.~!\n\r", from->name );
      imc_to_char( buf, victim );
   }
   else
   {
      snprintf( buf, IMC_DATA_LENGTH, "No player named %s exists here.", to );
      imc_send_tell( NULL, from->name, buf, 1 );
   }
}

/* send a ping with a given timestamp */
void imc_send_ping( char *to )
{
   PACKET out;

   if( imc_active < IA_UP )
    return;

   imc_initdata( &out );
   imcstrlcpy( out.type, "ping", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "*@", IMC_NAME_LENGTH );
   imcstrlcpy( out.to+2, to, IMC_MNAME_LENGTH-2 );

   imc_send( &out );
   imc_freedata( &out );
}

/* send a who-request to a remote mud */
void imc_send_who( imc_char_data *from, char *to, char *type )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( !strcasecmp( imc_mudof( to ), "*" ) )
      return; /* don't let them do this */

   setdata( &out, from );

   snprintf( out.to, IMC_NAME_LENGTH, "*@%s", to );
   imcstrlcpy( out.type, "who", IMC_TYPE_LENGTH );

   imc_addkey( &out, "type", type );

   imc_send( &out );
   imc_freedata( &out );
}

/* send a locate request to the network */
void imc_send_locate( imc_char_data *from, char *to )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( strchr( to, '@' ) )
      return;

   setdata( &out, from );

   snprintf( out.to, IMC_NAME_LENGTH, "%s@*", to );
   imcstrlcpy( out.type, "whois", IMC_TYPE_LENGTH );

   imc_send( &out );
   imc_freedata( &out );
}

/* beep a remote player */
void imc_send_beep( imc_char_data *from, char *to )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   if( !strcasecmp( imc_mudof( to ), "*" ) )
      return; /* don't let them do this */

   setdata( &out, from );
   imcstrlcpy( out.type, "beep", IMC_TYPE_LENGTH );
   imcstrlcpy( out.to, to, IMC_NAME_LENGTH );

   imc_send( &out );
   imc_freedata( &out );
}

/* called when a keepalive has been received */
void imc_recv_keepalive( char *from, char *version, char *flags, char *network, char *url )
{
   REMOTEINFO *p;

   if( !strcasecmp( from, this_imcmud->localname ) )
      return;
  
   /*  this should never fail, imc.c should create an entry if one doesn't exist (in the path update code) */
   if( !( p = imc_find_reminfo( from ) ) ) /* boggle */
      return;

   if( strcasecmp( version, p->version ) )    /* remote version has changed? */
   {
      IMCSTRFREE( p->version );              /* if so, update it */
      p->version = IMCSTRALLOC( version );
   }
   /* Not sure how or why this might change, but just in case it does */
   if( network && network[0] != '\0' && strcasecmp( network, p->network ) )
   {
      IMCSTRFREE( p->network );
      p->network = IMCSTRALLOC( network );
   }

   if( strcasecmp( url, p->url ) )
   {
      IMCSTRFREE( p->url );
      p->url = IMCSTRALLOC( url );
   }
}

void imc_send_keepalive( const char *to )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   imc_initdata( &out );
   imcstrlcpy( out.type, "is-alive", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, to, IMC_NAME_LENGTH );
   imc_addkey( &out, "versionid", IMC_VERSIONID );
   imc_addkey( &out, "url", this_imcmud->www );

   imc_send( &out );
   imc_freedata( &out );
}

void imc_request_keepalive( void )
{
   PACKET out;

   imc_initdata( &out );
   imcstrlcpy( out.type, "keepalive-request", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "*@*", IMC_NAME_LENGTH );

   imc_send( &out );
   imc_freedata( &out );

   imc_send_keepalive( "*@*" );
}

void imc_firstrefresh( void )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   imc_initdata( &out );  
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "IMC@$", IMC_NAME_LENGTH );
   imcstrlcpy( out.type, "ice-refresh", IMC_TYPE_LENGTH );

   imc_send( &out );
   imc_freedata( &out );
}

/* send a pingreply with the given timestamp */
void imc_send_pingreply( char *to, char *path )
{
   PACKET out;

   if( imc_active < IA_UP )
      return;

   imc_initdata( &out );
   imcstrlcpy( out.type, "ping-reply", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "*@", IMC_NAME_LENGTH );
   imcstrlcpy( out.to+2, to, IMC_MNAME_LENGTH-2 );
   imc_addkey( &out, "path", path );

   imc_send( &out );
   imc_freedata( &out );
}

/* called when a ping request is received */
void imc_recv_ping( char *from, char *path )
{
   /* ping 'em back */
   imc_send_pingreply( from, path );
}

void imc_traceroute( char *pathto, char *pathfrom )
{
   if( !strcasecmp( imc_firstinpath( pathfrom ), lastping ) )
   {
      CHAR_DATA *ch = NULL;

      if( ( ch = imc_find_user( pinger ) ) == NULL )
         return;

      imc_printf( ch, "%s: Return path: %s\n\rSend path:   %s\n\r",
	   imc_firstinpath( pathfrom ), pathfrom, pathto ? pathto : "unknown" );
   }
}

/* called when a ping reply is received */
void imc_recv_pingreply( char *from, char *pathto, char *pathfrom )
{
   REMOTEINFO *p;

   p = imc_find_reminfo( from ); /* should always exist */
   if( !p ) /* boggle */
      return;

   /* check for pending traceroutes */
   imc_traceroute( pathto, pathfrom );
}

int imctodikugender( int gender )
{
   int sex = 0;

   if( gender == 0 )
      sex = SEX_MALE;

   if( gender == 1 )
	sex = SEX_FEMALE;

   return sex;
}

int dikutoimcgender( int gender )
{
   int sex = 0;

   if( gender > 2 || gender < 0 )
      sex = 2;
   
   if( gender == SEX_MALE )
	sex = 0;

   if( gender == SEX_FEMALE )
	sex = 1;

   return sex;
}

int imc_get_ucache_gender( const char *name )
{
   IMCUCACHE_DATA *user;

   for( user = first_imcucache; user; user = user->next )
   {
	if( !strcasecmp( user->name, name ) )
	   return user->gender;
   }

   /* -1 means you aren't in the list and need to be put there. */
   return -1;
}

/* Saves the ucache info to disk because it would just be spamcity otherwise */
void imc_save_ucache( void )
{
   FILE *fp;
   IMCUCACHE_DATA *user;

   if( ( fp = fopen( IMC_UCACHE_FILE, "w" ) ) == NULL ) 
   {
	imclog( "%s", "Couldn't write to IMC2 ucache file." );
	return;
   }

   for( user = first_imcucache; user; user = user->next )
   {
	fprintf( fp, "%s", "#UCACHE\n" );
	fprintf( fp, "Name %s\n", user->name );
	fprintf( fp, "Sex  %d\n", user->gender );
	fprintf( fp, "Time %ld\n", (long int)user->time );
	fprintf( fp, "%s", "End\n\n" );
   }
   fprintf( fp, "%s", "#END\n" );
   IMCFCLOSE( fp );
   return;
}

void imc_prune_ucache( void )
{
   IMCUCACHE_DATA *ucache, *next_ucache;

   for( ucache = first_imcucache; ucache; ucache = next_ucache )
   {
	next_ucache = ucache->next;

	/* Info older than 30 days is removed since this person likely hasn't logged in at all */
	if( current_time - ucache->time >= 2592000 )
	{
	   IMCSTRFREE( ucache->name );
	   IMCUNLINK( ucache, first_imcucache, last_imcucache, next, prev );
	   IMCDISPOSE( ucache );
	}
   }
   imc_save_ucache( );
   return;
}

/* Updates user info if they exist, adds them if they don't. */
void imc_ucache_update( const char *name, int gender )
{
   IMCUCACHE_DATA *user;

   for( user = first_imcucache; user; user = user->next )
   {
      if( !strcasecmp( user->name, name ) )
	{
	   user->gender = gender;
	   user->time = current_time;
	   return;
	}
   }
   IMCCREATE( user, IMCUCACHE_DATA, 1 );
   user->name = IMCSTRALLOC( name );
   user->gender = gender;
   user->time = current_time;
   IMCLINK( user, first_imcucache, last_imcucache, next, prev );

   imc_save_ucache( );
   return;
}

void imc_send_ucache_update( const char *visname, int gender )
{
   PACKET out;

   imc_initdata( &out );
   imcstrlcpy( out.type, "user-cache", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, visname, IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "*@*", IMC_NAME_LENGTH );
   imc_addkeyi( &out, "gender", gender );

   imc_send( &out );
   imc_freedata( &out );
   return;
}

void imc_recv_usercache( const char *from, int gender )
{
   int sex;

   sex = imc_get_ucache_gender( from );

   if( sex == gender )
      return;

   imc_ucache_update( from, gender );
   return;
}

void imc_send_ucache_request( char *targetuser )
{
   PACKET out;

   imc_initdata( &out );
   imcstrlcpy( out.type, "user-cache-request", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   snprintf( out.to, IMC_NAME_LENGTH, "*@%s", imc_mudof( targetuser ) );
   imc_addkey( &out, "user", targetuser );

   imc_send( &out );
   imc_freedata( &out );
   return;
}

void imc_process_ucache_request( char *from, char *user )
{
   PACKET out;
   int gender;

   gender = imc_get_ucache_gender( user );

   /* Gender of -1 means they aren't in the mud's ucache table. Don't waste the reply packet. */
   if( gender == -1 )
      return;

   imc_initdata( &out );
   imcstrlcpy( out.type, "user-cache-reply", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   snprintf( out.to, IMC_NAME_LENGTH, "*@%s", imc_mudof( from ) );
   imc_addkey( &out, "user", user );
   imc_addkeyi( &out, "gender", gender );

   imc_send( &out );
   imc_freedata( &out );
   return;
}

void imc_process_ucache_reply( const char *from, const char *user, int gender )
{
   int sex;

   sex = imc_get_ucache_gender( user );

   if( sex == gender )
      return;

   imc_ucache_update( user, gender );
   return;
}

void imc_recv_msg_r( char *from, char *realfrom, char *chan, char *txt, int emote )
{
   IMC_CHANNEL *c;
   char *mud;

   mud = imc_mudof( from );

   /* forged? */
   if( !strchr( chan, ':' ) || strcasecmp( mud, channel_mudof( chan ) ) )
      return;

   c = imc_findchannel( chan );
   if( !c )
      return;

   if( !c->local_name || ( c->open ) )
      return;

   /* We assume that anything redirected is automatically audible - since we trust the router... 
    * What's all this *WE* business? *I* don't trust it :)
    */
   imc_showchannel( c, realfrom, txt, emote );
}

void imc_recv_msg_b( char *from, char *chan, char *txt, int emote, char *sender )
{
   IMC_CHANNEL *c;

   c = imc_findchannel( chan );
   if( !c )
      return;

   if( !c->local_name || !c->open )
      return;

   if( sender && sender[0] != '\0' )
      imc_showchannel( c, sender, txt, emote );
   else
      imc_showchannel( c, from, txt, emote );
}

void imc_recv_update( char *from, char *chan, char *owner, char *localname, char *operators, char *policy, char *invited, char *excluded, char *level )
{
   IMC_CHANNEL *c;
   char *mud;

   mud = imc_mudof( from );

   /* forged? */
   if( !strchr( chan, ':' ) || strcasecmp( mud, channel_mudof( chan ) ) )
      return;

   c = imc_findchannel( chan );

   if( !c )
   {
      int value = get_imcpermvalue( level );
      if( value < 0 || value > IMCPERM_IMP )
         value = IMCPERM_IMP;

	IMCCREATE( c, IMC_CHANNEL, 1 );
      c->name = IMCSTRALLOC( chan );
      c->owner = IMCSTRALLOC( owner );
      c->operators = IMCSTRALLOC( operators );
      c->invited = IMCSTRALLOC( invited );
      c->excluded = IMCSTRALLOC( excluded );
      c->local_name = NULL;

      if( localname && localname[0] != '\0' )
         c->dlname = IMCSTRALLOC( localname );
      else
         c->dlname = NULL;

	c->level = value;
	c->refreshed = TRUE;
	IMCLINK( c, first_imc_channel, last_imc_channel, next, prev );
   }
   else
   {
	IMCSTRFREE( c->name );
 	IMCSTRFREE( c->owner );
	IMCSTRFREE( c->operators );
	IMCSTRFREE( c->invited );
	IMCSTRFREE( c->excluded );
      IMCSTRFREE( c->dlname );
      c->name = IMCSTRALLOC( chan );
      c->owner = IMCSTRALLOC( owner );
      c->operators = IMCSTRALLOC( operators );
      c->invited = IMCSTRALLOC( invited );
      c->excluded = IMCSTRALLOC( excluded );

      if( localname && localname[0] != '\0' )
         c->dlname = IMCSTRALLOC( localname );
      else
         c->dlname = NULL;

	c->refreshed = TRUE;
   }

   /* 3.20+ only supports open and private. Anything not one of these becomes private. */
   if( !strcasecmp( policy, "open" ) )
      c->open = TRUE;
   else
      c->open = FALSE;
}

void imc_recv_destroy( char *from, char *channel )
{
   IMC_CHANNEL *c;
   char *mud;

   mud = imc_mudof( from );

   if( !strchr( channel, ':' ) || strcasecmp( mud, channel_mudof( channel ) ) )
      return;

   c = imc_findchannel( channel );
   if( !c )
      return;

   imc_freechan( c );
   imc_save_channels();
}

void imc_recv_channel_whoreply( char *to, char *channel, char *list )
{
   IMC_CHANNEL *c;
   CHAR_DATA *vic;

   if( !strcasecmp( to, "*" ) ) /* ignore messages to system */
      return;

   if( !strchr( channel, ':' ) )
      return;

   c = imc_findchannel( channel );
   if( !c )
      return;

   if( !( vic = imc_find_user( to ) ) )
      return;

   imc_printf( vic, "~G%s", list );
   return;
}

void imc_recv_channel_who( char *from, char *to, char *channel, char *lname, int level )
{
   IMC_CHANNEL *c;
   DESCRIPTOR_DATA *d;
   CHAR_DATA *person;
   PACKET out;
   char buf[IMC_DATA_LENGTH];

   if( !strchr( channel, ':' ) )
      return;

   c = imc_findchannel( channel );
   if( !c )
      return;

   if( !c->local_name )
      snprintf( buf, IMC_DATA_LENGTH, "Channel %s is not locally configured on %s\n\r", lname, this_imcmud->localname );
   else if( c->level > level )
      snprintf( buf, IMC_DATA_LENGTH, "Channel %s is above your permission level on %s\n\r", lname, this_imcmud->localname );
   else
   {
      int count = 0;

      snprintf( buf, IMC_DATA_LENGTH, "The following people are listening to %s on %s:\n\r\n\r", lname, this_imcmud->localname );
      for( d = first_descriptor; d; d = d->next )
      {
         person = d->original ? d->original : d->character;

         if( !person )
            continue;

	   if( IMCISINVIS(person) )
	      continue;

         if( !imc_hasname( IMC_LISTEN(person), c->local_name ) )
            continue;

         snprintf( buf+strlen(buf), IMC_DATA_LENGTH-strlen(buf), "%s\n\r", CH_IMCNAME(person) );
         count++;
      }
      /* Send no response to a broadcast request if nobody is listening. */
      if( count == 0 && !strcasecmp( to, "*" ) )
         return;
      else
         imcstrlcat( buf, "Nobody", IMC_DATA_LENGTH );
   }

   imc_initdata( &out );
   imcstrlcpy( out.type, "ice-chan-whoreply", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, "*", IMC_NAME_LENGTH );
   imcstrlcpy( out.to, from, IMC_NAME_LENGTH );
   imc_addkey( &out, "channel", c->name );
   imc_addkey( &out, "list", buf );

   imc_send( &out );
   imc_freedata( &out );
   return;
}

void imc_sendnotify( CHAR_DATA *ch, char *chan, bool chon )
{
   PACKET out;
   IMC_CHANNEL *channel;

   if( !IMCIS_SET( IMCFLAG(ch), IMC_NOTIFY ) )
      return;

   if( !( channel = imc_findlchannel( chan ) ) )
      return;

   imc_initdata( &out );
   imcstrlcpy( out.type, "channel-notify", IMC_TYPE_LENGTH );
   imcstrlcpy( out.from, CH_IMCNAME(ch), IMC_NAME_LENGTH );
   imcstrlcpy( out.to, "*@*", IMC_NAME_LENGTH );
   imc_addkey( &out, "channel", channel->name );
   imc_addkeyi( &out, "status", chon );

   imc_send( &out );
   imc_freedata( &out );
   return;
}

void imc_recv_channel_notify( const char *from, char *chan, bool chon )
{
   IMC_CHANNEL *c;
   DESCRIPTOR_DATA *d;
   CHAR_DATA *ch;
   char buf[LGST];

   if( !( c = imc_findchannel( chan ) ) )
      return;

   if( !c->local_name || c->local_name[0] == '\0' )
      return;

   if( chon == TRUE )
      snprintf( buf, LGST, c->emoteformat, from, "has joined the channel." );
   else
      snprintf( buf, LGST, c->emoteformat, from, "has left the channel." );

   for( d = first_descriptor; d; d = d->next )
   {
      ch = d->original ? d->original : d->character;

	if( !ch || d->connected != CON_PLAYING )
	   continue;

      /* Freaking stupid PC_DATA crap! */
      if( IS_NPC(ch) )
         continue;

      if( IMCPERM(ch) < c->level || !imc_hasname( IMC_LISTEN(ch), c->local_name ) )
         continue;

      if( !IMCIS_SET( IMCFLAG(ch), IMC_NOTIFY ) )
         continue;

      imc_printf( ch, "%s\n\r", buf );
   }
   return;
}

/* Handles all incoming packets destined for this mud */
void imc_recv( PACKET *p )
{
   imc_char_data d;
   REMOTEINFO *i;
   IMC_PHANDLER *ph;

   getdata( p, &d );

   i = imc_find_reminfo( imc_mudof( p->from ) );
   if( i )
   {
      IMCSTRFREE( i->path );
      i->path = IMCSTRALLOC( p->i.path );
   }

   /* user-cache: Genger targetting socials. Keep a local table of known users */
   if( !strcasecmp( p->type, "user-cache" ) && !imc_isbanned( p->from ) )
      imc_recv_usercache( p->from, imc_getkeyi( p, "gender", -1 ) );

   else if( !strcasecmp( p->type, "user-cache-request" ) && !imc_isbanned( p->from ) )
      imc_process_ucache_request( p->from, imc_getkey( p, "user", "" ) );

   else if( !strcasecmp( p->type, "user-cache-reply" ) && !imc_isbanned( p->from ) )
      imc_process_ucache_reply( p->from, imc_getkey( p, "user", "" ), imc_getkeyi( p, "gender", -1 ) );

   /* chat: message to a channel (broadcast) */
   else if( !strcasecmp( p->type, "chat" ) && !imc_isbanned( p->from ) )
      do_imcchannel( &d, imc_getkeyi( p, "channel", 15 ), imc_getkey( p, "text", "" ), 0 );

   /* emote: emote to a channel (broadcast) */
   else if( !strcasecmp( p->type, "emote" ) && !imc_isbanned( p->from ) )
      do_imcchannel( &d, imc_getkeyi( p, "channel", 15 ), imc_getkey( p, "text", "" ), 1 );

   /* tell: tell a player here something */
   else if( !strcasecmp( p->type, "tell" ) )
   {
      if( !imc_isbanned( p->from ) )
         imc_recv_tell( &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 ) );

   /* who: receive a who request */
   else if( !strcasecmp( p->type, "who" ) || !strcasecmp( p->type, "wHo" ) )
   {
      if( !imc_isbanned( p->from ) )
         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" ) )
   {
      if( !imc_isbanned( p->from ) )
         imc_recv_whois( &d, p->to );
   }

   /* beep: beep a player */
   else if( !strcasecmp( p->type, "beep" ) )
   {
      if( !imc_isbanned( p->from ) )
         imc_recv_beep( &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, "flags", "" ), imc_getkey( p, "networkname", "" ),
         imc_getkey( p, "url", "??" ) );

   /* ping: receive a ping request */
   else if( !strcasecmp( p->type, "ping" ) && !imc_isbanned( p->from ) )
      imc_recv_ping( imc_mudof( p->from ), p->i.path );

   /* ping-reply: receive a ping reply */
   else if( !strcasecmp( p->type, "ping-reply" ) )
      imc_recv_pingreply( imc_mudof( p->from ), imc_getkey( p, "path", NULL ), p->i.path );

   else if( !strcasecmp( p->type, "keepalive-request" ) )
      imc_send_keepalive( p->from );

   else if( !strcasecmp( p->type, "close-notify" ) )
   {
      REMOTEINFO *r;

      if( ( r = imc_find_reminfo( imc_getkey( p, "host", "" ) ) ) != NULL )
         r->expired = TRUE;
   }

   /* ICE channel packets, used to be in their own function. */
   else if( !strcasecmp( p->type, "ice-msg-r" ) && !imc_isbanned( p->from ) )
   {
      imc_recv_msg_r( p->from, imc_getkey( p, "realfrom", "" ), imc_getkey( p, "channel", "" ),
	   imc_getkey( p, "text", "" ), imc_getkeyi( p, "emote", 0 ) );
   }
   else if( !strcasecmp( p->type, "ice-msg-b" ) && !imc_isbanned( p->from ) )
   {
      imc_recv_msg_b( p->from, imc_getkey( p, "channel", "" ), imc_getkey( p, "text", "" ),
         imc_getkeyi( p, "emote", 0 ), imc_getkey( p, "sender", "" ) );
   }
   else if( !strcasecmp( p->type, "ice-update" ) )
   {
      imc_recv_update( p->from, imc_getkey( p, "channel", "" ), imc_getkey( p, "owner", "" ),
         imc_getkey( p, "localname", "" ), imc_getkey( p, "operators", "" ),
         imc_getkey( p, "policy", "" ), imc_getkey( p, "invited", "" ),
         imc_getkey( p, "excluded", "" ), imc_getkey( p, "level", "" ) );
   }
   else if( !strcasecmp( p->type, "ice-destroy" ) )
      imc_recv_destroy( p->from, imc_getkey( p, "channel", "" ) );
   else if( !strcasecmp( p->type, "ice-chan-who" ) && !imc_isbanned( p->from ) )
   {
      imc_recv_channel_who( p->from, p->to, imc_getkey( p, "channel", "" ),
         imc_getkey( p, "lname", "..." ), imc_getkeyi( p, "level", 0 ) );
   }
   else if( !strcasecmp( p->type, "ice-chan-whoreply" ) && !imc_isbanned( p->from ) )
      imc_recv_channel_whoreply( p->to, imc_getkey( p, "channel", "" ), imc_getkey( p, "list", "" ) );
   else if( !strcasecmp( p->type, "channel-notify" ) && !imc_isbanned( p->from ) )
      imc_recv_channel_notify( p->from, imc_getkey( p, "channel", "" ), imc_getkeyi( p, "status", FALSE ) );
   else
      for( ph = first_phandler; ph; ph = ph->next )
         if( !strcasecmp( ph->name, p->type ) )
            (*ph->func)( &d, p );
   return;
}

/* 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;

   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->routername ? this_imcmud->routername : "router" );
      else        /* socket was closed */
         imclog( "EOF encountered on connection to %s", this_imcmud->routername ? this_imcmud->routername : "router" );

      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 );
      imcstrlcpy( 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 );
      imcstrlcpy( newbuf, this_imcmud->inbuf, newsize );
      IMCDISPOSE( this_imcmud->inbuf );
      this_imcmud->inbuf = newbuf;
      this_imcmud->insize = newsize;
   }

   imcstrlcat( this_imcmud->inbuf, temp, this_imcmud->insize );

   imc_stats.rx_bytes += r;
}

/* write to descriptor */
void do_imcwrite( void )
{
   int size, w;

   if( this_imcmud->state == IMC_CONNECTING )
   {
      /* Wait for server password */
      this_imcmud->state = IMC_WAIT1;
      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 */
         imclog( "Write error on connection to %s", this_imcmud->routername ? this_imcmud->routername : "router" );
      else			/* socket was closed */
         imclog( "EOF encountered on connection to %s", this_imcmud->routername ? this_imcmud->routername : "router" );

      imc_shutdown( TRUE );
      return;
   }

   if( w < 0 )			/* EAGAIN */
      return;

   if( imcpacketdebug )
      imclog( "Packet sent: %s", this_imcmud->outbuf );

   /* throw away data we wrote */
   imcstrlcpy( 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 */
   imcstrlcpy( buffer, buffer + i, this_imcmud->insize );

   return buf;
}

void imc_finalize_connection( char *name, char *netname, char *version )
{
   int rversion;

   this_imcmud->state = IMC_CONNECTED;

   if( version && version[0] != '\0' )
   {
      /* check for a version string (assume version 2 if not present) */
      if( sscanf( version, "version=%d", &rversion ) != 1 )
         rversion = 2;

      /* check for generator/interpreter */
      if( !imc_vinfo[rversion].generate || !imc_vinfo[rversion].interpret )
      {
         imclog( "%s: Unsupported version %d", name, rversion );
         imc_shutdown( FALSE );
         return;
      }
   }

   if( netname && netname[0] != '\0' )
   {
      IMCSTRFREE( this_imcmud->network );
      this_imcmud->network = IMCSTRALLOC( netname );
   }

   IMCSTRFREE( this_imcmud->routername );
   this_imcmud->routername = 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 router - Samson 8-12-03 */
void imc_handle_autosetup( char *source, char *routername, char *cmd, char *txt, char *md5 )
{
   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 router. Autosetup denied.", routername );
         imc_shutdown( FALSE );
         return;
      }
      if( !strcasecmp( txt, "full" ) )
      {
         imclog( "%s has reached its connection limit. Autosetup denied.", routername );
         imc_shutdown( FALSE );
         return;
      }
      if( !strcasecmp( txt, "ban" ) )
      {
         imclog( "%s has banned your connection. Autosetup denied.", routername );
         imc_shutdown( FALSE );
         return;
      }
      imclog( "%s: Invalid 'reject' response. Autosetup failed.", routername );
      imclog( "Data received: %s %s %s %s %s", source, routername, cmd, txt, md5 );
      imc_shutdown( FALSE );
      return;
   }

   if( !strcasecmp( cmd, "accept" ) )
   {
      imclog( "Autosetup completed successfully." );
      if( md5 && md5[0] != '\0' && !strcasecmp( md5, "MD5-SET" ) )
      {
         imclog( "MD5 Authentication has been enabled." );
         this_imcmud->md5pass = TRUE;
         imc_save_config();
      }
      imc_finalize_connection( routername, txt, "" );
      return;
   }

   imclog( "%s: Invalid autosetup response.", routername );
   imclog( "Data received: %s %s %s %s %s", source, routername, cmd, txt, md5 );
   imc_shutdown( FALSE );
   return;
}

/* handle a password response from a router - for connection negotiation. DON'T REMOVE THIS */
/* Updated for new MD5 supporting routers - Samson 2-18-04 */
void serverpassword( char *argument )
{
   char arg1[IMC_PW_LENGTH], name[IMC_MNAME_LENGTH], pw[IMC_PW_LENGTH];
   char version[IMC_PW_LENGTH], netname[IMC_MNAME_LENGTH], md5[IMC_PW_LENGTH], response[IMC_PACKET_LENGTH];

   argument = imc_getarg( argument, arg1, IMC_PW_LENGTH );
   argument = imc_getarg( argument, name, IMC_MNAME_LENGTH );
   argument = imc_getarg( argument, pw, IMC_PW_LENGTH );
   argument = imc_getarg( argument, version, IMC_PW_LENGTH );
   argument = imc_getarg( argument, netname, IMC_MNAME_LENGTH );
   argument = imc_getarg( argument, md5, IMC_PW_LENGTH );

   if( !name || name[0] == '\0' )
   {
      imclog( "%s", "Incomplete authentication packet. Unable to connect." );
      imc_shutdown( FALSE );
      return;
   }

   if( !strcasecmp( arg1, "MD5-AUTH-INIT" ) )
   {
      char pwd[IMC_DATA_LENGTH];
      char *cryptpwd;
      long auth_value = 0;

      if( !pw || pw[0] == '\0' )
      {
         imclog( "MD5 Authentication failure: No auth_value was returned by %s.", name );
         imc_shutdown( FALSE );
         return;
      }

      /* Lets encrypt this bastard now! */
      auth_value = atol( pw );
      snprintf( pwd, IMC_DATA_LENGTH, "%ld%s%s", auth_value, this_imcmud->clientpw, this_imcmud->serverpw );
      cryptpwd = imc_crypt( pwd );

      snprintf( response, IMC_PACKET_LENGTH, "MD5-AUTH-RESP %s %s version=%d",
         this_imcmud->localname, cryptpwd, IMC_VERSION );
      do_imcsend( response );
      return;
   }

   /* MD5 response is pretty simple. If you blew the authentication, it happened on the router anyway. 
    * name=Routername pw=Networkname
    */
   if( !strcasecmp( arg1, "MD5-AUTH-APPR" ) )
   {
      imclog( "%s", "MD5 Authentication completed." );
      imc_finalize_connection( name, pw, version );
      return;
   }

   /* The old way. Nice and icky, but still very much required for compatibility. */
   if( !strcasecmp( arg1, "PW" ) )
   {
      if( strcasecmp( this_imcmud->serverpw, pw ) )
      {
	   imclog( "%s sent an improper serverpassword.", name );
         imc_shutdown( FALSE );
         return;
      }

      imclog( "%s", "Standard Authentication completed." );
      if( md5 && md5[0] != '\0' && !strcasecmp( md5, "MD5-SET" ) )
      {
         imclog( "MD5 Authentication has been enabled." );
         this_imcmud->md5pass = TRUE;
         imc_save_config();
      }
      imc_finalize_connection( name, netname, version );
      return;
   }

   /* Should only be received from routers supporting this obviously
    * arg1=autosetup name=routername pw=command version=response netname=MD5-SET
    */
   if( !strcasecmp( arg1, "autosetup" ) )
   {
      imc_handle_autosetup( arg1, name, pw, version, netname );
      return;
   }

   imclog( "Invalid authentication response received from %s!!", name );
   imclog( "Data received: %s %s %s %s %s", arg1, name, pw, version, netname );
   imc_shutdown( FALSE );
   return;
}

/* interpret an incoming packet using the right version */
PACKET *do_interpret_packet( char *fline )
{
   int v;
   PACKET *p;

   if( !fline[0] )
      return NULL;

   v = IMC_VERSION;

   p = (*imc_vinfo[v].interpret)(fline);
   return p;
}

int imc_fill_fdsets( int maxfd, fd_set *iread, fd_set *iwrite, fd_set *exc )
{
   if( imc_active < IA_UP )
      return maxfd;

   /* set up fd_sets for select */

   if( maxfd < this_imcmud->desc )
      maxfd = this_imcmud->desc;

   switch( this_imcmud->state )
   {
      case IMC_CONNECTING:	/* connected/error when writable */
         FD_SET( this_imcmud->desc, iwrite );
         break;
      case IMC_CONNECTED:
      case IMC_WAIT1:
         FD_SET( this_imcmud->desc, iread );
         if( this_imcmud->outbuf && this_imcmud->outbuf[0] != '\0' )
	      FD_SET( this_imcmud->desc, iwrite );
         break;
   }
   return maxfd;
}

/* low-level idle function: read/write buffers as needed, etc */
void imc_idle_select( fd_set *iread, fd_set *iwrite, fd_set *exc, time_t now )
{
   char *command;
   PACKET *p;

   if( this_imcmud->desc < 1 )
	return;

   if( imc_active < IA_CONFIG1 )
      return;

   if( imc_sequencenumber < (unsigned long)imc_now )
      imc_sequencenumber = (unsigned long)imc_now;

   if( imc_active < IA_UP )
      return;

   /* handle results of the select */
   if( this_imcmud->state != IMC_CLOSED && FD_ISSET( this_imcmud->desc, exc ) )
   {
      imc_shutdown( TRUE );
	return;
   }

   if( this_imcmud->state != IMC_CLOSED && FD_ISSET( this_imcmud->desc, iread ) )
      do_imcread( );

   while( this_imcmud->state != IMC_CLOSED && ( command = imcgetline( this_imcmud->inbuf ) ) != NULL )
   {
      if( strlen( command ) > (unsigned int)imc_stats.max_pkt )
	   imc_stats.max_pkt = strlen( command );

      if( imcpacketdebug )
         imclog( "Packet received: %s", command );

      switch( this_imcmud->state )
      {
         case IMC_CLOSED:
	      break;
         case IMC_WAIT1:
	      serverpassword( command );
	      break;
         case IMC_CONNECTED:
	      p = do_interpret_packet( command );
	      if( p )
	      {
	         imc_stats.rx_pkts++;

               /* check for duplication, and register the packet in the sequence memory */
               if( p->i.sequence && checkrepeat( imc_mudof( p->i.from ), p->i.sequence ) )
                  break;

               /* update our routing info */
               updateroutes( p->i.path );

               /* Receive it if it's for us, otherwise it gets silently dropped */
               if( !strcasecmp( imc_mudof( p->i.to ), "*" ) || !strcasecmp( imc_mudof( p->i.to ), this_imcmud->localname ) )
               {
                  imcstrlcpy( p->to, imc_nameof( p->i.to ), IMC_NAME_LENGTH );    /* strip the name from the 'to' */
                  imcstrlcpy( p->from, p->i.from, IMC_NAME_LENGTH );

                  imc_recv( p );
               }
	         imc_freedata( p );
	      }
	      break;
      }
   }

   if( this_imcmud->desc > 0 ) /* Something could have caused shutdown during reading */
   {
      if( this_imcmud->state != IMC_CLOSED && ( FD_ISSET( this_imcmud->desc, iwrite ) || this_imcmud->newoutput ) )
      {
         do_imcwrite( );
         this_imcmud->newoutput = this_imcmud->outbuf[0];
         if( this_imcmud->disconnect == TRUE )
            imc_shutdown( FALSE );
      }
   }
}

void imc_loop( void )
{
   fd_set in_set, out_set, exc_set;
   static struct timeval null_time;
   int maxdesc = 0;

#ifdef IMCCIRCLE
   current_time = time(NULL);
#endif

   if( imcwait > 0 )
      imcwait--;

   /* Condition reached only if network shutdown after startup */
   if( imcwait == 1 )
   {
      imcconnect_attempts++;
      if( imcconnect_attempts > 5 )
      {
         imcwait = -2;
         imclog( "Unable to reestablish connection to %s. Abandoning reconnect.", this_imcmud->routername );
         return;
      }
	imc_startup( TRUE );
	return;
   }

   if( imc_active == IA_NONE || this_imcmud->desc == -1 )
	return;

   /* Will prune the cache once every 24hrs after bootup time */
   if( imcucache_clock <= current_time )
   {
	imcucache_clock = current_time + 86400;
	imc_prune_ucache( );
   }

   FD_ZERO( &in_set  );
   FD_ZERO( &out_set );
   FD_ZERO( &exc_set );

   maxdesc = imc_fill_fdsets( maxdesc, &in_set, &out_set, &exc_set );
   if( select( maxdesc+1, &in_set, &out_set, &exc_set, &null_time ) < 0 )
   {
	perror( "imc_loop: select: poll" );
	imc_shutdown( TRUE );
	return;
   }
   imc_idle_select( &in_set, &out_set, &exc_set, current_time );
   return;
}

/************************************
 * User login and logout functions. *
 ************************************/

void imc_adjust_perms( CHAR_DATA *ch )
{
   if( !this_imcmud )
      return;

   /* Ugly hack to let the permission system adapt freely, but retains the ability to override that adaptation
    * in the event you need to restrict someone to a lower level, or grant someone a higher level. This of
    * course comes at the cost of forgetting you may have done so and caused the override flag to be set, but hey.
    * This isn't a perfect system and never will be. Samson 2-8-04.
    */
   if( !IMCIS_SET( IMCFLAG(ch), IMC_PERMOVERRIDE ) )
   {
      if( CH_IMCLEVEL(ch) < this_imcmud->minlevel )
	   IMCPERM(ch) = IMCPERM_NONE;
      else if( CH_IMCLEVEL(ch) >= this_imcmud->minlevel && CH_IMCLEVEL(ch) < this_imcmud->immlevel )
	   IMCPERM(ch) = IMCPERM_MORT;
      else if( CH_IMCLEVEL(ch) >= this_imcmud->immlevel && CH_IMCLEVEL(ch) < this_imcmud->adminlevel )
	   IMCPERM(ch) = IMCPERM_IMM;
      else if( CH_IMCLEVEL(ch) >= this_imcmud->adminlevel && CH_IMCLEVEL(ch) < this_imcmud->implevel )
	   IMCPERM(ch) = IMCPERM_ADMIN;
      else if( CH_IMCLEVEL(ch) >= this_imcmud->implevel )
	   IMCPERM(ch) = IMCPERM_IMP;
   }
   return;
}

void imc_char_login( CHAR_DATA *ch )
{
   char buf[IMC_NAME_LENGTH];
   int gender, sex;

   if( !this_imcmud )
	return;

   imc_adjust_perms( ch );

   if( imc_active != IA_UP )
   {
	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;
   }

   if( IMCPERM(ch) < IMCPERM_MORT )
	return;

   snprintf( buf, IMC_NAME_LENGTH, "%s@%s", CH_IMCNAME(ch), this_imcmud->localname );
   gender = imc_get_ucache_gender( buf );
   sex = dikutoimcgender( CH_IMCSEX(ch) );

   if( gender == sex )
      return;

   imc_ucache_update( buf, sex );
   if( !IMCIS_SET( IMCFLAG(ch), IMC_INVIS ) )
      imc_send_ucache_update( CH_IMCNAME(ch), sex );

   return;
}

bool imc_loadchar( CHAR_DATA *ch, FILE *fp, const char *word )
{
   bool fMatch = FALSE;

   if( IS_NPC(ch) )
	return FALSE;

   if( IMCPERM(ch) == IMCPERM_NOTSET )
      imc_adjust_perms( ch );

   switch( word[0] )
   {
	case 'I':
         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 && imc_active == IA_UP )
		{
		   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_findlchannel( arg ) ) )
              

Showing the first 100 KB of 222.5 KB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571