Newer
Older
/* Synchronet single-key console functions */
/* $Id$ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright Rob Swindell - http://www.synchro.net/copyright.html *
* *
* 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"
/****************************************************************************/
/* Waits for remote or local user to hit a key. Inactivity timer is checked */
/* and hangs up if inactive for 4 minutes. Returns key hit, or uppercase of */
/* key hit if mode&K_UPPER or key out of KEY BUFFER. Does not print key. */
/* Called from functions all over the place. */
/****************************************************************************/
char sbbs_t::getkey(long mode)
{
uchar ch,coldkey,c=0,spin=sbbs_random(5);
time_t last_telnet_cmd=0;
if(online==ON_REMOTE && !input_thread_running)
online=FALSE;
if(!online) {
YIELD(); // just in case someone is looping on getkey() when they shouldn't
}
if((sys_status&SS_USERON || action==NODE_DFLT) && !(mode&(K_GETSTR|K_NOSPIN)))
mode|=(useron.misc&SPIN);
lncntr=0;
timeout=time(NULL);
if(mode&K_SPIN)
outchar(' ');
do {
if(sys_status&SS_ABORT) {
if(mode&K_SPIN) /* back space once if on spinning cursor */
if(mode&K_SPIN) {
switch(c++) {
case 0:
outchar(BS);
outchar('|');
break;
case 1:
outchar(BS);
outchar('/');
break;
case 2:
outchar(BS);
outchar('-');
break;
case 3:
outchar(BS);
outchar('\\');
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
break;
}
} else {
switch(spin) {
case 0:
switch(c++) {
case 0:
outchar(BS);
outchar('');
break;
case 1:
outchar(BS);
outchar('/');
break;
case 2:
outchar(BS);
outchar('');
break;
case 3:
outchar(BS);
outchar('\\');
c=0;
break;
}
break;
case 1:
switch(c++) {
case 0:
outchar(BS);
outchar('');
break;
case 1:
outchar(BS);
outchar('');
break;
case 2:
outchar(BS);
outchar('');
break;
case 3:
outchar(BS);
outchar('');
break;
case 4:
outchar(BS);
outchar('');
break;
case 5:
outchar(BS);
outchar('');
c=0;
break;
}
break;
case 2:
switch(c++) {
case 0:
outchar(BS);
outchar('-');
break;
case 1:
outchar(BS);
outchar('=');
break;
case 2:
outchar(BS);
outchar('');
break;
case 3:
outchar(BS);
outchar('=');
c=0;
break;
}
break;
case 3:
switch(c++) {
case 0:
outchar(BS);
outchar('');
break;
case 1:
outchar(BS);
outchar('');
break;
case 2:
outchar(BS);
outchar('');
break;
case 3:
outchar(BS);
outchar('');
c=0;
break;
}
break;
case 4:
switch(c++) {
case 0:
outchar(BS);
outchar('');
break;
case 1:
outchar(BS);
outchar('');
break;
case 2:
outchar(BS);
outchar('');
break;
case 3:
outchar(BS);
outchar('');
c=0;
break;
}
break;
}
ch=inkey(mode,mode&K_SPIN ? 250:1000);
if(sys_status&SS_ABORT)
return(0);
now=time(NULL);
if(ch) {
if(mode&K_NUMBER && isprint(ch) && !isdigit(ch))
continue;
if(mode&K_ALPHA && isprint(ch) && !isalpha(ch))
continue;
if(mode&K_NOEXASC && ch&0x80)
continue;
if(mode&K_SPIN)
if(mode&K_COLD && ch>' ' && useron.misc&COLDKEYS) {
if(mode&K_UPPER)
outchar(toupper(ch));
else
outchar(ch);
while((coldkey=inkey(mode,1000))==0 && online && !(sys_status&SS_ABORT))
;
if(coldkey==BS || coldkey==DEL)
if(mode&K_UPPER)
return(toupper(ch));
if(sys_status&SS_USERON && !(sys_status&SS_LCHAT)) gettimeleft();
else if(online && now-answertime>SEC_LOGON && !(sys_status&SS_LCHAT)) {
console&=~(CON_R_ECHOX|CON_L_ECHOX);
console|=(CON_R_ECHO|CON_L_ECHO);
bputs(text[TakenTooLongToLogon]);
if(sys_status&SS_USERON && online && (timeleft/60)<(5-timeleft_warn)
&& !SYSOP && !(sys_status&SS_LCHAT)) {
timeleft_warn=5-(timeleft/60);
SAVELINE;
attr(LIGHTGRAY);
bprintf(text[OnlyXminutesLeft]
,((ushort)timeleft/60)+1,(timeleft/60) ? "s" : nulstr);
if(!(startup->options&BBS_OPT_NO_TELNET_GA)
&& now!=last_telnet_cmd && now-timeout>=60 && !((now-timeout)%60)) {
// Let's make sure the socket is up
// Sending will trigger a socket d/c detection
send_telnet_cmd(TELNET_GA,0);
last_telnet_cmd=now;
}
if(online==ON_REMOTE && !(console&CON_NO_INACT)
&& (now-timeout >= cfg.sec_warn || now-timeout >= cfg.sec_hangup)) {
if(sys_status&SS_USERON && cfg.sec_warn < cfg.sec_hangup) {

rswindell
committed
bputs(text[AreYouThere]);
}
while(!inkey(K_NONE,100) && online && now-timeout < cfg.sec_hangup) {
}
if(now-timeout >= cfg.sec_hangup) {
if(online==ON_REMOTE) {
console|=CON_R_ECHO;
console&=~CON_R_ECHOX;

rswindell
committed
}
bputs(text[CallBackWhenYoureThere]);
logline(LOG_NOTICE,nulstr,"Inactive");
hangup();
return(0);

rswindell
committed
}
if(sys_status&SS_USERON) {

rswindell
committed
RESTORELINE;
}
timeout=now;
}
return(0);
}
/****************************************************************************/
/* Outputs a string highlighting characters preceded by a tilde */
/****************************************************************************/
void sbbs_t::mnemonics(const char *str)
long l;
if(!strchr(str,'~')) {
mnestr=str;
bputs(str);
ctrl_a_codes=strchr(str,1);
if(!ctrl_a_codes) {
if(str[0]=='@' && str[strlen(str)-1]=='@' && !strchr(str,' ')) {
return;
}
attr(cfg.color[clr_mnelow]);
}
if(str[l]=='~' && str[l+1]!=0) {
outchar('(');
l++;
if(!ctrl_a_codes)
attr(cfg.color[clr_mnehigh]);
outchar(')');
if(!ctrl_a_codes)
attr(cfg.color[clr_mnelow]);
}
if(str[l]==CTRL_A && str[l+1]!=0) {
l++;
if(str[l] == 'Z') /* EOF (uppercase 'Z') */
break;
} else {
if(str[l] == '@') {
int i = show_atcode(str + l);
if(i) {
l += i;
continue;
}
}
outchar(str[l++]);
}
if(!ctrl_a_codes)
attr(cfg.color[clr_mnecmd]);
}
/****************************************************************************/
/* Prompts user for Y or N (yes or no) and CR is interpreted as a Y */
/* Called from quite a few places */
/****************************************************************************/
bool sbbs_t::yesno(const char *str, long mode)
bprintf(mode, text[YesNoQuestion], str);
while(online) {
if(sys_status&SS_ABORT)
ch=text[YNQP][1];
else
ch=getkey(K_UPPER|K_COLD);
if(ch==text[YNQP][0] || ch==CR) {
if(bputs(text[Yes], mode) && !(mode&P_NOCRLF))
if(!(mode&P_SAVEATR))
attr(LIGHTGRAY);
if(ch==text[YNQP][1]) {
if(bputs(text[No], mode) && !(mode&P_NOCRLF))
if(!(mode&P_SAVEATR))
attr(LIGHTGRAY);
return(false);
}
}
return(true);
}
/****************************************************************************/
/* Prompts user for N or Y (no or yes) and CR is interpreted as a N */
/****************************************************************************/
bool sbbs_t::noyes(const char *str, long mode)
return true;
bprintf(mode, text[NoYesQuestion], str);
while(online) {
if(sys_status&SS_ABORT)
ch=text[YNQP][1];
else
ch=getkey(K_UPPER|K_COLD);
if(ch==text[YNQP][1] || ch==CR) {
if(bputs(text[No], mode) && !(mode&P_NOCRLF))
if(!(mode&P_SAVEATR))
attr(LIGHTGRAY);
if(ch==text[YNQP][0]) {
if(bputs(text[Yes], mode) && !(mode&P_NOCRLF))
if(!(mode&P_SAVEATR))
attr(LIGHTGRAY);
return(false);
}
}
return(true);
}
/****************************************************************************/
/* Waits for remote or local user to hit a key among 'keys'. */
/* If 'keys' is NULL, *any* non-numeric key is valid input. */
/* 'max' is non-zero, allow that a decimal number input up to that size */
/* and return the value OR'd with 0x80000000. */
/* default mode value is K_UPPER */
/****************************************************************************/
long sbbs_t::getkeys(const char *keys, ulong max, long mode)
uchar ch,n=0,c=0;

rswindell
committed
ulong i=0;
if(keys != NULL) {
SAFECOPY(str,keys);
}
ch=getkey(mode);
if(max && ch>0x7f) /* extended ascii chars are digits to isdigit() */
continue;
if(sys_status&SS_ABORT) { /* return -1 if Ctrl-C hit */
if(!(mode&(K_NOECHO|K_NOCRLF))) {
attr(LIGHTGRAY);
CRLF;
}
if(ch && !n && ((keys == NULL && !isdigit(ch)) || (strchr(str,ch)))) { /* return character if in string */
if(ch > ' ') {
if(!(mode&K_NOECHO))
outchar(ch);
if(useron.misc&COLDKEYS) {
while(online && !(sys_status&SS_ABORT)) {
c=getkey(0);
if(c==CR || c==BS || c==DEL)
break;
}
if(sys_status&SS_ABORT) {
if(!(mode&(K_NOECHO|K_NOCRLF))) {
CRLF;
}
return(-1);
}
if(c==BS || c==DEL) {
if(!(mode&K_NOECHO))
backspace();
continue;
}
if(!(mode&(K_NOECHO|K_NOCRLF))) {
attr(LIGHTGRAY);
CRLF;
}
lncntr=0;
if(ch==CR && max) { /* return 0 if no number */
if(!(mode&(K_NOECHO|K_NOCRLF))) {
attr(LIGHTGRAY);
CRLF;
}
lncntr=0;
if(n)
return(i|0x80000000L); /* return number plus high bit */
return(0);
}
if((ch==BS || ch==DEL) && n) {
if(!(mode&K_NOECHO))
backspace();
else if(max && isdigit(ch) && (i*10)+(ch&0xf)<=max && (ch!='0' || n)) {
i*=10;
n++;
i+=ch&0xf;
if(!(mode&K_NOECHO))
outchar(ch);
if(i*10>max && !(useron.misc&COLDKEYS) && keybuf_level() < 1) {
if(!(mode&(K_NOECHO|K_NOCRLF))) {
attr(LIGHTGRAY);
CRLF;
}
return(i|0x80000000L);
}
}
}
return(-1);
}
/****************************************************************************/
/* Prints PAUSE message and waits for a key stoke */
/****************************************************************************/
void sbbs_t::pause()
{
char ch;
uint tempattrs=curatr; /* was lclatr(-1) */
size_t len;
lncntr=0;
if(online==ON_REMOTE)
rioctl(IOFI);
bputs(text[Pause]);
len = bstrlen(text[Pause]);
if(sys_status&SS_USERON && !(useron.misc&(HTML|WIP|NOPAUSESPIN))
&& !(cfg.node_misc&NM_NOPAUSESPIN))
l|=K_SPIN;
ch=getkey(l);
if(ch==text[YNQP][1] || ch==text[YNQP][2])
sys_status|=SS_ABORT;
else if(ch==LF) // down arrow == display one more line
lncntr=rows-2;
if(text[Pause][0]!='@')
backspace(len);
getnodedat(cfg.node_num,&thisnode,0);
nodesync();
attr(tempattrs);
}
/****************************************************************************/
/* Puts a character into the input buffer */
/****************************************************************************/

rswindell
committed
void sbbs_t::ungetkey(char ch, bool insert)
#if 0 /* this way breaks ansi_getxy() */
RingBufWrite(&inbuf,(uchar*)&ch,sizeof(uchar));
#else
if(keybuf_space()) {
if(insert) {
if(keybufbot == 0)
keybufbot = KEY_BUFSIZE - 1;
else
keybufbot--;
keybuf[keybufbot] = ch;
} else {
keybuf[keybuftop++]=ch;
if(keybuftop==KEY_BUFSIZE)
keybuftop=0;
}

rswindell
committed
}
#endif
/****************************************************************************/
/* Puts a string into the input buffer */
/****************************************************************************/
void sbbs_t::ungetstr(const char* str, bool insert)
{
size_t i;
for(i = 0; str[i] != '\0'; i++)
ungetkey(str[i], insert);
}
size_t sbbs_t::keybuf_space(void)
{
return sizeof(keybuf) - (keybuf_level() + 1);
}
size_t sbbs_t::keybuf_level(void)
{
if(keybufbot > keybuftop)
return (sizeof(keybuf) - keybufbot) + keybuftop;
return keybuftop - keybufbot;
}