diff options
Diffstat (limited to 'src/HTAlert.c')
| -rw-r--r-- | src/HTAlert.c | 358 |
1 files changed, 228 insertions, 130 deletions
diff --git a/src/HTAlert.c b/src/HTAlert.c index 74d8d2b8..98f53c86 100644 --- a/src/HTAlert.c +++ b/src/HTAlert.c @@ -10,7 +10,6 @@ */ #include <HTUtils.h> -#include <tcp.h> #include <HTAlert.h> #include <LYGlobalDefs.h> #include <LYCurses.h> @@ -22,27 +21,58 @@ #include <LYLeaks.h> -#define FREE(x) if (x) {free(x); x = NULL;} - - /* Issue a message about a problem. HTAlert() ** -------------------------------- */ PUBLIC void HTAlert ARGS1( CONST char *, Msg) { - if (TRACE) { - fprintf(stderr, "\nAlert!: %s", Msg); - fflush(stderr); - _user_message("Alert!: %s", Msg); - fprintf(stderr, "\n\n"); - fflush(stderr); - } else - _user_message("Alert!: %s", Msg); + CTRACE(tfp, "\nAlert!: %s\n\n", Msg); + CTRACE_FLUSH(tfp); + _user_message("Alert!: %s", Msg); sleep(AlertSecs); } +/* Issue an informational message. HTInfoMsg() +** -------------------------------- +*/ +PUBLIC void HTInfoMsg ARGS1( + CONST char *, Msg) +{ + _statusline(Msg); + if (Msg && *Msg) { + CTRACE(tfp, "Info message: %s\n", Msg); + sleep(InfoSecs); + } +} + +/* Issue an important message. HTUserMsg() +** -------------------------------- +*/ +PUBLIC void HTUserMsg ARGS1( + CONST char *, Msg) +{ + _statusline(Msg); + if (Msg && *Msg) { + CTRACE(tfp, "User message: %s\n", Msg); + sleep(MessageSecs); + } +} + +PUBLIC void HTUserMsg2 ARGS2( + CONST char *, Msg2, + CONST char *, Arg) +{ + _user_message(Msg2, Arg); + if (Msg2 && *Msg2) { + CTRACE(tfp, "User message: "); + CTRACE(tfp, Msg2, Arg); + CTRACE(tfp, "\n"); + sleep(MessageSecs); + } +} + /* Issue a progress message. HTProgress() ** ------------------------- */ @@ -50,9 +80,69 @@ PUBLIC void HTProgress ARGS1( CONST char *, Msg) { if (TRACE) - fprintf(stderr, "%s\n", Msg); + fprintf(tfp, "%s\n", Msg); else - statusline(Msg); + statusline(Msg); +} + +/* Issue a read-progress message. HTReadProgress() +** ------------------------------ +*/ +PUBLIC void HTReadProgress ARGS2( + long, bytes, + long, total) +{ + static long kb_units = 1024; + static time_t first, last; + static long bytes_last; + long transfer_rate, divisor; + char line[80]; + time_t now = time((time_t *)0); /* once per second */ + char *units = "bytes"; + + if (bytes == 0) { + first = last = now; + bytes_last = bytes; + } else if ((bytes > 0) && + (now != first)) + /* 1 sec delay for transfer_rate calculation :-( */ { + transfer_rate = (bytes) / (now - first); /* bytes/sec */ + + /* optimal refresh time: every 0.2 sec */ + if ((bytes - bytes_last) > (transfer_rate / 5)) { + + bytes_last += (transfer_rate / 5); /* until we got next second */ + + if (now != last) { + last = now; + bytes_last = bytes; + } + units = gettext("bytes"); + divisor = 1; + if (LYshow_kb_rate + && (total >= kb_units || bytes >= kb_units)) { + units = gettext("KB"); + divisor = kb_units; + bytes /= divisor; + if (total > 0) total /= divisor; + } + + if (total > 0) + sprintf (line, gettext("Read %ld of %ld %s of data"), bytes, total, units); + else + sprintf (line, gettext("Read %ld %s of data"), bytes, units); + if ((transfer_rate > 0) + && (!LYshow_kb_rate || (bytes * divisor >= kb_units))) + sprintf (line + strlen(line), gettext(", %ld %s/sec."), transfer_rate / divisor, units); + else + sprintf (line + strlen(line), "."); + if (total < 0) { + if (total < -1) + strcat(line, gettext(" (Press 'z' to abort)")); + } + HTProgress(line); + } + } } PRIVATE BOOL conf_cancelled = NO; /* used by HTConfirm only - kw */ @@ -80,9 +170,9 @@ PUBLIC BOOL HTConfirm ARGS1(CONST char *, Msg) #ifdef VMS extern BOOLEAN HadVMSInterrupt; #endif /* VMS */ - - _user_message("%s (y/n) ", Msg); - + + _user_message(gettext("%s (y/n) "), Msg); + while (1) { c = LYgetch(); #ifdef VMS @@ -115,11 +205,11 @@ PUBLIC char * HTPrompt ARGS2( Tmp[199] = '\0'; _statusline(Msg); - if (deflt) - strncpy(Tmp, deflt, 199); + if (deflt) + strncpy(Tmp, deflt, 199); if (!dump_output_immediately) - LYgetstr(Tmp, VISIBLE, sizeof(Tmp), NORECALL); + LYgetstr(Tmp, VISIBLE, sizeof(Tmp), NORECALL); StrAllocCopy(rep, Tmp); @@ -139,37 +229,37 @@ PUBLIC char * HTPromptPassword ARGS1( pw[0] = '\0'; if (!dump_output_immediately) { - _statusline(Msg ? Msg : PASSWORD_PROMPT); - LYgetstr(pw, HIDDEN, sizeof(pw), NORECALL); /* hidden */ - StrAllocCopy(result, pw); + _statusline(Msg ? Msg : PASSWORD_PROMPT); + LYgetstr(pw, HIDDEN, sizeof(pw), NORECALL); /* hidden */ + StrAllocCopy(result, pw); } else { - printf("\n%s\n", PASSWORD_REQUIRED); + printf("\n%s\n", PASSWORD_REQUIRED); StrAllocCopy(result, ""); } return result; } -/* Prompt both username and password. HTPromptUsernameAndPassword() -** ---------------------------------- +/* Prompt both username and password. HTPromptUsernameAndPassword() +** ---------------------------------- ** ** On entry, -** Msg is the prompting message. -** *username and -** *password are char pointers which contain default +** Msg is the prompting message. +** *username and +** *password are char pointers which contain default ** or zero-length strings; they are changed -** to point to result strings. -** IsProxy should be TRUE if this is for +** to point to result strings. +** IsProxy should be TRUE if this is for ** proxy authentication. ** -** If *username is not NULL, it is taken -** to point to a default value. -** Initial value of *password is -** completely discarded. +** If *username is not NULL, it is taken +** to point to a default value. +** Initial value of *password is +** completely discarded. ** ** On exit, -** *username and *password point to newly allocated -** strings -- original strings pointed to by them -** are NOT freed. +** *username and *password point to newly allocated +** strings -- original strings pointed to by them +** are NOT freed. ** */ PUBLIC void HTPromptUsernameAndPassword ARGS4( @@ -182,12 +272,12 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( authentication_info[0] && authentication_info[1]) || (IsProxy == TRUE && proxyauth_info[0] && proxyauth_info[1])) { - /* + /* ** The -auth or -pauth parameter gave us both the username ** and password to use for the first realm or proxy server, ** respectively, so just use them without any prompting. - FM */ - StrAllocCopy(*username, (IsProxy ? + StrAllocCopy(*username, (IsProxy ? proxyauth_info[0] : authentication_info[0])); if (IsProxy) { FREE(proxyauth_info[0]); @@ -202,15 +292,15 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( FREE(authentication_info[1]); } } else if (dump_output_immediately) { - /* + /* * We are not interactive and don't have both the * username and password from the command line, * but might have one or the other. - FM */ - if ((IsProxy == FALSE && authentication_info[0]) || + if ((IsProxy == FALSE && authentication_info[0]) || (IsProxy == TRUE && proxyauth_info[0])) { /* - ** Use the command line username. - FM + ** Use the command line username. - FM */ StrAllocCopy(*username, (IsProxy ? proxyauth_info[0] : authentication_info[0])); @@ -221,14 +311,14 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( } } else { /* - ** Default to "WWWuser". - FM + ** Default to "WWWuser". - FM */ - StrAllocCopy(*username, "WWWuser"); + StrAllocCopy(*username, "WWWuser"); } - if ((IsProxy == FALSE && authentication_info[1]) || + if ((IsProxy == FALSE && authentication_info[1]) || (IsProxy == TRUE && proxyauth_info[1])) { /* - ** Use the command line password. - FM + ** Use the command line password. - FM */ StrAllocCopy(*password, (IsProxy ? proxyauth_info[1] : authentication_info[1])); @@ -239,23 +329,23 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( } } else { /* - ** Default to a zero-length string. - FM + ** Default to a zero-length string. - FM */ StrAllocCopy(*password, ""); } printf("\n%s\n", USERNAME_PASSWORD_REQUIRED); } else { - /* + /* * We are interactive and don't have both the * username and password from the command line, * but might have one or the other. - FM */ - if ((IsProxy == FALSE && authentication_info[0]) || + if ((IsProxy == FALSE && authentication_info[0]) || (IsProxy == TRUE && proxyauth_info[0])) { /* - ** Offer the command line username in the - ** prompt for the first realm. - FM + ** Offer the command line username in the + ** prompt for the first realm. - FM */ StrAllocCopy(*username, (IsProxy ? proxyauth_info[0] : authentication_info[0])); @@ -273,10 +363,10 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( } else { *username = HTPrompt(USERNAME_PROMPT, *username); } - if ((IsProxy == FALSE && authentication_info[1]) || + if ((IsProxy == FALSE && authentication_info[1]) || (IsProxy == TRUE && proxyauth_info[1])) { /* - ** Use the command line password for the first realm. - FM + ** Use the command line password for the first realm. - FM */ StrAllocCopy(*password, (IsProxy ? proxyauth_info[1] : authentication_info[1])); @@ -287,13 +377,13 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( } } else if (*username != NULL && *username[0] != '\0') { /* - ** We have a non-zero length username, - ** so prompt for the password. - FM + ** We have a non-zero length username, + ** so prompt for the password. - FM */ *password = HTPromptPassword(PASSWORD_PROMPT); } else { /* - ** Return a zero-length password. - FM + ** Return a zero-length password. - FM */ StrAllocCopy(*password, ""); } @@ -314,11 +404,9 @@ PUBLIC void HTPromptUsernameAndPassword ARGS4( ** Returns FALSE on cancel, ** TRUE if the cookie should be set. */ -PUBLIC BOOL HTConfirmCookie ARGS6( - void *, dp, +PUBLIC BOOL HTConfirmCookie ARGS4( + void *, dp, CONST char *, server, - CONST char *, domain, - CONST char *, path, CONST char *, name, CONST char *, value) { @@ -331,57 +419,70 @@ PUBLIC BOOL HTConfirmCookie ARGS6( #endif /* VMS */ if ((de = (domain_entry *)dp) == NULL) - return FALSE; - + return FALSE; + +#ifdef ENHANCED_COOKIES + /* If the user has specified a list of domains to allow or deny + ** from the config file, then they'll already have de->bv set to + ** ACCEPT_ALWAYS or REJECT_ALWAYS so we can relax and let the + ** default cookie handling code cope with this fine. I hope. + */ +#endif /* - ** If the user has specified a constant action, don't prompt at all. + ** If the user has specified a constant action, don't prompt at all. */ - if (de->bv == ACCEPT_ALWAYS) - return TRUE; + if (de->bv == ACCEPT_ALWAYS || de->bv == FROM_FILE) + return TRUE; if (de->bv == REJECT_ALWAYS) - return FALSE; + return FALSE; if (dump_output_immediately) { - /* + /* ** Non-interactive, can't respond. Use the LYSetCookies value * based on its compilation or configuration setting, or on the ** command line toggle. - FM */ - return LYSetCookies; + return LYSetCookies; } /* - ** Figure out how much of the cookie we can show. - ** The '37' is the length of ADVANCED_COOKIE_CONFIRMATION, - ** minus the length of the %s directives (10 chars) + ** Estimate how much of the cookie we can show. */ if (de != NULL) { - if (de->bv == ACCEPT_ALWAYS) + if (de->bv == ACCEPT_ALWAYS) return TRUE; - if (de->bv == REJECT_ALWAYS) + if (de->bv == REJECT_ALWAYS) return FALSE; } - space_free = (((LYcols - 1) - 37) - strlen(server)); + space_free = (((LYcols - 1) + - strlen(ADVANCED_COOKIE_CONFIRMATION)) + - strlen(server)); if (space_free < 0) - space_free = 0; + space_free = 0; namelen = strlen(name); valuelen = strlen(value); if ((namelen + valuelen) > space_free) { - /* + /* ** Argh... there isn't enough space on our single line for ** the whole cookie. Reduce them both by a percentage. ** This should be smarter. */ - float percentage; - percentage = (float)space_free/(float)(namelen + valuelen); - namelen = (int)(percentage*(float)namelen); - valuelen = (int)(percentage*(float)valuelen); + float percentage; + percentage = (float)space_free/(float)(namelen + valuelen); + namelen = (int)(percentage*(float)namelen); + valuelen = (int)(percentage*(float)valuelen); + } + if(!LYAcceptAllCookies) { + sprintf(message, ADVANCED_COOKIE_CONFIRMATION, + server, namelen, name, valuelen, value); + _statusline(message); } - sprintf(message, ADVANCED_COOKIE_CONFIRMATION, - server, namelen, name, valuelen, value); - _statusline(message); while (1) { - ch = LYgetch(); + if(!LYAcceptAllCookies) { + ch = LYgetch(); + } else { + ch = 'A'; + } #ifdef VMS if (HadVMSInterrupt) { HadVMSInterrupt = FALSE; @@ -390,56 +491,52 @@ PUBLIC BOOL HTConfirmCookie ARGS6( #endif /* VMS */ switch(TOUPPER(ch)) { case 'A': - /* + /* ** Set to accept all cookies for this domain. */ de->bv = ACCEPT_ALWAYS; - _user_message(ALWAYS_ALLOWING_COOKIES, de->domain); - sleep(MessageSecs); + HTUserMsg2(ALWAYS_ALLOWING_COOKIES, de->domain); return TRUE; case 'N': case 7: /* Ctrl-G */ case 3: /* Ctrl-C */ - /* + /* ** Reject the cookie. */ - _statusline(REJECTING_COOKIE); - sleep(MessageSecs); + HTUserMsg(REJECTING_COOKIE); return FALSE; - case 'V': - /* + case 'V': + /* ** Set to reject all cookies from this domain. */ de->bv = REJECT_ALWAYS; - _user_message(NEVER_ALLOWING_COOKIES, de->domain); - sleep(MessageSecs); + HTUserMsg2(NEVER_ALLOWING_COOKIES, de->domain); return FALSE; case 'Y': - /* + /* ** Accept the cookie. */ - _statusline(ALLOWING_COOKIE); - sleep(InfoSecs); + HTInfoMsg(ALLOWING_COOKIE); return TRUE; default: - continue; + continue; } } } -/* Confirm redirection of POST. HTConfirmPostRedirect() +/* Confirm redirection of POST. HTConfirmPostRedirect() ** ---------------------------- ** ** On entry, -** Redirecting_url is the Location. +** Redirecting_url is the Location. ** server_status is the server status code. ** ** On exit, -** Returns 0 on cancel, +** Returns 0 on cancel, ** 1 for redirect of POST with content, ** 303 for redirect as GET without content */ @@ -451,11 +548,11 @@ PUBLIC int HTConfirmPostRedirect ARGS2( char StatusInfo[256]; char url[256]; int on_screen = 0; /* 0 - show menu - * 1 - show url + * 1 - show url * 2 - menu is already on screen */ if (server_status == 303 || - server_status == 302) { + server_status == 302) { /* * HTTP.c should not have called us for either of * these because we're treating 302 as historical, @@ -464,34 +561,35 @@ PUBLIC int HTConfirmPostRedirect ARGS2( return 303; } - if (dump_output_immediately) - if (server_status == 301) { + if (dump_output_immediately) { + if (server_status == 301) { /* - ** Treat 301 as historical, i.e., like 303 (GET - ** without content), when not interactive. - FM + ** Treat 301 as historical, i.e., like 303 (GET + ** without content), when not interactive. - FM */ - return 303; - } else { + return 303; + } else { /* - ** Treat anything else (e.g., 305, 306 or 307) as too - ** dangerous to redirect without confirmation, and thus - ** cancel when not interactive. - FM + ** Treat anything else (e.g., 305, 306 or 307) as too + ** dangerous to redirect without confirmation, and thus + ** cancel when not interactive. - FM */ return 0; } + } StatusInfo[254] = StatusInfo[255] = '\0'; url[254] = url[(LYcols < 250 ? LYcols-1 : 255)] = '\0'; if (user_mode == NOVICE_MODE) { - on_screen = 2; - move(LYlines-2, 0); - sprintf(StatusInfo, SERVER_ASKED_FOR_REDIRECTION, server_status); + on_screen = 2; + move(LYlines-2, 0); + sprintf(StatusInfo, SERVER_ASKED_FOR_REDIRECTION, server_status); addstr(StatusInfo); clrtoeol(); - move(LYlines-1, 0); + move(LYlines-1, 0); sprintf(url, "URL: %.*s", (LYcols < 250 ? LYcols-6 : 250), Redirecting_url); - addstr(url); + addstr(url); clrtoeol(); if (server_status == 301) { _statusline(PROCEED_GET_CANCEL); @@ -509,14 +607,14 @@ PUBLIC int HTConfirmPostRedirect ARGS2( StrAllocCat(show_POST_url, Redirecting_url); } while (1) { - int c; + int c; switch (on_screen) { case 0: _statusline(StatusInfo); break; case 1: - _statusline(show_POST_url); + _statusline(show_POST_url); } c = LYgetch(); switch (TOUPPER(c)) { @@ -525,22 +623,22 @@ PUBLIC int HTConfirmPostRedirect ARGS2( ** Proceed with 301 or 307 redirect of POST ** with same method and POST content. - FM */ - FREE(show_POST_url); - return 1; + FREE(show_POST_url); + return 1; - case 7: - case 'C': - /* + case 7: + case 'C': + /* ** Cancel request. */ - FREE(show_POST_url); + FREE(show_POST_url); return 0; case 'U': - /* + /* ** Show URL for intermediate or advanced mode. */ - if (user_mode != NOVICE_MODE) { + if (user_mode != NOVICE_MODE) { if (on_screen == 1) { on_screen = 0; } else { @@ -552,7 +650,7 @@ PUBLIC int HTConfirmPostRedirect ARGS2( case 'G': if (server_status == 301) { /* - ** Treat as 303 (GET without content). + ** Treat as 303 (GET without content). */ FREE(show_POST_url); return 303; @@ -560,7 +658,7 @@ PUBLIC int HTConfirmPostRedirect ARGS2( /* fall through to default */ default: - /* + /* ** Get another character. */ if (on_screen == 1) { |