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 50.6 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 375 376 377 378 379 380 381 382
						break;
					case 'Q':	/* quit */
						done=1;
						break;
					case '*':
						sprintf(str,"%smenu/chan.*",cfg.text_dir);
						if(fexist(str))
							menu("chan");
						else {
							bputs(text[ChatChanLstHdr]);
							bputs(text[ChatChanLstTitles]);
							if(cfg.total_chans>=10) {
								bputs("     ");
383
								bputs(text[ChatChanLstTitles]);
384
							}
rswindell's avatar
rswindell committed
385 386 387 388
							CRLF;
							bputs(text[ChatChanLstUnderline]);
							if(cfg.total_chans>=10) {
								bputs("     ");
389
								bputs(text[ChatChanLstUnderline]);
390
							}
rswindell's avatar
rswindell committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
							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
408 409
											,cfg.chan[k]->cost);
									}
410
								}
411
								CRLF;
412
							}
413
							CRLF;
414
						}
rswindell's avatar
rswindell committed
415 416 417
						break;
					case '?':	/* menu */
						menu("multchat");
418 419
						break;
				}
420
			} else {
rswindell's avatar
rswindell committed
421 422 423 424 425 426 427 428 429 430 431 432
				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
433
						sprintf(tmp,"%*s",(int)bstrlen(str),nulstr);
434
						strcat(pgraph,tmp);
435
					}
rswindell's avatar
rswindell committed
436 437 438 439
					strcat(pgraph,line);
					strcat(pgraph,crlf);
					if(!wordwrap[0])
						break;
440
					j++;
441
				}
rswindell's avatar
rswindell committed
442
				if(pgraph[0]) {
443
					if(channel && useron.chat&CHAT_ACTION) {
rswindell's avatar
rswindell committed
444 445 446 447 448 449 450 451 452 453 454
						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))
455
								break;
456
						}
rswindell's avatar
rswindell committed
457 458 459 460 461 462 463 464 465 466 467

						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;
468
									continue;
469
								}
rswindell's avatar
rswindell committed
470 471 472 473 474 475
								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)))
476
									break;
477
							}
rswindell's avatar
rswindell committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
							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);
503
								putnmsg(&cfg,usr[j],buf);
504
							}
rswindell's avatar
rswindell committed
505 506 507 508 509 510 511 512 513 514 515 516 517


							/* 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);
518
								putnmsg(&cfg,usr[i],buf);
519
							}
rswindell's avatar
rswindell committed
520 521
							for(i=0;i<qusrs;i++) {
								getnodedat(qusr[i],&node,0);
522
								putnmsg(&cfg,qusr[i],buf);
523
							}
524 525
							continue;
						}
526
					}
rswindell's avatar
rswindell committed
527 528 529 530 531 532 533 534 535 536

					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);
537
						putnmsg(&cfg,usr[i],buf);
538
					}
rswindell's avatar
rswindell committed
539 540
					for(i=0;i<qusrs;i++) {
						getnodedat(qusr[i],&node,0);
541
						putnmsg(&cfg,qusr[i],buf);
542
					}
rswindell's avatar
rswindell committed
543 544
					if(!usrs && channel && gurubuf
						&& cfg.chan[channel-1]->misc&CHAN_GURU)
545
						guruchat(pgraph,gurubuf,cfg.chan[channel-1]->guru,guru_lastanswer);
546 547
				}
			}
548
		}
rswindell's avatar
rswindell committed
549
		if(sys_status&SS_ABORT)
550
			break;
551
	}
rswindell's avatar
rswindell committed
552 553 554
	lncntr=0;
}

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

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

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

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

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

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

698 699
/****************************************************************************/
/****************************************************************************/
rswindell's avatar
rswindell committed
700
bool sbbs_t::sysop_page(void)
701
{
702 703
	char	str[256];
	int		i;
704

705 706 707 708 709
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
		return(false); 
	}

rswindell's avatar
rswindell committed
710
	if(startup->options&BBS_OPT_SYSOP_AVAILABLE 
rswindell's avatar
rswindell committed
711
		|| (cfg.sys_chat_ar[0] && chk_ar(cfg.sys_chat_ar,&useron,&client))
rswindell's avatar
rswindell committed
712 713
		|| useron.exempt&FLAG('C')) {

714 715 716
		sprintf(str,"%s paged sysop for chat",useron.alias);
		logline("C",str);

717
		ftouch(syspage_semfile);
rswindell's avatar
rswindell committed
718
		for(i=0;i<cfg.total_pages;i++)
rswindell's avatar
rswindell committed
719
			if(chk_ar(cfg.page[i]->ar,&useron,&client))
rswindell's avatar
rswindell committed
720 721 722 723
				break;
		if(i<cfg.total_pages) {
			bprintf(text[PagingGuru],cfg.sys_op);
			external(cmdstr(cfg.page[i]->cmd,nulstr,nulstr,NULL)
724
				,cfg.page[i]->misc&XTRN_STDIO ? EX_STDIO : 0); 
725
		}
rswindell's avatar
rswindell committed
726 727 728 729 730
		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);
731 732 733 734
				outchar('.'); 
			}
			CRLF; 
		}
rswindell's avatar
rswindell committed
735 736 737 738 739 740
		else {
			sys_status^=SS_SYSPAGE;
			bprintf(text[SysopPageIsNow]
				,sys_status&SS_SYSPAGE ? text[ON] : text[OFF]);
			nosound();	
		}
741 742
		if(!(sys_status&SS_SYSPAGE))
			remove(syspage_semfile);
rswindell's avatar
rswindell committed
743 744 745 746 747 748 749

		return(true);
	}

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

	return(false);
750 751 752 753 754 755 756 757
}

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

rswindell's avatar
rswindell committed
758
	if(!cfg.total_chans || cnum>=cfg.total_chans || !chk_ar(cfg.chan[cnum]->ar,&useron,&client)) {
759
		bputs(text[CantAccessThatChannel]);
760 761
		return(false); 
	}
762 763
	if(!(useron.exempt&FLAG('J')) && cfg.chan[cnum]->cost>useron.cdt+useron.freecdt) {
		bputs(text[NotEnoughCredits]);
764 765
		return(false); 
	}
766 767 768
	return(true);
}

769 770 771
/****************************************************************************/
/* Private split-screen (or interspersed) chat with node or local sysop		*/
/****************************************************************************/
772 773 774 775
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
776 777 778
			,*sep=text[PrivateChatSeparator]
			,*local_sep=text[SysopChatSeparator]
			;
779
	char 	tmp[512];
780
	char	outpath[MAX_PATH+1];
781
	char	inpath[MAX_PATH+1];
782 783
	uchar	ch;
	int 	in,out,i,n,echo=1,x,y,activity,remote_activity;
784
    int		local_y=1,remote_y=1;
785
	node_t	node;
786
	time_t	last_nodechk=0;
787 788 789 790

	if(local) 
		n=0;
	else {
791 792 793 794 795 796

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

797 798 799 800 801
		n=getnodetopage(0,0);
		if(!n)
			return;
		if(n==cfg.node_num) {
			bputs(text[NoNeedToPageSelf]);
802 803
			return; 
		}
804 805 806
		getnodedat(n,&node,0);
		if(node.action==NODE_PCHT && node.aux!=cfg.node_num) {
			bprintf(text[NodeNAlreadyInPChat],n);
807 808
			return; 
		}
809 810 811 812
		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));
813 814
			return; 
		}
815
		if(node.action!=NODE_PAGE) {
816
			bprintf(text[PagingUser]
817
				,node.misc&NODE_ANON ? text[UNKNOWN_USER] : username(&cfg,node.useron,tmp)
818
				,node.misc&NODE_ANON ? 0 : node.useron);
819 820 821
			sprintf(str,text[NodePChatPageMsg]
				,cfg.node_num,thisnode.misc&NODE_ANON
					? text[UNKNOWN_USER] : useron.alias);
822
			putnmsg(&cfg,n,str);
823 824
			sprintf(str,"%s paged %s on node %d to private chat"
				,useron.alias,username(&cfg,node.useron,tmp),n);
825 826
			logline("C",str); 
		}
827

828 829 830 831 832
		if(getnodedat(cfg.node_num,&thisnode,true)==0) {
			thisnode.action=action=NODE_PAGE;
			thisnode.aux=n;
			putnodedat(cfg.node_num,&thisnode);
		}
833 834 835 836 837 838 839 840 841 842

		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));
843 844
					break; 
				}
845 846 847
				action=NODE_PAGE;
				checkline();
				gettimeleft();
848
				SYNC; 
849
				inkey(K_NONE,500);
850 851
			} 
		}
852 853
	}

854 855
	gettimeleft();

856 857 858 859 860 861
	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);
	}
862 863 864 865

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

866 867 868 869 870 871
	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;
	}

872
	if(((sys_status&SS_USERON && useron.chat&CHAT_SPLITP) || !(sys_status&SS_USERON))
873
		&& term_supports(ANSI) && rows>=24 && cols>=80)
874
		sys_status|=SS_SPLITP;
875 876
	else
		sys_status&=~SS_SPLITP;
877 878
	/*
	if(!(useron.misc&EXPERT))
879
		menu("privchat");
880 881 882 883 884 885 886 887 888
	*/

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

889
	sprintf(outpath,"%schat.dab",cfg.node_dir);
890
	if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
891
		errormsg(WHERE,ERR_OPEN,outpath,O_RDWR|O_DENYNONE|O_CREAT);
892 893
		return; 
	}
894 895

	if(local)
896
		sprintf(inpath,"%slchat.dab",cfg.node_dir);
897
	else
898 899
		sprintf(inpath,"%schat.dab",cfg.node_path[n-1]);
	if(!fexist(inpath))		/* Wait while it's created for the first time */
900
		mswait(2000);
901
	if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
902
		close(out);
903
		errormsg(WHERE,ERR_OPEN,inpath,O_RDWR|O_DENYNONE|O_CREAT);
904 905
		return; 
	}
906

deuce's avatar
deuce committed
907
	if((p=(char *)malloc(PCHAT_LEN))==NULL) {
908 909
		close(in);
		close(out);
910
		errormsg(WHERE,ERR_ALLOC,nulstr,PCHAT_LEN);
911 912
		return; 
	}
913 914 915
	memset(p,0,PCHAT_LEN);
	write(in,p,PCHAT_LEN);
	write(out,p,PCHAT_LEN);
deuce's avatar
deuce committed
916
	free(p);
917 918 919
	lseek(in,0L,SEEK_SET);
	lseek(out,0L,SEEK_SET);

920 921 922 923
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
		thisnode.misc&=~NODE_RPCHT; 		/* Clear "reset pchat flag" */
		putnodedat(cfg.node_num,&thisnode);
	}
924 925

	if(!local) {
926 927 928 929
		if(getnodedat(n,&node,true)==0) {
			node.misc|=NODE_RPCHT;				/* Set "reset pchat flag" */
			putnodedat(n,&node); 				/* on other node */
		}
930 931 932 933 934 935 936 937 938 939 940 941 942

											/* 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;
943 944
			SLEEP(500); 
		}
945 946 947 948 949 950 951 952
	}

	action=NODE_PCHT;
	SYNC;

	if(sys_status&SS_SPLITP) {
		lncntr=0;
		CLS;
953 954
		ansi_save();
		ansi_gotoxy(1,13);
955 956
		remote_y=1;
		bprintf(local ? local_sep : sep
957
			,thisnode.misc&NODE_MSGW ? 'T':' '
rswindell's avatar