nuts333.c
nuts333/nuts333.c · 205.3 KB
From the archive nuts333.tar.gz
Download the archive « Back to the archive contents
/*****************************************************************************
NUTS version 3.3.3 (Triple Three :) - Copyright (C) Neil Robertson 1996
Last update: 18th November 1996
This software is provided as is. It is not intended as any sort of bullet
proof system for commercial operation (though you may use it to set up a
pay-to-use system, just don't attempt to sell the code itself) and I accept
no liability for any problems that may arise from you using it. Since this is
freeware (NOT public domain , I have not relinquished the copyright) you may
distribute it as you see fit and you may alter the code to suit your needs.
Read the COPYRIGHT file for further information.
Neil Robertson.
Email : neil@ogham.demon.co.uk
Home page: http://www.wso.co.uk/neil.html (need JavaScript enabled browser)
NUTS page: http://www.wso.co.uk/nuts.html
Newsgroup: alt.talkers.nuts
NB: This program listing looks best when the tab length is 5 chars which is
"set ts=5" in vi.
*****************************************************************************/
#include <stdio.h>
#ifdef _AIX
#include <sys/select.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.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 "nuts333.h"
#define VERSION "3.3.3"
/*** This function calls all the setup routines and also contains the
main program loop ***/
main(argc,argv)
int argc;
char *argv[];
{
fd_set readmask;
int i,len;
char inpstr[ARR_SIZE];
char *remove_first();
UR_OBJECT user,next;
NL_OBJECT nl;
strcpy(progname,argv[0]);
if (argc<2) strcpy(confile,CONFIGFILE);
else strcpy(confile,argv[1]);
/* Startup */
printf("\n*** NUTS %s server booting ***\n\n",VERSION);
init_globals();
write_syslog("\n*** SERVER BOOTING ***\n",0);
set_date_time();
init_signals();
load_and_parse_config();
init_sockets();
if (auto_connect) init_connections();
else printf("Skipping connect stage.\n");
check_messages(NULL,1);
/* Run in background automatically. */
switch(fork()) {
case -1: boot_exit(11); /* fork failure */
case 0: break; /* child continues */
default: sleep(1); exit(0); /* parent dies */
}
reset_alarm();
printf("\n*** Booted with PID %d ***\n\n",getpid());
sprintf(text,"*** Booted successfully with PID %d %s ***\n\n",getpid(),long_date(1));
write_syslog(text,0);
/**** 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<3;++i) {
if (FD_ISSET(listen_sock[i],&readmask))
accept_connection(listen_sock[i],i);
}
/* Cycle through client-server connections to other talkers */
for(nl=nl_first;nl!=NULL;nl=nl->next) {
no_prompt=0;
if (nl->type==UNCONNECTED || !FD_ISSET(nl->socket,&readmask))
continue;
/* See if remote site has disconnected */
if (!(len=read(nl->socket,inpstr,sizeof(inpstr)-3))) {
if (nl->stage==UP)
sprintf(text,"NETLINK: Remote disconnect by %s.\n",nl->service);
else sprintf(text,"NETLINK: Remote disconnect by site %s.\n",nl->site);
write_syslog(text,1);
sprintf(text,"~OLSYSTEM:~RS Lost link to %s in the %s.\n",nl->service,nl->connect_room->name);
write_room(NULL,text);
shutdown_netlink(nl);
continue;
}
inpstr[len]='\0';
exec_netcom(nl,inpstr);
}
/* 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) {
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);
sprintf(text,"%s\n",inpstr);
write_user(user,text);
}
/* else save current one for next time */
else {
if (inpstr[0]) strncpy(user->inpstr_old,inpstr,REVIEW_LEN);
}
}
/* Main input check */
clear_words();
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],"NU"),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->vis) {
sprintf(text,"%s comes back from being AFK.\n",user->name);
write_room_except(user->room,text,user);
}
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->room==NULL) {
sprintf(text,"ACT %s NL\n",user->name);
write_sock(user->netlink->socket,text);
}
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) prompt(user);
else {
switch(com_num) {
case -1 : /* Unknown command */
case HOME:
case QUIT:
case MODE:
case PROMPT:
case SUICIDE:
case REBOOT:
case SHUTDOWN: prompt(user);
}
}
}
user=next;
}
} /* end while */
}
/************ MAIN LOOP FUNCTIONS ************/
/*** Set up readmask for select ***/
setup_readmask(mask)
fd_set *mask;
{
UR_OBJECT user;
NL_OBJECT nl;
int i;
FD_ZERO(mask);
for(i=0;i<3;++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 */
for(nl=nl_first;nl!=NULL;nl=nl->next)
if (nl->type!=UNCONNECTED) FD_SET(nl->socket,mask);
}
/*** Accept incoming connections on listen sockets ***/
accept_connection(lsock,num)
int lsock,num;
{
UR_OBJECT user,create_user();
NL_OBJECT create_netlink();
char *get_ip_address(),site[80];
struct sockaddr_in acc_addr;
int accept_sock,size;
size=sizeof(struct sockaddr_in);
accept_sock=accept(lsock,(struct sockaddr *)&acc_addr,&size);
if (num==2) {
accept_server_connection(accept_sock,acc_addr); return;
}
strcpy(site,get_ip_address(acc_addr));
if (site_banned(site)) {
write_sock(accept_sock,"\n\rLogins from your site/domain are banned.\n\n\r");
close(accept_sock);
sprintf(text,"Attempted login from banned site %s.\n",site);
write_syslog(text,1);
return;
}
more(NULL,accept_sock,MOTD1); /* send pre-login message */
if (num_of_users+num_of_logins>=max_users && !num) {
write_sock(accept_sock,"\n\rSorry, the talker is full at the moment.\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=3;
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,site);
user->site_port=(int)ntohs(acc_addr.sin_port);
echo_on(user);
write_user(user,"Give me a name: ");
num_of_logins++;
}
/*** Get net address of accepted connection ***/
char *get_ip_address(acc_addr)
struct sockaddr_in acc_addr;
{
static char site[80];
struct hostent *host;
strcpy(site,(char *)inet_ntoa(acc_addr.sin_addr)); /* get number addr */
if ((host=gethostbyaddr((char *)&acc_addr.sin_addr,4,AF_INET))!=NULL)
strcpy(site,host->h_name); /* copy name addr. */
strtolower(site);
return site;
}
/*** See if users site is banned ***/
site_banned(site)
char *site;
{
FILE *fp;
char line[82],filename[80];
sprintf(filename,"%s/%s",DATAFILES,SITEBAN);
if (!(fp=fopen(filename,"r"))) return 0;
fscanf(fp,"%s",line);
while(!feof(fp)) {
if (strstr(site,line)) { fclose(fp); return 1; }
fscanf(fp,"%s",line);
}
fclose(fp);
return 0;
}
/*** See if user is banned ***/
user_banned(name)
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;
}
/*** Attempt to get '\n' terminated line of input from a character
mode client else store data read so far in user buffer. ***/
get_charclient_line(user,inpstr,len)
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!=2 && user->login!=1) || password_echo))
write(user->socket,inpstr,len);
return 0;
}
/*** Put string terminate char. at first char < 32 ***/
terminate(str)
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. ***/
wordfind(inpstr)
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. ***/
clear_words()
{
int w;
for(w=0;w<MAX_WORDS;++w) word[w][0]='\0';
word_count=0;
}
/************ PARSE CONFIG FILE **************/
load_and_parse_config()
{
FILE *fp;
char line[81]; /* Should be long enough */
char c,filename[80];
int i,section_in,got_init,got_rooms;
RM_OBJECT rm1,rm2;
NL_OBJECT nl;
section_in=0;
got_init=0;
got_rooms=0;
sprintf(filename,"%s/%s",DATAFILES,confile);
printf("Parsing config file \"%s\"...\n",filename);
if (!(fp=fopen(filename,"r"))) {
perror("NUTS: 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],"SITES:")) section_in=3;
else {
fprintf(stderr,"NUTS: 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_sites_section(); break;
default:
fprintf(stderr,"NUTS: Section header expected on line %d.\n",config_line);
boot_exit(1);
}
fgets(line,81,fp);
}
fclose(fp);
/* See if required sections were present (SITES is optional) and if
required parameters were set. */
if (!got_init) {
fprintf(stderr,"NUTS: INIT section missing from config file.\n");
boot_exit(1);
}
if (!got_rooms) {
fprintf(stderr,"NUTS: ROOMS section missing from config file.\n");
boot_exit(1);
}
if (!verification[0]) {
fprintf(stderr,"NUTS: Verification not set in config file.\n");
boot_exit(1);
}
if (!port[0]) {
fprintf(stderr,"NUTS: Main port number not set in config file.\n");
boot_exit(1);
}
if (!port[1]) {
fprintf(stderr,"NUTS: Wiz port number not set in config file.\n");
boot_exit(1);
}
if (!port[2]) {
fprintf(stderr,"NUTS: Link port number not set in config file.\n");
boot_exit(1);
}
if (port[0]==port[1] || port[1]==port[2] || port[0]==port[2]) {
fprintf(stderr,"NUTS: Port numbers must be unique.\n");
boot_exit(1);
}
if (room_first==NULL) {
fprintf(stderr,"NUTS: 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,"NUTS: Room %s has undefined link label '%s'.\n",rm1->name,rm1->link_label[i]);
boot_exit(1);
}
}
}
/* Check external links */
for(rm1=room_first;rm1!=NULL;rm1=rm1->next) {
for(nl=nl_first;nl!=NULL;nl=nl->next) {
if (!strcmp(nl->service,rm1->name)) {
fprintf(stderr,"NUTS: Service name %s is also the name of a room.\n",nl->service);
boot_exit(1);
}
if (rm1->netlink_name[0]
&& !strcmp(rm1->netlink_name,nl->service)) {
rm1->netlink=nl; break;
}
}
if (rm1->netlink_name[0] && rm1->netlink==NULL) {
fprintf(stderr,"NUTS: Service name %s not defined for room %s.\n",rm1->netlink_name,rm1->name);
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,"NUTS: Can't open description file for room %s.\n",rm1->name);
sprintf(text,"ERROR: Couldn't open description file for room %s.\n",rm1->name);
write_syslog(text,0);
continue;
}
i=0;
c=getc(fp);
while(!feof(fp)) {
if (i==ROOM_DESC_LEN) {
fprintf(stderr,"NUTS: Description too long for room %s.\n",rm1->name);
sprintf(text,"ERROR: Description too long for room %s.\n",rm1->name);
write_syslog(text,0);
break;
}
rm1->desc[i]=c;
c=getc(fp); ++i;
}
rm1->desc[i]='\0';
fclose(fp);
}
}
/*** Parse init section ***/
parse_init_section()
{
static int in_section=0;
int op,val;
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","allow_caps_in_name","charecho_def",
"time_out_maxlevel","*"
};
if (!strcmp(wrd[0],"INIT:")) {
if (++in_section>1) {
fprintf(stderr,"NUTS: Unexpected INIT section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
op=0;
while(strcmp(options[op],wrd[0])) {
if (options[op][0]=='*') {
fprintf(stderr,"NUTS: Unknown INIT option on line %d.\n",config_line);
boot_exit(1);
}
++op;
}
if (!wrd[1][0]) {
fprintf(stderr,"NUTS: Required parameter missing on line %d.\n",config_line);
boot_exit(1);
}
if (wrd[2][0] && wrd[2][0]!='#') {
fprintf(stderr,"NUTS: 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:
case 2:
if ((port[op]=val)<1 || val>65535) {
fprintf(stderr,"NUTS: Illegal port number on line %d.\n",config_line);
boot_exit(1);
}
return;
case 3:
if ((system_logging=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: System_logging must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 4:
if ((minlogin_level=get_level(wrd[1]))==-1) {
if (strcmp(wrd[1],"NONE")) {
fprintf(stderr,"NUTS: Unknown level specifier for minlogin_level on line %d.\n",config_line);
boot_exit(1);
}
minlogin_level=-1;
}
return;
case 5: /* message lifetime */
if ((mesg_life=val)<1) {
fprintf(stderr,"NUTS: Illegal message lifetime on line %d.\n",config_line);
boot_exit(1);
}
return;
case 6: /* wizport_level */
if ((wizport_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Unknown level specifier for wizport_level on line %d.\n",config_line);
boot_exit(1);
}
return;
case 7: /* prompt defaults */
if ((prompt_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Prompt_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 8: /* gatecrash level */
if ((gatecrash_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Unknown level specifier for gatecrash_level on line %d.\n",config_line);
boot_exit(1);
}
return;
case 9:
if (val<1) {
fprintf(stderr,"NUTS: Number too low for min_private_users on line %d.\n",config_line);
boot_exit(1);
}
min_private_users=val;
return;
case 10:
if ((ignore_mp_level=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: 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 ((rem_user_maxlevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: 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 ((rem_user_deflevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Unknown level specifier for rem_user_deflevel on line %d.\n",config_line);
boot_exit(1);
}
return;
case 13:
if (strlen(wrd[1])>VERIFY_LEN) {
fprintf(stderr,"NUTS: Verification too long on line %d.\n",config_line);
boot_exit(1);
}
strcpy(verification,wrd[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,"NUTS: Invalid message check time on line %d.\n",config_line);
boot_exit(1);
}
sscanf(wrd[1],"%d:%d",&mesg_check_hour,&mesg_check_min);
if (mesg_check_hour>23 || mesg_check_min>59) {
fprintf(stderr,"NUTS: Invalid message check time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 15:
if ((max_users=val)<1) {
fprintf(stderr,"NUTS: Invalid value for max_users on line %d.\n",config_line);
boot_exit(1);
}
return;
case 16:
if ((heartbeat=val)<1) {
fprintf(stderr,"NUTS: Invalid value for heartbeat on line %d.\n",config_line);
boot_exit(1);
}
return;
case 17:
if ((login_idle_time=val)<10) {
fprintf(stderr,"NUTS: Invalid value for login_idle_time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 18:
if ((user_idle_time=val)<10) {
fprintf(stderr,"NUTS: Invalid value for user_idle_time on line %d.\n",config_line);
boot_exit(1);
}
return;
case 19:
if ((password_echo=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Password_echo must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 20:
if ((ignore_sigterm=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Ignore_sigterm must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 21:
if ((auto_connect=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Auto_connect must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 22:
if ((max_clones=val)<0) {
fprintf(stderr,"NUTS: Invalid value for max_clones on line %d.\n",config_line);
boot_exit(1);
}
return;
case 23:
if ((ban_swearing=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Ban_swearing must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 24:
if (!strcmp(wrd[1],"NONE")) crash_action=0;
else if (!strcmp(wrd[1],"IGNORE")) crash_action=1;
else if (!strcmp(wrd[1],"REBOOT")) crash_action=2;
else {
fprintf(stderr,"NUTS: Crash_action must be NONE, IGNORE or REBOOT on line %d.\n",config_line);
boot_exit(1);
}
return;
case 25:
if ((colour_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Colour_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 26:
if ((time_out_afks=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Time_out_afks must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 27:
if ((allow_caps_in_name=yn_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Allow_caps_in_name must be YES or NO on line %d.\n",config_line);
boot_exit(1);
}
return;
case 28:
if ((charecho_def=onoff_check(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Charecho_def must be ON or OFF on line %d.\n",config_line);
boot_exit(1);
}
return;
case 29:
if ((time_out_maxlevel=get_level(wrd[1]))==-1) {
fprintf(stderr,"NUTS: Unknown level specifier for time_out_maxlevel on line %d.\n",config_line);
boot_exit(1);
}
return;
}
}
/*** Parse rooms section ***/
parse_rooms_section()
{
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,"NUTS: Unexpected ROOMS section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
if (!wrd[2][0]) {
fprintf(stderr,"NUTS: Required parameter(s) missing on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[0])>ROOM_LABEL_LEN) {
fprintf(stderr,"NUTS: Room label too long on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[1])>ROOM_NAME_LEN) {
fprintf(stderr,"NUTS: 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[0])) {
fprintf(stderr,"NUTS: Duplicate room label on line %d.\n",config_line);
boot_exit(1);
}
if (!strcmp(room->name,wrd[1])) {
fprintf(stderr,"NUTS: Duplicate room name on line %d.\n",config_line);
boot_exit(1);
}
}
room=create_room();
strcpy(room->label,wrd[0]);
strcpy(room->name,wrd[1]);
/* Parse internal links bit ie hl,gd,of etc. MUST NOT be any spaces between
the commas */
i=0;
ptr1=wrd[2];
ptr2=wrd[2];
while(1) {
while(*ptr2!=',' && *ptr2!='\0') ++ptr2;
if (*ptr2==',' && *(ptr2+1)=='\0') {
fprintf(stderr,"NUTS: Missing link label on line %d.\n",config_line);
boot_exit(1);
}
c=*ptr2; *ptr2='\0';
if (!strcmp(ptr1,room->label)) {
fprintf(stderr,"NUTS: 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,"NUTS: Too many links on line %d.\n",config_line);
boot_exit(1);
}
*ptr2=c;
ptr1=++ptr2;
}
/* Parse access privs */
if (wrd[3][0]=='#') { room->access=PUBLIC; return; }
if (!wrd[3][0] || !strcmp(wrd[3],"BOTH")) room->access=PUBLIC;
else if (!strcmp(wrd[3],"PUB")) room->access=FIXED_PUBLIC;
else if (!strcmp(wrd[3],"PRIV")) room->access=FIXED_PRIVATE;
else {
fprintf(stderr,"NUTS: Unknown room access type on line %d.\n",config_line);
boot_exit(1);
}
/* Parse external link stuff */
if (!wrd[4][0] || wrd[4][0]=='#') return;
if (!strcmp(wrd[4],"ACCEPT")) {
if (wrd[5][0] && wrd[5][0]!='#') {
fprintf(stderr,"NUTS: Unexpected word following ACCEPT keyword on line %d.\n",config_line);
boot_exit(1);
}
room->inlink=1;
return;
}
if (!strcmp(wrd[4],"CONNECT")) {
if (!wrd[5][0]) {
fprintf(stderr,"NUTS: External link name missing on line %d.\n",config_line);
boot_exit(1);
}
if (wrd[6][0] && wrd[6][0]!='#') {
fprintf(stderr,"NUTS: Unexpected word following external link name on line %d.\n",config_line);
boot_exit(1);
}
strcpy(room->netlink_name,wrd[5]);
return;
}
fprintf(stderr,"NUTS: Unknown connection option on line %d.\n",config_line);
boot_exit(1);
}
/*** Parse sites section ***/
parse_sites_section()
{
NL_OBJECT nl;
static int in_section=0;
if (!strcmp(wrd[0],"SITES:")) {
if (++in_section>1) {
fprintf(stderr,"NUTS: Unexpected SITES section header on line %d.\n",config_line);
boot_exit(1);
}
return;
}
if (!wrd[3][0]) {
fprintf(stderr,"NUTS: Required parameter(s) missing on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[0])>SERV_NAME_LEN) {
fprintf(stderr,"NUTS: Link name length too long on line %d.\n",config_line);
boot_exit(1);
}
if (strlen(wrd[3])>VERIFY_LEN) {
fprintf(stderr,"NUTS: Verification too long on line %d.\n",config_line);
boot_exit(1);
}
if ((nl=create_netlink())==NULL) {
fprintf(stderr,"NUTS: Memory allocation failure creating netlink on line %d.\n",config_line);
boot_exit(1);
}
if (!wrd[4][0] || wrd[4][0]=='#' || !strcmp(wrd[4],"ALL")) nl->allow=ALL;
else if (!strcmp(wrd[4],"IN")) nl->allow=IN;
else if (!strcmp(wrd[4],"OUT")) nl->allow=OUT;
else {
fprintf(stderr,"NUTS: Unknown netlink access type on line %d.\n",config_line);
boot_exit(1);
}
if ((nl->port=atoi(wrd[2]))<1 || nl->port>65535) {
fprintf(stderr,"NUTS: Illegal port number on line %d.\n",config_line);
boot_exit(1);
}
strcpy(nl->service,wrd[0]);
strtolower(wrd[1]);
strcpy(nl->site,wrd[1]);
strcpy(nl->verification,wrd[3]);
}
yn_check(wd)
char *wd;
{
if (!strcmp(wd,"YES")) return 1;
if (!strcmp(wd,"NO")) return 0;
return -1;
}
onoff_check(wd)
char *wd;
{
if (!strcmp(wd,"ON")) return 1;
if (!strcmp(wd,"OFF")) return 0;
return -1;
}
/************ INITIALISATION FUNCTIONS *************/
/*** Initialise globals ***/
init_globals()
{
verification[0]='\0';
port[0]=0;
port[1]=0;
port[2]=0;
auto_connect=1;
max_users=50;
max_clones=1;
ban_swearing=0;
heartbeat=2;
keepalive_interval=60; /* DO NOT TOUCH!!! */
net_idle_time=300; /* Must be > than the above */
login_idle_time=180;
user_idle_time=300;
time_out_afks=0;
wizport_level=WIZ;
minlogin_level=-1;
mesg_life=1;
no_prompt=0;
num_of_users=0;
num_of_logins=0;
system_logging=1;
password_echo=0;
ignore_sigterm=0;
crash_action=2;
prompt_def=1;
colour_def=1;
charecho_def=0;
time_out_maxlevel=USER;
mesg_check_hour=0;
mesg_check_min=0;
allow_caps_in_name=1;
rs_countdown=0;
rs_announce=0;
rs_which=-1;
rs_user=NULL;
gatecrash_level=GOD+1; /* minimum user level which can enter private rooms */
min_private_users=2; /* minimum num. of users in room before can set to priv */
ignore_mp_level=GOD; /* User level which can ignore the above var. */
rem_user_maxlevel=USER;
rem_user_deflevel=USER;
user_first=NULL;
user_last=NULL;
room_first=NULL;
room_last=NULL; /* This variable isn't used yet */
nl_first=NULL;
nl_last=NULL;
clear_words();
time(&boot_time);
}
/*** Initialise the signal traps etc ***/
init_signals()
{
void sig_handler();
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(sig)
int sig;
{
force_listen=1;
switch(sig) {
case SIGTERM:
if (ignore_sigterm) {
write_syslog("SIGTERM signal received - ignoring.\n",1);
return;
}
write_room(NULL,"\n\n~OLSYSTEM:~FR~LI SIGTERM received, initiating shutdown!\n\n");
talker_shutdown(NULL,"a termination signal (SIGTERM)",0);
case SIGSEGV:
switch(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);
case 1:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI WARNING - A segmentation fault has just occured!\n\n");
write_syslog("WARNING: A segmentation fault occured!\n",1);
longjmp(jmpvar,0);
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);
}
case SIGBUS:
switch(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);
case 1:
write_room(NULL,"\n\n\07~OLSYSTEM:~FR~LI WARNING - A bus error has just occured!\n\n");
write_syslog("WARNING: A bus error occured!\n",1);
longjmp(jmpvar,0);
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);
}
}
}
/*** Initialise sockets on ports ***/
init_sockets()
{
struct sockaddr_in bind_addr;
int i,on,size;
printf("Initialising sockets on ports: %d, %d, %d\n",port[0],port[1],port[2]);
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<3;++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 , do we need this? Not really. */
fcntl(listen_sock[i],F_SETFL,O_NDELAY);
}
}
/*** Initialise connections to remote servers. Basically this tries to connect
to the services listed in the config file and it puts the open sockets in
the NL_OBJECT linked list which the talker then uses ***/
init_connections()
{
NL_OBJECT nl;
RM_OBJECT rm;
int ret,cnt=0;
printf("Connecting to remote servers...\n");
errno=0;
for(rm=room_first;rm!=NULL;rm=rm->next) {
if ((nl=rm->netlink)==NULL) continue;
++cnt;
printf(" Trying service %s at %s %d: ",nl->service,nl->site,nl->port);
fflush(stdout);
if ((ret=connect_to_site(nl))) {
if (ret==1) {
printf("%s.\n",sys_errlist[errno]);
sprintf(text,"NETLINK: Failed to connect to %s: %s.\n",nl->service,sys_errlist[errno]);
}
else {
printf("Unknown hostname.\n");
sprintf(text,"NETLINK: Failed to connect to %s: Unknown hostname.\n",nl->service);
}
write_syslog(text,1);
}
else {
printf("CONNECTED.\n");
sprintf(text,"NETLINK: Connected to %s (%s %d).\n",nl->service,nl->site,nl->port);
write_syslog(text,1);
nl->connect_room=rm;
}
}
if (cnt) printf(" See system log for any further information.\n");
else printf(" No remote connections configured.\n");
}
/*** Do the actual connection ***/
connect_to_site(nl)
NL_OBJECT nl;
{
struct sockaddr_in con_addr;
struct hostent *he;
int inetnum;
char *sn;
sn=nl->site;
/* See if number address */
while(*sn && (*sn=='.' || isdigit(*sn))) sn++;
/* Name address given */
if(*sn) {
if(!(he=gethostbyname(nl->site))) return 2;
memcpy((char *)&con_addr.sin_addr,he->h_addr,(size_t)he->h_length);
}
/* Number address given */
else {
if((inetnum=inet_addr(nl->site))==-1) return 1;
memcpy((char *)&con_addr.sin_addr,(char *)&inetnum,(size_t)sizeof(inetnum));
}
/* Set up stuff and disable interrupts */
if ((nl->socket=socket(AF_INET,SOCK_STREAM,0))==-1) return 1;
con_addr.sin_family=AF_INET;
con_addr.sin_port=htons(nl->port);
signal(SIGALRM,SIG_IGN);
/* Attempt the connect. This is where the talker may hang. */
if (connect(nl->socket,(struct sockaddr *)&con_addr,sizeof(con_addr))==-1) {
reset_alarm(); return 1;
}
reset_alarm();
nl->type=OUTGOING;
nl->stage=VERIFYING;
nl->last_recvd=time(0);
return 0;
}
/************* WRITE FUNCTIONS ************/
/*** Write a NULL terminated string to a socket ***/
write_sock(sock,str)
int sock;
char *str;
{
write(sock,str,strlen(str));
}
/*** Send message to user ***/
write_user(user,str)
UR_OBJECT user;
char *str;
{
int buffpos,sock,i;
char *start,buff[OUT_BUFF_SIZE],mesg[ARR_SIZE],*colour_com_strip();
if (user==NULL) return;
if (user->type==REMOTE_TYPE) {
if (user->netlink->ver_major<=3
&& user->netlink->ver_minor<2) str=colour_com_strip(str);
if (str[strlen(str)-1]!='\n')
sprintf(mesg,"MSG %s\n%s\nEMSG\n",user->name,str);
else sprintf(mesg,"MSG %s\n%sEMSG\n",user->name,str);
write_sock(user->netlink->socket,mesg);
return;
}
start=str;
buffpos=0;
sock=user->socket;
/* Process string and write to buffer. We use pointers here instead of arrays
since these are supposedly much faster (though in reality I guess it depends
on the compiler) which is necessary since this routine is used all the
time. */
while(*str) {
if (*str=='\n') {
if (buffpos>OUT_BUFF_SIZE-6) {
write(sock,buff,buffpos); buffpos=0;
}
/* Reset terminal before every newline */
if (user->colour) {
memcpy(buff+buffpos,"\033[0m",4); buffpos+=4;
}
*(buff+buffpos)='\n'; *(buff+buffpos+1)='\r';
buffpos+=2; ++str;
}
else {
/* See if its a / before a ~ , if so then we print colour command
as text */
if (*str=='/' && *(str+1)=='~') { ++str; continue; }
if (str!=start && *str=='~' && *(str-1)=='/') {
*(buff+buffpos)=*str; goto CONT;
}
/* Process colour commands eg ~FR. We have to strip out the commands
from the string even if user doesnt have colour switched on hence
the user->colour check isnt done just yet */
if (*str=='~') {
if (buffpos>OUT_BUFF_SIZE-6) {
write(sock,buff,buffpos); buffpos=0;
}
++str;
for(i=0;i<NUM_COLS;++i) {
if (!strncmp(str,colcom[i],2)) {
if (user->colour) {
memcpy(buff+buffpos,colcode[i],strlen(colcode[i]));
buffpos+=strlen(colcode[i])-1;
}
else buffpos--;
++str;
goto CONT;
}
}
*(buff+buffpos)=*(--str);
}
else *(buff+buffpos)=*str;
CONT:
++buffpos; ++str;
}
if (buffpos==OUT_BUFF_SIZE) {
write(sock,buff,OUT_BUFF_SIZE); buffpos=0;
}
}
if (buffpos) write(sock,buff,buffpos);
/* Reset terminal at end of string */
if (user->colour) write_sock(sock,"\033[0m");
}
/*** Write to users of level 'level' and above or below depending on above
variable; if 1 then above else below ***/
write_level(level,above,str,user)
int level,above;
char *str;
UR_OBJECT user;
{
UR_OBJECT u;
for(u=user_first;u!=NULL;u=u->next) {
if (u!=user && !u->login && u->type!=CLONE_TYPE) {
if ((above && u->level>=level) || (!above && u->level<=level))
write_user(u,str);
}
}
}
/*** Subsid function to below but this one is used the most ***/
write_room(rm,str)
RM_OBJECT rm;
char *str;
{
write_room_except(rm,str,NULL);
}
/*** Write to everyone in room rm except for "user". If rm is NULL write
to all rooms. ***/
write_room_except(rm,str,user)
RM_OBJECT rm;
char *str;
UR_OBJECT user;
{
UR_OBJECT u;
char text2[ARR_SIZE];
for(u=user_first;u!=NULL;u=u->next) {
if (u->login
|| u->room==NULL
|| (u->room!=rm && rm!=NULL)
|| (u->ignall && !force_listen)
|| (u->ignshout && (com_num==SHOUT || com_num==SEMOTE))
|| u==user) continue;
if (u->type==CLONE_TYPE) {
if (u->clone_hear==CLONE_HEAR_NOTHING || u->owner->ignall) continue;
/* Ignore anything not in clones room, eg shouts, system messages
and semotes since the clones owner will hear them anyway. */
if (rm!=u->room) continue;
if (u->clone_hear==CLONE_HEAR_SWEARS) {
if (!contains_swearing(str)) continue;
}
sprintf(text2,"~FT[ %s ]:~RS %s",u->room->name,str);
write_user(u->owner,text2);
}
else write_user(u,str);
}
}
/*** Write a string to system log ***/
write_syslog(str,write_time)
char *str;
int write_time;
{
FILE *fp;
if (!system_logging || !(fp=fopen(SYSLOG,"a"))) return;
if (!write_time) fputs(str,fp);
else fprintf(fp,"%02d/%02d %02d:%02d:%02d: %s",tmday,tmonth+1,thour,tmin,tsec,str);
fclose(fp);
}
/******** LOGIN/LOGOUT FUNCTIONS ********/
/*** Login function. Lots of nice inline code :) ***/
login(user,inpstr)
UR_OBJECT user;
char *inpstr;
{
UR_OBJECT u;
int i;
char name[ARR_SIZE],passwd[ARR_SIZE];
name[0]='\0'; passwd[0]='\0';
switch(user->login) {
case 3:
sscanf(inpstr,"%s",name);
if(name[0]<33) {
write_user(user,"\nGive me a name: "); return;
}
if (!strcmp(name,"quit")) {
write_user(user,"\n\n*** Abandoning login attempt ***\n\n");
disconnect_user(user); return;
}
if (!strcmp(name,"who")) {
who(user,0);
write_user(user,"\nGive me a name: ");
return;
}
if (!strcmp(name,"version")) {
sprintf(text,"\nNUTS version %s\n\nGive me a name: ",VERSION);
write_user(user,text); return;
}
if (strlen(name)<3) {
write_user(user,"\nName too short.\n\n");
attempts(user); return;
}
if (strlen(name)>USER_NAME_LEN) {
write_user(user,"\nName too long.\n\n");
attempts(user); return;
}
/* see if only letters in login */
for (i=0;i<strlen(name);++i) {
if (!isalpha(name[i])) {
write_user(user,"\nOnly letters are allowed in a name.\n\n");
attempts(user); return;
}
}
if (!allow_caps_in_name) strtolower(name);
name[0]=toupper(name[0]);
if (user_banned(name)) {
write_user(user,"\nYou are banned from this talker.\n\n");
disconnect_user(user);
sprintf(text,"Attempted login by banned user %s.\n",name);
write_syslog(text,1);
return;
}
strcpy(user->name,name);
/* If user has hung on another login clear that session */
for(u=user_first;u!=NULL;u=u->next) {
if (u->login && u!=user && !strcmp(u->name,user->name)) {
disconnect_user(u); break;
}
}
if (!load_user_details(user)) {
if (user->port==port[1]) {
write_user(user,"\nSorry, new logins cannot be created on this port.\n\n");
disconnect_user(user);
return;
}
if (minlogin_level>-1) {
write_user(user,"\nSorry, new logins cannot be created at this time.\n\n");
disconnect_user(user);
return;
}
write_user(user,"New user...\n");
}
else {
if (user->port==port[1] && user->level<wizport_level) {
sprintf(text,"\nSorry, only users of level %s and above can log in on this port.\n\n",level_name[wizport_level]);
write_user(user,text);
disconnect_user(user);
return;
}
if (user->level<minlogin_level) {
write_user(user,"\nSorry, the talker is locked out to users of your level.\n\n");
disconnect_user(user);
return;
}
}
write_user(user,"Give me a password: ");
echo_off(user);
user->login=2;
return;
case 2:
sscanf(inpstr,"%s",passwd);
if (strlen(passwd)<3) {
write_user(user,"\n\nPassword too short.\n\n");
attempts(user); return;
}
if (strlen(passwd)>PASS_LEN) {
write_user(user,"\n\nPassword too long.\n\n");
attempts(user); return;
}
/* if new user... */
if (!user->pass[0]) {
strcpy(user->pass,(char *)crypt(passwd,"NU"));
write_user(user,"\nPlease confirm password: ");
user->login=1;
}
else {
if (!strcmp(user->pass,(char *)crypt(passwd,"NU"))) {
echo_on(user); connect_user(user); return;
}
write_user(user,"\n\nIncorrect login.\n\n");
attempts(user);
}
return;
case 1:
sscanf(inpstr,"%s",passwd);
if (strcmp(user->pass,(char*)crypt(passwd,"NU"))) {
write_user(user,"\n\nPasswords do not match.\n\n");
attempts(user);
return;
}
echo_on(user);
strcpy(user->desc,"hasn't used .desc yet");
strcpy(user->in_phrase,"enters");
strcpy(user->out_phrase,"goes");
user->last_site[0]='\0';
user->level=0;
user->muzzled=0;
user->command_mode=0;
user->prompt=prompt_def;
user->colour=colour_def;
user->charmode_echo=charecho_def;
save_user_details(user,1);
sprintf(text,"New user \"%s\" created.\n",user->name);
write_syslog(text,1);
connect_user(user);
}
}
/*** Count up attempts made by user to login ***/
attempts(user)
UR_OBJECT user;
{
user->attempts++;
if (user->attempts==3) {
write_user(user,"\nMaximum attempts reached.\n\n");
disconnect_user(user); return;
}
user->login=3;
user->pass[0]='\0';
write_user(user,"Give me a name: ");
echo_on(user);
}
/*** Load the users details ***/
load_user_details(user)
UR_OBJECT user;
{
FILE *fp;
char line[81],filename[80];
int temp1,temp2,temp3;
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"r"))) return 0;
fscanf(fp,"%s",user->pass); /* password */
fscanf(fp,"%d %d %d %d %d %d %d %d %d %d",&temp1,&temp2,&user->last_login_len,&temp3,&user->level,&user->prompt,&user->muzzled,&user->charmode_echo,&user->command_mode,&user->colour);
user->last_login=(time_t)temp1;
user->total_login=(time_t)temp2;
user->read_mail=(time_t)temp3;
fscanf(fp,"%s\n",user->last_site);
/* Need to do the rest like this 'cos they may be more than 1 word each */
fgets(line,USER_DESC_LEN+2,fp);
line[strlen(line)-1]=0;
strcpy(user->desc,line);
fgets(line,PHRASE_LEN+2,fp);
line[strlen(line)-1]=0;
strcpy(user->in_phrase,line);
fgets(line,PHRASE_LEN+2,fp);
line[strlen(line)-1]=0;
strcpy(user->out_phrase,line);
fclose(fp);
return 1;
}
/*** Save a users stats ***/
save_user_details(user,save_current)
UR_OBJECT user;
int save_current;
{
FILE *fp;
char filename[80];
if (user->type==REMOTE_TYPE || user->type==CLONE_TYPE) return 0;
sprintf(filename,"%s/%s.D",USERFILES,user->name);
if (!(fp=fopen(filename,"w"))) {
sprintf(text,"%s: failed to save your details.\n",syserror);
write_user(user,text);
sprintf(text,"SAVE_USER_STATS: Failed to save %s's details.\n",user->name);
write_syslog(text,1);
return 0;
}
fprintf(fp,"%s\n",user->pass);
if (save_current)
fprintf(fp,"%d %d %d ",(int)time(0),(int)user->total_login,(int)(time(0)-user->last_login));
else fprintf(fp,"%d %d %d ",(int)user->last_login,(int)user->total_login,user->last_login_len);
fprintf(fp,"%d %d %d %d %d %d %d\n",(int)user->read_mail,user->level,user->prompt,user->muzzled,user->charmode_echo,user->command_mode,user->colour);
if (save_current) fprintf(fp,"%s\n",user->site);
else fprintf(fp,"%s\n",user->last_site);
fprintf(fp,"%s\n",user->desc);
fprintf(fp,"%s\n",user->in_phrase);
fprintf(fp,"%s\n",user->out_phrase);
fclose(fp);
return 1;
}
/*** Connect the user to the talker proper ***/
connect_user(user)
UR_OBJECT user;
{
UR_OBJECT u,u2;
RM_OBJECT rm;
char temp[30];
/* See if user already connected */
for(u=user_first;u!=NULL;u=u->next) {
if (user!=u && user->type!=CLONE_TYPE && !strcmp(user->name,u->name)) {
rm=u->room;
if (u->type==REMOTE_TYPE) {
write_user(u,"\n~FB~OLYou are pulled back through cyberspace...\n");
sprintf(text,"REMVD %s\n",u->name);
write_sock(u->netlink->socket,text);
sprintf(text,"%s vanishes.\n",u->name);
destruct_user(u);
write_room(rm,text);
reset_access(rm);
num_of_users--;
break;
}
write_user(user,"\n\nYou are already connected - switching to old session...\n");
sprintf(text,"%s swapped sessions.\n",user->name);
write_syslog(text,1);
close(u->socket);
u->socket=user->socket;
strcpy(u->site,user->site);
u->site_port=user->site_port;
destruct_user(user);
num_of_logins--;
sprintf(text,"~OLSESSION SWAP:~RS %s %s\n",u->name,u->desc);
write_room_except(rm,text,u);
if (rm==NULL) {
sprintf(text,"ACT %s look\n",u->name);
write_sock(u->netlink->socket,text);
}
else {
look(u); prompt(u);
}
/* Reset the sockets on any clones */
for(u2=user_first;u2!=NULL;u2=u2->next) {
if (u2->type==CLONE_TYPE && u2->owner==user) {
u2->socket=u->socket; u->owner=u;
}
}
return;
}
}
/* Announce users logon. You're probably wondering why Ive done it this strange
way , well its to avoid a minor bug that arose when as in 3.3.1 it created
the string in 2 parts and sent the parts seperately over a netlink. If you
want more details email me. */
sprintf(text,"~OLSIGN ON:~RS %s %s\n",user->name,user->desc);
write_level(USER,0,text,NULL);
sprintf(text,"~OLSIGN ON:~RS %s %s ~RS~FT(%s:%d)\n",user->name,user->desc,user->site,user->site_port);
write_level(WIZ,1,text,NULL);
/* send post-login message and other logon stuff to user */
write_user(user,"\n");
more(user,user->socket,MOTD2);
if (user->last_site[0]) {
sprintf(temp,"%s",ctime(&user->last_login));
temp[strlen(temp)-1]=0;
sprintf(text,"Welcome %s...\n\n~BBYou were last logged in on %s from %s.\n\n",user->name,temp,user->last_site);
}
else sprintf(text,"Welcome %s...\n\n",user->name);
write_user(user,text);
user->room=room_first;
user->last_login=time(0); /* set to now */
sprintf(text,"~FTYour level is:~RS~OL %s\n",level_name[user->level]);
write_user(user,text);
look(user);
if (has_unread_mail(user)) write_user(user,"\07~FT~OL~LI** YOU HAVE UNREAD MAIL **\n");
prompt(user);
/* write to syslog and set up some vars */
sprintf(text,"%s logged in on port %d from %s:%d.\n",user->name,user->port,user->site,user->site_port);
write_syslog(text,1);
num_of_users++;
num_of_logins--;
user->login=0;
}
/*** Disconnect user from talker ***/
disconnect_user(user)
UR_OBJECT user;
{
RM_OBJECT rm;
NL_OBJECT nl;
rm=user->room;
if (user->login) {
close(user->socket);
destruct_user(user);
num_of_logins--;
return;
}
if (user->type!=REMOTE_TYPE) {
save_user_details(user,1);
sprintf(text,"%s logged out.\n",user->name);
write_syslog(text,1);
write_user(user,"\n~OL~FBYou are removed from this reality...\n\n");
close(user->socket);
sprintf(text,"~OLSIGN OFF:~RS %s %s\n",user->name,user->desc);
write_room(NULL,text);
if (user->room==NULL) {
sprintf(text,"REL %s\n",user->name);
write_sock(user->netlink->socket,text);
for(nl=nl_first;nl!=NULL;nl=nl->next)
if (nl->mesg_user==user) {
nl->mesg_user=(UR_OBJECT)-1; break;
}
}
}
else {
write_user(user,"\n~FR~OLYou are pulled back in disgrace to your own domain...\n");
sprintf(text,"REMVD %s\n",user->name);
write_sock(user->netlink->socket,text);
sprintf(text,"~FR~OL%s is banished from here!\n",user->name);
write_room_except(rm,text,user);
sprintf(text,"NETLINK: Remote user %s removed.\n",user->name);
write_syslog(text,1);
}
if (user->malloc_start!=NULL) free(user->malloc_start);
num_of_users--;
/* Destroy any clones */
destroy_user_clones(user);
destruct_user(user);
reset_access(rm);
destructed=0;
}
/*** Tell telnet not to echo characters - for password entry ***/
echo_off(user)
UR_OBJECT user;
{
char seq[4];
if (password_echo) return;
sprintf(seq,"%c%c%c",255,251,1);
write_user(user,seq);
}
/*** Tell telnet to echo characters ***/
echo_on(user)
UR_OBJECT user;
{
char seq[4];
if (password_echo) return;
sprintf(seq,"%c%c%c",255,252,1);
write_user(user,seq);
}
/************ MISCELLANIOUS FUNCTIONS *************/
/*** Stuff that is neither speech nor a command is dealt with here ***/
misc_ops(user,inpstr)
UR_OBJECT user;
char *inpstr;
{
switch(user->misc_op) {
case 1:
if (toupper(inpstr[0])=='Y') {
if (rs_countdown && !rs_which) {
if (rs_countdown>60)
sprintf(text,"\n\07~OLSYSTEM: ~FR~LISHUTDOWN INITIATED, shutdown in %d minutes, %d seconds!\n\n",rs_countdown/60,rs_countdown%60);
else sprintf(text,"\n\07~OLSYSTEM: ~FR~LISHUTDOWN INITIATED, shutdown in %d seconds!\n\n",rs_countdown);
write_room(NULL,text);
sprintf(text,"%s initiated a %d seconds SHUTDOWN countdown.\n",user->name,rs_countdown);
write_syslog(text,1);
rs_user=user;
rs_announce=time(0);
user->misc_op=0;
prompt(user);
return 1;
}
talker_shutdown(user,NULL,0);
}
/* This will reset any reboot countdown that was started, oh well */
rs_countdown=0;
rs_announce=0;
rs_which=-1;
rs_user=NULL;
user->misc_op=0;
prompt(user);
return 1;
case 2:
if (toupper(inpstr[0])=='E'
|| more(user,user->socket,user->page_file)!=1) {
user->misc_op=0; user->filepos=0; user->page_file[0]='\0';
prompt(user);
}
return 1;
case 3: /* writing on board */
case 4: /* Writing mail */
case 5: /* doing profile */
editor(user,inpstr); return 1;
case 6:
if (toupper(inpstr[0])=='Y') delete_user(user,1);
else { user->misc_op=0; prompt(user); }
return 1;
case 7:
if (toupper(inpstr[0])=='Y') {
if (rs_countdown && rs_which==1) {
if (rs_countdown>60)
sprintf(text,"\n\07~OLSYSTEM: ~FY~LIREBOOT INITIATED, rebooting in %d minutes, %d seconds!\n\n",rs_countdown/60,rs_countdown%60);
else sprintf(text,"\n\07~OLSYSTEM: ~FY~LIREBOOT INITIATED, rebooting in %d seconds!\n\n",rs_countdown);
write_room(NULL,text);
sprintf(text,"%s initiated a %d seconds REBOOT countdown.\n",user->name,rs_countdown);
write_syslog(text,1);
rs_user=user;
rs_announce=time(0);
user->misc_op=0;
prompt(user);
return 1;
}
talker_shutdown(user,NULL,1);
}
if (rs_which==1 && rs_countdown && rs_user==NULL) {
rs_countdown=0;
rs_announce=0;
rs_which=-1;
}
user->misc_op=0;
prompt(user);
return 1;
}
return 0;
}
/*** The editor used for writing profiles, mail and messages on the boards ***/
editor(user,inpstr)
UR_OBJECT user;
char *inpstr;
{
int cnt,line;
char *edprompt="\n~FGSave~RS, ~FYredo~RS or ~FRabort~RS (s/r/a): ";
char *ptr;
if (user->edit_op) {
switch(toupper(*inpstr)) {
case 'S':
sprintf(text,"%s finishes composing some text.\n",user->name);
write_room_except(user->room,text,user);
switch(user->misc_op) {
case 3: write_board(user,NULL,1); break;
case 4: smail(user,NULL,1); break;
case 5: enter_profile(user,1); break;
}
editor_done(user);
return;
case 'R':
user->edit_op=0;
user->edit_line=1;
user->charcnt=0;
user->malloc_end=user->malloc_start;
*user->malloc_start='\0';
sprintf(text,"\nRedo message...\n\n%d>",user->edit_line);
write_user(user,text);
return;
case 'A':
write_user(user,"\nMessage aborted.\n");
sprintf(text,"%s gives up composing some text.\n",user->name);
write_room_except(user->room,text,user);
editor_done(user);
return;
default:
write_user(user,edprompt);
return;
}
}
/* Allocate memory if user has just started editing */
if (user->malloc_start==NULL) {
if ((user->malloc_start=(char *)malloc(MAX_LINES*81))==NULL) {
sprintf(text,"%s: failed to allocate buffer memory.\n",syserror);
write_user(user,text);
write_syslog("ERROR: Failed to allocate memory in editor().\n",0);
user->misc_op=0;
prompt(user);
return;
}
user->ignall_store=user->ignall;
user->ignall=1; /* Dont want chat mucking up the edit screen */
user->edit_line=1;
user->charcnt=0;
user->malloc_end=user->malloc_start;
*user->malloc_start='\0';
sprintf(text,"~FTMaximum of %d lines, end with a '.' on a line by itself.\n\n1>",MAX_LINES);
write_user(user,text);
sprintf(text,"%s starts composing some text...\n",user->name);
write_room_except(user->room,text,user);
return;
}
/* Check for empty line */
if (!word_count) {
if (!user->charcnt) {
sprintf(text,"%d>",user->edit_line);
write_user(user,text);
return;
}
*user->malloc_end++='\n';
if (user->edit_line==MAX_LINES) goto END;
sprintf(text,"%d>",++user->edit_line);
write_user(user,text);
user->charcnt=0;
return;
}
/* If nothing carried over and a dot is entered then end */
if (!user->charcnt && !strcmp(inpstr,".")) goto END;
line=user->edit_line;
cnt=user->charcnt;
/* loop through input and store in allocated memory */
while(*inpstr) {
*user->malloc_end++=*inpstr++;
if (++cnt==80) { user->edit_line++; cnt=0; }
if (user->edit_line>MAX_LINES
|| user->malloc_end - user->malloc_start>=MAX_LINES*81) goto END;
}
if (line!=user->edit_line) {
ptr=(char *)(user->malloc_end-cnt);
*user->malloc_end='\0';
sprintf(text,"%d>%s",user->edit_line,ptr);
write_user(user,text);
user->charcnt=cnt;
return;
}
else {
*user->malloc_end++='\n';
user->charcnt=0;
}
if (user->edit_line!=MAX_LINES) {
sprintf(text,"%d>",++user->edit_line);
write_user(user,text);
return;
}
/* User has finished his message , prompt for what to do now */
END:
*user->malloc_end='\0';
if (*user->malloc_start) {
write_user(user,edprompt);
user->edit_op=1; return;
}
write_user(user,"\nNo text.\n");
sprintf(text,"%s gives up composing some text.\n",user->name);
write_room_except(user->room,text,user);
editor_done(user);
}
/*** Reset some values at the end of editing ***/
editor_done(user)
UR_OBJECT user;
{
user->misc_op=0;
user->edit_op=0;
user->edit_line=0;
free(user->malloc_start);
user->malloc_start=NULL;
user->malloc_end=NULL;
user->ignall=user->ignall_store;
prompt(user);
}
/*** Record speech and emotes in the room. ***/
record(rm,str)
RM_OBJECT rm;
char *str;
{
strncpy(rm->revbuff[rm->revline],str,REVIEW_LEN);
rm->revbuff[rm->revline][REVIEW_LEN]='\n';
rm->revbuff[rm->revline][REVIEW_LEN+1]='\0';
rm->revline=(rm->revline+1)%REVIEW_LINES;
}
/*** Records tells and pemotes sent to the user. ***/
record_tell(user,str)
UR_OBJECT user;
char *str;
{
strncpy(user->revbuff[user->revline],str,REVIEW_LEN);
user->revbuff[user->revline][REVIEW_LEN]='\n';
user->revbuff[user->revline][REVIEW_LEN+1]='\0';
user->revline=(user->revline+1)%REVTELL_LINES;
}
/*** Set room access back to public if not enough users in room ***/
reset_access(rm)
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<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);
}
}
/*** Exit because of error during bootup ***/
boot_exit(code)
int code;
{
switch(code) {
case 1:
write_syslog("BOOT FAILURE: Error while parsing configuration file.\n",0);
exit(1);
case 2:
perror("NUTS: Can't open main port listen socket");
write_syslog("BOOT FAILURE: Can't open main port listen socket.\n",0);
exit(2);
case 3:
perror("NUTS: Can't open wiz port listen socket");
write_syslog("BOOT FAILURE: Can't open wiz port listen socket.\n",0);
exit(3);
case 4:
perror("NUTS: Can't open link port listen socket");
write_syslog("BOOT FAILURE: Can't open link port listen socket.\n",0);
exit(4);
case 5:
perror("NUTS: Can't bind to main port");
write_syslog("BOOT FAILURE: Can't bind to main port.\n",0);
exit(5);
case 6:
perror("NUTS: Can't bind to wiz port");
write_syslog("BOOT FAILURE: Can't bind to wiz port.\n",0);
exit(6);
case 7:
perror("NUTS: Can't bind to link port");
write_syslog("BOOT FAILURE: Can't bind to link port.\n",0);
exit(7);
case 8:
perror("NUTS: Listen error on main port");
write_syslog("BOOT FAILURE: Listen error on main port.\n",0);
exit(8);
case 9:
perror("NUTS: Listen error on wiz port");
write_syslog("BOOT FAILURE: Listen error on wiz port.\n",0);
exit(9);
case 10:
perror("NUTS: Listen error on link port");
write_syslog("BOOT FAILURE: Listen error on link port.\n",0);
exit(10);
case 11:
perror("NUTS: Failed to fork");
write_syslog("BOOT FAILURE: Failed to fork.\n",0);
exit(11);
}
}
/*** User prompt ***/
prompt(user)
UR_OBJECT user;
{
int hr,min;
if (no_prompt) return;
if (user->type==REMOTE_TYPE) {
sprintf(text,"PRM %s\n",user->name);
write_sock(user->netlink->socket,text);
return;
}
if (user->command_mode && !user->misc_op) {
if (!user->vis) write_user(user,"~FTCOM+> ");
else write_user(user,"~FTCOM> ");
return;
}
if (!user->prompt || user->misc_op) return;
hr=(int)(time(0)-user->last_login)/3600;
min=((int)(time(0)-user->last_login)%3600)/60;
if (!user->vis)
sprintf(text,"~FT<%02d:%02d, %02d:%02d, %s+>\n",thour,tmin,hr,min,user->name);
else sprintf(text,"~FT<%02d:%02d, %02d:%02d, %s>\n",thour,tmin,hr,min,user->name);
write_user(user,text);
}
/*** Page a file out to user. Colour commands in files will only work if
user!=NULL since if NULL we dont know if his terminal can support colour
or not. Return values:
0 = cannot find file, 1 = found file, 2 = found and finished ***/
more(user,sock,filename)
UR_OBJECT user;
int sock;
char *filename;
{
int i,buffpos,num_chars,lines,retval,len;
char buff[OUT_BUFF_SIZE],text2[83],*str,*colour_com_strip();
FILE *fp;
if (!(fp=fopen(filename,"r"))) {
if (user!=NULL) user->filepos=0;
return 0;
}
/* jump to reading posn in file */
if (user!=NULL) fseek(fp,user->filepos,0);
text[0]='\0';
buffpos=0;
num_chars=0;
retval=1;
len=0;
/* If user is remote then only do 1 line at a time */
if (sock==-1) {
lines=1; fgets(text2,82,fp);
}
else {
lines=0; fgets(text,sizeof(text)-1,fp);
}
/* Go through file */
while(!feof(fp) && (lines<23 || user==NULL)) {
if (sock==-1) {
lines++;
if (user->netlink->ver_major<=3 && user->netlink->ver_minor<2)
str=colour_com_strip(text2);
else str=text2;
if (str[strlen(str)-1]!='\n')
sprintf(text,"MSG %s\n%s\nEMSG\n",user->name,str);
else sprintf(text,"MSG %s\n%sEMSG\n",user->name,str);
write_sock(user->netlink->socket,text);
num_chars+=strlen(str);
fgets(text2,82,fp);
continue;
}
str=text;
/* Process line from file */
while(*str) {
if (*str=='\n') {
if (buffpos>OUT_BUFF_SIZE-6) {
write(sock,buff,buffpos); buffpos=0;
}
/* Reset terminal before every newline */
if (user!=NULL && user->colour) {
memcpy(buff+buffpos,"\033[0m",4); buffpos+=4;
}
*(buff+buffpos)='\n'; *(buff+buffpos+1)='\r';
buffpos+=2; ++str;
}
else {
/* Process colour commands in the file. See write_user()
function for full comments on this code. */
if (*str=='/' && *(str+1)=='~') { ++str; continue; }
if (str!=text && *str=='~' && *(str-1)=='/') {
*(buff+buffpos)=*str; goto CONT;
}
if (*str=='~') {
if (buffpos>OUT_BUFF_SIZE-6) {
write(sock,buff,buffpos); buffpos=0;
}
++str;
for(i=0;i<NUM_COLS;++i) {
if (!strncmp(str,colcom[i],2)) {
if (user!=NULL && user->colour) {
memcpy(buffpos+buff,colcode[i],strlen(colcode[i]));
buffpos+=strlen(colcode[i])-1;
}
else buffpos--;
++str;
goto CONT;
}
}
*(buff+buffpos)=*(--str);
}
else *(buff+buffpos)=*str;
CONT:
++buffpos; ++str;
}
if (buffpos==OUT_BUFF_SIZE) {
write(sock,buff,OUT_BUFF_SIZE); buffpos=0;
}
}
len=strlen(text);
num_chars+=len;
lines+=len/80+(len<80);
fgets(text,sizeof(text)-1,fp);
}
if (buffpos && sock!=-1) write(sock,buff,buffpos);
/* if user is logging on dont page file */
if (user==NULL) { fclose(fp); return 2; };
if (feof(fp)) {
user->filepos=0; no_prompt=0; retval=2;
}
else {
/* store file position and file name */
user->filepos+=num_chars;
strcpy(user->page_file,filename);
/* We use E here instead of Q because when on a remote system and
in COMMAND mode the Q will be intercepted by the home system and
quit the user */
write_user(user," ~BB*** Press <return> to continue, 'e'<return> to exit ***");
no_prompt=1;
}
fclose(fp);
return retval;
}
/*** Set global vars. hours,minutes,seconds,date,day,month,year ***/
set_date_time()
{
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; /* Will this work past the year 2000? Hmm... */
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;
}
/*** Return pos. of second word in inpstr ***/
char *remove_first(inpstr)
char *inpstr;
{
char *pos=inpstr;
while(*pos<33 && *pos) ++pos;
while(*pos>32) ++pos;
while(*pos<33 && *pos) ++pos;
return pos;
}
/*** Get user struct pointer from name ***/
UR_OBJECT get_user(name)
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;
}
/* Search for close match name */
for(u=user_first;u!=NULL;u=u->next) {
if (u->login || u->type==CLONE_TYPE) continue;
if (strstr(u->name,name)) return u;
}
return NULL;
}
/*** Get room struct pointer from abbreviated name ***/
RM_OBJECT get_room(name)
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;
}
/*** Return level value based on level name ***/
get_level(name)
char *name;
{
int i;
i=0;
while(level_name[i][0]!='*') {
if (!strcmp(level_name[i],name)) return i;
++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. ***/
has_room_access(user,rm)
UR_OBJECT user;
RM_OBJECT rm;
{
if ((rm->access & PRIVATE)
&& user->level<gatecrash_level
&& user->invite_room!=rm
&& !((rm->access & FIXED) && user->level>=WIZ)) return 0;
return 1;
}
/*** See if user has unread mail, mail file has last read time on its
first line ***/
has_unread_mail(user)
UR_OBJECT user;
{
FILE *fp;
int tm;
char filename[80];
sprintf(filename,"%s/%s.M",USERFILES,user->name);
if (!(fp=fopen(filename,"r"))) return 0;
fscanf(fp,"%d",&tm);
fclose(fp);
if (tm>(int)user->read_mail) return 1;
return 0;
}
/*** This is function that sends mail to other users ***/
send_mail(user,to,ptr)
UR_OBJECT user;
char *to,*ptr;
{
NL_OBJECT nl;
FILE *infp,*outfp;
char *c,d,*service,filename[80],line[DNL+1];
/* See if remote mail */
c=to; service=NULL;
while(*c) {
if (*c=='@') {
service=c+1; *c='\0';
for(nl=nl_first;nl!=NULL;nl=nl->next) {
if (!strcmp(nl->service,service) && nl->stage==UP) {
send_external_mail(nl,user,to,ptr);
return;
}
}
sprintf(text,"Service %s unavailable.\n",service);
write_user(user,text);
return;
}
++c;
}
/* Local mail */
if (!(outfp=fopen("tempfile","w"))) {
write_user(user,"Error in mail delivery.\n");
write_syslog("ERROR: Couldn't open tempfile in send_mail().\n",0);
return;
}
/* Write current time on first line of tempfile */
fprintf(outfp,"%d\r",(int)time(0));
/* Copy current mail file into tempfile if it exists */
sprintf(filename,"%s/%s.M",USERFILES,to);
if (infp=fopen(filename,"r")) {
/* Discard first line of mail file. */
fgets(line,DNL,infp);
/* Copy rest of file */
d=getc(infp);
while(!feof(infp)) { putc(d,outfp); d=getc(infp); }
fclose(infp);
}
/* Put new mail in tempfile */
if (user!=NULL) {
if (user->type==REMOTE_TYPE)
fprintf(outfp,"~OLFrom: %s@%s %s\n",user->name,user->netlink->service,long_date(0));
else fprintf(outfp,"~OLFrom: %s %s\n",user->name,long_date(0));
}
else fprintf(outfp,"~OLFrom: MAILER %s\n",long_date(0));
fputs(ptr,outfp);
fputs("\n",outfp);
fclose(outfp);
rename("tempfile",filename);
write_user(user,"Mail sent.\n");
write_user(get_user(to),"\07~FT~OL~LI** YOU HAVE NEW MAIL **\n");
}
/*** Spool mail file and ask for confirmation of users existence on remote
site ***/
send_external_mail(nl,user,to,ptr)
NL_OBJECT nl;
UR_OBJECT user;
char *to,*ptr;
{
FILE *fp;
char filename[80];
/* Write out to spool file first */
sprintf(filename,"%s/OUT_%s_%s@%s",MAILSPOOL,user->name,to,nl->service);
if (!(fp=fopen(filename,"a"))) {
sprintf(text,"%s: unable to spool mail.\n",syserror);
write_user(user,text);
sprintf(text,"ERROR: Couldn't open file %s to append in send_external_mail().\n",filename);
write_syslog(text,0);
return;
}
putc('\n',fp);
fputs(ptr,fp);
fclose(fp);
/* Ask for verification of users existence */
sprintf(text,"EXISTS? %s %s\n",to,user->name);
write_sock(nl->socket,text);
/* Rest of delivery process now up to netlink functions */
write_user(user,"Mail sent.\n");
}
/*** See if string contains any swearing ***/
contains_swearing(str)
char *str;
{
char *s;
int i;
if ((s=(char *)malloc(strlen(str)+1))==NULL) {
write_syslog("ERROR: Failed to allocate memory in contains_swearing().\n",0);
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;
}
/*** Count the number of colour commands in a string ***/
colour_com_count(str)
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,colcom[i],2)) {
cnt++; s++; continue;
}
}
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(str)
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,colcom[i],2)) { s++; goto CONT; }
}
--s; *t++=*s;
}
else *t++=*s;
CONT:
s++;
}
*t='\0';
return text2;
}
/*** Date string for board messages, mail, .who and .allclones ***/
char *long_date(which)
int which;
{
static char dstr[80];
if (which) sprintf(dstr,"on %s %d %s %d at %02d:%02d",day[twday],tmday,month[tmonth],tyear,thour,tmin);
else sprintf(dstr,"[ %s %d %s %d at %02d:%02d ]",day[twday],tmday,month[tmonth],tyear,thour,tmin);
return dstr;
}
/*** Clear the review buffer in the room ***/
clear_revbuff(rm)
RM_OBJECT rm;
{
int c;
for(c=0;c<REVIEW_LINES;++c) rm->revbuff[c][0]='\0';
rm->revline=0;
}
/*** Clear the screen ***/
cls(user)
UR_OBJECT user;
{
int i;
for(i=0;i<5;++i) write_user(user,"\n\n\n\n\n\n\n\n\n\n");
}
/*** Convert string to upper case ***/
strtoupper(str)
char *str;
{
while(*str) { *str=toupper(*str); str++; }
}
/*** Convert string to lower case ***/
strtolower(str)
char *str;
{
while(*str) { *str=tolower(*str); str++; }
}
/*** Returns 1 if string is a positive number ***/
isnumber(str)
char *str;
{
while(*str) if (!isdigit(*str++)) return 0;
return 1;
}
/************ OBJECT FUNCTIONS ************/
/*** Construct user/clone object ***/
UR_OBJECT create_user()
{
UR_OBJECT user;
int i;
if ((user=(UR_OBJECT)malloc(sizeof(struct user_struct)))==NULL) {
write_syslog("ERROR: Memory allocation failure in create_user().\n",0);
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->name[0]='\0';
user->desc[0]='\0';
user->in_phrase[0]='\0';
user->out_phrase[0]='\0';
user->afk_mesg[0]='\0';
user->pass[0]='\0';
user->site[0]='\0';
user->site_port=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->buffpos=0;
user->filepos=0;
user->read_mail=time(0);
user->room=NULL;
user->invite_room=NULL;
user->port=0;
user->login=0;
user->socket=-1;
user->attempts=0;
user->command_mode=0;
user->level=0;
user->vis=1;
user->ignall=0;
user->ignall_store=0;
user->ignshout=0;
user->igntell=0;
user->muzzled=0;
user->remote_com=-1;
user->netlink=NULL;
user->pot_netlink=NULL;
user->last_input=time(0);
user->last_login=time(0);
user->last_login_len=0;
user->total_login=0;
user->prompt=prompt_def;
user->colour=colour_def;
user->charmode_echo=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->malloc_start=NULL;
user->malloc_end=NULL;
user->owner=NULL;
for(i=0;i<REVTELL_LINES;++i) user->revbuff[i][0]='\0';
return user;
}
/*** Destruct an object. ***/
destruct_user(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()
{
RM_OBJECT room;
int i;
if ((room=(RM_OBJECT)malloc(sizeof(struct room_struct)))==NULL) {
fprintf(stderr,"NUTS: Memory allocation failure in create_room().\n");
boot_exit(1);
}
room->name[0]='\0';
room->label[0]='\0';
room->desc[0]='\0';
room->topic[0]='\0';
room->access=-1;
room->revline=0;
room->mesg_cnt=0;
room->inlink=0;
room->netlink=NULL;
room->netlink_name[0]='\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';
if (room_first==NULL) room_first=room;
else room_last->next=room;
room_last=room;
return room;
}
/*** Construct link object ***/
NL_OBJECT create_netlink()
{
NL_OBJECT nl;
if ((nl=(NL_OBJECT)malloc(sizeof(struct netlink_struct)))==NULL) {
sprintf(text,"NETLINK: Memory allocation failure in create_netlink().\n");
write_syslog(text,1);
return NULL;
}
if (nl_first==NULL) {
nl_first=nl; nl->prev=NULL; nl->next=NULL;
}
else {
nl_last->next=nl; nl->next=NULL; nl->prev=nl_last;
}
nl_last=nl;
nl->service[0]='\0';
nl->site[0]='\0';
nl->verification[0]='\0';
nl->mail_to[0]='\0';
nl->mail_from[0]='\0';
nl->mailfile=NULL;
nl->buffer[0]='\0';
nl->ver_major=0;
nl->ver_minor=0;
nl->ver_patch=0;
nl->keepalive_cnt=0;
nl->last_recvd=0;
nl->port=0;
nl->socket=0;
nl->mesg_user=NULL;
nl->connect_room=NULL;
nl->type=UNCONNECTED;
nl->stage=DOWN;
nl->connected=0;
nl->lastcom=-1;
nl->allow=ALL;
nl->warned=0;
return nl;
}
/*** Destruct a netlink (usually a closed incoming one). ***/
destruct_netlink(nl)
NL_OBJECT nl;
{
if (nl!=nl_first) {
nl->prev->next=nl->next;
if (nl!=nl_last) nl->next->prev=nl->prev;
else { nl_last=nl->prev; nl_last->next=NULL; }
}
else {
nl_first=nl->next;
if (nl!=nl_last) nl_first->prev=NULL;
else nl_last=NULL;
}
free(nl);
}
/*** Destroy all clones belonging to given user ***/
destroy_user_clones(user)
UR_OBJECT user;
{
UR_OBJECT u;
for(u=user_first;u!=NULL;u=u->next) {
if (u->type==CLONE_TYPE && u->owner==user) {
sprintf(text,"The clone of %s shimmers and vanishes.\n",u->name);
write_room(u->room,text);
destruct_user(u);
}
}
}
/************ NUTS PROTOCOL AND LINK MANAGEMENT FUNCTIONS ************/
/* Please don't alter these functions. If you do you may introduce
incompatabilities which may prevent other systems connecting or cause
bugs on the remote site and yours. You may think it looks simple but
even the newline count is important in some places. */
/*** Accept incoming server connection ***/
accept_server_connection(sock,acc_addr)
int sock;
struct sockaddr_in acc_addr;
{
NL_OBJECT nl,nl2,create_netlink();
RM_OBJECT rm;
char site[81];
/* Send server type id and version number */
sprintf(text,"NUTS %s\n",VERSION);
write_sock(sock,text);
strcpy(site,get_ip_address(acc_addr));
sprintf(text,"NETLINK: Received request connection from site %s.\n",site);
write_syslog(text,1);
/* See if legal site, ie site is in config sites list. */
for(nl2=nl_first;nl2!=NULL;nl2=nl2->next)
if (!strcmp(nl2->site,site)) goto OK;
write_sock(sock,"DENIED CONNECT 1\n");
close(sock);
write_syslog("NETLINK: Request denied, remote site not in valid sites list.\n",1);
return;
/* Find free room link */
OK:
for(rm=room_first;rm!=NULL;rm=rm->next) {
if (rm->netlink==NULL && rm->inlink) {
if ((nl=create_netlink())==NULL) {
write_sock(sock,"DENIED CONNECT 2\n");
close(sock);
write_syslog("NETLINK: Request denied, unable to create netlink object.\n",1);
return;
}
rm->netlink=nl;
nl->socket=sock;
nl->type=INCOMING;
nl->stage=VERIFYING;
nl->connect_room=rm;
nl->allow=nl2->allow;
nl->last_recvd=time(0);
strcpy(nl->service,"<verifying>");
strcpy(nl->site,site);
write_sock(sock,"GRANTED CONNECT\n");
write_syslog("NETLINK: Request granted.\n",1);
return;
}
}
write_sock(sock,"DENIED CONNECT 3\n");
close(sock);
write_syslog("NETLINK: Request denied, no free room links.\n",1);
}
/*** Deal with netlink data on link nl ***/
exec_netcom(nl,inpstr)
NL_OBJECT nl;
char *inpstr;
{
int netcom_num,lev;
char w1[ARR_SIZE],w2[ARR_SIZE],w3[ARR_SIZE],*c,ctemp;
/* The most used commands have been truncated to save bandwidth, ie ACT is
short for action, EMSG is short for end message. Commands that don't get
used much ie VERIFICATION have been left long for readability. */
char *netcom[]={
"DISCONNECT","TRANS","REL","ACT","GRANTED",
"DENIED","MSG","EMSG","PRM","VERIFICATION",
"VERIFY","REMVD","ERROR","EXISTS?","EXISTS_NO",
"EXISTS_YES","MAIL","ENDMAIL","MAILERROR","KA",
"RSTAT","*"
};
/* The buffer is large (ARR_SIZE*2) but if a bug occurs with a remote system
and no newlines are sent for some reason it may overflow and this will
probably cause a crash. Oh well, such is life. */
if (nl->buffer[0]) {
strcat(nl->buffer,inpstr); inpstr=nl->buffer;
}
nl->last_recvd=time(0);
/* Go through data */
while(*inpstr) {
w1[0]='\0'; w2[0]='\0'; w3[0]='\0'; lev=0;
if (*inpstr!='\n') sscanf(inpstr,"%s %s %s %d",w1,w2,w3,&lev);
/* Find first newline */
c=inpstr; ctemp=1; /* hopefully we'll never get char 1 in the string */
while(*c) {
if (*c=='\n') { ctemp=*(c+1); *(c+1)='\0'; break; }
++c;
}
/* If no newline then input is incomplete, store and return */
if (ctemp==1) {
if (inpstr!=nl->buffer) strcpy(nl->buffer,inpstr);
return;
}
/* Get command number */
netcom_num=0;
while(netcom[netcom_num][0]!='*') {
if (!strcmp(netcom[netcom_num],w1)) break;
netcom_num++;
}
/* Deal with initial connects */
if (nl->stage==VERIFYING) {
if (nl->type==OUTGOING) {
if (strcmp(w1,"NUTS")) {
sprintf(text,"NETLINK: Incorrect connect message from %s.\n",nl->service);
write_syslog(text,1);
shutdown_netlink(nl);
return;
}
/* Store remote version for compat checks */
nl->stage=UP;
w2[10]='\0';
sscanf(w2,"%d.%d.%d",&nl->ver_major,&nl->ver_minor,&nl->ver_patch);
goto NEXT_LINE;
}
else {
/* Incoming */
if (netcom_num!=9) {
/* No verification, no connection */
sprintf(text,"NETLINK: No verification sent by site %s.\n",nl->site);
write_syslog(text,1);
shutdown_netlink(nl);
return;
}
nl->stage=UP;
}
}
/* If remote is currently sending a message relay it to user, don't
interpret it unless its EMSG or ERROR */
if (nl->mesg_user!=NULL && netcom_num!=7 && netcom_num!=12) {
/* If -1 then user logged off before end of mesg received */
if (nl->mesg_user!=(UR_OBJECT)-1) write_user(nl->mesg_user,inpstr);
goto NEXT_LINE;
}
/* Same goes for mail except its ENDMAIL or ERROR */
if (nl->mailfile!=NULL && netcom_num!=17) {
fputs(inpstr,nl->mailfile); goto NEXT_LINE;
}
nl->lastcom=netcom_num;
switch(netcom_num) {
case 0:
if (nl->stage==UP) {
sprintf(text,"~OLSYSTEM:~FY~RS Disconnecting from service %s in the %s.\n",nl->service,nl->connect_room->name);
write_room(NULL,text);
}
shutdown_netlink(nl);
break;
case 1: nl_transfer(nl,w2,w3,lev,inpstr); break;
case 2: nl_release(nl,w2); break;
case 3: nl_action(nl,w2,inpstr); break;
case 4: nl_granted(nl,w2); break;
case 5: nl_denied(nl,w2,inpstr); break;
case 6: nl_mesg(nl,w2); break;
case 7: nl->mesg_user=NULL; break;
case 8: nl_prompt(nl,w2); break;
case 9: nl_verification(nl,w2,w3,0); break;
case 10: nl_verification(nl,w2,w3,1); break;
case 11: nl_removed(nl,w2); break;
case 12: nl_error(nl); break;
case 13: nl_checkexist(nl,w2,w3); break;
case 14: nl_user_notexist(nl,w2,w3); break;
case 15: nl_user_exist(nl,w2,w3); break;
case 16: nl_mail(nl,w2,w3); break;
case 17: nl_endmail(nl); break;
case 18: nl_mailerror(nl,w2,w3); break;
case 19: /* Keepalive signal, do nothing */ break;
case 20: nl_rstat(nl,w2); break;
default:
sprintf(text,"NETLINK: Received unknown command '%s' from %s.\n",w1,nl->service);
write_syslog(text,1);
write_sock(nl->socket,"ERROR\n");
}
NEXT_LINE:
/* See if link has closed */
if (nl->type==UNCONNECTED) return;
*(c+1)=ctemp;
inpstr=c+1;
}
nl->buffer[0]='\0';
}
/*** Deal with user being transfered over from remote site ***/
nl_transfer(nl,name,pass,lev,inpstr)
NL_OBJECT nl;
char *name,*pass,*inpstr;
int lev;
{
UR_OBJECT u,create_user();
/* link for outgoing users only */
if (nl->allow==OUT) {
sprintf(text,"DENIED %s 4\n",name);
write_sock(nl->socket,text);
return;
}
if (strlen(name)>USER_NAME_LEN) name[USER_NAME_LEN]='\0';
/* See if user is banned */
if (user_banned(name)) {
if (nl->ver_major==3 && nl->ver_minor>=3 && nl->ver_patch>=3)
sprintf(text,"DENIED %s 9\n",name); /* new error for 3.3.3 */
else sprintf(text,"DENIED %s 6\n",name); /* old error to old versions */
write_sock(nl->socket,text);
return;
}
/* See if user already on here */
if (u=get_user(name)) {
sprintf(text,"DENIED %s 5\n",name);
write_sock(nl->socket,text);
return;
}
/* See if user of this name exists on this system by trying to load up
datafile */
if ((u=create_user())==NULL) {
sprintf(text,"DENIED %s 6\n",name);
write_sock(nl->socket,text);
return;
}
u->type=REMOTE_TYPE;
strcpy(u->name,name);
if (load_user_details(u)) {
if (strcmp(u->pass,pass)) {
/* Incorrect password sent */
sprintf(text,"DENIED %s 7\n",name);
write_sock(nl->socket,text);
destruct_user(u);
destructed=0;
return;
}
}
else {
/* Get the users description */
if (nl->ver_major<=3 && nl->ver_minor<=3 && nl->ver_patch<1)
strcpy(text,remove_first(remove_first(remove_first(inpstr))));
else strcpy(text,remove_first(remove_first(remove_first(remove_first(inpstr)))));
text[USER_DESC_LEN]='\0';
terminate(text);
strcpy(u->desc,text);
strcpy(u->in_phrase,"enters");
strcpy(u->out_phrase,"goes");
if (nl->ver_major==3 && nl->ver_minor>=3 && nl->ver_patch>=1) {
if (lev>rem_user_maxlevel) u->level=rem_user_maxlevel;
else u->level=lev;
}
else u->level=rem_user_deflevel;
}
/* See if users level is below minlogin level */
if (u->level<minlogin_level) {
if (nl->ver_major==3 && nl->ver_minor>=3 && nl->ver_patch>=3)
sprintf(text,"DENIED %s 8\n",u->name); /* new error for 3.3.3 */
else sprintf(text,"DENIED %s 6\n",u->name); /* old error to old versions */
write_sock(nl->socket,text);
destruct_user(u);
destructed=0;
return;
}
strcpy(u->site,nl->service);
sprintf(text,"%s enters from cyberspace.\n",u->name);
write_room(nl->connect_room,text);
sprintf(text,"NETLINK: Remote user %s received from %s.\n",u->name,nl->service);
write_syslog(text,1);
u->room=nl->connect_room;
u->netlink=nl;
u->read_mail=time(0);
u->last_login=time(0);
num_of_users++;
sprintf(text,"GRANTED %s\n",name);
write_sock(nl->socket,text);
}
/*** User is leaving this system ***/
nl_release(nl,name)
NL_OBJECT nl;
char *name;
{
UR_OBJECT u;
if ((u=get_user(name))!=NULL && u->type==REMOTE_TYPE) {
sprintf(text,"%s leaves this plain of existence.\n",u->name);
write_room_except(u->room,text,u);
sprintf(text,"NETLINK: Remote user %s released.\n",u->name);
write_syslog(text,1);
destroy_user_clones(u);
destruct_user(u);
num_of_users--;
return;
}
sprintf(text,"NETLINK: Release requested for unknown/invalid user %s from %s.\n",name,nl->service);
write_syslog(text,1);
}
/*** Remote user performs an action on this system ***/
nl_action(nl,name,inpstr)
NL_OBJECT nl;
char *name,*inpstr;
{
UR_OBJECT u;
char *c,ctemp;
if (!(u=get_user(name))) {
sprintf(text,"DENIED %s 8\n",name);
write_sock(nl->socket,text);
return;
}
if (u->socket!=-1) {
sprintf(text,"NETLINK: Action requested for local user %s from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
inpstr=remove_first(remove_first(inpstr));
/* remove newline character */
c=inpstr; ctemp='\0';
while(*c) {
if (*c=='\n') { ctemp=*c; *c='\0'; break; }
++c;
}
u->last_input=time(0);
if (u->misc_op) {
if (!strcmp(inpstr,"NL")) misc_ops(u,"\n");
else misc_ops(u,inpstr+4);
return;
}
if (u->afk) {
write_user(u,"You are no longer AFK.\n");
if (u->vis) {
sprintf(text,"%s comes back from being AFK.\n",u->name);
write_room_except(u->room,text,u);
}
u->afk=0;
}
word_count=wordfind(inpstr);
if (!strcmp(inpstr,"NL")) return;
exec_com(u,inpstr);
if (ctemp) *c=ctemp;
if (!u->misc_op) prompt(u);
}
/*** Grant received from remote system ***/
nl_granted(nl,name)
NL_OBJECT nl;
char *name;
{
UR_OBJECT u;
RM_OBJECT old_room;
if (!strcmp(name,"CONNECT")) {
sprintf(text,"NETLINK: Connection to %s granted.\n",nl->service);
write_syslog(text,1);
/* Send our verification and version number */
sprintf(text,"VERIFICATION %s %s\n",verification,VERSION);
write_sock(nl->socket,text);
return;
}
if (!(u=get_user(name))) {
sprintf(text,"NETLINK: Grant received for unknown user %s from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
/* This will probably occur if a user tried to go to the other site , got
lagged then changed his mind and went elsewhere. Don't worry about it. */
if (u->remote_com!=GO) {
sprintf(text,"NETLINK: Unexpected grant for %s received from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
/* User has been granted permission to move into remote talker */
write_user(u,"~FB~OLYou traverse cyberspace...\n");
if (u->vis) {
sprintf(text,"%s %s to the %s.\n",u->name,u->out_phrase,nl->service);
write_room_except(u->room,text,u);
}
else write_room_except(u->room,invisleave,u);
sprintf(text,"NETLINK: %s transfered to %s.\n",u->name,nl->service);
write_syslog(text,1);
old_room=u->room;
u->room=NULL; /* Means on remote talker */
u->netlink=nl;
u->pot_netlink=NULL;
u->remote_com=-1;
u->misc_op=0;
u->filepos=0;
u->page_file[0]='\0';
reset_access(old_room);
sprintf(text,"ACT %s look\n",u->name);
write_sock(nl->socket,text);
}
/*** Deny received from remote system ***/
nl_denied(nl,name,inpstr)
NL_OBJECT nl;
char *name,*inpstr;
{
UR_OBJECT u,create_user();
int errnum;
char *neterr[]={
"this site is not in the remote services valid sites list",
"the remote service is unable to create a link",
"the remote service has no free room links",
"the link is for incoming users only",
"a user with your name is already logged on the remote site",
"the remote service was unable to create a session for you",
"incorrect password. Use '.go <service> <remote password>'",
"your level there is below the remote services current minlogin level",
"you are banned from that service"
};
errnum=0;
sscanf(remove_first(remove_first(inpstr)),"%d",&errnum);
if (!strcmp(name,"CONNECT")) {
sprintf(text,"NETLINK: Connection to %s denied, %s.\n",nl->service,neterr[errnum-1]);
write_syslog(text,1);
/* If wiz initiated connect let them know its failed */
sprintf(text,"~OLSYSTEM:~RS Connection to %s failed, %s.\n",nl->service,neterr[errnum-1]);
write_level(com_level[CONN],1,text,NULL);
close(nl->socket);
nl->type=UNCONNECTED;
nl->stage=DOWN;
return;
}
/* Is for a user */
if (!(u=get_user(name))) {
sprintf(text,"NETLINK: Deny for unknown user %s received from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
sprintf(text,"NETLINK: Deny %d for user %s received from %s.\n",errnum,name,nl->service);
write_syslog(text,1);
sprintf(text,"Sorry, %s.\n",neterr[errnum-1]);
write_user(u,text);
prompt(u);
u->remote_com=-1;
u->pot_netlink=NULL;
}
/*** Text received to display to a user on here ***/
nl_mesg(nl,name)
NL_OBJECT nl;
char *name;
{
UR_OBJECT u;
if (!(u=get_user(name))) {
sprintf(text,"NETLINK: Message received for unknown user %s from %s.\n",name,nl->service);
write_syslog(text,1);
nl->mesg_user=(UR_OBJECT)-1;
return;
}
nl->mesg_user=u;
}
/*** Remote system asking for prompt to be displayed ***/
nl_prompt(nl,name)
NL_OBJECT nl;
char *name;
{
UR_OBJECT u;
if (!(u=get_user(name))) {
sprintf(text,"NETLINK: Prompt received for unknown user %s from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
if (u->type==REMOTE_TYPE) {
sprintf(text,"NETLINK: Prompt received for remote user %s from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
prompt(u);
}
/*** Verification received from remote site ***/
nl_verification(nl,w2,w3,com)
NL_OBJECT nl;
char *w2,*w3;
int com;
{
NL_OBJECT nl2;
if (!com) {
/* We're verifiying a remote site */
if (!w2[0]) {
shutdown_netlink(nl); return;
}
for(nl2=nl_first;nl2!=NULL;nl2=nl2->next) {
if (!strcmp(nl->site,nl2->site) && !strcmp(w2,nl2->verification)) {
switch(nl->allow) {
case IN : write_sock(nl->socket,"VERIFY OK IN\n"); break;
case OUT: write_sock(nl->socket,"VERIFY OK OUT\n"); break;
case ALL: write_sock(nl->socket,"VERIFY OK ALL\n");
}
strcpy(nl->service,nl2->service);
/* Only 3.2.0 and above send version number with verification */
sscanf(w3,"%d.%d.%d",&nl->ver_major,&nl->ver_minor,&nl->ver_patch);
sprintf(text,"NETLINK: Connected to %s in the %s.\n",nl->service,nl->connect_room->name);
write_syslog(text,1);
sprintf(text,"~OLSYSTEM:~RS New connection to service %s in the %s.\n",nl->service,nl->connect_room->name);
write_room(NULL,text);
return;
}
}
write_sock(nl->socket,"VERIFY BAD\n");
shutdown_netlink(nl);
return;
}
/* The remote site has verified us */
if (!strcmp(w2,"OK")) {
/* Set link permissions */
if (!strcmp(w3,"OUT")) {
if (nl->allow==OUT) {
sprintf(text,"NETLINK: WARNING - Permissions deadlock, both sides are outgoing only.\n");
write_syslog(text,1);
}
else nl->allow=IN;
}
else {
if (!strcmp(w3,"IN")) {
if (nl->allow==IN) {
sprintf(text,"NETLINK: WARNING - Permissions deadlock, both sides are incoming only.\n");
write_syslog(text,1);
}
else nl->allow=OUT;
}
}
sprintf(text,"NETLINK: Connection to %s verified.\n",nl->service);
write_syslog(text,1);
sprintf(text,"~OLSYSTEM:~RS New connection to service %s in the %s.\n",nl->service,nl->connect_room);
write_room(NULL,text);
return;
}
if (!strcmp(w2,"BAD")) {
sprintf(text,"NETLINK: Connection to %s has bad verification.\n",nl->service);
write_syslog(text,1);
/* Let wizes know its failed , may be wiz initiated */
sprintf(text,"~OLSYSTEM:~RS Connection to %s failed, bad verification.\n",nl->service);
write_level(com_level[CONN],1,text,NULL);
shutdown_netlink(nl);
return;
}
sprintf(text,"NETLINK: Unknown verify return code from %s.\n",nl->service);
write_syslog(text,1);
shutdown_netlink(nl);
}
/* Remote site only sends REMVD (removed) notification if user on remote site
tries to .go back to his home site or user is booted off. Home site doesn't
bother sending reply since remote site will remove user no matter what. */
nl_removed(nl,name)
NL_OBJECT nl;
char *name;
{
UR_OBJECT u;
if (!(u=get_user(name))) {
sprintf(text,"NETLINK: Removed notification for unknown user %s received from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
if (u->room!=NULL) {
sprintf(text,"NETLINK: Removed notification of local user %s received from %s.\n",name,nl->service);
write_syslog(text,1);
return;
}
sprintf(text,"NETLINK: %s returned from %s.\n",u->name,u->netlink->service);
write_syslog(text,1);
u->room=u->netlink->connect_room;
u->netlink=NULL;
if (u->vis) {
sprintf(text,"%s %s\n",u->name,u->in_phrase);
write_room_except(u->room,text,u);
}
else write_room_except(u->room,invisenter,u);
look(u);
prompt(u);
}
/*** Got an error back from site, deal with it ***/
nl_error(nl)
NL_OBJECT nl;
{
if (nl->mesg_user!=NULL) nl->mesg_user=NULL;
/* lastcom value may be misleading, the talker may have sent off a whole load
of commands before it gets a response due to lag, any one of them could
have caused the error */
sprintf(text,"NETLINK: Received ERROR from %s, lastcom = %d.\n",nl->service,nl->lastcom);
write_syslog(text,1);
}
/*** Does user exist? This is a question sent by a remote mailer to
verifiy mail id's. ***/
nl_checkexist(nl,to,from)
NL_OBJECT nl;
char *to,*from;
{
FILE *fp;
char filename[80];
sprintf(filename,"%s/%s.D",USERFILES,to);
if (!(fp=fopen(filename,"r"))) {
sprintf(text,"EXISTS_NO %s %s\n",to,from);
write_sock(nl->socket,text);
return;
}
fclose(fp);
sprintf(text,"EXISTS_YES %s %s\n",to,from);
write_sock(nl->socket,text);
}
/*** Remote user doesnt exist ***/
nl_user_notexist(nl,to,from)
NL_OBJECT nl;
char *to,*from;
{
UR_OBJECT user;
char filename[80];
char text2[ARR_SIZE];
if ((user=get_user(from))!=NULL) {
sprintf(text,"~OLSYSTEM:~RS User %s does not exist at %s, your mail bounced.\n",to,nl->service);
write_user(user,text);
}
else {
sprintf(text2,"There is no user named %s at %s, your mail bounced.\n",to,nl->service);
send_mail(NULL,from,text2);
}
sprintf(filename,"%s/OUT_%s_%s@%s",MAILSPOOL,from,to,nl->service);
unlink(filename);
}
/*** Remote users exists, send him some mail ***/
nl_user_exist(nl,to,from)
NL_OBJECT nl;
char *to,*from;
{
UR_OBJECT user;
FILE *fp;
char text2[ARR_SIZE],filename[80],line[82];
sprintf(filename,"%s/OUT_%s_%s@%s",MAILSPOOL,from,to,nl->service);
if (!(fp=fopen(filename,"r"))) {
if ((user=get_user(from))!=NULL) {
sprintf(text,"~OLSYSTEM:~RS An error occured during mail delivery to %s@%s.\n",to,nl->service);
write_user(user,text);
}
else {
sprintf(text2,"An error occured during mail delivery to %s@%s.\n",to,nl->service);
send_mail(NULL,from,text2);
}
return;
}
sprintf(text,"MAIL %s %s\n",to,from);
write_sock(nl->socket,text);
fgets(line,80,fp);
while(!feof(fp)) {
write_sock(nl->socket,line);
fgets(line,80,fp);
}
fclose(fp);
write_sock(nl->socket,"\nENDMAIL\n");
unlink(filename);
}
/*** Got some mail coming in ***/
nl_mail(nl,to,from)
NL_OBJECT nl;
char *to,*from;
{
char filename[80];
sprintf(text,"NETLINK: Mail received for %s from %s.\n",to,nl->service);
write_syslog(text,1);
sprintf(filename,"%s/IN_%s_%s@%s",MAILSPOOL,to,from,nl->service);
if (!(nl->mailfile=fopen(filename,"w"))) {
sprintf(text,"ERROR: Couldn't open file %s to write in nl_mail().\n",filename);
write_syslog(text,0);
sprintf(text,"MAILERROR %s %s\n",to,from);
write_sock(nl->socket,text);
return;
}
strcpy(nl->mail_to,to);
strcpy(nl->mail_from,from);
}
/*** End of mail message being sent from remote site ***/
nl_endmail(nl)
NL_OBJECT nl;
{
FILE *infp,*outfp;
char c,infile[80],mailfile[80],line[DNL+1];
fclose(nl->mailfile);
nl->mailfile=NULL;
sprintf(mailfile,"%s/IN_%s_%s@%s",MAILSPOOL,nl->mail_to,nl->mail_from,nl->service);
/* Copy to users mail file to a tempfile */
if (!(outfp=fopen("tempfile","w"))) {
write_syslog("ERROR: Couldn't open tempfile in netlink_endmail().\n",0);
sprintf(text,"MAILERROR %s %s\n",nl->mail_to,nl->mail_from);
write_sock(nl->socket,text);
goto END;
}
fprintf(outfp,"%d\r",(int)time(0));
/* Copy old mail file to tempfile */
sprintf(infile,"%s/%s.M",USERFILES,nl->mail_to);
if (!(infp=fopen(infile,"r"))) goto SKIP;
fgets(line,DNL,infp);
c=getc(infp);
while(!feof(infp)) { putc(c,outfp); c=getc(infp); }
fclose(infp);
/* Copy received file */
SKIP:
if (!(infp=fopen(mailfile,"r"))) {
sprintf(text,"ERROR: Couldn't open file %s to read in netlink_endmail().\n",mailfile);
write_syslog(text,0);
sprintf(text,"MAILERROR %s %s\n",nl->mail_to,nl->mail_from);
write_sock(nl->socket,text);
goto END;
}
fprintf(outfp,"~OLFrom: %s@%s %s",nl->mail_from,nl->service,long_date(0));
c=getc(infp);
while(!feof(infp)) { putc(c,outfp); c=getc(infp); }
fclose(infp);
fclose(outfp);
rename("tempfile",infile);
write_user(get_user(nl->mail_to),"\07~FT~OL~LI** YOU HAVE NEW MAIL **\n");
END:
nl->mail_to[0]='\0';
nl->mail_from[0]='\0';
unlink(mailfile);
}
/*** An error occured at remote site ***/
nl_mailerror(nl,to,from)
NL_OBJECT nl;
char *to,*from;
{
UR_OBJECT user;
if ((user=get_user(from))!=NULL) {
sprintf(text,"~OLSYSTEM:~RS An error occured during mail delivery to %s@%s.\n",to,nl->service);
write_user(user,text);
}
else {
sprintf(text,"An error occured during mail delivery to %s@%s.\n",to,nl->service);
send_mail(NULL,from,text);
}
}
/*** Send statistics of this server to requesting user on remote site ***/
nl_rstat(nl,to)
NL_OBJECT nl;
char *to;
{
char str[80];
gethostname(str,80);
if (nl->ver_major<=3 && nl->ver_minor<2)
sprintf(text,"MSG %s\n\n*** Remote statistics ***\n\n",to);
else sprintf(text,"MSG %s\n\n~BB*** Remote statistics ***\n\n",to);
write_sock(nl->socket,text);
sprintf(text,"NUTS version : %s\nHost : %s\n",VERSION,str);
write_sock(nl->socket,text);
sprintf(text,"Ports (Main/Wiz/Link): %d ,%d, %d\n",port[0],port[1],port[2]);
write_sock(nl->socket,text);
sprintf(text,"Number of users : %d\nRemote user maxlevel : %s\n",num_of_users,level_name[rem_user_maxlevel]);
write_sock(nl->socket,text);
sprintf(text,"Remote user deflevel : %s\n\nEMSG\nPRM %s\n",level_name[rem_user_deflevel],to);
write_sock(nl->socket,text);
}
/*** Shutdown the netlink and pull any remote users back home ***/
shutdown_netlink(nl)
NL_OBJECT nl;
{
UR_OBJECT u;
char mailfile[80];
if (nl->type==UNCONNECTED) return;
/* See if any mail halfway through being sent */
if (nl->mail_to[0]) {
sprintf(text,"MAILERROR %s %s\n",nl->mail_to,nl->mail_from);
write_sock(nl->socket,text);
fclose(nl->mailfile);
sprintf(mailfile,"%s/IN_%s_%s@%s",MAILSPOOL,nl->mail_to,nl->mail_from,nl->service);
unlink(mailfile);
nl->mail_to[0]='\0';
nl->mail_from[0]='\0';
}
write_sock(nl->socket,"DISCONNECT\n");
close(nl->socket);
for(u=user_first;u!=NULL;u=u->next) {
if (u->pot_netlink==nl) { u->remote_com=-1; continue; }
if (u->netlink==nl) {
if (u->room==NULL) {
write_user(u,"~FB~OLYou feel yourself dragged back across the ether...\n");
u->room=u->netlink->connect_room;
u->netlink=NULL;
if (u->vis) {
sprintf(text,"%s %s\n",u->name,u->in_phrase);
write_room_except(u->room,text,u);
}
else write_room_except(u->room,invisenter,u);
look(u); prompt(u);
sprintf(text,"NETLINK: %s recovered from %s.\n",u->name,nl->service);
write_syslog(text,1);
continue;
}
if (u->type==REMOTE_TYPE) {
sprintf(text,"%s vanishes!\n",u->name);
write_room(u->room,text);
destruct_user(u);
num_of_users--;
}
}
}
if (nl->stage==UP)
sprintf(text,"NETLINK: Disconnected from %s.\n",nl->service);
else sprintf(text,"NETLINK: Disconnected from site %s.\n",nl->site);
write_syslog(text,1);
if (nl->type==INCOMING) {
nl->connect_room->netlink=NULL;
destruct_netlink(nl);
return;
}
nl->type=UNCONNECTED;
nl->stage=DOWN;
nl->warned=0;
}
/*************** START OF COMMAND FUNCTIONS AND THEIR SUBSIDS **************/
/*** Deal with user input ***/
exec_com(user,inpstr)
UR_OBJECT user;
char *inpstr;
{
int i,len;
char filename[80],*comword=NULL;
com_num=-1;
if (word[0][0]=='.') comword=(word[0]+1);
else comword=word[0];
if (!comword[0]) {
write_user(user,"Unknown command.\n"); return;
}
/* get com_num */
if (!strcmp(word[0],">")) strcpy(word[0],"tell");
if (!strcmp(word[0],"<")) strcpy(word[0],"pemote");
if (!strcmp(word[0],"-")) strcpy(word[0],"echo");
if (!strcmp(word[0],"!")) strcpy(word[0],"shout");
if (inpstr[0]==';') strcpy(word[0],"emote");
else if (inpstr[0]=='#') strcpy(word[0],"semote");
else inpstr=remove_first(inpstr);
i=0;
len=strlen(comword);
while(command[i][0]!='*') {
if (!strncmp(command[i],comword,len)) { com_num=i; break; }
++i;
}
if (user->room!=NULL && (com_num==-1 || com_level[com_num] > user->level)) {
write_user(user,"Unknown command.\n"); return;
}
/* See if user has gone across a netlink and if so then intercept certain
commands to be run on home site */
if (user->room==NULL) {
switch(com_num) {
case HOME:
case QUIT:
case MODE:
case PROMPT:
case COLOUR:
case REBOOT:
case SUICIDE:
case SHUTDOWN:
case CHARECHO:
write_user(user,"~FY~OL*** Home execution ***\n"); break;
default:
sprintf(text,"ACT %s %s %s\n",user->name,word[0],inpstr);
write_sock(user->netlink->socket,text);
no_prompt=1;
return;
}
}
/* Dont want certain commands executed by remote users */
if (user->type==REMOTE_TYPE) {
switch(com_num) {
case PASSWD :
case ENTPRO :
case ACCREQ :
case CONN :
case DISCONN:
write_user(user,"Sorry, remote users cannot use that command.\n");
return;
}
}
/* Main switch */
switch(com_num) {
case QUIT: disconnect_user(user); break;
case LOOK: look(user); break;
case MODE: toggle_mode(user); break;
case SAY :
if (word_count<2) {
write_user(user,"Say what?\n"); return;
}
say(user,inpstr);
break;
case SHOUT : shout(user,inpstr); break;
case TELL : tell(user,inpstr); break;
case EMOTE : emote(user,inpstr); break;
case SEMOTE: semote(user,inpstr); break;
case PEMOTE: pemote(user,inpstr); break;
case ECHO : echo(user,inpstr); break;
case GO : go(user); break;
case IGNALL: toggle_ignall(user); break;
case PROMPT: toggle_prompt(user); break;
case DESC : set_desc(user,inpstr); break;
case INPHRASE :
case OUTPHRASE:
set_iophrase(user,inpstr); break;
case PUBCOM :
case PRIVCOM: set_room_access(user); break;
case LETMEIN: letmein(user); break;
case INVITE : invite(user); break;
case TOPIC : set_topic(user,inpstr); break;
case MOVE : move(user); break;
case BCAST : bcast(user,inpstr); break;
case WHO : who(user,0); break;
case PEOPLE : who(user,1); break;
case HELP : help(user); break;
case SHUTDOWN: shutdown_com(user); break;
case NEWS:
sprintf(filename,"%s/%s",DATAFILES,NEWSFILE);
switch(more(user,user->socket,filename)) {
case 0: write_user(user,"There is no news.\n"); break;
case 1: user->misc_op=2;
}
break;
case READ : read_board(user); break;
case WRITE : write_board(user,inpstr,0); break;
case WIPE : wipe_board(user); break;
case SEARCH: search_boards(user); break;
case REVIEW: review(user); break;
case HOME : home(user); break;
case STATUS: status(user); break;
case VER:
sprintf(text,"NUTS version %s\n",VERSION);
write_user(user,text); break;
case RMAIL : rmail(user); break;
case SMAIL : smail(user,inpstr,0); break;
case DMAIL : dmail(user); break;
case FROM : mail_from(user); break;
case ENTPRO : enter_profile(user,0); break;
case EXAMINE : examine(user); break;
case RMST : rooms(user,1); break;
case RMSN : rooms(user,0); break;
case NETSTAT : netstat(user); break;
case NETDATA : netdata(user); break;
case CONN : connect_netlink(user); break;
case DISCONN : disconnect_netlink(user); break;
case PASSWD : change_pass(user); break;
case KILL : kill_user(user); break;
case PROMOTE : promote(user); break;
case DEMOTE : demote(user); break;
case LISTBANS: listbans(user); break;
case BAN : ban(user); break;
case UNBAN : unban(user); break;
case VIS : visibility(user,1); break;
case INVIS : visibility(user,0); break;
case SITE : site(user); break;
case WAKE : wake(user); break;
case WIZSHOUT: wizshout(user,inpstr); break;
case MUZZLE : muzzle(user); break;
case UNMUZZLE: unmuzzle(user); break;
case MAP:
sprintf(filename,"%s/%s",DATAFILES,MAPFILE);
switch(more(user,user->socket,filename)) {
case 0: write_user(user,"There is no map.\n"); break;
case 1: user->misc_op=2;
}
break;
case LOGGING : logging(user); break;
case MINLOGIN : minlogin(user); break;
case SYSTEM : system_details(user); break;
case CHARECHO : toggle_charecho(user); break;
case CLEARLINE: clearline(user); break;
case FIX : change_room_fix(user,1); break;
case UNFIX : change_room_fix(user,0); break;
case VIEWLOG : viewlog(user); break;
case ACCREQ : account_request(user,inpstr); break;
case REVCLR : revclr(user); break;
case CREATE : create_clone(user); break;
case DESTROY : destroy_clone(user); break;
case MYCLONES : myclones(user); break;
case ALLCLONES: allclones(user); break;
case SWITCH: clone_switch(user); break;
case CSAY : clone_say(user,inpstr); break;
case CHEAR : clone_hear(user); break;
case RSTAT : remote_stat(user); break;
case SWBAN : swban(user); break;
case AFK : afk(user,inpstr); break;
case CLS : cls(user); break;
case COLOUR : toggle_colour(user); break;
case IGNSHOUT: toggle_ignshout(user); break;
case IGNTELL : toggle_igntell(user); break;
case SUICIDE : suicide(user); break;
case DELETE : delete_user(user,0); break;
case REBOOT : reboot_com(user); break;
case RECOUNT : check_messages(user,2); break;
case REVTELL : revtell(user); break;
default: write_user(user,"Command not executed in exec_com().\n");
}
}
/*** Display details of room ***/
look(user)
UR_OBJECT user;
{
RM_OBJECT rm;
UR_OBJECT u;
char temp[81],null[1],*ptr;
char *afk="~BR(AFK)";
int i,exits,users;
rm=user->room;
if (rm->access & PRIVATE) sprintf(text,"\n~FTRoom: ~FR%s\n\n",rm->name);
else sprintf(text,"\n~FTRoom: ~FG%s\n\n",rm->name);
write_user(user,text);
write_user(user,user->room->desc);
exits=0; null[0]='\0';
strcpy(text,"\n~FTExits are:");
for(i=0;i<MAX_LINKS;++i) {
if (rm->link[i]==NULL) break;
if (rm->link[i]->access & PRIVATE)
sprintf(temp," ~FR%s",rm->link[i]->name);
else sprintf(temp," ~FG%s",rm->link[i]->name);Showing the first 100 KB of 205.3 KB. Download the archive for the full file.