Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

chat.cpp 51 KB
Newer Older
1
/* Synchronet real-time chat functions */
2
// vi: tabstop=4
3 4 5 6 7 8 9

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

#define PCHAT_LEN 1000		/* Size of Private chat file */

const char *weekday[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"
				,"Saturday"};
const char *month[]={"January","February","March","April","May","June"
				,"July","August","September","October","November","December"};

46 47
/****************************************************************************/
/****************************************************************************/
rswindell's avatar
rswindell committed
48
void sbbs_t::multinodechat(int channel)
49
{
rswindell's avatar
rswindell committed
50 51
	char	line[256],str[256],ch,done
			,usrs,preusrs,qusrs,*gurubuf=NULL,savch,*p
52 53
			,pgraph[400],buf[400]
			,usr[MAX_NODES],preusr[MAX_NODES],qusr[MAX_NODES];
54
	char	guru_lastanswer[512];
55
	char 	tmp[512];
56 57 58 59
	int 	file;
	long	i,j,k,n;
	node_t 	node;

60 61
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
62
		return;
63 64
	}

rswindell's avatar
rswindell committed
65 66 67 68 69
	if(channel<1 || channel>cfg.total_chans)
		channel=1;

	if(!chan_access(channel-1))
		return;
70
	if(useron.misc&(RIP|WIP|HTML) ||!(useron.misc&EXPERT))
rswindell's avatar
rswindell committed
71 72
		menu("multchat");
	bputs(text[WelcomeToMultiChat]);
73
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
74
		thisnode.aux=channel;
75 76
		putnodedat(cfg.node_num,&thisnode);
	}
rswindell's avatar
rswindell committed
77 78
	bprintf(text[WelcomeToChannelN],channel,cfg.chan[channel-1]->name);
	if(gurubuf) {
deuce's avatar
deuce committed
79
		free(gurubuf);
80
		gurubuf=NULL;
81
	}
rswindell's avatar
rswindell committed
82
	if(cfg.chan[channel-1]->misc&CHAN_GURU && cfg.chan[channel-1]->guru<cfg.total_gurus
rswindell's avatar
rswindell committed
83
		&& chk_ar(cfg.guru[cfg.chan[channel-1]->guru]->ar,&useron,&client)) {
rswindell's avatar
rswindell committed
84 85 86
		sprintf(str,"%s%s.dat",cfg.ctrl_dir,cfg.guru[cfg.chan[channel-1]->guru]->code);
		if((file=nopen(str,O_RDONLY))==-1) {
			errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
87
			return;
88
		}
89
		if((gurubuf=(char *)malloc((size_t)filelength(file)+1))==NULL) {
rswindell's avatar
rswindell committed
90
			close(file);
91
			errormsg(WHERE,ERR_ALLOC,str,(size_t)filelength(file)+1);
92
			return;
93
		}
94
		read(file,gurubuf,(size_t)filelength(file));
rswindell's avatar
rswindell committed
95
		gurubuf[filelength(file)]=0;
96
		close(file);
97
	}
rswindell's avatar
rswindell committed
98 99 100 101 102 103 104 105 106 107
	usrs=0;
	for(i=1;i<=cfg.sys_nodes && i<=cfg.sys_lastnode;i++) {
		if(i==cfg.node_num)
			continue;
		getnodedat(i,&node,0);
		if(node.action!=NODE_MCHT || node.status!=NODE_INUSE)
			continue;
		if(node.aux && (node.aux&0xff)!=channel)
			continue;
		printnodedat(i,&node);
deuce's avatar
deuce committed
108
		preusr[usrs]=(char)i;
109
		usr[usrs++]=(char)i;
110
	}
rswindell's avatar
rswindell committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	preusrs=usrs;
	if(gurubuf)
		bprintf(text[NodeInMultiChatLocally]
			,cfg.sys_nodes+1,cfg.guru[cfg.chan[channel-1]->guru]->name,channel);
	bputs(text[YoureOnTheAir]);
	done=0;
	while(online && !done) {
		checkline();
		gettimeleft();
		action=NODE_MCHT;
		qusrs=usrs=0;
        for(i=1;i<=cfg.sys_nodes;i++) {
			if(i==cfg.node_num)
				continue;
			getnodedat(i,&node,0);
			if(node.action!=NODE_MCHT
				|| (node.aux && channel && (node.aux&0xff)!=channel))
				continue;
			if(node.status==NODE_QUIET)
				qusr[qusrs++]=(char)i;
			else if(node.status==NODE_INUSE)
132
				usr[usrs++]=(char)i;
133
		}
rswindell's avatar
rswindell committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		if(preusrs>usrs) {
			if(!usrs && channel && cfg.chan[channel-1]->misc&CHAN_GURU
				&& cfg.chan[channel-1]->guru<cfg.total_gurus)
				bprintf(text[NodeJoinedMultiChat]
					,cfg.sys_nodes+1,cfg.guru[cfg.chan[channel-1]->guru]->name
					,channel);
			outchar(BEL);
			for(i=0;i<preusrs;i++) {
				for(j=0;j<usrs;j++)
					if(preusr[i]==usr[j])
						break;
				if(j==usrs) {
					getnodedat(preusr[i],&node,0);
					if(node.misc&NODE_ANON)
						sprintf(str,"%.80s",text[UNKNOWN_USER]);
					else
						username(&cfg,node.useron,str);
					bprintf(text[NodeLeftMultiChat]
152 153 154
						,preusr[i],str,channel);
				}
			}
155
		}
rswindell's avatar
rswindell committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		else if(preusrs<usrs) {
			if(!preusrs && channel && cfg.chan[channel-1]->misc&CHAN_GURU
				&& cfg.chan[channel-1]->guru<cfg.total_gurus)
				bprintf(text[NodeLeftMultiChat]
					,cfg.sys_nodes+1,cfg.guru[cfg.chan[channel-1]->guru]->name
					,channel);
			outchar(BEL);
			for(i=0;i<usrs;i++) {
				for(j=0;j<preusrs;j++)
					if(usr[i]==preusr[j])
						break;
				if(j==preusrs) {
					getnodedat(usr[i],&node,0);
					if(node.misc&NODE_ANON)
						sprintf(str,"%.80s",text[UNKNOWN_USER]);
					else
						username(&cfg,node.useron,str);
					bprintf(text[NodeJoinedMultiChat]
174 175 176
						,usr[i],str,channel);
				}
			}
177
		}
rswindell's avatar
rswindell committed
178 179 180 181 182 183
		preusrs=usrs;
		for(i=0;i<usrs;i++)
			preusr[i]=usr[i];
		attr(cfg.color[clr_multichat]);
		SYNC;
		sys_status&=~SS_ABORT;
184
		if((ch=inkey(K_NONE,250))!=0 || wordwrap[0]) {
rswindell's avatar
rswindell committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
			if(ch=='/') {
				bputs(text[MultiChatCommandPrompt]);
				strcpy(str,"ACELWQ?*");
				if(SYSOP)
					strcat(str,"0");
				i=getkeys(str,cfg.total_chans);
				if(i&0x80000000L) {  /* change channel */
					savch=(char)(i&~0x80000000L);
					if(savch==channel)
						continue;
					if(!chan_access(savch-1))
						continue;
					bprintf(text[WelcomeToChannelN]
						,savch,cfg.chan[savch-1]->name);

					usrs=0;
					for(i=1;i<=cfg.sys_nodes;i++) {
						if(i==cfg.node_num)
							continue;
						getnodedat(i,&node,0);
						if(node.action!=NODE_MCHT
							|| node.status!=NODE_INUSE)
							continue;
						if(node.aux && (node.aux&0xff)!=savch)
							continue;
						printnodedat(i,&node);
						if(node.aux&0x1f00) {	/* password */
							bprintf(text[PasswordProtected]
								,node.misc&NODE_ANON
								? text[UNKNOWN_USER]
								: username(&cfg,node.useron,tmp));
							if(!getstr(str,8,K_UPPER|K_ALPHA|K_LINE))
								break;
							if(strcmp(str,unpackchatpass(tmp,&node)))
								break;
220 221
							bputs(text[CorrectPassword]);
						}
deuce's avatar
deuce committed
222
						preusr[usrs]=(char)i;
223
						usr[usrs++]=(char)i;
224
					}
rswindell's avatar
rswindell committed
225 226
					if(i<=cfg.sys_nodes) {	/* failed password */
						bputs(text[WrongPassword]);
227
						continue;
228
					}
rswindell's avatar
rswindell committed
229
					if(gurubuf) {
deuce's avatar
deuce committed
230
						free(gurubuf);
231
						gurubuf=NULL;
232
					}
rswindell's avatar
rswindell committed
233 234
					if(cfg.chan[savch-1]->misc&CHAN_GURU
						&& cfg.chan[savch-1]->guru<cfg.total_gurus
rswindell's avatar
rswindell committed
235
						&& chk_ar(cfg.guru[cfg.chan[savch-1]->guru]->ar,&useron,&client
rswindell's avatar
rswindell committed
236 237 238 239 240
						)) {
						sprintf(str,"%s%s.dat",cfg.ctrl_dir
							,cfg.guru[cfg.chan[savch-1]->guru]->code);
						if((file=nopen(str,O_RDONLY))==-1) {
							errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
241
							break;
242
						}
243
						if((gurubuf=(char *)malloc((size_t)filelength(file)+1))==NULL) {
rswindell's avatar
rswindell committed
244 245
							close(file);
							errormsg(WHERE,ERR_ALLOC,str
246
								,(size_t)filelength(file)+1);
247
							break;
248
						}
249
						read(file,gurubuf,(size_t)filelength(file));
rswindell's avatar
rswindell committed
250
						gurubuf[filelength(file)]=0;
251
						close(file);
252
					}
rswindell's avatar
rswindell committed
253 254 255 256 257 258 259 260 261 262 263
					preusrs=usrs;
					if(gurubuf)
						bprintf(text[NodeInMultiChatLocally]
							,cfg.sys_nodes+1
							,cfg.guru[cfg.chan[savch-1]->guru]->name
							,savch);
					channel=savch;
					if(!usrs && cfg.chan[savch-1]->misc&CHAN_PW
						&& !noyes(text[PasswordProtectChanQ])) {
						bputs(text[PasswordPrompt]);
						if(getstr(str,8,K_UPPER|K_ALPHA|K_LINE)) {
264
							getnodedat(cfg.node_num,&thisnode,true);
rswindell's avatar
rswindell committed
265
							thisnode.aux=channel;
266
							packchatpass(str,&thisnode);
267
						}
rswindell's avatar
rswindell committed
268
						else {
269
							getnodedat(cfg.node_num,&thisnode,true);
270 271
							thisnode.aux=channel;
						}
272
					}
rswindell's avatar
rswindell committed
273
					else {
274
						getnodedat(cfg.node_num,&thisnode,true);
275
						thisnode.aux=channel;
276
					}
rswindell's avatar
rswindell committed
277 278 279 280
					putnodedat(cfg.node_num,&thisnode);
					bputs(text[YoureOnTheAir]);
					if(cfg.chan[channel-1]->cost
						&& !(useron.exempt&FLAG('J')))
281
						subtract_cdt(&cfg,&useron,cfg.chan[channel-1]->cost);
282
				}
rswindell's avatar
rswindell committed
283 284 285 286 287 288 289 290 291 292 293 294 295
				else switch(i) {	/* other command */
					case '0':	/* Global channel */
						if(!SYSOP)
							break;
                        usrs=0;
						for(i=1;i<=cfg.sys_nodes;i++) {
							if(i==cfg.node_num)
								continue;
							getnodedat(i,&node,0);
							if(node.action!=NODE_MCHT
								|| node.status!=NODE_INUSE)
								continue;
							printnodedat(i,&node);
deuce's avatar
deuce committed
296
							preusr[usrs]=(char)i;
297
							usr[usrs++]=(char)i;
298
						}
rswindell's avatar
rswindell committed
299
						preusrs=usrs;
300 301 302 303
						if(getnodedat(cfg.node_num,&thisnode,true)==0) {
							thisnode.aux=channel=0;
							putnodedat(cfg.node_num,&thisnode);
						}
rswindell's avatar
rswindell committed
304 305 306 307 308 309 310 311 312 313 314
						break;
					case 'A':   /* Action commands */
						useron.chat^=CHAT_ACTION;
						bprintf("\r\nAction commands are now %s\r\n"
							,useron.chat&CHAT_ACTION
							? text[ON]:text[OFF]);
						putuserrec(&cfg,useron.number,U_CHAT,8
							,ultoa(useron.chat,str,16));
						break;
					case 'C':   /* List of action commands */
						CRLF;
315
						for(i=0;channel && i<cfg.total_chatacts;i++) {
rswindell's avatar
rswindell committed
316 317 318 319 320 321
							if(cfg.chatact[i]->actset
								!=cfg.chan[channel-1]->actset)
								continue;
							bprintf("%-*.*s",LEN_CHATACTCMD
								,LEN_CHATACTCMD,cfg.chatact[i]->cmd);
							if(!((i+1)%8)) {
322
								CRLF;
323
							}
rswindell's avatar
rswindell committed
324
							else
325
								bputs(" ");
326
						}
rswindell's avatar
rswindell committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340
						CRLF;
						break;
					case 'E':   /* Toggle echo */
						useron.chat^=CHAT_ECHO;
						bprintf(text[EchoIsNow]
							,useron.chat&CHAT_ECHO
							? text[ON]:text[OFF]);
						putuserrec(&cfg,useron.number,U_CHAT,8
							,ultoa(useron.chat,str,16));
						break;
					case 'L':	/* list nodes */
						CRLF;
						for(i=1;i<=cfg.sys_nodes && i<=cfg.sys_lastnode;i++) {
							getnodedat(i,&node,0);
341
							printnodedat(i,&node);
342
						}
rswindell's avatar
rswindell committed
343 344 345 346 347 348 349 350 351 352 353
						CRLF;
						break;
					case 'W':   /* page node(s) */
						j=getnodetopage(0,0);
						if(!j)
							break;
						for(i=0;i<usrs;i++)
							if(usr[i]==j)
								break;
						if(i>=usrs) {
							bputs(text[UserNotFound]);
354 355
							break; 
						}
rswindell's avatar
rswindell committed
356 357 358 359 360 361 362 363 364 365 366 367 368

						bputs(text[NodeMsgPrompt]);
						if(!getstr(line,66,K_LINE|K_MSG))
							break;

						sprintf(buf,text[ChatLineFmt]
							,thisnode.misc&NODE_ANON
							? text[AnonUserChatHandle]
							: useron.handle
							,cfg.node_num,'*',line);
						strcat(buf,crlf);
						if(useron.chat&CHAT_ECHO)
							bputs(buf);
369
						putnmsg(&cfg,j,buf);
rswindell's avatar
rswindell committed
370 371 372 373 374
						break;
					case 'Q':	/* quit */
						done=1;
						break;
					case '*':
375
						if(menu_exists("chan"))
rswindell's avatar
rswindell committed
376 377 378 379 380 381
							menu("chan");
						else {
							bputs(text[ChatChanLstHdr]);
							bputs(text[ChatChanLstTitles]);
							if(cfg.total_chans>=10) {
								bputs("     ");
382
								bputs(text[ChatChanLstTitles]);
383
							}
rswindell's avatar
rswindell committed
384 385 386 387
							CRLF;
							bputs(text[ChatChanLstUnderline]);
							if(cfg.total_chans>=10) {
								bputs("     ");
388
								bputs(text[ChatChanLstUnderline]);
389
							}
rswindell's avatar
rswindell committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
							CRLF;
							if(cfg.total_chans>=10)
								j=(cfg.total_chans/2)+(cfg.total_chans&1);
							else
								j=cfg.total_chans;
							for(i=0;i<j && !msgabort();i++) {
								bprintf(text[ChatChanLstFmt],i+1
									,cfg.chan[i]->name
									,cfg.chan[i]->cost);
								if(cfg.total_chans>=10) {
									k=(cfg.total_chans/2)
										+i+(cfg.total_chans&1);
									if(k<cfg.total_chans) {
										bputs("     ");
										bprintf(text[ChatChanLstFmt]
											,k+1
											,cfg.chan[k]->name
407 408
											,cfg.chan[k]->cost);
									}
409
								}
410
								CRLF;
411
							}
412
							CRLF;
413
						}
rswindell's avatar
rswindell committed
414 415 416
						break;
					case '?':	/* menu */
						menu("multchat");
417 418
						break;
				}
419
			} else {
rswindell's avatar
rswindell committed
420 421 422 423 424 425 426 427 428 429 430 431
				ungetkey(ch);
				j=0;
				pgraph[0]=0;
				while(j<5) {
					if(!getstr(line,66,K_WRAP|K_MSG|K_CHAT))
						break;
					if(j) {
						sprintf(str,text[ChatLineFmt]
							,thisnode.misc&NODE_ANON
							? text[AnonUserChatHandle]
							: useron.handle
							,cfg.node_num,':',nulstr);
deuce's avatar
deuce committed
432
						sprintf(tmp,"%*s",(int)bstrlen(str),nulstr);
433
						strcat(pgraph,tmp);
434
					}
rswindell's avatar
rswindell committed
435 436 437 438
					strcat(pgraph,line);
					strcat(pgraph,crlf);
					if(!wordwrap[0])
						break;
439
					j++;
440
				}
rswindell's avatar
rswindell committed
441
				if(pgraph[0]) {
442
					if(channel && useron.chat&CHAT_ACTION) {
rswindell's avatar
rswindell committed
443 444 445 446 447 448 449 450 451 452 453
						for(i=0;i<cfg.total_chatacts;i++) {
							if(cfg.chatact[i]->actset
								!=cfg.chan[channel-1]->actset)
								continue;
							sprintf(str,"%s ",cfg.chatact[i]->cmd);
							if(!strnicmp(str,pgraph,strlen(str)))
								break;
							sprintf(str,"%.*s"
								,LEN_CHATACTCMD+2,pgraph);
							str[strlen(str)-2]=0;
							if(!stricmp(cfg.chatact[i]->cmd,str))
454
								break;
455
						}
rswindell's avatar
rswindell committed
456 457 458 459 460 461 462 463 464 465 466

						if(i<cfg.total_chatacts) {
							p=pgraph+strlen(str);
							n=atoi(p);
							for(j=0;j<usrs;j++) {
								getnodedat(usr[j],&node,0);
								if(usrs==1) /* no need to search */
									break;
								if(n) {
									if(usr[j]==n)
										break;
467
									continue;
468
								}
rswindell's avatar
rswindell committed
469 470 471 472 473 474
								username(&cfg,node.useron,str);
								if(!strnicmp(str,p,strlen(str)))
									break;
								getuserrec(&cfg,node.useron,U_HANDLE
									,LEN_HANDLE,str);
								if(!strnicmp(str,p,strlen(str)))
475
									break;
476
							}
rswindell's avatar
rswindell committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
							if(!usrs
								&& cfg.chan[channel-1]->guru<cfg.total_gurus)
								strcpy(str
								,cfg.guru[cfg.chan[channel-1]->guru]->name);
							else if(j>=usrs)
								strcpy(str,"everyone");
							else if(node.misc&NODE_ANON)
								strcpy(str,text[UNKNOWN_USER]);
							else
								username(&cfg,node.useron,str);

							/* Display on same node */
							bprintf(cfg.chatact[i]->out
								,thisnode.misc&NODE_ANON
								? text[UNKNOWN_USER] : useron.alias
								,str);
							CRLF;

							if(usrs && j<usrs) {
								/* Display to dest user */
								sprintf(buf,cfg.chatact[i]->out
									,thisnode.misc&NODE_ANON
									? text[UNKNOWN_USER] : useron.alias
									,"you");
								strcat(buf,crlf);
502
								putnmsg(&cfg,usr[j],buf);
503
							}
rswindell's avatar
rswindell committed
504 505 506 507 508 509 510 511 512 513 514 515 516


							/* Display to all other users */
							sprintf(buf,cfg.chatact[i]->out
								,thisnode.misc&NODE_ANON
								? text[UNKNOWN_USER] : useron.alias
								,str);
							strcat(buf,crlf);

							for(i=0;i<usrs;i++) {
								if(i==j)
									continue;
								getnodedat(usr[i],&node,0);
517
								putnmsg(&cfg,usr[i],buf);
518
							}
rswindell's avatar
rswindell committed
519 520
							for(i=0;i<qusrs;i++) {
								getnodedat(qusr[i],&node,0);
521
								putnmsg(&cfg,qusr[i],buf);
522
							}
523 524
							continue;
						}
525
					}
rswindell's avatar
rswindell committed
526 527 528 529 530 531 532 533 534 535

					sprintf(buf,text[ChatLineFmt]
						,thisnode.misc&NODE_ANON
						? text[AnonUserChatHandle]
						: useron.handle
						,cfg.node_num,':',pgraph);
					if(useron.chat&CHAT_ECHO)
						bputs(buf);
					for(i=0;i<usrs;i++) {
						getnodedat(usr[i],&node,0);
536
						putnmsg(&cfg,usr[i],buf);
537
					}
rswindell's avatar
rswindell committed
538 539
					for(i=0;i<qusrs;i++) {
						getnodedat(qusr[i],&node,0);
540
						putnmsg(&cfg,qusr[i],buf);
541
					}
rswindell's avatar
rswindell committed
542 543
					if(!usrs && channel && gurubuf
						&& cfg.chan[channel-1]->misc&CHAN_GURU)
544
						guruchat(pgraph,gurubuf,cfg.chan[channel-1]->guru,guru_lastanswer);
545 546
				}
			}
547
		}
rswindell's avatar
rswindell committed
548
		if(sys_status&SS_ABORT)
549
			break;
550
	}
rswindell's avatar
rswindell committed
551 552 553
	lncntr=0;
}

554 555
/****************************************************************************/
/****************************************************************************/
rswindell's avatar
rswindell committed
556 557 558 559 560 561 562
bool sbbs_t::guru_page(void)
{
	char	path[MAX_PATH+1];
	char*	gurubuf;
	int 	file;
	long	i;

563 564
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
565
		return(false);
566 567
	}

rswindell's avatar
rswindell committed
568 569
	if(!cfg.total_gurus) {
		bprintf(text[SysopIsNotAvailable],"The Guru");
570
		return(false);
rswindell's avatar
rswindell committed
571
	}
rswindell's avatar
rswindell committed
572
	if(cfg.total_gurus==1 && chk_ar(cfg.guru[0]->ar,&useron,&client))
rswindell's avatar
rswindell committed
573 574 575 576 577 578
		i=0;
	else {
		for(i=0;i<cfg.total_gurus;i++)
			uselect(1,i,nulstr,cfg.guru[i]->name,cfg.guru[i]->ar);
		i=uselect(0,0,0,0,0);
		if(i<0)
579
			return(false);
rswindell's avatar
rswindell committed
580 581 582 583
	}
	sprintf(path,"%s%s.dat",cfg.ctrl_dir,cfg.guru[i]->code);
	if((file=nopen(path,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_OPEN,path,O_RDONLY);
584
		return(false);
rswindell's avatar
rswindell committed
585
	}
586
	if((gurubuf=(char *)malloc((size_t)filelength(file)+1))==NULL) {
rswindell's avatar
rswindell committed
587
		close(file);
588
		errormsg(WHERE,ERR_ALLOC,path,(size_t)filelength(file)+1);
589
		return(false);
rswindell's avatar
rswindell committed
590
	}
591
	read(file,gurubuf,(size_t)filelength(file));
rswindell's avatar
rswindell committed
592 593 594
	gurubuf[filelength(file)]=0;
	close(file);
	localguru(gurubuf,i);
deuce's avatar
deuce committed
595
	free(gurubuf);
rswindell's avatar
rswindell committed
596 597 598 599 600 601 602 603 604 605
	return(true);
}

/****************************************************************************/
/* The chat section                                                         */
/****************************************************************************/
void sbbs_t::chatsection()
{
	char	str[256],ch,no_rip_menu;

606 607
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
608
		return;
609 610
	}

611
	action=NODE_CHAT;
612
	if(useron.misc&(RIP|WIP|HTML) || !(useron.misc&EXPERT))
613
		menu("chat");
614 615 616 617 618
	ASYNC;
	bputs(text[ChatPrompt]);
	while(online) {
		no_rip_menu=0;
		ch=(char)getkeys("ACDJPQST?\r",0);
619
		if(ch>' ')
620 621 622 623 624
			logch(ch,0);
		switch(ch) {
			case 'S':
				useron.chat^=CHAT_SPLITP;
				putuserrec(&cfg,useron.number,U_CHAT,8
625
					,ultoa(useron.chat,str,16));
626 627 628 629 630 631 632
				bprintf("\r\nPrivate split-screen chat is now: %s\r\n"
					,useron.chat&CHAT_SPLITP ? text[ON]:text[OFF]);
				break;
			case 'A':
				CRLF;
				useron.chat^=CHAT_NOACT;
				putuserrec(&cfg,useron.number,U_CHAT,8
633
					,ultoa(useron.chat,str,16));
634 635 636 637
				if(getnodedat(cfg.node_num,&thisnode,true)==0) {
					thisnode.misc^=NODE_AOFF;
					printnodedat(cfg.node_num,&thisnode);
				}
638 639 640 641 642 643 644
				putnodedat(cfg.node_num,&thisnode);
				no_rip_menu=true;
				break;
			case 'D':
				CRLF;
				useron.chat^=CHAT_NOPAGE;
				putuserrec(&cfg,useron.number,U_CHAT,8
645
					,ultoa(useron.chat,str,16));
646 647 648 649
				if(getnodedat(cfg.node_num,&thisnode,true)==0) {
					thisnode.misc^=NODE_POFF;
					printnodedat(cfg.node_num,&thisnode);
				}
650 651 652 653
				putnodedat(cfg.node_num,&thisnode);
				no_rip_menu=true;
				break;
			case 'J':
rswindell's avatar
rswindell committed
654
				multinodechat();
655 656 657 658 659 660
				break;
			case 'P':   /* private node-to-node chat */
				privchat();
				break;
			case 'C':
				no_rip_menu=1;
rswindell's avatar
rswindell committed
661 662
				if(sysop_page())
					break;
663 664
				if(cfg.total_gurus && chk_ar(cfg.guru[0]->ar,&useron,&client) && text[ChatWithGuruInsteadQ][0]) {
					SAFEPRINTF(str,text[ChatWithGuruInsteadQ],cfg.guru[0]->name);
665
					if(!yesno(str))
666 667
						break; 
				}
668 669
				else
					break;
rswindell's avatar
rswindell committed
670
				/* FALL-THROUGH */
671
			case 'T':
rswindell's avatar
rswindell committed
672
				guru_page();
673 674 675 676
				no_rip_menu=1;
				break;
			case '?':
				if(useron.misc&EXPERT)
677
					menu("chat");
678 679 680
				break;
			default:	/* 'Q' or <CR> */
				lncntr=0;
rswindell's avatar
rswindell committed
681
//				if(gurubuf)
deuce's avatar
deuce committed
682
//					free(gurubuf);
683 684
				return; 
		}
685
		action=NODE_CHAT;
686
		if(!(useron.misc&EXPERT) || useron.misc&(WIP|HTML)
687
			|| (useron.misc&RIP && !no_rip_menu)) {
688 689
			menu("chat"); 
		}
690
		ASYNC;
691 692
		bputs(text[ChatPrompt]); 
	}
rswindell's avatar
rswindell committed
693
//	if(gurubuf)
deuce's avatar
deuce committed
694
//		free(gurubuf);
695 696
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
static char* sysop_available_semfile(scfg_t* scfg)
{
	static char semfile[MAX_PATH+1];
	SAFEPRINTF(semfile, "%ssysavail.chat", scfg->ctrl_dir);
	return semfile;
}

extern "C" BOOL DLLCALL sysop_available(scfg_t* scfg)
{
	return fexist(sysop_available_semfile(scfg));
}

extern "C" BOOL DLLCALL set_sysop_availability(scfg_t* scfg, BOOL available)
{
	if(available)
		return ftouch(sysop_available_semfile(scfg));
	return remove(sysop_available_semfile(scfg)) == 0;
}

716 717
/****************************************************************************/
/****************************************************************************/
rswindell's avatar
rswindell committed
718
bool sbbs_t::sysop_page(void)
719
{
720 721
	char	str[256];
	int		i;
722

723 724 725 726 727
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
		return(false); 
	}

728
	if(sysop_available(&cfg)
rswindell's avatar
rswindell committed
729
		|| (cfg.sys_chat_ar[0] && chk_ar(cfg.sys_chat_ar,&useron,&client))
rswindell's avatar
rswindell committed
730 731
		|| useron.exempt&FLAG('C')) {

732 733 734
		sprintf(str,"%s paged sysop for chat",useron.alias);
		logline("C",str);

735
		ftouch(syspage_semfile);
rswindell's avatar
rswindell committed
736
		for(i=0;i<cfg.total_pages;i++)
rswindell's avatar
rswindell committed
737
			if(chk_ar(cfg.page[i]->ar,&useron,&client))
rswindell's avatar
rswindell committed
738 739 740 741
				break;
		if(i<cfg.total_pages) {
			bprintf(text[PagingGuru],cfg.sys_op);
			external(cmdstr(cfg.page[i]->cmd,nulstr,nulstr,NULL)
742
				,cfg.page[i]->misc&XTRN_STDIO ? EX_STDIO : 0); 
743
		}
rswindell's avatar
rswindell committed
744 745 746 747 748
		else if(cfg.sys_misc&SM_SHRTPAGE) {
			bprintf(text[PagingGuru],cfg.sys_op);
			for(i=0;i<10 && !lkbrd(1);i++) {
				sbbs_beep(1000,200);
				mswait(200);
749 750 751 752
				outchar('.'); 
			}
			CRLF; 
		}
rswindell's avatar
rswindell committed
753 754 755 756 757 758
		else {
			sys_status^=SS_SYSPAGE;
			bprintf(text[SysopPageIsNow]
				,sys_status&SS_SYSPAGE ? text[ON] : text[OFF]);
			nosound();	
		}
759 760
		if(!(sys_status&SS_SYSPAGE))
			remove(syspage_semfile);
rswindell's avatar
rswindell committed
761 762 763 764 765 766 767

		return(true);
	}

	bprintf(text[SysopIsNotAvailable],cfg.sys_op);

	return(false);
768 769 770 771 772 773 774 775
}

/****************************************************************************/
/* Returns 1 if user online has access to channel "channum"                 */
/****************************************************************************/
bool sbbs_t::chan_access(uint cnum)
{

rswindell's avatar
rswindell committed
776
	if(!cfg.total_chans || cnum>=cfg.total_chans || !chk_ar(cfg.chan[cnum]->ar,&useron,&client)) {
777
		bputs(text[CantAccessThatChannel]);
778 779
		return(false); 
	}
780 781
	if(!(useron.exempt&FLAG('J')) && cfg.chan[cnum]->cost>useron.cdt+useron.freecdt) {
		bputs(text[NotEnoughCredits]);
782 783
		return(false); 
	}
784 785 786
	return(true);
}

787 788 789
/****************************************************************************/
/* Private split-screen (or interspersed) chat with node or local sysop		*/
/****************************************************************************/
790 791 792 793
void sbbs_t::privchat(bool local)
{
	char	str[128],c,*p,localbuf[5][81],remotebuf[5][81]
			,localline=0,remoteline=0,localchar=0,remotechar=0
794 795 796
			,*sep=text[PrivateChatSeparator]
			,*local_sep=text[SysopChatSeparator]
			;
797
	char 	tmp[512];
798
	char	outpath[MAX_PATH+1];
799
	char	inpath[MAX_PATH+1];
800 801
	uchar	ch;
	int 	in,out,i,n,echo=1,x,y,activity,remote_activity;
802
    int		local_y=1,remote_y=1;
803
	node_t	node;
804
	time_t	last_nodechk=0;
805 806 807 808

	if(local) 
		n=0;
	else {
809 810 811 812 813 814

		if(useron.rest&FLAG('C')) {
			bputs(text[R_Chat]);
			return; 
		}

815 816 817 818 819
		n=getnodetopage(0,0);
		if(!n)
			return;
		if(n==cfg.node_num) {
			bputs(text[NoNeedToPageSelf]);
820 821
			return; 
		}
822 823 824
		getnodedat(n,&node,0);
		if(node.action==NODE_PCHT && node.aux!=cfg.node_num) {
			bprintf(text[NodeNAlreadyInPChat],n);
825 826
			return; 
		}
827 828 829 830
		if((node.action!=NODE_PAGE || node.aux!=cfg.node_num)
			&& node.misc&NODE_POFF && !SYSOP) {
			bprintf(text[CantPageNode],node.misc&NODE_ANON
				? text[UNKNOWN_USER] : username(&cfg,node.useron,tmp));
831 832
			return; 
		}
833
		if(node.action!=NODE_PAGE) {
834
			bprintf(text[PagingUser]
835
				,node.misc&NODE_ANON ? text[UNKNOWN_USER] : username(&cfg,node.useron,tmp)
836
				,node.misc&NODE_ANON ? 0 : node.useron);
837 838 839
			sprintf(str,text[NodePChatPageMsg]
				,cfg.node_num,thisnode.misc&NODE_ANON
					? text[UNKNOWN_USER] : useron.alias);
840
			putnmsg(&cfg,n,str);
841 842
			sprintf(str,"%s paged %s on node %d to private chat"
				,useron.alias,username(&cfg,node.useron,tmp),n);
843 844
			logline("C",str); 
		}
845

846 847 848 849 850
		if(getnodedat(cfg.node_num,&thisnode,true)==0) {
			thisnode.action=action=NODE_PAGE;
			thisnode.aux=n;
			putnodedat(cfg.node_num,&thisnode);
		}
851 852 853 854 855 856 857 858 859 860

		if(node.action!=NODE_PAGE || node.aux!=cfg.node_num) {
			bprintf(text[WaitingForNodeInPChat],n);
			while(online && !(sys_status&SS_ABORT)) {
				getnodedat(n,&node,0);
				if((node.action==NODE_PAGE || node.action==NODE_PCHT)
					&& node.aux==cfg.node_num) {
					bprintf(text[NodeJoinedPrivateChat]
						,n,node.misc&NODE_ANON ? text[UNKNOWN_USER]
							: username(&cfg,node.useron,tmp));
861 862
					break; 
				}
863 864 865
				action=NODE_PAGE;
				checkline();
				gettimeleft();
866
				SYNC; 
867
				inkey(K_NONE,500);
868 869
			} 
		}
870 871
	}

872 873
	gettimeleft();

874 875 876 877 878 879
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
		thisnode.action=action=NODE_PCHT;
		thisnode.aux=n;
		thisnode.misc&=~NODE_LCHAT;
		putnodedat(cfg.node_num,&thisnode);
	}
880 881 882 883

	if(!online || sys_status&SS_ABORT)
		return;

884 885 886 887 888 889
	if(local) {
		/* If an external sysop chat event handler is installed, just run that and do nothing else */
		if(user_event(EVENT_LOCAL_CHAT))
			return;
	}

890
	if(((sys_status&SS_USERON && useron.chat&CHAT_SPLITP) || !(sys_status&SS_USERON))
891
		&& term_supports(ANSI) && rows>=24 && cols>=80)
892
		sys_status|=SS_SPLITP;
893 894
	else
		sys_status&=~SS_SPLITP;
895 896
	/*
	if(!(useron.misc&EXPERT))
897
		menu("privchat");
898 899 900 901 902 903 904 905 906
	*/

	if(!(sys_status&SS_SPLITP)) {
		if(local)
			bprintf(text[SysopIsHere],cfg.sys_op);
		else
			bputs(text[WelcomeToPrivateChat]);
	}

907
	sprintf(outpath,"%schat.dab",cfg.node_dir);
908
	if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
909
		errormsg(WHERE,ERR_OPEN,outpath,O_RDWR|O_DENYNONE|O_CREAT);
910 911
		return; 
	}
912 913

	if(local)
914
		sprintf(inpath,"%slchat.dab",cfg.node_dir);
915
	else
916 917
		sprintf(inpath,"%schat.dab",cfg.node_path[n-1]);
	if(!fexist(inpath))		/* Wait while it's created for the first time */
918
		mswait(2000);
919
	if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
920
		close(out);
921
		errormsg(WHERE,ERR_OPEN,inpath,O_RDWR|O_DENYNONE|O_CREAT);
922 923
		return; 
	}
924

deuce's avatar
deuce committed
925
	if((p=(char *)malloc(PCHAT_LEN))==NULL) {
926 927
		close(in);
		close(out);
928
		errormsg(WHERE,ERR_ALLOC,nulstr,PCHAT_LEN);
929 930
		return; 
	}
931 932 933
	memset(p,0,PCHAT_LEN);
	write(in,p,PCHAT_LEN);
	write(out,p,PCHAT_LEN);
deuce's avatar
deuce committed
934
	free(p);
935 936 937
	lseek(in,0L,SEEK_SET);
	lseek(out,0L,SEEK_SET);

938 939 940 941
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
		thisnode.misc&=~NODE_RPCHT; 		/* Clear "reset pchat flag" */
		putnodedat(cfg.node_num,&thisnode);
	}
942 943

	if(!local) {
944 945 946 947
		if(getnodedat(n,&node,true)==0) {
			node.misc|=NODE_RPCHT;				/* Set "reset pchat flag" */
			putnodedat(n,&node); 				/* on other node */
		}
948 949 950 951 952 953 954 955 956 957 958 959 960

											/* Wait for other node */
											/* to acknowledge and reset */
		while(online && !(sys_status&SS_ABORT)) {
			getnodedat(n,&node,0);
			if(!(node.misc&NODE_RPCHT))
				break;
			getnodedat(cfg.node_num,&thisnode,0);
			if(thisnode.misc&NODE_RPCHT)
				break;
			checkline();
			gettimeleft();
			SYNC;