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.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* chat.cpp */

/* Synchronet real-time chat functions */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html		*
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 46
 *																			*
 * 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"};

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

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

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

	if(!chan_access(channel-1))
		return;
71
	if(useron.misc&(RIP|WIP|HTML) ||!(useron.misc&EXPERT))
rswindell's avatar
rswindell committed
72 73
		menu("multchat");
	bputs(text[WelcomeToMultiChat]);
74 75 76 77
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
		thisnode.aux=channel;		
		putnodedat(cfg.node_num,&thisnode);
	}
rswindell's avatar
rswindell committed
78 79
	bprintf(text[WelcomeToChannelN],channel,cfg.chan[channel-1]->name);
	if(gurubuf) {
deuce's avatar
deuce committed
80
		free(gurubuf);
81 82
		gurubuf=NULL; 
	}
rswindell's avatar
rswindell committed
83
	if(cfg.chan[channel-1]->misc&CHAN_GURU && cfg.chan[channel-1]->guru<cfg.total_gurus
rswindell's avatar
rswindell committed
84
		&& chk_ar(cfg.guru[cfg.chan[channel-1]->guru]->ar,&useron,&client)) {
rswindell's avatar
rswindell committed
85 86 87
		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);
88 89
			return; 
		}
90
		if((gurubuf=(char *)malloc((size_t)filelength(file)+1))==NULL) {
rswindell's avatar
rswindell committed
91
			close(file);
92
			errormsg(WHERE,ERR_ALLOC,str,(size_t)filelength(file)+1);
93 94
			return; 
		}
95
		read(file,gurubuf,(size_t)filelength(file));
rswindell's avatar
rswindell committed
96
		gurubuf[filelength(file)]=0;
97 98
		close(file); 
	}
rswindell's avatar
rswindell committed
99 100 101 102 103 104 105 106 107 108
	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);
109 110
		preusr[usrs]=usr[usrs++]=(char)i; 
	}
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 133
				usr[usrs++]=(char)i; 
		}
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 155
						,preusr[i],str,channel); 
				} 
			} 
		}
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 177
						,usr[i],str,channel); 
				} 
			} 
		}
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 220
			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;
								bputs(text[CorrectPassword]);  }
221 222
						preusr[usrs]=usr[usrs++]=(char)i; 
					}
rswindell's avatar
rswindell committed
223 224
					if(i<=cfg.sys_nodes) {	/* failed password */
						bputs(text[WrongPassword]);
225 226
						continue; 
					}
rswindell's avatar
rswindell committed
227
					if(gurubuf) {
deuce's avatar
deuce committed
228
						free(gurubuf);
229 230
						gurubuf=NULL; 
					}
rswindell's avatar
rswindell committed
231 232
					if(cfg.chan[savch-1]->misc&CHAN_GURU
						&& cfg.chan[savch-1]->guru<cfg.total_gurus
rswindell's avatar
rswindell committed
233
						&& chk_ar(cfg.guru[cfg.chan[savch-1]->guru]->ar,&useron,&client
rswindell's avatar
rswindell committed
234 235 236 237 238
						)) {
						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);
239 240
							break; 
						}
241
						if((gurubuf=(char *)malloc((size_t)filelength(file)+1))==NULL) {
rswindell's avatar
rswindell committed
242 243
							close(file);
							errormsg(WHERE,ERR_ALLOC,str
244
								,(size_t)filelength(file)+1);
245 246
							break; 
						}
247
						read(file,gurubuf,(size_t)filelength(file));
rswindell's avatar
rswindell committed
248
						gurubuf[filelength(file)]=0;
249 250
						close(file); 
					}
rswindell's avatar
rswindell committed
251 252 253 254 255 256 257 258 259 260 261
					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)) {
262
							getnodedat(cfg.node_num,&thisnode,true);
rswindell's avatar
rswindell committed
263
							thisnode.aux=channel;
264 265
							packchatpass(str,&thisnode); 
						}
rswindell's avatar
rswindell committed
266
						else {
267
							getnodedat(cfg.node_num,&thisnode,true);
268 269 270
							thisnode.aux=channel; 
						} 
					}
rswindell's avatar
rswindell committed
271
					else {
272
						getnodedat(cfg.node_num,&thisnode,true);
273 274
						thisnode.aux=channel; 
					}
rswindell's avatar
rswindell committed
275 276 277 278
					putnodedat(cfg.node_num,&thisnode);
					bputs(text[YoureOnTheAir]);
					if(cfg.chan[channel-1]->cost
						&& !(useron.exempt&FLAG('J')))
279 280
						subtract_cdt(&cfg,&useron,cfg.chan[channel-1]->cost); 
				}
rswindell's avatar
rswindell committed
281 282 283 284 285 286 287 288 289 290 291 292 293
				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);
294 295
							preusr[usrs]=usr[usrs++]=(char)i; 
						}
rswindell's avatar
rswindell committed
296
						preusrs=usrs;
297 298 299 300
						if(getnodedat(cfg.node_num,&thisnode,true)==0) {
							thisnode.aux=channel=0;
							putnodedat(cfg.node_num,&thisnode);
						}
rswindell's avatar
rswindell committed
301 302 303 304 305 306 307 308 309 310 311
						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;
312
						for(i=0;channel && i<cfg.total_chatacts;i++) {
rswindell's avatar
rswindell committed
313 314 315 316 317 318
							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)) {
319 320
								CRLF; 
							}
rswindell's avatar
rswindell committed
321
							else
322 323
								bputs(" "); 
						}
rswindell's avatar
rswindell committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337
						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);
338 339
							printnodedat(i,&node); 
						}
rswindell's avatar
rswindell committed
340 341 342 343 344 345 346 347 348 349 350
						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]);
351 352
							break; 
						}
rswindell's avatar
rswindell committed
353 354 355 356 357 358 359 360 361 362 363 364 365

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

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


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

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

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

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

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

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

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

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

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

702 703 704 705 706
	if(useron.rest&FLAG('C')) {
		bputs(text[R_Chat]);
		return(false); 
	}

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

711 712 713
		sprintf(str,"%s paged sysop for chat",useron.alias);
		logline("C",str);

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

		return(true);
	}

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

	return(false);
744 745 746 747 748 749 750 751
}

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

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

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

	if(local) 
		n=0;
	else {
785 786 787 788 789 790

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

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

822 823 824 825 826
		if(getnodedat(cfg.node_num,&thisnode,true)==0) {
			thisnode.action=action=NODE_PAGE;
			thisnode.aux=n;
			putnodedat(cfg.node_num,&thisnode);
		}
827 828 829 830 831 832 833 834 835 836

		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));
837 838
					break; 
				}
839 840 841
				action=NODE_PAGE;
				checkline();
				gettimeleft();
842
				SYNC; 
843
				inkey(K_NONE,500);
844 845
			} 
		}
846 847
	}

848 849
	gettimeleft();

850 851 852 853 854 855
	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);
	}
856 857 858 859

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

860
	if(((sys_status&SS_USERON && useron.chat&CHAT_SPLITP) || !(sys_status&SS_USERON))
861
		&& term_supports(ANSI) && rows>=24 && cols>=80)
862
		sys_status|=SS_SPLITP;
863 864
	else
		sys_status&=~SS_SPLITP;
865 866
	/*
	if(!(useron.misc&EXPERT))
867
		menu("privchat");
868 869 870 871 872 873 874 875 876
	*/

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

877
	sprintf(outpath,"%schat.dab",cfg.node_dir);
878
	if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
879
		errormsg(WHERE,ERR_OPEN,outpath,O_RDWR|O_DENYNONE|O_CREAT);
880 881
		return; 
	}
882 883

	if(local)
884
		sprintf(inpath,"%slchat.dab",cfg.node_dir);
885
	else
886 887
		sprintf(inpath,"%schat.dab",cfg.node_path[n-1]);
	if(!fexist(inpath))		/* Wait while it's created for the first time */
888
		mswait(2000);
889
	if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,SH_DENYNO,DEFFILEMODE))==-1) {
890 891
		close(out);
		errormsg(WHERE,ERR_OPEN,str,O_RDWR|O_DENYNONE|O_CREAT);
892 893
		return; 
	}
894

deuce's avatar
deuce committed
895
	if((p=(char *)malloc(PCHAT_LEN))==NULL) {
896 897 898
		close(in);
		close(out);
		errormsg(WHERE,ERR_ALLOC,str,PCHAT_LEN);
899 900
		return; 
	}
901 902 903
	memset(p,0,PCHAT_LEN);
	write(in,p,PCHAT_LEN);
	write(out,p,PCHAT_LEN);
deuce's avatar
deuce committed
904
	free(p);
905 906 907
	lseek(in,0L,SEEK_SET);
	lseek(out,0L,SEEK_SET);

908 909 910 911
	if(getnodedat(cfg.node_num,&thisnode,true)==0) {
		thisnode.misc&=~NODE_RPCHT; 		/* Clear "reset pchat flag" */
		putnodedat(cfg.node_num,&thisnode);
	}
912 913

	if(!local) {
914 915 916 917
		if(getnodedat(n,&node,true)==0) {
			node.misc|=NODE_RPCHT;				/* Set "reset pchat flag" */
			putnodedat(n,&node); 				/* on other node */
		}
918 919 920 921 922 923 924 925 926 927 928 929 930

											/* 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;
931 932
			SLEEP(500); 
		}
933 934 935 936 937 938 939 940
	}

	action=NODE_PCHT;
	SYNC;

	if(sys_status&SS_SPLITP) {
		lncntr=0;
		CLS;
941 942
		ansi_save();
		ansi_gotoxy(1,13);
943 944
		remote_y=1;
		bprintf(local ? local_sep : sep
945
			,thisnode.misc&NODE_MSGW ? 'T':' '
946
			,sectostr(timeleft,tmp)
947
			,thisnode.misc&NODE_NMSG ? 'M':' ');
948
		CRLF;
949 950
		local_y=14; 
	}
951 952 953 954 955 956 957 958

	while(online && (local || !(sys_status&SS_ABORT))) {
		lncntr=0;
		if(sys_status&SS_SPLITP)
			lbuflen=0;
		action=NODE_PCHT;
		activity=0;
		remote_activity=0;
959
		if((ch=inkey(K_GETSTR,100))!=0) {
960 961 962
			activity=1;
			if(echo)
				attr(cfg.color[clr_chatlocal]);
963
			if(ch==BS || ch==DEL) {
964 965
				if(localchar) {
					if(echo)
966
						backspace();
967
					localchar--;
968 969 970
					localbuf[localline][localchar]=0; 
				} 
			}
971 972
			else if(ch==TAB) {
				if(echo)
973 974
					outchar(' ');
				localbuf[localline][localchar]=' ';
975 976 977
				localchar++;
				while(localchar<78 && localchar%8) {
					if(echo)
978
						outchar(' ');
979 980 981
					localbuf[localline][localchar++]=' '; 
				} 
			}
982
			else if(ch==CTRL_R) {
983 984 985 986 987 988 989 990 991 992 993
				if(sys_status&SS_SPLITP) {
					CLS;
					attr(cfg.color[clr_chatremote]);
					remotebuf[remoteline][remotechar]=0;
					for(i=0;i<=remoteline;i++) {
						bputs(remotebuf[i]); 
						if(i!=remoteline) 
							bputs(crlf);
					}
					remote_y=1+remoteline;
					bputs("\1i_\1n");  /* Fake cursor */
994 995
					ansi_save();
					ansi_gotoxy(1,13);
996
					bprintf(local ? local_sep : sep
997
						,thisnode.misc&NODE_MSGW ? 'T':' '
998
						,sectostr(timeleft,tmp)
999
						,thisnode.misc&NODE_NMSG ? 'M':' ');
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
					CRLF;
					attr(cfg.color[clr_chatlocal]);
					localbuf[localline][localchar]=0;
					for(i=0;i<=localline;i++) {
						bputs(localbuf[i]);
						if(i!=localline) 
							bputs(crlf);
					}
					local_y=15+localline;
				}
1010
				continue;
1011
			}
1012
			else if(ch>=' ' || ch==CR) {
1013 1014 1015
				if(ch!=CR) {
					if(echo)
						outchar(ch);
1016 1017
					localbuf[localline][localchar]=ch; 
				}
1018

1019
				if(ch==CR || (localchar>68 && ch==' ') || ++localchar>78) {
1020 1021 1022 1023 1024

					localbuf[localline][localchar]=0;
					localchar=0;

					if(sys_status&SS_SPLITP && local_y==24) {
1025
						ansi_gotoxy(1,13);
1026
						bprintf(local ? local_sep : sep
1027
							,thisnode.misc&NODE_MSGW ? 'T':' '
1028
							,sectostr(timeleft,tmp)
1029
							,thisnode.misc&NODE_NMSG ? 'M':' ');
1030 1031
						attr(cfg.color[clr_chatlocal]);
						for(x=13,y=0;x<rows;x++,y++) {
1032
							rprintf("\x1b[%d;1H\x1b[K",x+1);
1033
							if(y<=localline)
1034 1035
								bprintf("%s\r\n",localbuf[y]); 
						}
1036
						ansi_gotoxy(1,local_y=(15+localline));
1037 1038
						localline=0; 
					}