kof0.70.c
KoF0.70/kof0.70.c · 463.4 KB
From the archive KoF0.70b.tar.gz
Download the archive « Back to the archive contents
/**************************************************************************
* Amnuts version 2.2.0 - Copyright (C) Andrew Collington *
* Last update: 20th September, 1999 *
* amnuts@iname.com | http://www.talker.com/amnuts/ *
* which is (heavily) modified *
* NUTS version 3.3.3 (Triple Three :) - Copyright (C) Neil Robertson 1996 *
* Last update: 18th November 1996 *
***************************************************************************/
/**************************************************************************
* KoF 0.70.b is done by Lauri Tamm *
* ( larts@hot.ee or lauri@erm.ee ) *
**************************************************************************/
#include <stdio.h>
#ifdef _AIX
#include <sys/select.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <time.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <setjmp.h>
#include <errno.h>
#include <sys/file.h>
#include <dirent.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include "kof0.70.h"
#include "commands.h"
#include "prototypes.h"
/******************************************************************************
The setting up of the talker configs and the main program loop
*****************************************************************************/
int main(int argc,char *argv[])
{
fd_set readmask;
int i,len;
char inpstr[ARR_SIZE];
UR_OBJECT user,next;
strcpy(progname,argv[0]);
if (argc<2) strcpy(confile,CONFIGFILE);
else strcpy(confile,argv[1]);
/* Run in background automatically. */
/* moved here because of .reboot wasn't working properly with the forking after
the rest of the initiation had taken place.
*/
switch(fork()) {
case -1: boot_exit(11); /* fork failure */
case 0: break; /* child continues */
default: sleep(1); exit(0); /* parent dies */
}
/* Startup and do initial counts and parses */
create_system();
set_date_time();
init_signals();
write_syslog(SYSLOG,0,"------------------------------------------------------------------------------\nSERVER BOOTING\n");
printf("\n------------------------------------------------------------------------------\n");
printf("KoF %s server booting %s\n",KOFVERS,long_date(1));
printf("------------------------------------------------------------------------------\n");
printf("System name : %s, release %s, %s\n",amsys->sysname,amsys->sysrelease,amsys->sysversion);
printf("Running on : %s, %s\n",amsys->sysmachine,amsys->sysnodename);
load_and_parse_config();
(amsys->flood_protect) ? printf("Flood protection is ON.\n") : printf("Flood protection is OFF.\n");
if (amsys->personal_rooms) {
if (amsys->startup_room_parse) parse_user_rooms();
else printf("Personal rooms are active, but not being parsed at startup.\n");
}
else printf("Personal rooms disabled\n");
printf("Checking user directory structure\n"); check_directories();
printf("Processing user list\n"); process_users();
printf("Counting users\n"); count_users();
printf("Parsing command structure\n"); parse_commands();
purge(0,NULL,0);
if (!amsys->auto_purge) printf("PURGE: Auto-purge is turned off\n");
else printf("PURGE: Checked %d user%s, %d %s deleted due to lack of use.\n",
amsys->purge_count,PLTEXT_S(amsys->purge_count),amsys->users_purged,PLTEXT_WAS(amsys->users_purged));
check_messages(NULL,1);
count_suggestions();
printf("There %s %d suggestion%s.\n",PLTEXT_WAS(amsys->suggestion_count),amsys->suggestion_count,PLTEXT_S(amsys->suggestion_count));
count_motds(0);
printf("There %s %d login motd%s and %d post-login motd%s\n",
PLTEXT_WAS(amsys->motd1_cnt),amsys->motd1_cnt,PLTEXT_S(amsys->motd1_cnt),amsys->motd2_cnt,PLTEXT_S(amsys->motd2_cnt));
/* open the talker after everything else has parsed */
init_sockets();
if (argc>2) if (atoi(argv[2])>1) do_srload();
/* finish off the boot-up process */
reset_alarm();
printf("------------------------------------------------------------------------------\n");
printf("Booted with PID %u\n",amsys->pid);
printf("------------------------------------------------------------------------------\n\n");
write_syslog(SYSLOG,0,"------------------------------------------------------------------------------\nSERVER BOOTED with PID %u %s\n",amsys->pid,long_date(1));
write_syslog(SYSLOG,0,"------------------------------------------------------------------------------\n\n");
/******************************************************************************
Main program loop
*****************************************************************************/
setjmp(jmpvar); /* jump to here if we crash and crash_action = IGNORE */
while(1) {
/* set up mask then wait */
setup_readmask(&readmask);
if (select(FD_SETSIZE,&readmask,0,0,0)==-1) continue;
/* check for connection to listen sockets */
for(i=0;i<port_total;++i) {
if (FD_ISSET(listen_sock[i],&readmask))
accept_connection(listen_sock[i],i);
}
/* Cycle through users. Use a while loop instead of a for because
user structure may be destructed during loop in which case we
may lose the user->next link. */
user=user_first;
while (user!=NULL) {
next=user->next; /* store in case user object is destructed */
/* If remote user or clone ignore */
if (user->type!=USER_TYPE) { user=next; continue; }
/* see if any data on socket else continue */
if (!FD_ISSET(user->socket,&readmask)) { user=next; continue; }
/* see if client (eg telnet) has closed socket */
inpstr[0]='\0';
if (!(len=read(user->socket,inpstr,sizeof(inpstr)))) {
disconnect_user(user); user=next;
continue;
}
/* ignore control code replies */
if ((unsigned char)inpstr[0]==255) { user=next; continue; }
/* Deal with input chars. If the following if test succeeds we
are dealing with a character mode client so call function. */
if (inpstr[len-1]>=32 || user->buffpos) {
if (get_charclient_line(user,inpstr,len)) goto GOT_LINE;
user=next; continue;
}
else terminate(inpstr);
GOT_LINE:
no_prompt=0;
com_num=-1;
force_listen=0;
destructed=0;
user->buff[0]='\0';
user->buffpos=0;
user->last_input=time(0);
if (user->login>0) {
login(user,inpstr); user=next; continue;
}
/* If a dot on its own then execute last inpstr unless its a misc
op or the user is on a remote site */
if (!user->misc_op) {
if (!strcmp(inpstr,".") && user->inpstr_old[0]) {
strcpy(inpstr,user->inpstr_old);
vwrite_user(user,"%s\n",inpstr);
}
/* else save current one for next time */
else {
if (inpstr[0]) strncpy(user->inpstr_old,inpstr,REVIEW_LEN);
}
}
/* Main input check */
clear_words();
check_macros(user,inpstr);
word_count=wordfind(inpstr);
if (user->afk) {
if (user->afk==2) {
if (!word_count) {
if (user->command_mode) prompt(user);
user=next; continue;
}
if (strcmp((char *)crypt(word[0],crypt_salt),user->pass)) {
write_user(user,"Incorrect password.\n");
prompt(user); user=next; continue;
}
cls(user);
write_user(user,"Session unlocked, you are no longer AFK.\n");
}
else write_user(user,"You are no longer AFK.\n");
user->afk_mesg[0]='\0';
if (user->revbuff[0][0])
write_user(user,"\nYou have some tells in your preview buffer. Use ~FTprev~RS to view them.\n\n");
if (user->vis) vwrite_room_except(user->room,user,"%s~RS comes back from being AFK.\n",user->recap);
if (user->afk==2) {
user->afk=0; prompt(user); user=next; continue;
}
user->afk=0;
}
if (!word_count) {
if (misc_ops(user,inpstr)) { user=next; continue; }
if (user->command_mode) prompt(user);
user=next; continue;
}
if (misc_ops(user,inpstr)) { user=next; continue; }
com_num=-1;
if (user->command_mode || strchr(".>;:</&![@'*+-,",inpstr[0]))
exec_com(user,inpstr);
else say(user,inpstr);
if (!destructed) {
if (user->room!=NULL && !destructed) prompt(user);
else {
switch(com_num) {
/* case -1 : Not in enumerated values - Unknown command */
case QUIT:
case MODE:
case PROMPT:
case SUICIDE:
case REBOOT:
case SHUTDOWN: prompt(user);
default: break;
}
}
}
user=next;
} /* end while(user) */
} /* end while(1) */
} /* main */
/******************************************************************************
String functions - comparisons, convertions, etc
*****************************************************************************/
/*** Attempt to get '\n' terminated line of input from a character
mode client else store data read so far in user buffer. ***/
int get_charclient_line(UR_OBJECT user,char *inpstr,int len)
{
int l;
for(l=0;l<len;++l) {
/* see if delete entered */
if (inpstr[l]==8 || inpstr[l]==127) {
if (user->buffpos) {
user->buffpos--;
if (user->charmode_echo) write_user(user,"\b \b");
}
continue;
}
user->buff[user->buffpos]=inpstr[l];
/* See if end of line */
if (inpstr[l]<32 || user->buffpos+2==ARR_SIZE) {
terminate(user->buff);
strcpy(inpstr,user->buff);
if (user->charmode_echo) write_user(user,"\n");
return 1;
}
user->buffpos++;
}
if (user->charmode_echo
&& ((user->login!=LOGIN_PASSWD && user->login!=LOGIN_CONFIRM) || (amsys->password_echo || user->show_pass)))
write(user->socket,inpstr,len);
return 0;
}
/*** Put string terminate char. at first char < 32 ***/
void terminate(char *str)
{
int i;
for (i=0;i<ARR_SIZE;++i) if (*(str+i)<32) { *(str+i)=0; return; }
str[i-1]=0;
}
/*** Get words from sentence. This function prevents the words in the
sentence from writing off the end of a word array element. This is
difficult to do with sscanf() hence I use this function instead. ***/
int wordfind(char *inpstr)
{
int wn,wpos;
wn=0; wpos=0;
do {
while(*inpstr<33) if (!*inpstr++) return wn;
while(*inpstr>32 && wpos<WORD_LEN-1) {
word[wn][wpos]=*inpstr++; wpos++;
}
word[wn][wpos]='\0';
wn++; wpos=0;
} while (wn<MAX_WORDS);
return wn-1;
}
/*** clear word array etc. ***/
void clear_words()
{
int w;
for(w=0;w<MAX_WORDS;++w) word[w][0]='\0';
word_count=0;
}
/** check to see if string given is YES or NO **/
int yn_check(char *wd)
{
if (!strcmp(wd,"YES")) return 1;
if (!strcmp(wd,"NO")) return 0;
return -1;
}
/** check to see if string given is ON or OFF **/
int onoff_check(char *wd)
{
if (!strcmp(wd,"ON")) return 1;
if (!strcmp(wd,"OFF")) return 0;
return -1;
}
/** check to see if string given is OFF, MIN or MAX **/
int minmax_check(char *wd)
{
if (!strcmp(wd,"OFF")) return SBOFF;
if (!strcmp(wd,"MIN")) return SBMIN;
if (!strcmp(wd,"MAX")) return SBMAX;
return -1;
}
/** check to see if string given is OFF, AUTO, MANUAL **/
int resolve_check(char *wd)
{
if (!strcmp(wd,"OFF")) return 0;
if (!strcmp(wd,"AUTO")) return 1;
if (!strcmp(wd,"MANUAL")) return 2;
return -1;
}
/*** Tell telnet not to echo characters - for password entry ***/
void echo_off(UR_OBJECT user)
{
if (amsys->password_echo || user->show_pass) return;
vwrite_user(user,"%c%c%c",255,251,1);
}
/*** Tell telnet to echo characters ***/
void echo_on(UR_OBJECT user)
{
if (amsys->password_echo || user->show_pass) return;
vwrite_user(user,"%c%c%c",255,252,1);
}
/*** Return pos. of second word in inpstr ***/
char *remove_first(char *inpstr)
{
char *pos=inpstr;
while(*pos<33 && *pos) ++pos;
while(*pos>32) ++pos;
while(*pos<33 && *pos) ++pos;
return pos;
}
/*** See if string contains any swearing ***/
int contains_swearing(char *str)
{
char *s;
int i;
if ((s=(char *)malloc(strlen(str)+1))==NULL) {
write_syslog(SYSLOG,0,"ERROR: Failed to allocate memory in contains_swearing().\n");
return 0;
}
strcpy(s,str);
strtolower(s);
i=0;
while(swear_words[i][0]!='*') {
if (strstr(s,swear_words[i])) { free(s); return 1; }
++i;
}
free(s);
return 0;
}
/* go through the given string and replace any of the words found in the
swear_words array with the default swear censor, *swear_censor
*/
char *censor_swear_words(char *has_swears)
{
int i;
char *clean;
clean='\0';
i=0;
while (swear_words[i][0]!='*') {
while(has_swears!=NULL) {
clean=has_swears;
has_swears=replace_string(clean,swear_words[i],swear_censor);
}
++i;
has_swears=clean;
}
return clean;
}
/*** Count the number of colour commands in a string ***/
int colour_com_count(char *str)
{
char *s;
int i,cnt;
s=str; cnt=0;
while(*s) {
if (*s=='~') {
++s;
for(i=0;i<NUM_COLS;++i) {
if (!strncmp(s,colour_codes[i].txt_code,2)) { cnt++; s++; break; }
}
continue;
}
++s;
}
return cnt;
}
/*** Strip out colour commands from string for when we are sending strings
over a netlink to a talker that doesn't support them ***/
char *colour_com_strip(char *str)
{
char *s,*t;
static char text2[ARR_SIZE];
int i;
s=str; t=text2;
while(*s) {
if (*s=='~') {
++s;
for(i=0;i<NUM_COLS;++i) {
if (!strncmp(s,colour_codes[i].txt_code,2)) { s++; goto CONT; }
}
--s; *t++=*s;
}
else *t++=*s;
CONT:
s++;
}
*t='\0';
return text2;
}
/*** Convert string to upper case ***/
void strtoupper(char *str)
{
while(*str) { *str=toupper(*str); str++; }
}
/*** Convert string to lower case ***/
void strtolower(char *str)
{
while(*str) { *str=tolower(*str); str++; }
}
/*** Returns 1 if string is a positive number ***/
int is_number(char *str)
{
while(*str) if (!isdigit(*str++)) return 0;
return 1;
}
/*** Peforms the same as strstr, in that it returns a pointer to the first occurence
of pat in str - except that this is performed case insensitive
***/
char *istrstr(char *str, char *pat)
{
char *pptr, *sptr, *start;
int slen, plen;
slen=strlen(str);
plen=strlen(pat);
for (start=(char *)str,pptr=(char *)pat;slen>=plen;start++,slen--) {
/* find start of pattern in string */
while (toupper(*start)!=toupper(*pat)) {
start++; slen--;
/* if pattern longer than string */
if (slen<plen) return(NULL);
}
sptr=start;
pptr=(char *)pat;
while (toupper(*sptr)==toupper(*pptr)) {
sptr++; pptr++;
/* if end of pattern then pattern was found */
if (*pptr=='\0') return (start);
} /* end while */
} /* end for */
return(NULL);
}
/*** Take the string 'inpstr' and replace any occurence of 'old' with
the string 'new'
***/
char *replace_string(char *inpstr, char *old, char *new)
{
int old_len,new_len;
char *x,*y;
if (NULL==(x=(char *)istrstr(inpstr,old))) return x;
old_len=strlen(old);
new_len=strlen(new);
memmove(y=x+new_len,x+old_len,strlen(x+old_len)+1);
memcpy(x,new,new_len);
return inpstr;
}
/*** Searches string s1 for string s2 ***/
int instr(char *s1, char *s2)
{
int f,g;
for (f=0;*(s1+f);++f) {
for (g=0;;++g) {
if (*(s2+g)=='\0' && g>0) return f;
if (*(s2+g)!=*(s1+f+g)) break;
}
}
return -1;
}
/* used to copy out a chuck of text in macros */
void midcpy(char *strf, char *strt, int fr, int to)
{
int f;
for (f=fr;f<=to;++f) {
if (!strf[f]) { strt[f-fr]='\0'; return; }
strt[f-fr]=strf[f];
}
strt[f-fr]='\0';
}
/*** Get ordinal value of a date and return the string ***/
char *ordinal_text(int num)
{
char *ords[]={"th","st","nd","rd"};
if (((num%=100)>9 && num<20) || (num%=10)>3) num=0;
return ords[num];
}
/*** Date string for board messages, mail, .who and .allclones, etc ***/
char *long_date(int which)
{
static char dstr[80];
int ap,hour;
if (thour>=12) {
(thour>12) ? (hour=(int)thour-12) : (hour=12);
ap=1;
}
else {
(!thour) ? (hour=12) : (hour=(int)thour);
ap=0;
}
if (which) sprintf(dstr,"on %s %d%s %s %d at %02d:%02d%s",day[twday],tmday,ordinal_text(tmday),month[tmonth],(int)tyear,hour,(int)tmin,!ap?"am":"pm");
else sprintf(dstr,"[ %s %d%s %s %d at %02d:%02d%s ]",day[twday],tmday,ordinal_text(tmday),month[tmonth],(int)tyear,hour,(int)tmin,!ap?"am":"pm");
return dstr;
}
/* takes string str and determines what smiley type it should have. The type is then
stored in 'type'. The smiley type is determind by the last 2 characters in str.
*/
void smiley_type(char *str, char *type) {
switch(str[strlen(str)-1]) {
case '?': strcpy(type,"ask"); break;
case '!': strcpy(type,"exclaim"); break;
case ')': if (str[strlen(str)-2]==':') strcpy(type,"smile");
else if (str[strlen(str)-2]=='=') strcpy(type,"smile");
else if (str[strlen(str)-2]==';') strcpy(type,"wink");
else strcpy(type,"say");
break;
case '(': if (str[strlen(str)-2]==':' || str[strlen(str)-2]=='=') strcpy(type,"sorrow");
else strcpy(type,"say");
break;
case ':': if (str[strlen(str)-2]=='(') strcpy(type,"smile");
else if (str[strlen(str)-2]==')') strcpy(type,"sorrow");
else strcpy(type,"say");
break;
case '=': if (str[strlen(str)-2]=='(') strcpy(type,"smile");
else if (str[strlen(str)-2]==')') strcpy(type,"sorrow");
else strcpy(type,"say");
break;
case ';': if (str[strlen(str)-2]=='(') strcpy(type,"wink");
else if (str[strlen(str)-2]==')') strcpy(type,"sorrow");
else strcpy(type,"say");
break;
case 'P': if (str[strlen(str)-2]==':') strcpy(type,"grin");
else strcpy(type,"say");
break;
case 'p': if (str[strlen(str)-2]==':') strcpy(type,"grin");
else strcpy(type,"say");
break;
case 'o': if (str[strlen(str)-2]==':') strcpy(type,"wonder");
else strcpy(type,"say");
break;
case 'O': if (str[strlen(str)-2]==':') strcpy(type,"wonder");
else strcpy(type,"say");
break;
case 'D': if (str[strlen(str)-2]==':') strcpy(type,"laugh");
else strcpy(type,"say");
break;
case 'd': if (str[strlen(str)-2]==':') strcpy(type,"laugh");
else strcpy(type,"say");
break;
case 'H': if (str[strlen(str)-2]==':') strcpy(type,"sneer");
else strcpy(type,"say");
break;
case 'h': if (str[strlen(str)-2]==':') strcpy(type,"sneer");
else strcpy(type,"say");
break;
default : strcpy(type,"say"); break;
}
}
/* This allows you to center text to a given size. It will also allow you
to have markers on the edges of the text, if mark = 1, and the marker must
be passed if mark = 1. marker is an int - one symbol only.
Based on code by Mike Irving (Moe - MoeNUTS)
*/
char *center_string(int cstrlen, int mark, char *marker, char *str, ...)
{
va_list args;
char text2[ARR_SIZE*2];
int len=0,spc=0,odd=0,cnt=0;
/* first build up the string */
vtext[0]='\0';
va_start(args,str);
vsprintf(vtext,str,args);
va_end(args);
/* get size */
cnt=colour_com_count(vtext);
len=strlen(colour_com_strip(vtext));
spc=(int)((cstrlen/2)-(len/2));
odd=((spc+spc+len)-cstrlen);
/* if greater than size given then don't do anything except return */
if (len>cstrlen) return(vtext);
sprintf(text2,"%*.*s%s",spc,spc," ",vtext);
/* if marked, then add spaces on the other side too */
if (mark) {
/* if markers can't be placed without over-writing text then return */
if (len>(cstrlen-2)) return(vtext);
/* if they forgot to pass a marker, use a default one */
if (!marker) marker="|";
/* compensate for uneven spaces */
sprintf(vtext,"%s%*.*s\n",text2,spc-odd,spc-odd," ");
vtext[0]=marker[0];
vtext[strlen(vtext)-2]=marker[0];
}
else {
strcpy(vtext,text2);
strcat(vtext,"\n");
}
return(vtext);
}
/******************************************************************************
Object functions
*****************************************************************************/
/*** Construct system object and reset some global variables ***/
void create_system(void)
{
int i;
struct utsname uts;
if ((amsys=(SYS_OBJECT)malloc(sizeof(struct system_struct)))==NULL) {
fprintf(stderr,"KoF: Failed to create system object in create_system().\n");
boot_exit(21);
}
amsys->auto_connect=1;
amsys->max_users=50;
amsys->max_clones=1;
amsys->ban_swearing=0;
amsys->heartbeat=2;
amsys->keepalive_interval=60; /* DO NOT TOUCH!!! */
amsys->net_idle_time=300; /* Must be > than the above */
amsys->login_idle_time=180;
amsys->user_idle_time=300;
amsys->time_out_afks=0;
amsys->wizport_level=WIZ;
amsys->minlogin_level=-1;
amsys->mesg_life=1;
amsys->num_of_users=0;
amsys->num_of_logins=0;
amsys->logging=BIT_SET(amsys->logging,SYSLOG);
amsys->logging=BIT_SET(amsys->logging,REQLOG);
amsys->password_echo=0;
amsys->ignore_sigterm=0;
amsys->crash_action=2;
amsys->prompt_def=1;
amsys->colour_def=1;
amsys->charecho_def=0;
amsys->time_out_maxlevel=USER;
amsys->mesg_check_hour=0;
amsys->mesg_check_min=0;
amsys->rs_countdown=0;
amsys->rs_announce=0;
amsys->rs_which=-1;
amsys->rs_user=NULL;
amsys->gatecrash_level=CHAOS+1; /* minimum user level which can enter private rooms */
amsys->min_private_users=2; /* minimum num. of users in room before can set to priv */
amsys->ignore_mp_level=ARCH; /* User level which can ignore the above var. */
amsys->rem_user_maxlevel=USER;
amsys->rem_user_deflevel=USER;
amsys->logons_old=0;
amsys->logons_new=0;
amsys->purge_count=0;
amsys->purge_skip=0;
amsys->users_purged=0;
amsys->purge_date=1;
amsys->suggestion_count=0;
amsys->forwarding=1;
amsys->auto_purge=0;
amsys->user_count=0;
amsys->allow_recaps=1;
amsys->auto_promote=1;
amsys->personal_rooms=1;
amsys->startup_room_parse=1;
amsys->motd1_cnt=0;
amsys->motd2_cnt=0;
amsys->random_motds=1;
amsys->last_cmd_cnt=0;
amsys->resolve_ip=1; /* auto resolve ip */
amsys->flood_protect=1;
amsys->pid=(unsigned int)getpid();
time(&amsys->boot_time);
if ((uname(&uts))<0) {
strcpy(amsys->sysname,"[undetermined]");
strcpy(amsys->sysmachine,"[undetermined]");
strcpy(amsys->sysrelease,"[undetermined]");
strcpy(amsys->sysversion,"[undetermined]");
strcpy(amsys->sysnodename,"[undetermined]");
}
else {
strncpy(amsys->sysname,uts.sysname,63);
strncpy(amsys->sysmachine,uts.machine,63);
strncpy(amsys->sysrelease,uts.release,63);
strncpy(amsys->sysversion,uts.version,63);
strncpy(amsys->sysnodename,uts.nodename,63);
}
user_first=NULL;
user_last=NULL;
room_first=NULL;
room_last=NULL; /* This variable isn't used yet */
first_dir_entry=NULL;
first_command=NULL;
first_wiz_entry=NULL;
last_wiz_entry=NULL;
force_listen=0;
no_prompt=0;
logon_flag=0;
for (i=0;i<port_total;i++) {
port[i]=0;
listen_sock[i]=0;
}
for (i=0;i<LASTLOGON_NUM;i++) {
last_login_info[i].name[0]='\0';
last_login_info[i].time[0]='\0';
last_login_info[i].on=0;
}
for (i=0;i<16;i++) cmd_history[i][0]='\0';
clear_words();
}
/*** Construct user/clone object ***/
UR_OBJECT create_user(void)
{
UR_OBJECT user;
if ((user=(UR_OBJECT)malloc(sizeof(struct user_struct)))==NULL) {
write_syslog(SYSLOG,0,"ERROR: Memory allocation failure in create_user().\n");
return NULL;
}
/* Append object into linked list. */
if (user_first==NULL) {
user_first=user; user->prev=NULL;
}
else {
user_last->next=user; user->prev=user_last;
}
user->next=NULL;
user_last=user;
/* initialise user structure */
user->type=USER_TYPE;
user->socket=-1;
user->attempts=0;
user->login=0;
user->port=0;
user->site_port=0;
user->name[0]='\0';
user->site[0]='\0';
reset_user(user);
return user;
}
/* reset the user variables */
void reset_user(UR_OBJECT user)
{
int i;
strcpy(user->email,"#UNSET");
strcpy(user->homepage,"#UNSET");
strcpy(user->verify_code,"#NONE");
strcpy(user->version,USERVER);
user->recap[0]='\0';
user->bw_recap[0]='\0';
strcpy(user->desc,"is a guest.");
strcpy(user->in_phrase,"enters");
strcpy(user->out_phrase,"goes");
user->afk_mesg[0]='\0';
user->pass[0]='\0';
user->last_site[0]='\0';
user->page_file[0]='\0';
user->mail_to[0]='\0';
user->inpstr_old[0]='\0';
user->buff[0]='\0';
user->tname[0]='\0';
user->tsite[0]='\0';
user->tport[0]='\0';
user->call[0]='\0';
user->samesite_check_store[0]='\0';
user->hang_word[0]='\0';
user->hang_word_show[0]='\0';
user->hang_guess[0]='\0';
user->invite_by[0]='\0';
strcpy(user->logout_room,room_first->name);
strcpy(user->date,(long_date(1)));
for (i=0; i<MAX_IGNORES; ++i) user->ignoreuser[i][0]='\0';
for (i=0;i<MAX_COPIES;++i) user->copyto[i][0]='\0';
for (i=0;i<10;++i) user->macros[i][0]='\0';
for (i=0;i<MAX_FRIENDS;++i) user->friend[i][0]='\0';
for(i=0;i<REVTELL_LINES;++i) user->revbuff[i][0]='\0';
user->room=NULL;
user->invite_room=NULL;
user->malloc_start=NULL;
user->malloc_end=NULL;
user->owner=NULL;
user->wrap_room=NULL;
user->t_expire=time(0)+(NEWBIE_EXPIRES*86400);
user->read_mail=time(0);
user->last_input=time(0);
user->last_login=time(0);
user->level=GUEST;
user->real_level=user->level;
user->unarrest=GUEST;
user->arrestby=0;
user->buffpos=0;
user->filepos=0;
user->command_mode=1; /* larts */
user->vis=1;
user->ignall=0;
user->ignall_store=0;
user->ignshouts=0;
user->igntells=0;
user->remote_com=-1;
user->last_login_len=0;
user->total_login=0;
user->prompt=amsys->prompt_def;
user->colour=amsys->colour_def;
user->charmode_echo=amsys->charecho_def;
user->misc_op=0;
user->edit_op=0;
user->edit_line=0;
user->charcnt=0;
user->warned=0;
user->accreq=0;
user->afk=0;
user->revline=0;
user->clone_hear=CLONE_HEAR_ALL;
user->wipe_to=0;
user->wipe_from=0;
user->wrap=0;
user->pager=23;
user->logons=0;
user->expire=1;
user->lroom=0;
user->monitor=0;
user->gender=NEUTER;
user->language=0; /* larts */
user->age=0;
user->hideemail=1;
user->misses=0;
user->hits=0;
user->kills=0;
user->deaths=0;
user->bullets=6;
user->hps=10;
user->alert=0;
user->mail_verified=0;
user->autofwd=0;
user->editing=0;
user->hwrap_lev=0;
user->ignlogons=0;
user->ignwiz=0;
user->ignbeeps=0;
user->afkline=0;
user->editline=0;
user->show_pass=0;
user->samesite_all_store=0;
user->hwrap_id=0;
user->hwrap_same=0;
user->hwrap_func=0;
user->cmd_type=1;
user->hang_stage=-1;
user->show_rdesc=1;
user->lmail_lev=-3; /* has to be -3 */
for (i=0;i<MAX_REMINDERS;i++) {
user->reminder[i].day=0;
user->reminder[i].month=0;
user->reminder[i].year=0;
user->reminder[i].msg[0]='\0';
}
user->temp_remind.day=0;
user->temp_remind.month=0;
user->temp_remind.year=0;
user->temp_remind.msg[0]='\0';
for (i=0;i<MAX_XCOMS;i++) user->xcoms[i]=-1;
for (i=0;i<MAX_GCOMS;i++) user->gcoms[i]=-1;
for (i=0;i<MAX_PAGES;i++) user->pages[i]=0;
user->pagecnt=0;
user->login_prompt=1;
user->user_page_pos=0;
user->user_page_lev=0;
}
/*** Destruct an object. ***/
void destruct_user(UR_OBJECT user)
{
/* Remove from linked list */
if (user==user_first) {
user_first=user->next;
if (user==user_last) user_last=NULL;
else user_first->prev=NULL;
}
else {
user->prev->next=user->next;
if (user==user_last) {
user_last=user->prev; user_last->next=NULL;
}
else user->next->prev=user->prev;
}
free(user);
destructed=1;
}
/*** Construct room object ***/
RM_OBJECT create_room(void)
{
RM_OBJECT room;
int i;
if ((room=(RM_OBJECT)malloc(sizeof(struct room_struct)))==NULL) {
fprintf(stderr,"KoF: Memory allocation failure in create_room().\n");
boot_exit(1);
}
/* Append object into linked list. */
if (room_first==NULL) {
room_first=room; room->prev=NULL;
}
else {
room_last->next=room; room->prev=room_last;
}
room->next=NULL;
room_last=room;
room->name[0]='\0';
room->label[0]='\0';
room->desc[0]='\0';
room->topic[0]='\0';
room->map[0]='\0';
room->access=-1;
room->revline=0;
room->mesg_cnt=0;
room->next=NULL;
for(i=0;i<MAX_LINKS;++i) {
room->link_label[i][0]='\0'; room->link[i]=NULL;
}
for(i=0;i<REVIEW_LINES;++i) room->revbuff[i][0]='\0';
return room;
}
/*** Destruct a room object. ***/
void destruct_room(RM_OBJECT rm) {
/* Remove from linked list */
if (rm==room_first) {
room_first=rm->next;
if (rm==room_last) room_last=NULL;
else room_first->prev=NULL;
}
else {
rm->prev->next=rm->next;
if (rm==room_last) {
room_last=rm->prev; room_last->next=NULL;
}
else rm->next->prev=rm->prev;
}
free(rm);
}
/* add a command to the commands linked list. Get which command via the passed id
int and the enum in the header file */
int add_command(int cmd_id)
{
int inserted;
struct command_struct *cmd,*tmp;
if ((cmd=(struct command_struct *)malloc(sizeof(struct command_struct)))==NULL) {
write_syslog(SYSLOG,0,"ERROR: Memory allocation failure in add_command().\n");
return 0;
}
/*
do an insertion sort on the linked list
this could take a long time, but it only needs to be done once when booting, so it
doesn't really matter
*/
inserted=0;
strcpy(cmd->name,command_table[cmd_id].name);
if (first_command==NULL) {
first_command=cmd;
cmd->prev=NULL;
cmd->next=NULL;
last_command=cmd;
}
else {
tmp=first_command;
inserted=0;
while(tmp!=NULL) {
/* insert as first item in the list */
if ((strcmp(cmd->name,tmp->name)<0) && tmp==first_command) {
first_command->prev=cmd;
cmd->prev=NULL;
cmd->next=first_command;
first_command=cmd;
inserted=1;
}
/* insert in the middle of the list somewhere */
else if (strcmp(cmd->name,tmp->name)<0) {
tmp->prev->next=cmd;
cmd->prev=tmp->prev;
tmp->prev=cmd;
cmd->next=tmp;
inserted=1;
}
if (inserted) break;
tmp=tmp->next;
} /* end while */
/* insert at the end of the list */
if (!inserted) {
last_command->next=cmd;
cmd->prev=last_command;
cmd->next=NULL;
last_command=cmd;
}
} /* end else */
cmd->id=cmd_id;
strcpy(cmd->alias,command_table[cmd_id].alias);
cmd->min_lev=command_table[cmd_id].level;
cmd->function=command_table[cmd_id].function;
cmd->count=0;
return 1;
}
/* destruct command nodes */
int rem_command(int cmd_id)
{
struct command_struct *cmd;
cmd=first_command;
/* as command object not being passed, first find what node we want to delete
for the id integer that *is* passed.
*/
while(cmd!=NULL) {
if (cmd->id==cmd_id) break;
cmd=cmd->next;
}
if (cmd==first_command) {
first_command=cmd->next;
if (cmd==last_command) last_command=NULL;
else first_command->prev=NULL;
}
else {
cmd->prev->next=cmd->next;
if (cmd==last_command) {
last_command=cmd->prev; last_command->next=NULL;
}
else cmd->next->prev=cmd->prev;
}
free(cmd);
return 1;
}
/* add a user node to the user linked list */
int add_user_node(char *name, int level)
{
struct user_dir_struct *new;
if ((new=(struct user_dir_struct *)malloc(sizeof(struct user_dir_struct)))==NULL) {
write_syslog(SYSLOG,0,"ERROR: Memory allocation failure in add_user_node().\n");
return 0;
}
if (first_dir_entry==NULL) {
first_dir_entry=new;
new->prev=NULL;
}
else {
last_dir_entry->next=new;
new->prev=last_dir_entry;
}
new->next=NULL;
last_dir_entry=new;
++amsys->user_count;
strcpy(new->name,name);
new->level=level;
new->date[0]='\0';
return 1;
}
/* remove a user node from the user linked list
have needed to use an additional check for the correct user, ie, lev.
if lev = -1 then don't do a check on the user node's level, else use
the lev and the name as a check
*/
int rem_user_node(char *name, int lev)
{
int level,found;
struct user_dir_struct *entry;
entry=first_dir_entry;
found=0;
if (lev!=-1) {
while(entry!=NULL) {
if ((!strcmp(entry->name,name)) && (entry->level==lev)) {
level=entry->level; found=1; break;
}
entry=entry->next;
}
}
else {
while(entry!=NULL) {
if (!strcmp(entry->name,name)) {
level=entry->level; found=1; break;
}
entry=entry->next;
}
}
if (!found) return 0;
if (entry==first_dir_entry) {
first_dir_entry=entry->next;
if (entry==last_dir_entry) last_dir_entry=NULL;
else first_dir_entry->prev=NULL;
}
else {
entry->prev->next=entry->next;
if (entry==last_dir_entry) {
last_dir_entry=entry->prev; last_dir_entry->next=NULL;
}
else entry->next->prev=entry->prev;
}
free(entry);
--amsys->user_count;
return 1;
}
/* put a date string in a node in the directory linked list that
matches name
*/
void add_user_date_node(char *name, char *date)
{
struct user_dir_struct *entry;
for (entry=first_dir_entry;entry!=NULL;entry=entry->next) {
if (!strcmp(entry->name,name)) {
strcpy(entry->date,date);
break;
}
}
return;
}
/* add a node to the wizzes linked list */
int add_wiz_node(char *name, int level)
{
struct wiz_list_struct *new;
if ((new=(struct wiz_list_struct *)malloc(sizeof(struct wiz_list_struct)))==NULL) {
write_syslog(SYSLOG,0,"ERROR: Memory allocation failure in add_wiz_node().\n");
return 0;
}
if (first_wiz_entry==NULL) {
first_wiz_entry=new;
new->prev=NULL;
}
else {
last_wiz_entry->next=new;
new->prev=last_wiz_entry;
}
new->next=NULL;
last_wiz_entry=new;
strcpy(new->name,name);
new->level=level;
return 1;
}
/* remove a node from the wizzes linked list
have needed to use an additional check for the correct user, ie, lev.
if lev = -1 then don't do a check on the user node's level, else use
the lev and the name as a check
*/
int rem_wiz_node(char *name)
{
int level,found;
struct wiz_list_struct *entry;
entry=first_wiz_entry;
found=0;
while(entry!=NULL) {
if (!strcmp(entry->name,name)) {
level=entry->level; found=1; break;
}
entry=entry->next;
}
if (!found) return 0;
if (entry==first_wiz_entry) {
first_wiz_entry=entry->next;
if (entry==last_wiz_entry) last_wiz_entry=NULL;
else first_wiz_entry->prev=NULL;
}
else {
entry->prev->next=entry->next;
if (entry==last_wiz_entry) {
last_wiz_entry=entry->prev; last_wiz_entry->next=NULL;
}
else entry->next->prev=entry->prev;
}
free(entry);
return 1;
}
/*** alter the level of a node in the user linked list ***/
int user_list_level(char *name, int lvl)
{
struct user_dir_struct *entry;
entry=first_dir_entry;
while(entry!=NULL) {
if (!strcmp(entry->name,name)) {
entry->level=lvl;
return(1);
}
entry=entry->next;
}
return(0);
}
/******************************************************************************
Perfom checks and searches
*****************************************************************************/
/*** Check to see if the directory structure in USERFILES is correct, ie, there
is one directory for each of the level names given in *user_level[]
Also, check if level names are unique.
***/
void check_directories(void) {
struct stat stbuf;
int levels,found,i,j;
char dirname[80];
levels=found=0;
/* Check for unique directory names */
for (i=0; i<NUM_LEVELS; ++i) {
for (j=i+1; j<NUM_LEVELS; ++j) {
if (!strcmp(user_level[i].name,user_level[j].name)) {
fprintf(stderr,"KoF: Level names are not unique.\n");
boot_exit(14);
}
}
}
i=0;
/* check the directories needed exist */
strcpy(dirname,USERFILES);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERMAILS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERPROFILES);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERFRIENDS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERHISTORYS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERCOMMANDS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERMACROS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
sprintf(dirname,"%s/%s",USERFILES,USERROOMS);
if (stat(dirname,&stbuf)==-1) {
fprintf(stderr,"KoF: Directory stat failure in check_directories().\n");
boot_exit(15);
}
if ((stbuf.st_mode & S_IFMT)!=S_IFDIR) goto SKIP;
return;
SKIP:
fprintf(stderr,"KoF: Directory structure is incorrect.\n");
boot_exit(16);
}
/*** Check to see if the pattern 'pat' appears in the string 'str'.
Uses recursion to acheive this
***/
int pattern_match(char *str, char *pat) {
int i, slraw;
/* if end of both, strings match */
if ((*pat=='\0') && (*str=='\0')) return(1);
if (*pat=='\0') return(0);
if (*pat=='*') {
if (*(pat+1)=='\0') return(1);
for(i=0,slraw=strlen(str);i<=slraw;i++)
if ((*(str+i)==*(pat+1)) || (*(pat+1)=='?'))
if (pattern_match(str+i+1,pat+2)==1) return(1);
} /* end if */
else {
if (*str=='\0') return(0);
if ((*pat=='?') || (*pat==*str))
if (pattern_match(str+1,pat+1)==1) return(1);
} /* end else */
return(0);
}
/*** See if users site is banned ***/
int site_banned(char *sbanned, int new) {
FILE *fp;
char line[82],filename[80];
if (new) sprintf(filename,"%s/%s",DATAFILES,NEWBAN);
else sprintf(filename,"%s/%s",DATAFILES,SITEBAN);
if (!(fp=fopen(filename,"r"))) return (0);
fscanf(fp,"%s",line);
while(!feof(fp)) {
/* first do full name comparison */
if (!strcmp(sbanned,line)) { fclose(fp); return 1; }
/* check using pattern matching */
if (pattern_match(sbanned,line)) {
fclose(fp);
return(1);
}
fscanf(fp,"%s",line);
}
fclose(fp);
return(0);
}
/*** checks to see if someone is already in login-stage on the ports ***/
int login_port_flood(char *asite)
{
UR_OBJECT u;
int cnt=0;
for(u=user_first;u!=NULL;u=u->next)
if (u->login && (!strcmp(asite,u->site) || !strcmp(asite,u->ipsite))) cnt++;
if (cnt>=LOGIN_FLOOD_CNT) return 1;
return 0;
}
/*** See if user is banned ***/
int user_banned(char *name) {
FILE *fp;
char line[82],filename[80];
sprintf(filename,"%s/%s",DATAFILES,USERBAN);
if (!(fp=fopen(filename,"r"))) return 0;
fscanf(fp,"%s",line);
while(!feof(fp)) {
if (!strcmp(line,name)) { fclose(fp); return 1; }
fscanf(fp,"%s",line);
}
fclose(fp);
return 0;
}
/*** Set room access back to public if not enough users in room ***/
void reset_access(RM_OBJECT rm) {
UR_OBJECT u;
int cnt;
if (rm==NULL || rm->access!=PRIVATE) return;
cnt=0;
for(u=user_first;u!=NULL;u=u->next) if (u->room==rm) ++cnt;
if (cnt<amsys->min_private_users) {
write_room(rm,"Room access returned to ~FGPUBLIC.\n");
rm->access=PUBLIC;
/* Reset any invites into the room & clear review buffer */
for(u=user_first;u!=NULL;u=u->next) {
if (u->invite_room==rm) u->invite_room=NULL;
}
clear_revbuff(rm);
}
}
/*** Get user struct pointer from name ***/
UR_OBJECT get_user(char *name) {
UR_OBJECT u;
name[0]=toupper(name[0]);
/* Search for exact name */
for(u=user_first;u!=NULL;u=u->next) {
if (u->login || u->type==CLONE_TYPE) continue;
if (!strcmp(u->name,name)) return u;
}
return NULL;
}
/*** Get user struct pointer from abreviated name ***/
UR_OBJECT get_user_name(UR_OBJECT user, char *i_name) {
UR_OBJECT u,last;
int found=0;
char name[USER_NAME_LEN],buff[ARR_SIZE];
last=NULL;
strncpy(name,i_name,USER_NAME_LEN);
name[0]=toupper(name[0]); buff[0]=0;
for (u=user_first;u!=NULL;u=u->next)
if (!strcmp(u->name,name) && u->room!=NULL) return u;
for (u=user_first;u!=NULL;u=u->next) {
if (u->type==CLONE_TYPE) continue;
if (instr(u->name,name) != -1) {
strcat(buff,u->name);
strcat(buff, " ");
found++;
last=u;
}
} /* end for */
if (found == 0) return NULL;
if (found >1) {
write_user(user, "~FR~OLName is not unique.\n\n");
vwrite_user(user," ~OL%s\n\n",buff);
return NULL;
}
else return(last);
}
/*** Get room struct pointer from abbreviated name ***/
RM_OBJECT get_room(char *name) {
RM_OBJECT rm;
for(rm=room_first;rm!=NULL;rm=rm->next)
if (!strncmp(rm->name,name,strlen(name))) return rm;
return NULL;
}
/*** Get room struct pointer from full name ***/
RM_OBJECT get_room_full(char *name) {
RM_OBJECT rm;
for(rm=room_first;rm!=NULL;rm=rm->next)
if (!strcmp(rm->name,name)) return rm;
return NULL;
}
/*** Return level value based on level name ***/
int get_level(char *name) {
int i;
for (i=0;i<NUM_LEVELS;i++) {
if (!strcasecmp(user_level[i].name,name)) return i;
}
return -1;
}
/*** See if a user has access to a room. If room is fixed to private then
it is considered a wizroom so grant permission to any user of WIZ and
above for those. ***/
int has_room_access(UR_OBJECT user, RM_OBJECT rm) {
int i=0;
/* level room checks */
while (priv_room[i].name[0]!='*') {
if (!strcmp(rm->name,priv_room[i].name)
&& user->level<priv_room[i].level
&& user->invite_room!=rm) return 0;
i++;
}
/* personal room checks */
if (rm->access==PERSONAL_UNLOCKED) return 1; /* open to all */
if ((rm->access==PERSONAL_LOCKED && is_my_room(user,rm))
|| has_room_key(user->name,rm)
|| user->level==CHAOS
|| user->invite_room==rm) return 1;
if ((rm->access==PERSONAL_LOCKED && !is_my_room(user,rm))
&& user->level<CHAOS
&& user->invite_room!=rm) return 0;
/* fixed room checks */
if ((rm->access & PRIVATE)
&& user->level<amsys->gatecrash_level
&& user->invite_room!=rm
&& !((rm->access & FIXED) && user->level>=WIZ)) return 0;
/* have access */
return 1;
}
/* Check the room you're logging into isn't private */
void check_start_room(UR_OBJECT user) {
RM_OBJECT rm;
if (!user->logout_room[0] || !user->lroom) {
user->room=room_first;
return;
}
rm=get_room(user->logout_room);
if (rm==NULL) {
user->room=room_first;
return;
}
if (rm->access==PRIVATE || (rm->access==FIXED_PRIVATE && user->level<WIZ)) {
vwrite_user(user,"\nThe room you logged out of is now private - connecting into the %s.\n\n",room_first->name);
user->room=room_first;
return;
}
user->room=rm;
vwrite_user(user,"\nYou are connecting into the ~FG%s~RS room.\n\n",rm->name);
}
/*** find out if a user is listed in the user linked list ***/
int find_user_listed(char *name) {
struct user_dir_struct *entry;
name[0]=toupper(name[0]);
for (entry=first_dir_entry;entry!=NULL;entry=entry->next)
if (!strcmp(entry->name,name)) return 1;
return 0;
}
/*** check to see if a given email has the correct format ***/
int validate_email(char *email) {
int dots;
char *p;
dots=0;
p=email;
if (!*p) return 0;
while (*p!='@' && *p) {
if (!isalnum(*p) && *p!='.' && *p!='_' && *p!='-') return 0;
p++;
}
if (*p!='@') return 0;
p++;
if (*p==' ' || *p=='.' || *p=='@' || !*p) return 0;
while (*p) {
while (*p!='.') {
if (!*p) {
if (!(dots)) return 0;
else return 1;
}
p++;
} /* end while */
dots++;
p++;
if (*p==' ' || *p=='.' || !*p) return 0;
if (!*p) return 1;
} /* end while */
return 1;
}
/*** Checks to see if a user with the given name is currently logged on ***/
int user_logged_on(char *name) {
UR_OBJECT u;
for (u=user_first;u!=NULL;u=u->next) {
if (u->login) continue;
if (!strcmp(name,u->name)) return 1;
}
return 0;
}
/*** Checks to see if the room a user is in is private ***/
int in_private_room(UR_OBJECT user) {
if (user->room->access==PRIVATE || user->room->access==FIXED_PRIVATE) return 1;
return 0;
}
/*** Check to see if a command has been given to a user ***/
int has_gcom(UR_OBJECT user, int cmd_id) {
int i;
for (i=0;i<MAX_GCOMS;i++) if (user->gcoms[i]==cmd_id) return 1;
return 0;
}
/*** Check to see if a command has been taken from a user ***/
int has_xcom(UR_OBJECT user, int cmd_id) {
int i;
for (i=0;i<MAX_XCOMS;i++) if (user->xcoms[i]==cmd_id) return 1;
return 0;
}
/**** check to see if the room given is a personal room ***/
int is_personal_room(RM_OBJECT rm) {
if (rm->access==PERSONAL_LOCKED || rm->access==PERSONAL_UNLOCKED) return 1;
return 0;
}
/**** find out if the room given is the perosnal room of the user ***/
int is_my_room(UR_OBJECT user,RM_OBJECT rm) {
char name[ROOM_NAME_LEN+1];
sprintf(name,"(%s)",user->name);
strtolower(name);
if (!strcmp(name,rm->name)) return 1;
return 0;
}
/*** check to see how many people are in a given room ***/
int room_visitor_count(RM_OBJECT rm) {
UR_OBJECT u;
int cnt=0;
if (rm==NULL) return cnt;
for (u=user_first;u!=NULL;u=u->next) if (u->room==rm) ++cnt;
return cnt;
}
/*** See if user is banned ***/
int has_room_key(char *visitor,RM_OBJECT rm) {
FILE *fp;
char line[82],filename[80],rmname[USER_NAME_LEN];
/* get user's name from room name */
midcpy(rm->name,rmname,1,strlen(rm->name)-2);
rmname[0]=toupper(rmname[0]);
/* check if user is listed */
sprintf(filename,"%s/%s/%s.K",USERFILES,USERROOMS,rmname);
if (!(fp=fopen(filename,"r"))) return 0;
fscanf(fp,"%s",line);
while(!feof(fp)) {
if (!strcmp(line,visitor)) { fclose(fp); return 1; }
fscanf(fp,"%s",line);
}
fclose(fp);
return 0;
}
/******************************************************************************
Setting up of the sockets
*****************************************************************************/
/*** Set up readmask for select ***/
void setup_readmask(fd_set *mask) {
UR_OBJECT user;
int i;
FD_ZERO(mask);
for(i=0;i<port_total;++i) FD_SET(listen_sock[i],mask);
/* Do users */
for (user=user_first;user!=NULL;user=user->next)
if (user->type==USER_TYPE) FD_SET(user->socket,mask);
/* Do client-server stuff */
}
/*** Accept incoming connections on listen sockets ***/
void accept_connection(int lsock, int num) {
UR_OBJECT user;
char named_site[80],ip_site[16],motdname[80];
struct sockaddr_in acc_addr;
struct hostent *host;
int accept_sock;
unsigned int size;
named_site[0]=ip_site[0]='\0';
size=sizeof(struct sockaddr_in);
accept_sock=accept(lsock,(struct sockaddr *)&acc_addr,&size);
/* Get number addr */
strcpy(ip_site,(char *)inet_ntoa(acc_addr.sin_addr));
/* Get named site
Hanging usually happens on BSD systems when using gethostbyaddr. If this happens
to you then alter the resolve_ip setting in the config file.
*/
switch(amsys->resolve_ip) {
case 0:
/* don't resolve */
strcpy(named_site,ip_site);
break;
case 1:
/* resolve automatically */
if ((host=gethostbyaddr((char *)&acc_addr.sin_addr,sizeof(acc_addr.sin_addr),AF_INET))!=NULL) {
strcpy(named_site,host->h_name);
strtolower(named_site);
}
else strcpy(named_site,ip_site);
break;
case 2:
/* resolve with function by tref */
strcpy(named_site,((char *)resolve_ip(ip_site)));
break;
}
if (site_banned(ip_site,0) || site_banned(named_site,0)) {
write_sock(accept_sock,"\n\rLogins from your site/domain are banned.\n\n\r");
close(accept_sock);
write_syslog(SYSLOG,1,"Attempted login from banned site %s (%s).\n",named_site,ip_site);
return;
}
if (amsys->flood_protect && (login_port_flood(ip_site) || login_port_flood(named_site))) {
write_sock(accept_sock,"\n\rYou have attempted to flood this talker's ports. We view this as\n\r");
write_sock(accept_sock,"a sign that you are trying to hack this system. Therefor you have now\n\r");
write_sock(accept_sock,"had logins from your site/domain banned.\n\n\r");
close(accept_sock);
write_syslog(SYSLOG,1,"Attempted port flood from site %s (%s).\n",named_site,ip_site);
auto_ban_site(ip_site);
return;
}
/* get random motd1 and send pre-login message */
if (amsys->motd1_cnt) {
sprintf(motdname,"%s/motd1/motd%d",MOTDFILES,(get_motd_num(1)));
more(NULL,accept_sock,motdname);
}
else {
sprintf(text,"Welcome to %s!\n\n\rSorry, but the login screen appears to be missing at this time.\n\r",talker_name);
write_sock(accept_sock,text);
}
if (amsys->num_of_users+amsys->num_of_logins>=amsys->max_users && !num) {
write_sock(accept_sock,"\n\rSorry, but the talker is full at the moment. Please try later.\n\n\r");
close(accept_sock);
return;
}
if ((user=create_user())==NULL) {
sprintf(text,"\n\r%s: unable to create session.\n\n\r",syserror);
write_sock(accept_sock,text);
close(accept_sock);
return;
}
user->socket=accept_sock;
user->login=LOGIN_NAME;
user->last_input=time(0);
if (!num) user->port=port[0];
else {
user->port=port[1];
write_user(user,"** Wizport login **\n\n");
}
strcpy(user->site,named_site);
strcpy(user->ipsite,ip_site);
user->site_port=(int)ntohs(acc_addr.sin_port);
echo_on(user);
write_user(user,"Give me a name: ");
amsys->num_of_logins++;
}
/*** Resolve an IP address if the resolve_ip set to MANUAL in config file.
This code was written by tref, and submitted to ewtoo.org by ruGG.
Claimed useful for BSD systems in which gethostbyaddr() calls caused
extreme hanging/blocking of the talker. Note, popen is a blocking
call however.
***/
char *resolve_ip(char* host) {
FILE* hp;
static char str[256];
char *txt,*t;
sprintf(str,"/usr/bin/host %s",host);
hp=popen(str,"r");
*str=0;
fgets(str,255,hp);
pclose(hp);
txt=strchr(str,':');
if (txt) {
txt++;
while (isspace(*txt)) txt++; t=txt;
while (*t && *t!='\n') t++; *t=0;
return(txt);
}
return(host);
}
/*** Initialise sockets on ports ***/
void init_sockets(void) {
struct sockaddr_in bind_addr;
int i,on,size;
on=1;
size=sizeof(struct sockaddr_in);
bind_addr.sin_family=AF_INET;
bind_addr.sin_addr.s_addr=INADDR_ANY;
for(i=0;i<port_total;++i) {
/* create sockets */
if ((listen_sock[i]=socket(AF_INET,SOCK_STREAM,0))==-1) boot_exit(i+2);
/* allow reboots on port even with TIME_WAITS */
setsockopt(listen_sock[i],SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
/* bind sockets and set up listen queues */
bind_addr.sin_port=htons(port[i]);
if (bind(listen_sock[i],(struct sockaddr *)&bind_addr,size)==-1) boot_exit(i+5);
if (listen(listen_sock[i],10)==-1) boot_exit(i+8);
/* Set to non-blocking */
fcntl(listen_sock[i],F_SETFL,O_NDELAY);
}
}
/******************************************************************************
The loading up and parsing of the configuration file
*****************************************************************************/
void load_and_parse_config(void) {
FILE *fp;
char line[81]; /* Should be long enough */
char c,filename[80];
int i,section_in,got_init,got_rooms,got_topics;
RM_OBJECT rm1,rm2;
section_in=0;
got_init=0;
got_rooms=0;
got_topics=0;
sprintf(filename,"%s/%s",DATAFILES,confile);
printf("Parsing config file \"%s\"...\n",filename);
if (!(fp=fopen(filename,"r"))) {
perror("KoF: Can't open config file"); boot_exit(1);
}
/* Main reading loop */
config_line=0;
fgets(line,81,fp);
while(!feof(fp)) {
config_line++;
for(i=0;i<8;++i) wrd[i][0]='\0';
sscanf(line,"%s %s %s %s %s %s %s %s",wrd[0],wrd[1],wrd[2],wrd[3],wrd[4],wrd[5],wrd[6],wrd[7]);
if (wrd[0][0]=='#' || wrd[0][0]=='\0') {
fgets(line,81,fp); continue;
}
/* See if new section */
if (wrd[0][strlen(wrd[0])-1]==':') {
if (!strcmp(wrd[0],"INIT:")) section_in=1;
else if (!strcmp(wrd[0],"ROOMS:")) section_in=2;
else if (!strcmp(wrd[0],"TOPICS:")) section_in=3;
else if (!strcmp(wrd[0],"SITES:")) section_in=4;
else {
fprintf(stderr,"KoF: Unknown section header on line %d.\n",config_line);
fclose(fp); boot_exit(1);
}
}
switch(section_in) {
case 1: parse_init_section(); got_init=1; break;
case 2: parse_rooms_section(); got_rooms=1; break;
case 3: parse_topics_section(remove_first(line)); got_topics=1; break;
case 4:
default:
fprintf(stderr,"KoF: Section header expected on line %d.\n",config_line);
fclose(fp); boot_exit(1);
}
fgets(line,81,fp);
}
fclose(fp);
/* See if required sections were present (SITES and TOPICS is optional) and if
required parameters were set. */
if (!got_init) {
fprintf(stderr,"KoF: INIT section missing from config file.\n");
boot_exit(1);
}
if (got_topics && !got_rooms) {
fprintf(stderr,"KoF: TOPICS section must come after ROOMS section in the config file.\n");
boot_exit(1);
}
if (!got_rooms) {
fprintf(stderr,"KoF: ROOMS section missing from config file.\n");
boot_exit(1);
}
if (!port[0]) {
fprintf(stderr,"KoF: Main port number not set in config file.\n");
boot_exit(1);
}
if (!port[1]) {
fprintf(stderr,"KoF: Wiz port number not set in config file.\n");
boot_exit(1);
}
if (room_first==NULL) {
fprintf(stderr,"KoF: No rooms configured in config file.\n");
boot_exit(1);
}
/* Parsing done, now check data is valid. Check room stuff first. */
for(rm1=room_first;rm1!=NULL;rm1=rm1->next) {
for(i=0;i<MAX_LINKS;++i) {
if (!rm1->link_label[i][0]) break;
for(rm2=room_first;rm2!=NULL;rm2=rm2->next) {
if (rm1==rm2) continue;
if (!strcmp(rm1->link_label[i],rm2->label)) {
rm1->link[i]=rm2;
break;
}
}
if (rm1->link[i]==NULL) {
fprintf(stderr,"KoF: Room %s has undefined link label '%s'.\n",rm1->name,rm1->link_label[i]);
boot_exit(1);
}
}
}
/* Load room descriptions */
for(rm1=room_first;rm1!=NULL;rm1=rm1->next) {
sprintf(filename,"%s/%s.R",DATAFILES,rm1->name);
if (!(fp=fopen(filename,"r"))) {
fprintf(stderr,"KoF: Can't open description file for room %s.\n",rm1->name);
write_syslog(SYSLOG,0,"ERROR: Couldn't open description file for room %s.\n",rm1->name);
continue;
}
i=0;
c=getc(fp);
while(!feof(fp)) {
if (i==ROOM_DESC_LEN) {
fprintf(stderr,"KoF: Description too long for room %s.\n",rm1->name);
write_syslog(SYSLOG,0,"ERROR: Description too long for room %s.\n",rm1->name);
break;
}
rm1->desc[i]=c;
c=getc(fp);
++i;
}
rm1->desc[i]='\0';
fclose(fp);
}
}
/*** Parse init section ***/
void parse_init_section(void) {
static int in_section=0;
int op,val,tmp;
char *options[]={
"mainport","wizport","linkport","system_logging","minlogin_level","mesg_life",
"wizport_level","prompt_def","gatecrash_level","min_private","ignore_mp_level",
"rem_user_maxlevel","rem_user_deflevel","verification","mesg_check_time",
"max_users","heartbeat","login_idle_time","user_idle_time","password_echo",
"ignore_sigterm","auto_connect","max_clones","ban_swearing","crash_action",
"colour_def","time_out_afks","charecho_def","time_out_maxlevel","auto_purge",
"allow_recaps","auto_promote","personal_rooms","random_motds","startup_room_parse",
"resolve_ip","flood_protect","*"
};
if (!strcmp(wrd[0],"INIT:")) {
if (++in_section>1) {
fprintf(stderr,"KoF: Unexpected INIT section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
op=0; tmp=0;
while(strcmp(options[op],wrd[0])) {
if (options[op][0]=='*') {
fprintf(stderr,"KoF: Unknown INIT option on line %d.\n",config_line);
boot_exit(1);
}
++op;
}
if (!wrd[1][0]) {
fprintf(stderr,"KoF: Required parameter missing on line %d.\n",config_line);
boot_exit(1);
}
if (wrd[2][0] && wrd[2][0]!='#') {
fprintf(stderr,"KoF: Unexpected word following init parameter on line %d.\n",config_line);
boot_exit(1);
}
val=atoi(wrd[1]);
switch(op) {
case 0: /* main port */
case 1: /* wiz */
if ((port[op]=val)<1 || val>65535) {
fprintf(stderr,"KoF: Illegal port number on line %d.\n",config_line);
boot_exit(1);
}
return;
case 3:
if ((tmp=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: System_logging must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
/* set the bits correctly */
if (tmp) {
amsys->logging=BIT_SET(amsys->logging,SYSLOG);
amsys->logging=BIT_SET(amsys->logging,REQLOG);
}
else amsys->logging=0;
return;
case 4:
if ((amsys->minlogin_level=get_level(wrd[1]))==-1) {
if (strcmp(wrd[1],"NONE")) {
fprintf(stderr,"KoF: Unknown level specifier for minlogin_level on line %d.\n",config_line);
boot_exit(1);
}
amsys->minlogin_level=-1;
}
return;
case 5: /* message lifetime */
if ((amsys->mesg_life=val)<1) {
fprintf(stderr,"KoF: Illegal message lifetime on line %d.\n",config_line);
boot_exit(1);
}
return;
case 6: /* wizport_level */
if ((amsys->wizport_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for wizport_level on line %d.\n",config_line);
boot_exit(1);
}
return;
case 7: /* prompt defaults */
if ((amsys->prompt_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Prompt_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 8: /* gatecrash level */
if ((amsys->gatecrash_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for gatecrash_level on line %d.\n",config_line);
boot_exit(1);
}
return;
case 9:
if (val<1) {
fprintf(stderr,"KoF: Number too low for min_private_users on line %d.\n",config_line);
boot_exit(1);
}
amsys->min_private_users=val;
return;
case 10:
if ((amsys->ignore_mp_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for ignore_mp_level on line %d.\n",config_line);
boot_exit(1);
}
return;
case 11:
/* Max level a remote user can remotely log in if he doesn't have a local
account. ie if level set to WIZ a GOD can only be a WIZ if logging in
from another server unless he has a local account of level GOD */
if ((amsys->rem_user_maxlevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for rem_user_maxlevel on line %d.\n",config_line);
boot_exit(1);
}
return;
case 12:
/* Default level of remote user who does not have an account on site and
connection is from a server of version 3.3.0 or lower. */
if ((amsys->rem_user_deflevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for rem_user_deflevel on line %d.\n",config_line);
boot_exit(1);
}
return;
case 14: /* mesg_check_time */
if (wrd[1][2]!=':'
|| strlen(wrd[1])>5
|| !isdigit(wrd[1][0])
|| !isdigit(wrd[1][1])
|| !isdigit(wrd[1][3])
|| !isdigit(wrd[1][4])) {
fprintf(stderr,"KoF: Invalid message check time on line %d.\n",config_line);
boot_exit(1);
}
sscanf(wrd[1],"%d:%d",&amsys->mesg_check_hour,&amsys->mesg_check_min);
if (amsys->mesg_check_hour>23 || amsys->mesg_check_min>59) {
fprintf(stderr,"KoF: Invalid message check time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 15:
if ((amsys->max_users=val)<1) {
fprintf(stderr,"KoF: Invalid value for max_users on line %d.\n",config_line);
boot_exit(1);
}
return;
case 16:
if ((amsys->heartbeat=val)<1) {
fprintf(stderr,"KoF: Invalid value for heartbeat on line %d.\n",config_line);
boot_exit(1);
}
return;
case 17:
if ((amsys->login_idle_time=val)<10) {
fprintf(stderr,"KoF: Invalid value for login_idle_time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 18:
if ((amsys->user_idle_time=val)<10) {
fprintf(stderr,"KoF: Invalid value for user_idle_time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 19:
if ((amsys->password_echo=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Password_echo must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 20:
if ((amsys->ignore_sigterm=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Ignore_sigterm must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 21:
if ((amsys->auto_connect=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Auto_connect must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 22:
if ((amsys->max_clones=val)<0) {
fprintf(stderr,"KoF: Invalid value for max_clones on line %d.\n",config_line);
boot_exit(1);
}
return;
case 23:
if ((amsys->ban_swearing=minmax_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Ban_swearing must be OFF, MIN or MAX on line %d.\n",config_line);
boot_exit(1);
}
return;
case 24:
if (!strcmp(wrd[1],"NONE")) amsys->crash_action=0;
else if (!strcmp(wrd[1],"IGNORE")) amsys->crash_action=1;
else if (!strcmp(wrd[1],"REBOOT")) amsys->crash_action=2;
else {
fprintf(stderr,"KoF: Crash_action must be NONE, IGNORE or REBOOT on line %d.\n",config_line);
boot_exit(1);
}
return;
case 25:
if ((amsys->colour_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Colour_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 26:
if ((amsys->time_out_afks=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Time_out_afks must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 27:
if ((amsys->charecho_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Charecho_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 28:
if ((amsys->time_out_maxlevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"KoF: Unknown level specifier for time_out_maxlevel on line %d.\n",config_line);
boot_exit(1);
}
return;
case 29: /* auto purge on boot up */
if ((amsys->auto_purge=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Auto_purge must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 30: /* allow recapping of names */
if ((amsys->allow_recaps=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Allow_recaps must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 31: /* define whether auto promotes are on or off */
if ((amsys->auto_promote=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: auto_promote must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 32:
if ((amsys->personal_rooms=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Personal_rooms must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 33:
if ((amsys->random_motds=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Random_motds must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 34:
if ((amsys->startup_room_parse=yn_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Startup_room_parse must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 35:
if ((amsys->resolve_ip=resolve_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Resolve_ip must be OFF, AUTO or MANUAL on line %d.\n",config_line);
boot_exit(1);
}
return;
case 36: /* turns flood protection and auto-baning on and off */
if ((amsys->flood_protect=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"KoF: Flood_protect must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
} /* end switch */
}
/*** Parse rooms section ***/
void parse_rooms_section(void) {
static int in_section=0;
int i;
char *ptr1,*ptr2,c;
RM_OBJECT room;
if (!strcmp(wrd[0],"ROOMS:")) {
if (++in_section>1) {
fprintf(stderr,"KoF: Unexpected ROOMS section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
if (!wrd[3][0]) {
fprintf(stderr,"KoF: Required parameter(s) missing on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[0])>ROOM_NAME_LEN) {
fprintf(stderr,"KoF: Room map name too long on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[1])>ROOM_LABEL_LEN) {
fprintf(stderr,"KoF: Room label too long on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[2])>ROOM_NAME_LEN) {
fprintf(stderr,"KoF: Room name too long on line %d.\n",config_line);
boot_exit(1);
}
/* Check for duplicate label or name */
for(room=room_first;room!=NULL;room=room->next) {
if (!strcmp(room->label,wrd[1])) {
fprintf(stderr,"KoF: Duplicate room label on line %d.\n",config_line);
boot_exit(1);
}
if (!strcmp(room->name,wrd[2])) {
fprintf(stderr,"KoF: Duplicate room name on line %d.\n",config_line);
boot_exit(1);
}
}
room=create_room();
strcpy(room->map,wrd[0]);
strcpy(room->label,wrd[1]);
strcpy(room->name,wrd[2]);
/* Parse internal links bit ie hl,gd,of etc. MUST NOT be any spaces between
the commas */
i=0;
ptr1=wrd[3];
ptr2=wrd[3];
while(1) {
while(*ptr2!=',' && *ptr2!='\0') ++ptr2;
if (*ptr2==',' && *(ptr2+1)=='\0') {
fprintf(stderr,"KoF: Missing link label on line %d.\n",config_line);
boot_exit(1);
}
c=*ptr2; *ptr2='\0';
if (!strcmp(ptr1,room->label)) {
fprintf(stderr,"KoF: Room has a link to itself on line %d.\n",config_line);
boot_exit(1);
}
strcpy(room->link_label[i],ptr1);
if (c=='\0') break;
if (++i>=MAX_LINKS) {
fprintf(stderr,"KoF: Too many links on line %d.\n",config_line);
boot_exit(1);
}
*ptr2=c;
ptr1=++ptr2;
}
/* Parse access privs */
if (wrd[4][0]=='#') { room->access=PUBLIC; return; }
if (!wrd[4][0] || !strcmp(wrd[4],"BOTH")) room->access=PUBLIC;
else if (!strcmp(wrd[4],"PUB")) room->access=FIXED_PUBLIC;
else if (!strcmp(wrd[4],"PRIV")) room->access=FIXED_PRIVATE;
else {
fprintf(stderr,"KoF: Unknown room access type on line %d.\n",config_line);
boot_exit(1);
}
}
/*** Parse rooms desc (topic) section ***/
void parse_topics_section(char *topic) {
static int in_section=0;
int exists;
RM_OBJECT room;
if (!strcmp(wrd[0],"TOPICS:")) {
if (++in_section>1) {
fprintf(stderr,"KoF: Unexpected TOPICS section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
if (!wrd[1][0]) {
fprintf(stderr,"KoF: Required parameter(s) missing on line %d.\n",config_line);
boot_exit(1);
}
/* Check to see if room exists */
exists=0;
for(room=room_first;room!=NULL;room=room->next)
if (!strcmp(room->name,wrd[0])) { ++exists; break; }
if (!exists) {
fprintf(stderr,"KoF: Room does not exist on line %d.\n",config_line);
boot_exit(1);
}
if (topic[strlen(topic)-1]=='\n') topic[strlen(topic)-1]='\0';
strncpy(room->topic,topic,TOPIC_LEN);
}
/*** Parse the user rooms ***/
void parse_user_rooms(void) {
DIR *dirp;
struct dirent *dp;
char dirname[80],name[USER_NAME_LEN];
RM_OBJECT rm;
sprintf(dirname,"%s/%s",USERFILES,USERROOMS);
if (!(dirp=opendir(dirname))) {
fprintf(stderr,"KoF: Directory open failure in parse_user_rooms().\n");
boot_exit(19);
}
/* parse the names of the files but don't include . and .. */
while((dp=readdir(dirp))!=NULL) {
if (!strcmp(dp->d_name,".") || !strcmp(dp->d_name,"..")) continue;
if (strstr(dp->d_name,".R")) {
strcpy(name,dp->d_name);
name[strlen(name)-2]='\0';
rm=create_room();
if (!(personal_room_store(name,0,rm))) {
write_syslog(SYSLOG,1,"ERROR: Could not read personal room attributes. Using standard config.\n");
}
strtolower(name);
sprintf(rm->name,"(%s)",name);
}
}
(void) closedir(dirp);
}
/******************************************************************************
Signal handlers and exit functions
*****************************************************************************/
/*** Initialise the signal traps etc ***/
void init_signals(void)
{
SIGNAL(SIGTERM,sig_handler);
SIGNAL(SIGSEGV,sig_handler);
SIGNAL(SIGBUS,sig_handler);
SIGNAL(SIGILL,SIG_IGN);
SIGNAL(SIGTRAP,SIG_IGN);
SIGNAL(SIGIOT,SIG_IGN);
SIGNAL(SIGTSTP,SIG_IGN);
SIGNAL(SIGCONT,SIG_IGN);
SIGNAL(SIGHUP,SIG_IGN);
SIGNAL(SIGINT,SIG_IGN);
SIGNAL(SIGQUIT,SIG_IGN);
SIGNAL(SIGABRT,SIG_IGN);
SIGNAL(SIGFPE,SIG_IGN);
SIGNAL(SIGPIPE,SIG_IGN);
SIGNAL(SIGTTIN,SIG_IGN);
SIGNAL(SIGTTOU,SIG_IGN);
}
/*** Talker signal handler function. Can either shutdown, ignore or reboot
if a unix error occurs though if we ignore it we're living on borrowed
time as usually it will crash completely after a while anyway. ***/
void sig_handler(int sig)
{
force_listen=1;
dump_commands(sig);
switch(sig) {
case SIGTERM:
if (amsys->ignore_sigterm) {
write_syslog(SYSLOG,1,"SIGTERM signal received - ignoring.\n");
return;
}
write_room(NULL,"\n\n~OLSYSTEM:~FR~LI SIGTERM received, initiating shutdown!\n\n");
talker_shutdown(NULL,"a termination signal (SIGTERM)",0);
break; /* don't really need this here, but better safe... */
case SIGSEGV:
switch(amsys->crash_action) {
case 0:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI PANIC - Segmentation fault, initiating shutdown!\n\n");
talker_shutdown(NULL,"a segmentation fault (SIGSEGV)",0);
break;
case 1:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI WARNING - A segmentation fault has just occured!\n\n");
write_syslog(SYSLOG,1,"WARNING: A segmentation fault occured!\n");
longjmp(jmpvar,0);
break;
case 2:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI PANIC - Segmentation fault, initiating reboot!\n\n");
talker_shutdown(NULL,"a segmentation fault (SIGSEGV)",1);
break;
} /* end switch */
case SIGBUS:
switch(amsys->crash_action) {
case 0:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI PANIC - Bus error, initiating shutdown!\n\n");
talker_shutdown(NULL,"a bus error (SIGBUS)",0);
break;
case 1:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI WARNING - A bus error has just occured!\n\n");
write_syslog(SYSLOG,1,"WARNING: A bus error occured!\n");
longjmp(jmpvar,0);
break;
case 2:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI PANIC - Bus error, initiating reboot!\n\n");
talker_shutdown(NULL,"a bus error (SIGBUS)",1);
break;
} /* end switch */
} /* end switch */
}
/*** Exit because of error during bootup ***/
void boot_exit(int code)
{
switch(code) {
case 1:
write_syslog(SYSLOG,1,"BOOT FAILURE: Error while parsing configuration file.\n");
exit(1);
case 2:
perror("KoF: Can't open main port listen socket");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't open main port listen socket.\n");
exit(2);
case 3:
perror("KoF: Can't open wiz port listen socket");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't open wiz port listen socket.\n");
exit(3);
case 4:
perror("KoF: Can't open link port listen socket");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't open link port listen socket.\n");
exit(4);
case 5:
perror("KoF: Can't bind to main port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't bind to main port.\n");
exit(5);
case 6:
perror("KoF: Can't bind to wiz port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't bind to wiz port.\n");
exit(6);
case 7:
perror("KoF: Can't bind to link port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Can't bind to link port.\n");
exit(7);
case 8:
perror("KoF: Listen error on main port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Listen error on main port.\n");
exit(8);
case 9:
perror("KoF: Listen error on wiz port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Listen error on wiz port.\n");
exit(9);
case 10:
perror("KoF: Listen error on link port");
write_syslog(SYSLOG,1,"BOOT FAILURE: Listen error on link port.\n");
exit(10);
case 11:
perror("KoF: Failed to fork");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to fork.\n");
exit(11);
case 12:
perror("KoF: Failed to parse user structure");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to parse user structure.\n");
exit(12);
case 13:
perror("KoF: Failed to parse user commands");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to parse user commands.\n");
exit(13);
case 14:
write_syslog(SYSLOG,1,"BOOT FAILURE: Level names are not unique.\n");
exit(14);
case 15:
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to read directory structure in USERFILES.\n");
exit(15);
case 16:
perror("KoF: Directory structure in USERFILES is incorrect");
write_syslog(SYSLOG,1,"BOOT FAILURE: Directory structure in USERFILES is incorrect.\n");
exit(16);
case 17:
perror("KoF: Failed to create temp user structure");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to create temp user structure.\n");
exit(17);
case 18:
perror("KoF: Failed to parse a user structure");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to parse a user structure.\n");
exit(18);
case 19:
perror("KoF: Failed to open directory USERROOMS for reading");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to open directory USERROOMS.\n");
exit(19);
case 20:
perror("KoF: Failed to open directory in MOTDFILES for reading");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to open a directory MOTDFILES.\n");
exit(20);
case 21:
perror("KoF: Failed to create system object");
write_syslog(SYSLOG,1,"BOOT FAILURE: Failed to create system object.\n");
exit(21);
} /* end switch */
}
/******************************************************************************
Event functions
*****************************************************************************/
void do_events(int sig)
{
set_date_time();
check_reboot_shutdown();
check_idle_and_timeout();
check_messages(NULL,0);
reset_alarm();
}
void reset_alarm(void)
{
SIGNAL(SIGALRM,do_events);
alarm(amsys->heartbeat);
}
/*** See if timed reboot or shutdown is underway ***/
void check_reboot_shutdown(void)
{
int secs;
char *w[]={ "~FRShutdown","~FYRebooting" };
if (amsys->rs_user==NULL) return;
amsys->rs_countdown-=amsys->heartbeat;
if (amsys->rs_countdown<=0) talker_shutdown(amsys->rs_user,NULL,amsys->rs_which);
/* Print countdown message every minute unless we have less than 1 minute
to go when we print every 10 secs */
secs=(int)(time(0)-amsys->rs_announce);
if (amsys->rs_countdown>=60 && secs>=60) {
vwrite_room(NULL,"~OLSYSTEM: %s in %d minute%s, %d second%s.\n",
w[amsys->rs_which],amsys->rs_countdown/60,PLTEXT_S(amsys->rs_countdown/60),amsys->rs_countdown%60,PLTEXT_S(amsys->rs_countdown%60));
amsys->rs_announce=time(0);
}
if (amsys->rs_countdown<60 && secs>=10) {
vwrite_room(NULL,"~OLSYSTEM: %s in %d second%s.\n",w[amsys->rs_which],amsys->rs_countdown,PLTEXT_S(amsys->rs_countdown));
amsys->rs_announce=time(0);
}
}
/*** login_time_out is the length of time someone can idle at login,
user_idle_time is the length of time they can idle once logged in.
Also ups users total login time. ***/
void check_idle_and_timeout(void)
{
UR_OBJECT user,next;
int tm;
/* Use while loop here instead of for loop for when user structure gets
destructed, we may lose ->next link and crash the program */
user=user_first;
while(user) {
next=user->next;
if (user->type==CLONE_TYPE) { user=next; continue; }
user->total_login+=amsys->heartbeat;
if (user->level>amsys->time_out_maxlevel) { user=next; continue; }
tm=(int)(time(0) - user->last_input);
if (user->login && tm>=amsys->login_idle_time) {
write_user(user,"\n\n*** Time out ***\n\n");
disconnect_user(user);
user=next;
continue;
}
if (user->warned) {
if (tm<amsys->user_idle_time-60) { user->warned=0; continue; }
if (tm>=amsys->user_idle_time) {
write_user(user,"\n\n\07~FR~OL~LI*** You have been timed out. ***\n\n");
disconnect_user(user);
user=next;
continue;
}
}
if ((!user->afk || (user->afk && amsys->time_out_afks))
&& !user->login
&& !user->warned
&& tm>=amsys->user_idle_time-60) {
write_user(user,"\n\07~FY~OL~LI*** WARNING - Input within 1 minute or you will be disconnected. ***\n\n");
user->warned=1;
}
user=next;
}
}
/*** Remove any expired messages from boards unless force = 2 in which case
just do a recount. ***/
void check_messages(UR_OBJECT user, int chforce)
{
RM_OBJECT rm;
FILE *infp,*outfp;
char id[82],filename[80],line[82],rmname[USER_NAME_LEN];
int valid,pt,write_rest;
int board_cnt,old_cnt,bad_cnt,tmp;
static int done=0;
infp=outfp=NULL;
switch(chforce) {
case 0:
if (amsys->mesg_check_hour==thour && amsys->mesg_check_min==tmin) {
if (done) return;
}
else { done=0; return; }
break;
case 1:
printf("Checking boards...\n");
case 2:
if (word_count>=2) {
strtolower(word[1]);
if (strcmp(word[1],"motds")) {
write_user(user,"Usage: recount [motds]\n");
return;
}
if (!count_motds(1)) {
write_user(user,"Sorry, could not recount the motds at this time.\n");
write_syslog(SYSLOG,1,"ERROR: Could not recount motds in check_messages().\n");
return;
}
vwrite_user(user,"There %s %d login motd%s and %d post-login motd%s\n",PLTEXT_WAS(amsys->motd1_cnt),amsys->motd1_cnt,PLTEXT_S(amsys->motd1_cnt),amsys->motd2_cnt,PLTEXT_S(amsys->motd2_cnt));
write_syslog(SYSLOG,1,"%s recounted the MOTDS.\n",user->name);
return;
}
}
done=1;
board_cnt=0;
old_cnt=0;
bad_cnt=0;
for(rm=room_first;rm!=NULL;rm=rm->next) {
tmp=rm->mesg_cnt;
rm->mesg_cnt=0;
if (rm->access==PERSONAL_LOCKED || rm->access==PERSONAL_UNLOCKED) {
midcpy(rm->name,rmname,1,strlen(rm->name)-2);
rmname[0]=toupper(rmname[0]);
sprintf(filename,"%s/%s/%s.B",USERFILES,USERROOMS,rmname);
}
else sprintf(filename,"%s/%s.B",DATAFILES,rm->name);
if (!(infp=fopen(filename,"r"))) continue;
if (chforce<2) {
if (!(outfp=fopen("tempfile","w"))) {
if (chforce) fprintf(stderr,"KoF: Couldn't open tempfile.\n");
write_syslog(SYSLOG,0,"ERROR: Couldn't open tempfile in check_messages().\n");
fclose(infp);
return;
}
}
board_cnt++;
/* We assume that once 1 in date message is encountered all the others
will be in date too , hence write_rest once set to 1 is never set to
0 again */
valid=1; write_rest=0;
fgets(line,82,infp); /* max of 80+newline+terminator = 82 */
while(!feof(infp)) {
if (*line=='\n') valid=1;
sscanf(line,"%s %d",id,&pt);
if (!write_rest) {
if (valid && !strcmp(id,"PT:")) {
if (chforce==2) rm->mesg_cnt++;
else {
/* 86400 = num. of secs in a day */
if ((int)time(0) - pt < amsys->mesg_life*86400) {
fputs(line,outfp);
rm->mesg_cnt++;
write_rest=1;
}
else old_cnt++;
}
valid=0;
}
}
else {
fputs(line,outfp);
if (valid && !strcmp(id,"PT:")) {
rm->mesg_cnt++; valid=0;
}
}
fgets(line,82,infp);
}
fclose(infp);
if (chforce<2) {
fclose(outfp);
unlink(filename);
if (!write_rest) unlink("tempfile");
else rename("tempfile",filename);
}
if (rm->mesg_cnt!=tmp) bad_cnt++;
}
switch(chforce) {
case 0:
if (bad_cnt) write_syslog(SYSLOG,1,"CHECK_MESSAGES: %d file%s checked, %d had an incorrect message count, %d message%s deleted.\n",
board_cnt,PLTEXT_S(board_cnt),bad_cnt,old_cnt,PLTEXT_S(old_cnt));
else write_syslog(SYSLOG,1,"CHECK_MESSAGES: %d file%s checked, %d message%s deleted.\n",board_cnt,PLTEXT_S(board_cnt),old_cnt,PLTEXT_S(old_cnt));
break;
case 1:
printf(" %d board file%s checked, %d out of date message%s found.\n",board_cnt,PLTEXT_S(board_cnt),old_cnt,PLTEXT_S(old_cnt));
break;
case 2:
vwrite_user(user,"%d board file%s checked, %d had an incorrect message count.\n",board_cnt,PLTEXT_S(board_cnt),bad_cnt);
write_syslog(SYSLOG,1,"%s forced a recount of the message boards.\n",user->name);
}
}
/******************************************************************************
Initializing of globals and other stuff
*****************************************************************************/
/*** Load the users details ***/
int load_user_details(UR_OBJECT user)
{
FILE *fp;
char user_words[14][21]; /* must be at least 1 longer than max words in the _options */
char line[ARR_SIZE],filename[80],*str;
int wn,wpos,wcnt,op,found,i,damaged,version_found;
char *userfile_options[]={
"version","password","promote_date","times","levels","general","user_set",
"user_ignores","fighting","purging","last_site","mail_verify","description",
"in_phrase","out_phrase","email","homepage","recap_name","*" /* KEEP HERE! */
};
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"r"))) return 0;
damaged=version_found=0;
fgets(line,ARR_SIZE-1,fp);
line[strlen(line)-1]=0;
while(!feof(fp)) {
/* make this into the functions own word array. This allows this array to
have a different length from the general words array. */
wn=0; wpos=0;
str=line;
do {
while(*str<33) if (!*str++) goto OUT;
while(*str>32 && wpos<20) user_words[wn][wpos++]=*str++;
user_words[wn++][wpos]='\0';
wpos=0;
} while (wn<14);
wn--;
OUT:
wcnt=wn;
/* now get the option we're on */
op=0; found=1;
while(strcmp(userfile_options[op],user_words[0])) {
if (userfile_options[op][0]=='*') { found=0; break; }
op++;
}
if (found) {
switch(op) {
case 0:
if (wcnt>=2) { /* make sure more than just option string was there */
strcpy(user->version,remove_first(line));
version_found=1; /* gotta compensate still for old versions */
}
break;
case 1:
if (wcnt>=2) strcpy(user->pass,remove_first(line));
break;
case 2:
if (wcnt>=2) strcpy(user->date,remove_first(line));
break;
case 3:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->last_login=(time_t)atoi(user_words[i]); break;
case 2: user->total_login=(time_t)atoi(user_words[i]); break;
case 3: user->last_login_len=atoi(user_words[i]); break;
case 4: user->read_mail=(time_t)atoi(user_words[i]); break;
}
break;
case 4:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->level=atoi(user_words[i]); break;
case 2: user->unarrest=atoi(user_words[i]); break;
case 3: user->arrestby=atoi(user_words[i]); break;
}
break;
case 5:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->accreq=atoi(user_words[i]); break;
case 2: user->charmode_echo=atoi(user_words[i]); break;
case 3: user->command_mode=atoi(user_words[i]); break;
case 4: user->prompt=atoi(user_words[i]); break;
case 5: user->vis=atoi(user_words[i]); break;
case 6: user->monitor=atoi(user_words[i]); break;
case 7: user->mail_verified=atoi(user_words[i]); break;
case 8: user->logons=atoi(user_words[i]); break;
}
break;
case 6:
for (i=1;i<wcnt;i++)
switch(i) {
case 1 : user->gender=atoi(user_words[i]); break;
case 2 : user->age=atoi(user_words[i]); break;
case 3 : user->wrap=atoi(user_words[i]); break;
case 4 : user->pager=atoi(user_words[i]); break;
case 5 : user->hideemail=atoi(user_words[i]); break;
case 6 : user->colour=atoi(user_words[i]); break;
case 7 : user->lroom=atoi(user_words[i]); break;
case 8 : user->alert=atoi(user_words[i]); break;
case 9 : user->autofwd=atoi(user_words[i]); break;
case 10: user->show_pass=atoi(user_words[i]); break;
case 11: user->show_rdesc=atoi(user_words[i]); break;
case 12: user->cmd_type=atoi(user_words[i]); break;
case 13: user->language=atoi(user_words[i]); break; /*larts*/
}
break;
case 7:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->ignall=atoi(user_words[i]); break;
case 2: user->igntells=atoi(user_words[i]); break;
case 3: user->ignshouts=atoi(user_words[i]); break;
case 5: user->ignlogons=atoi(user_words[i]); break;
case 6: user->ignwiz=atoi(user_words[i]); break;
case 8: user->ignbeeps=atoi(user_words[i]); break;
}
break;
case 8:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->hits=atoi(user_words[i]); break;
case 2: user->misses=atoi(user_words[i]); break;
case 3: user->deaths=atoi(user_words[i]); break;
case 4: user->kills=atoi(user_words[i]); break;
case 5: user->bullets=atoi(user_words[i]); break;
case 6: user->hps=atoi(user_words[i]); break;
}
break;
case 9:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: user->expire=atoi(user_words[i]); break;
case 2: user->t_expire=(time_t)atoi(user_words[i]); break;
}
break;
case 10:
for (i=1;i<wcnt;i++)
switch(i) {
case 1: strcpy(user->last_site,user_words[i]); break;
case 2: strcpy(user->logout_room,user_words[i]); break;
}
break;
case 11:
if (wcnt>=2) strcpy(user->verify_code,remove_first(line));
break;
case 12:
if (wcnt>=2) strcpy(user->desc,remove_first(line));
break;
case 13:
if (wcnt>=2) strcpy(user->in_phrase,remove_first(line));
break;
case 14:
if (wcnt>=2) strcpy(user->out_phrase,remove_first(line));
break;
case 15:
if (wcnt>=2) strcpy(user->email,remove_first(line));
break;
case 16:
if (wcnt>=2) strcpy(user->homepage,remove_first(line));
break;
case 17:
if (wcnt>=2) strcpy(user->recap,remove_first(line));
break;
default: damaged++; break;
}
}
else damaged++;
fgets(line,ARR_SIZE-1,fp);
line[strlen(line)-1]=0;
}
fclose(fp);
if (!version_found) load_user_details_old(user);
else if (damaged) write_syslog(SYSLOG,1,"DAMAGED userfile '%s.D'\n",user->name);
if (!amsys->allow_recaps || !user->recap[0]) strcpy(user->recap,user->name);
strcpy(user->bw_recap,colour_com_strip(user->recap));
user->real_level=user->level;
if (user->level>GUEST) user->accreq=-1; /* compensate for new accreq change */
get_macros(user);
get_xgcoms(user);
get_friends(user);
read_user_reminders(user);
return 1;
}
/*** Save the details for the user ***/
int save_user_details(UR_OBJECT user, int save_current)
{
FILE *fp;
char filename[80];
// if (user->type==REMOTE_TYPE || user->type==CLONE_TYPE) return 0;
if (user->type==CLONE_TYPE) return 0;
if (user->level==GUEST) return;
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"w"))) {
vwrite_user(user,"%s: failed to save your details.\n",syserror);
write_syslog(SYSLOG,1,"SAVE_USER_STATS: Failed to save %s's details.\n",user->name);
return 0;
}
/* reset normal level */
if (user->real_level<user->level) user->level=user->real_level;
/* print out the file */
fprintf(fp,"version %s\n",USERVER); /* of user struct */
fprintf(fp,"password %s\n",user->pass);
fprintf(fp,"promote_date %s\n",user->date);
if (save_current) fprintf(fp,"times %d %d %d %d\n",
(int)time(0),(int)user->total_login,(int)(time(0)-user->last_login),(int)user->read_mail);
else fprintf(fp,"times %d %d %d %d\n",(int)user->last_login,(int)user->total_login,user->last_login_len,(int)user->read_mail);
fprintf(fp,"levels %d %d %d\n",user->level,user->unarrest,user->arrestby);
fprintf(fp,"general %d %d %d %d %d %d %d %d\n",
user->accreq,user->charmode_echo,user->command_mode,user->prompt,user->vis,user->monitor,user->mail_verified,user->logons);
fprintf(fp,"user_set %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
user->gender,user->age,user->wrap,user->pager,user->hideemail,user->colour,user->lroom,
user->alert,user->autofwd,user->show_pass,user->show_rdesc,user->cmd_type,user->language);
fprintf(fp,"user_ignores %d %d %d %d %d %d\n",
user->ignall,user->igntells,user->ignshouts,user->ignlogons,user->ignwiz,user->ignbeeps);
fprintf(fp,"fighting %d %d %d %d %d %d\n",user->hits,user->misses,user->deaths,user->kills,user->bullets,user->hps);
if (!save_current) fprintf(fp,"purging %d %d\n",user->expire,(int)user->t_expire);
if (save_current) fprintf(fp,"last_site %s %s\n",user->site,user->room->name);
else fprintf(fp,"last_site %s %s\n",user->last_site,user->logout_room);
fprintf(fp,"mail_verify %s\n",user->verify_code);
fprintf(fp,"description %s\n",user->desc);
fprintf(fp,"in_phrase %s\n",user->in_phrase);
fprintf(fp,"out_phrase %s\n",user->out_phrase);
fprintf(fp,"email %s\n",user->email);
fprintf(fp,"homepage %s\n",user->homepage);
fprintf(fp,"recap_name %s\n",user->recap);
fclose(fp);
return 1;
}
/*** Load the users details ***/
int load_user_details_old(UR_OBJECT user)
{
FILE *fp;
char line[82],filename[80];
int temp1,temp2,temp3,temp4;
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"r"))) return 0;
/* Password */
fscanf(fp,"%s\n",user->pass);
/* version control of user structure */
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->version,line);
/* date when user last promoted/demoted */
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->date,line);
/* times, levels, and important stats */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",&temp1,&temp2,&user->last_login_len,&temp3,
&user->level,&user->prompt,&user->charmode_echo,&user->command_mode,&user->vis,
&user->monitor,&user->unarrest,&user->logons,&user->accreq,&user->mail_verified,&user->arrestby);
/* stats set using the 'set' function */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d",&user->gender,&user->age,&user->wrap,&user->pager,
&user->hideemail,&user->colour,&user->lroom,&user->alert,&user->autofwd,&user->show_pass,
&user->show_rdesc,&user->cmd_type,&user->language);
/* ignore status */
fscanf(fp,"%d %d %d %d %d %d",&user->ignall,&user->igntells,&user->ignshouts,
&user->ignlogons,&user->ignwiz,&user->ignbeeps);
/* Gun fight information */
fscanf(fp,"%d %d %d %d %d %d",&user->hits,&user->misses,&user->deaths,&user->kills,&user->bullets,&user->hps);
/* user expires and date */
fscanf(fp,"%d %d",&user->expire,&temp4);
/* site address and last room they were in */
fscanf(fp,"%s %s %s\n",user->last_site,user->logout_room,user->verify_code);
/* general text stuff
Need to do the rest like this 'cos they may be more than 1 word each
possible for one colour for each letter, *3 for the code length */
fgets(line,USER_DESC_LEN+USER_DESC_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->desc,line);
fgets(line,PHRASE_LEN+PHRASE_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->in_phrase,line);
fgets(line,PHRASE_LEN+PHRASE_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->out_phrase,line);
fgets(line,81,fp); line[strlen(line)-1]=0; strcpy(user->email,line);
fgets(line,81,fp); line[strlen(line)-1]=0; strcpy(user->homepage,line);
fscanf(fp,"%s\n",user->recap);
fclose(fp);
if (strcmp(user->version,USERVER)) {
/* As default, if the version cannot be read then it'll load up version 1.4.2 files */
if (!strcmp(user->version,"2.0.0")) {
load_oldversion_user(user,200);
write_syslog(SYSLOG,1,"Reading old user file (version 2.0.0) in load_user_details()\n");
}
else if (!strcmp(user->version,"2.0.1")) {
load_oldversion_user(user,201);
write_syslog(SYSLOG,1,"Reading old user file (version 2.0.1) in load_user_details()\n");
}
else if (!strcmp(user->version,"2.1.0")) {
load_oldversion_user(user,210);
write_syslog(SYSLOG,1,"Reading old user file (version 2.1.0) in load_user_details()\n");
}
else if (!strcmp(user->version,"2.1.1")) {
write_syslog(SYSLOG,1,"Reading old user file (version 2.1.1) in load_user_details()\n");
/* do nothing.. userfile type not changed */
}
else {
load_oldversion_user(user,142);
write_syslog(SYSLOG,1,"Reading old user file (version 1.4.2) in load_user_details()\n");
}
strcpy(user->version,USERVER);
if (user->level>GUEST) user->accreq=-1;
}
else {
user->last_login=(time_t)temp1;
user->total_login=(time_t)temp2;
user->read_mail=(time_t)temp3;
user->t_expire=(time_t)temp4;
}
if (!amsys->allow_recaps) strcpy(user->recap,user->name);
strcpy(user->bw_recap,colour_com_strip(user->recap));
user->real_level=user->level;
if (user->level>GUEST) user->accreq=-1; /* compensate for new accreq change */
get_macros(user);
get_xgcoms(user);
get_friends(user);
read_user_reminders(user);
return 1;
}
/* Below is the loading for Amnuts version 1.4.2 and 2.x.x user structures. */
int load_oldversion_user(UR_OBJECT user,int version)
{
FILE *fp;
char line[81],filename[80];
int temp1,temp2,temp3,temp4,oldvote;
reset_user(user); /* make sure reads in fresh */
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"r"))) return 0;
/* Password */
fscanf(fp,"%s\n",user->pass);
/* read version control and date last promoted if version 2.0.0, 2.0.1, 2.1.0 */
if (version>=200) {
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->version,line);
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->date,line);
}
else {
strcpy(user->version,"1.4.2");
strcpy(user->date,long_date(1));
}
if (version>=200) {
/* times, levels, and important stats */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d",&temp1,&temp2,&user->last_login_len,&temp3,
&user->level,&user->prompt,&user->charmode_echo,&user->command_mode,&user->vis,
&user->monitor,&user->unarrest,&user->logons,&user->accreq,&user->mail_verified);
}
else {
/* times, levels, and important stats */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",&temp1,&temp2,&user->last_login_len,&temp3,
&user->level,&user->prompt,&user->charmode_echo,&user->command_mode,&user->vis,
&user->monitor,&oldvote,&user->unarrest,&user->logons,&user->accreq,&user->mail_verified);
}
if (version>=210) {
/* stats set using the 'set' function */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d",&user->gender,&user->age,&user->wrap,&user->pager,&user->hideemail,
&user->colour,&user->lroom,&user->alert,&user->autofwd,&user->show_pass,&user->show_rdesc,&user->cmd_type,&user->language);
}
else if (version>=200) {
/* stats set using the 'set' function */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d %d %d",&user->gender,&user->age,&user->wrap,&user->pager,&user->hideemail,
&user->colour,&user->lroom,&user->alert,&user->autofwd,&user->show_pass,&user->show_rdesc,&user->cmd_type,&user->language);
}
else {
/* stats set using the 'set' function */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d %d",&user->gender,&user->age,&user->wrap,&user->pager,&user->hideemail,
&user->colour,&user->lroom,&user->alert,&user->autofwd,&user->show_pass,&user->language);
user->show_rdesc=1;
user->cmd_type=1; /* larts */
}
/* ignore status */
fscanf(fp,"%d %d %d %d %d %d",&user->ignall,&user->igntells,&user->ignshouts,
&user->ignlogons,&user->ignwiz,&user->ignbeeps);
/* Gun fight information */
fscanf(fp,"%d %d %d %d %d %d",&user->hits,&user->misses,&user->deaths,&user->kills,&user->bullets,&user->hps);
/* user expires and date */
fscanf(fp,"%d %d",&user->expire,&temp4);
/* site address and last room they were in */
fscanf(fp,"%s %s %s\n",user->last_site,user->logout_room,user->verify_code);
/* general text stuff
Need to do the rest like this 'cos they may be more than 1 word each
possible for one colour for each letter, *3 for the code length */
fgets(line,USER_DESC_LEN+USER_DESC_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->desc,line);
fgets(line,PHRASE_LEN+PHRASE_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->in_phrase,line);
fgets(line,PHRASE_LEN+PHRASE_LEN*3,fp); line[strlen(line)-1]=0; strcpy(user->out_phrase,line);
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->email,line);
fgets(line,82,fp); line[strlen(line)-1]=0; strcpy(user->homepage,line);
if (version>=200) {
fscanf(fp,"%s\n",user->recap);
}
else {
strcpy(user->recap,user->name);
}
fclose(fp);
user->last_login=(time_t)temp1;
user->total_login=(time_t)temp2;
user->read_mail=(time_t)temp3;
user->t_expire=(time_t)temp4;
return 1;
}
/*** Set global vars. hours,minutes,seconds,date,day,month,year ***/
void set_date_time(void)
{
struct tm *tm_struct; /* structure is defined in time.h */
time_t tm_num;
/* Set up the structure */
time(&tm_num);
tm_struct=localtime(&tm_num);
/* Get the values */
tday=tm_struct->tm_yday;
tyear=1900+tm_struct->tm_year;
tmonth=tm_struct->tm_mon;
tmday=tm_struct->tm_mday;
twday=tm_struct->tm_wday;
thour=tm_struct->tm_hour;
tmin=tm_struct->tm_min;
tsec=tm_struct->tm_sec;
}
/*** Get all users from the user directories and add them to the user lists.
If verbose mode is on, then attempt to get date string as well
***/
void process_users(void)
{
char name[USER_NAME_LEN+3];
DIR *dirp;
struct dirent *dp;
UR_OBJECT u;
/* open the directory file up */
dirp=opendir(USERFILES);
if (dirp==NULL) {
fprintf(stderr,"KoF: Directory open failure in process_users().\n");
boot_exit(12);
}
if ((u=create_user())==NULL) {
fprintf(stderr,"KoF: Create user failure in process_users().\n");
(void) closedir(dirp);
boot_exit(17);
}
/* count up how many files in the directory - this include . and .. */
while((dp=readdir(dirp))!=NULL) {
if (!strcmp(dp->d_name,".") || !strcmp(dp->d_name,"..")) continue;
if (strstr(dp->d_name,".D")) {
strcpy(name,dp->d_name);
name[strlen(name)-2]='\0';
strcpy(u->name,name);
if (load_user_details(u)) {
add_user_node(u->name,u->level);
if (u->level>=WIZ) add_wiz_node(u->name,u->level);
add_user_date_node(u->name,u->date);
} /* end if */
else {
fprintf(stderr,"KoF: Could not load userfile for '%s' in process_users().\n",name);
(void) closedir(dirp);
boot_exit(18);
}
} /* end if */
reset_user(u);
} /* end while */
destruct_user(u);
(void) closedir(dirp);
}
/* count up how many users of a certain level and in total in the linked user list */
void count_users(void)
{
struct user_dir_struct *entry;
int i;
/* first zero out all level counts */
amsys->user_count=0;
for (i=JAILED;i<=CHAOS;i++) amsys->level_count[i]=0;
/* count up users */
for (entry=first_dir_entry;entry!=NULL;entry=entry->next) {
amsys->level_count[entry->level]++;
amsys->user_count++;
}
}
/* Put commands in an ordered linked list for viewing with .help */
void parse_commands(void)
{
int cnt;
cnt=0;
while(command_table[cnt].name[0]!='*') {
if (!(add_command(cnt))) {
fprintf(stderr,"KoF: Memory allocation failure in parse_commands().\n");
boot_exit(13);
}
++cnt;
}
return;
}
/* needs only doing once when booting */
void count_suggestions(void)
{
char filename[80],line[82],id[20];
FILE *fp;
int valid;
sprintf(filename,"%s/%s",MISCFILES,SUGBOARD);
if (!(fp=fopen(filename,"r"))) return;
valid=1;
fgets(line,82,fp);
while(!feof(fp)) {
if (line[0]=='\n') valid=1;
sscanf(line,"%s",id);
if (valid && strstr(id,"From:")) {
++amsys->suggestion_count;
valid=0;
}
fgets(line,82,fp);
}
fclose(fp);
}
/* work out how many motds are stored - if an motd file is deleted after
this count has been made then you need to recount them with the recount
command. If you fail to do this and something buggers up because the count
is wrong then it's your own fault.
*/
int count_motds(int forcecnt)
{
char filename[80];
DIR *dirp;
struct dirent *dp;
int i;
amsys->motd1_cnt=0; amsys->motd2_cnt=0;
for (i=1;i<=2;i++) {
/* open the directory file up */
sprintf(filename,"%s/motd%d",MOTDFILES,i);
if ((dirp=opendir(filename))==NULL) {
switch(forcecnt) {
case 0:
fprintf(stderr,"KoF: Directory open failure in count_motds().\n");
boot_exit(20);
case 1: return 0;
}
}
/* count up how many files in the directory - not including . and .. */
while((dp=readdir(dirp))!=NULL) {
if (strstr(dp->d_name,"motd")) (i==1) ? ++amsys->motd1_cnt : ++amsys->motd2_cnt;
}
(void) closedir(dirp);
}
return 1;
}
/* return a random number for an motd file - if 0 then return 1 */
int get_motd_num(int motd)
{
int num;
srand(time(0));
if (!amsys->random_motds) return 1;
switch(motd) {
case 1: num=rand()%amsys->motd1_cnt+1; break;
case 2: num=rand()%amsys->motd2_cnt+1; break;
default: num=0; break;
}
return (!num) ? 1:num;
}
/******************************************************************************
File functions - reading, writing, counting of lines, etc
*****************************************************************************/
/* wipes ALL the files belonging to the user with name given */
void clean_files(char *name)
{
char filename[80];
name[0]=toupper(name[0]);
sprintf(filename,"%s/%s.D",USERFILES,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.M",USERFILES,USERMAILS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.P",USERFILES,USERPROFILES,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.H",USERFILES,USERHISTORYS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.C",USERFILES,USERCOMMANDS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.MAC",USERFILES,USERMACROS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.F",USERFILES,USERFRIENDS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.R",USERFILES,USERROOMS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.B",USERFILES,USERROOMS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.K",USERFILES,USERROOMS,name);
unlink(filename);
sprintf(filename,"%s/%s/%s.REM",USERFILES,USERREMINDERS,name);
unlink(filename);
}
/* remove a line from the top or bottom of a file.
where==0 then remove from the top
where==1 then remove from the bottom
*/
int remove_top_bottom(char *filename, int where)
{
char line[ARR_SIZE];
FILE *fpi, *fpo;
int i,cnt;
if (!(fpi=fopen(filename,"r"))) return 0;
if (!(fpo=fopen("temp_file","w"))) {
fclose(fpi); return 0;
}
/* remove from top */
if (where==0) {
/* get the first line */
fgets(line,ARR_SIZE,fpi);
/* get rest of file */
fgets(line,ARR_SIZE,fpi);
while(!feof(fpi)) {
line[strlen(line)-1]='\0';
fprintf(fpo,"%s\n",line);
fgets(line,ARR_SIZE-1,fpi);
}Showing the first 100 KB of 463.4 KB. Download the archive for the full file.