home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lynx2.8.1dev.10.tar.gz / lynx2.8.1dev.10.tar / lynx2-8 / src / HTAlert.c < prev    next >
C/C++ Source or Header  |  1998-05-02  |  14KB  |  574 lines

  1. /*    Displaying messages and getting input for Lynx Browser
  2. **    ==========================================================
  3. **
  4. **    REPLACE THIS MODULE with a GUI version in a GUI environment!
  5. **
  6. ** History:
  7. **       Jun 92 Created May 1992 By C.T. Barker
  8. **       Feb 93 Simplified, portablised TBL
  9. **
  10. */
  11.  
  12. #include <HTUtils.h>
  13. #include <tcp.h>
  14. #include <HTAlert.h>
  15. #include <LYGlobalDefs.h>
  16. #include <LYCurses.h>
  17. #include <LYStrings.h>
  18. #include <LYUtils.h>
  19. #include <LYSignal.h>
  20. #include <GridText.h>
  21. #include <LYCookie.h>
  22.  
  23. #include <LYLeaks.h>
  24.  
  25. #define FREE(x) if (x) {free(x); x = NULL;}
  26.  
  27.  
  28. /*    Issue a message about a problem.        HTAlert()
  29. **    --------------------------------
  30. */
  31. PUBLIC void HTAlert ARGS1(
  32.     CONST char *,    Msg)
  33. {
  34.     if (TRACE) {
  35.         fprintf(stderr, "\nAlert!: %s", Msg);
  36.     fflush(stderr);
  37.         _user_message("Alert!: %s", Msg);
  38.         fprintf(stderr, "\n\n");
  39.     fflush(stderr);
  40.     } else
  41.         _user_message("Alert!: %s", Msg);
  42.  
  43.     sleep(AlertSecs);
  44. }
  45.  
  46. /*    Issue a progress message.            HTProgress()
  47. **    -------------------------
  48. */
  49. PUBLIC void HTProgress ARGS1(
  50.     CONST char *,    Msg)
  51. {
  52.     if (TRACE)
  53.         fprintf(tfp, "%s\n", Msg);
  54.     else
  55.         statusline(Msg);
  56. }
  57.  
  58. PRIVATE BOOL conf_cancelled = NO; /* used by HTConfirm only - kw */
  59.  
  60. PUBLIC BOOL HTLastConfirmCancelled NOARGS
  61. {
  62.     if (conf_cancelled) {
  63.     conf_cancelled = NO;    /* reset */
  64.     return(YES);
  65.     } else {
  66.     return(NO);
  67.     }
  68. }
  69.  
  70. /*    Seek confirmation.                HTConfirm()
  71. **    ------------------
  72. */
  73. PUBLIC BOOL HTConfirm ARGS1(CONST char *, Msg)
  74. {
  75.     conf_cancelled = NO;
  76.     if (dump_output_immediately) { /* Non-interactive, can't respond */
  77.     return(NO);
  78.     } else {
  79.     int c;
  80. #ifdef VMS
  81.     extern BOOLEAN HadVMSInterrupt;
  82. #endif /* VMS */
  83.     
  84.     _user_message("%s (y/n) ", Msg);
  85.     
  86.     while (1) {
  87.         c = LYgetch();
  88. #ifdef VMS
  89.         if (HadVMSInterrupt) {
  90.         HadVMSInterrupt = FALSE;
  91.         c = 'N';
  92.         }
  93. #endif /* VMS */
  94.         if (TOUPPER(c) == 'Y')
  95.         return(YES);
  96.         if (c == 7 || c == 3) /* remember we had ^G or ^C */
  97.         conf_cancelled = YES;
  98.         if (TOUPPER(c) == 'N' || c == 7 || c == 3) /* ^G or ^C cancels */
  99.         return(NO);
  100.     }
  101.     }
  102. }
  103.  
  104. /*    Prompt for answer and get text back.        HTPrompt()
  105. **    ------------------------------------
  106. */
  107. PUBLIC char * HTPrompt ARGS2(
  108.     CONST char *,    Msg,
  109.     CONST char *,    deflt)
  110. {
  111.     char * rep = NULL;
  112.     char Tmp[200];
  113.  
  114.     Tmp[0] = '\0';
  115.     Tmp[199] = '\0';
  116.  
  117.     _statusline(Msg);
  118.     if (deflt) 
  119.         strncpy(Tmp, deflt, 199);
  120.  
  121.     if (!dump_output_immediately)
  122.         LYgetstr(Tmp, VISIBLE, sizeof(Tmp), NORECALL);
  123.  
  124.     StrAllocCopy(rep, Tmp);
  125.  
  126.     return rep;
  127. }
  128.  
  129. /*
  130. **    Prompt for password without echoing the reply.    HTPromptPassword()
  131. **    ----------------------------------------------
  132. */
  133. PUBLIC char * HTPromptPassword ARGS1(
  134.     CONST char *,    Msg)
  135. {
  136.     char *result = NULL;
  137.     char pw[120];
  138.  
  139.     pw[0] = '\0';
  140.  
  141.     if (!dump_output_immediately) {
  142.         _statusline(Msg ? Msg : PASSWORD_PROMPT);
  143.         LYgetstr(pw, HIDDEN, sizeof(pw), NORECALL); /* hidden */
  144.         StrAllocCopy(result, pw);
  145.     } else {
  146.         printf("\n%s\n", PASSWORD_REQUIRED);
  147.     StrAllocCopy(result, "");
  148.     }
  149.     return result;
  150. }
  151.  
  152. /*         Prompt both username and password.       HTPromptUsernameAndPassword()
  153. **      ----------------------------------
  154. **
  155. **  On entry,
  156. **      Msg             is the prompting message.
  157. **      *username and
  158. **      *password       are char pointers which contain default
  159. **            or zero-length strings; they are changed
  160. **                      to point to result strings.
  161. **    IsProxy        should be TRUE if this is for
  162. **            proxy authentication.
  163. **
  164. **                      If *username is not NULL, it is taken
  165. **                      to point to a default value.
  166. **                      Initial value of *password is
  167. **                      completely discarded.
  168. **
  169. **  On exit,
  170. **      *username and *password point to newly allocated
  171. **      strings -- original strings pointed to by them
  172. **      are NOT freed.
  173. **
  174. */
  175. PUBLIC void HTPromptUsernameAndPassword ARGS4(
  176.     CONST char *,    Msg,
  177.     char **,    username,
  178.     char **,    password,
  179.     BOOL,        IsProxy)
  180. {
  181.     if ((IsProxy == FALSE &&
  182.      authentication_info[0] && authentication_info[1]) ||
  183.     (IsProxy == TRUE &&
  184.      proxyauth_info[0] && proxyauth_info[1])) {
  185.     /* 
  186.     **  The -auth or -pauth parameter gave us both the username
  187.     **  and password to use for the first realm or proxy server,
  188.     **  respectively, so just use them without any prompting. - FM
  189.     */
  190.      StrAllocCopy(*username, (IsProxy ?
  191.                proxyauth_info[0] : authentication_info[0]));
  192.     if (IsProxy) {
  193.         FREE(proxyauth_info[0]);
  194.     } else {
  195.         FREE(authentication_info[0]);
  196.     }
  197.     StrAllocCopy(*password, (IsProxy ?
  198.                proxyauth_info[1] : authentication_info[1]));
  199.     if (IsProxy) {
  200.         FREE(proxyauth_info[1]);
  201.     } else {
  202.         FREE(authentication_info[1]);
  203.     }
  204.     } else if (dump_output_immediately) {
  205.         /*
  206.      *  We are not interactive and don't have both the
  207.      *  username and password from the command line,
  208.      *  but might have one or the other. - FM
  209.      */
  210.         if ((IsProxy == FALSE && authentication_info[0]) ||
  211.         (IsProxy == TRUE && proxyauth_info[0])) {
  212.         /*
  213.         **  Use the command line username. - FM
  214.         */
  215.         StrAllocCopy(*username, (IsProxy ?
  216.                proxyauth_info[0] : authentication_info[0]));
  217.         if (IsProxy) {
  218.         FREE(proxyauth_info[0]);
  219.         } else {
  220.         FREE(authentication_info[0]);
  221.         }
  222.     } else {
  223.         /*
  224.         **  Default to "WWWuser". - FM
  225.         */
  226.             StrAllocCopy(*username, "WWWuser");
  227.     }
  228.         if ((IsProxy == FALSE && authentication_info[1]) ||
  229.         (IsProxy == TRUE && proxyauth_info[1])) {
  230.         /*
  231.         **  Use the command line password. - FM
  232.         */
  233.         StrAllocCopy(*password, (IsProxy ?
  234.                proxyauth_info[1] : authentication_info[1]));
  235.         if (IsProxy) {
  236.         FREE(proxyauth_info[1]);
  237.         } else {
  238.         FREE(authentication_info[1]);
  239.         }
  240.     } else {
  241.         /*
  242.         **  Default to a zero-length string. - FM
  243.         */
  244.         StrAllocCopy(*password, "");
  245.     }
  246.     printf("\n%s\n", USERNAME_PASSWORD_REQUIRED);
  247.  
  248.     } else {
  249.         /*
  250.      *  We are interactive and don't have both the
  251.      *  username and password from the command line,
  252.      *  but might have one or the other. - FM
  253.      */
  254.         if ((IsProxy == FALSE && authentication_info[0]) ||
  255.         (IsProxy == TRUE && proxyauth_info[0])) {
  256.         /*
  257.         **  Offer the command line username in the
  258.         **  prompt for the first realm. - FM
  259.         */
  260.         StrAllocCopy(*username, (IsProxy ?
  261.                proxyauth_info[0] : authentication_info[0]));
  262.         if (IsProxy) {
  263.         FREE(proxyauth_info[0]);
  264.         } else {
  265.         FREE(authentication_info[0]);
  266.         }
  267.     }
  268.     /*
  269.      *  Prompt for confirmation or entry of the username. - FM
  270.      */
  271.     if (Msg != NULL) {
  272.         *username = HTPrompt(Msg, *username);
  273.     } else {
  274.         *username = HTPrompt(USERNAME_PROMPT, *username);
  275.     }
  276.         if ((IsProxy == FALSE && authentication_info[1]) ||
  277.         (IsProxy == TRUE && proxyauth_info[1])) {
  278.         /*
  279.         **  Use the command line password for the first realm. - FM
  280.         */
  281.         StrAllocCopy(*password, (IsProxy ?
  282.                proxyauth_info[1] : authentication_info[1]));
  283.         if (IsProxy) {
  284.         FREE(proxyauth_info[1]);
  285.         } else {
  286.         FREE(authentication_info[1]);
  287.         }
  288.     } else if (*username != NULL && *username[0] != '\0') {
  289.         /*
  290.         **  We have a non-zero length username,
  291.         **  so prompt for the password. - FM
  292.         */
  293.         *password = HTPromptPassword(PASSWORD_PROMPT);
  294.     } else {
  295.         /*
  296.         **  Return a zero-length password. - FM
  297.         */
  298.         StrAllocCopy(*password, "");
  299.     }
  300.     }
  301. }
  302.  
  303. /*    Confirm a cookie operation.            HTConfirmCookie()
  304. **    ---------------------------
  305. **
  306. **  On entry,
  307. **    server            is the server sending the Set-Cookie.
  308. **    domain            is the domain of the cookie.
  309. **    path            is the path of the cookie.
  310. **    name            is the name of the cookie.
  311. **    value            is the value of the cookie.
  312. **
  313. **  On exit,
  314. **    Returns FALSE on cancel,
  315. **        TRUE if the cookie should be set.
  316. */
  317. PUBLIC BOOL HTConfirmCookie ARGS6(
  318.     void *,        dp,
  319.     CONST char *,    server,
  320.     CONST char *,    domain,
  321.     CONST char *,    path,
  322.     CONST char *,    name,
  323.     CONST char *,    value)
  324. {
  325.     char message[256];
  326.     domain_entry *de;
  327.     int ch, namelen, valuelen, space_free;
  328.  
  329. #ifdef VMS
  330.     extern BOOLEAN HadVMSInterrupt;
  331. #endif /* VMS */
  332.  
  333.     if ((de = (domain_entry *)dp) == NULL)
  334.         return FALSE;
  335.   
  336.     /*
  337.     **  If the user has specified a constant action, don't prompt at all.
  338.     */
  339.     if (de->bv == ACCEPT_ALWAYS)
  340.         return TRUE;
  341.     if (de->bv == REJECT_ALWAYS)
  342.         return FALSE;
  343.  
  344.     if (dump_output_immediately) {
  345.         /*
  346.     **  Non-interactive, can't respond.  Use the LYSetCookies value
  347.     *   based on its compilation or configuration setting, or on the
  348.     **  command line toggle. - FM
  349.     */
  350.         return LYSetCookies;
  351.     }
  352.  
  353.     /*
  354.     **  Figure out how much of the cookie we can show.
  355.     **  The '37' is the length of ADVANCED_COOKIE_CONFIRMATION,
  356.     **  minus the length of the %s directives (10 chars)
  357.     */
  358.     if (de != NULL) {
  359.         if (de->bv == ACCEPT_ALWAYS) 
  360.         return TRUE;
  361.     if (de->bv == REJECT_ALWAYS) 
  362.         return FALSE;
  363.     }
  364.     space_free = (((LYcols - 1) - 37) - strlen(server));
  365.     if (space_free < 0)
  366.         space_free = 0;
  367.     namelen = strlen(name);
  368.     valuelen = strlen(value);
  369.     if ((namelen + valuelen) > space_free) {
  370.         /*
  371.     **  Argh... there isn't enough space on our single line for
  372.     **  the whole cookie.  Reduce them both by a percentage.
  373.     **  This should be smarter.
  374.     */
  375.         float percentage;
  376.         percentage = (float)space_free/(float)(namelen + valuelen);
  377.         namelen = (int)(percentage*(float)namelen);
  378.         valuelen = (int)(percentage*(float)valuelen);
  379.     }
  380.     sprintf(message, ADVANCED_COOKIE_CONFIRMATION,
  381.             server, namelen, name, valuelen, value);
  382.     _statusline(message);
  383.     while (1) {
  384.     ch = LYgetch();
  385. #ifdef VMS
  386.     if (HadVMSInterrupt) {
  387.         HadVMSInterrupt = FALSE;
  388.         ch = 'N';
  389.     }
  390. #endif /* VMS */
  391.     switch(TOUPPER(ch)) {
  392.         case 'A':
  393.             /*
  394.         **  Set to accept all cookies for this domain.
  395.         */
  396.         de->bv = ACCEPT_ALWAYS;
  397.         _user_message(ALWAYS_ALLOWING_COOKIES, de->domain);
  398.         sleep(MessageSecs);
  399.         return TRUE;
  400.  
  401.         case 'N':
  402.         case 7:    /* Ctrl-G */
  403.         case 3:    /* Ctrl-C */
  404.             /*
  405.         **  Reject the cookie.
  406.         */
  407.         _statusline(REJECTING_COOKIE);
  408.         sleep(MessageSecs);
  409.         return FALSE;
  410.  
  411.             case 'V':
  412.             /*
  413.         **  Set to reject all cookies from this domain.
  414.         */
  415.         de->bv = REJECT_ALWAYS;
  416.         _user_message(NEVER_ALLOWING_COOKIES, de->domain);
  417.         sleep(MessageSecs);
  418.         return FALSE;
  419.  
  420.         case 'Y':
  421.             /*
  422.         **  Accept the cookie.
  423.         */
  424.         _statusline(ALLOWING_COOKIE);
  425.         sleep(InfoSecs);
  426.         return TRUE;
  427.  
  428.         default:
  429.             continue;
  430.     }
  431.     }
  432. }
  433.  
  434. /*      Confirm redirection of POST.        HTConfirmPostRedirect()
  435. **    ----------------------------
  436. **
  437. **  On entry,
  438. **      Redirecting_url             is the Location.
  439. **    server_status            is the server status code.
  440. **
  441. **  On exit,
  442. **      Returns 0 on cancel,
  443. **      1 for redirect of POST with content,
  444. **    303 for redirect as GET without content
  445. */
  446. PUBLIC int HTConfirmPostRedirect ARGS2(
  447.     CONST char *,    Redirecting_url,
  448.     int,        server_status)
  449. {
  450.     char *show_POST_url = NULL;
  451.     char StatusInfo[256];
  452.     char url[256];
  453.     int on_screen = 0;    /* 0 - show menu
  454.                 * 1 - show url
  455.              * 2 - menu is already on screen */
  456.  
  457.     if (server_status == 303 ||
  458.         server_status == 302) {
  459.     /*
  460.      *  HTTP.c should not have called us for either of
  461.      *  these because we're treating 302 as historical,
  462.      *  so just return 303. - FM
  463.      */
  464.     return 303;
  465.     }
  466.  
  467.     if (dump_output_immediately)
  468.         if (server_status == 301) {
  469.         /*
  470.         **  Treat 301 as historical, i.e., like 303 (GET
  471.         **  without content), when not interactive. - FM
  472.         */
  473.             return 303;
  474.         } else {
  475.         /*
  476.         **  Treat anything else (e.g., 305, 306 or 307) as too
  477.         **  dangerous to redirect without confirmation, and thus
  478.         **  cancel when not interactive. - FM
  479.         */
  480.         return 0;
  481.     }
  482.  
  483.     StatusInfo[254] = StatusInfo[255] = '\0';
  484.     url[254] = url[(LYcols < 250 ? LYcols-1 : 255)] = '\0';
  485.     if (user_mode == NOVICE_MODE) {
  486.         on_screen = 2;
  487.         move(LYlines-2, 0);
  488.         sprintf(StatusInfo, SERVER_ASKED_FOR_REDIRECTION, server_status);
  489.     addstr(StatusInfo);
  490.     clrtoeol();
  491.         move(LYlines-1, 0);
  492.     sprintf(url, "URL: %.*s",
  493.             (LYcols < 250 ? LYcols-6 : 250), Redirecting_url);
  494.         addstr(url);
  495.     clrtoeol();
  496.     if (server_status == 301) {
  497.         _statusline(PROCEED_GET_CANCEL);
  498.     } else {
  499.         _statusline(PROCEED_OR_CANCEL);
  500.     }
  501.     } else {
  502.     sprintf(StatusInfo, "%d %.*s",
  503.                 server_status,
  504.                 251,
  505.                 ((server_status == 301) ?
  506.              ADVANCED_POST_GET_REDIRECT :
  507.              ADVANCED_POST_REDIRECT));
  508.     StrAllocCopy(show_POST_url, LOCATION_HEADER);
  509.     StrAllocCat(show_POST_url, Redirecting_url);
  510.     }
  511.     while (1) {
  512.     int c;  
  513.  
  514.     switch (on_screen) {
  515.         case 0:
  516.         _statusline(StatusInfo);
  517.         break;
  518.         case 1:
  519.             _statusline(show_POST_url);
  520.     }
  521.     c = LYgetch();
  522.     switch (TOUPPER(c)) {
  523.         case 'P':
  524.         /*
  525.         **  Proceed with 301 or 307 redirect of POST
  526.         **  with same method and POST content. - FM
  527.         */
  528.             FREE(show_POST_url);
  529.         return 1;    
  530.  
  531.          case 7:
  532.          case 'C':
  533.             /*
  534.         **  Cancel request.
  535.         */
  536.             FREE(show_POST_url);
  537.         return 0;
  538.  
  539.         case 'U':
  540.             /*
  541.         **  Show URL for intermediate or advanced mode.
  542.         */
  543.             if (user_mode != NOVICE_MODE) {
  544.             if (on_screen == 1) {
  545.             on_screen = 0;
  546.             } else {
  547.             on_screen = 1;
  548.             }
  549.         }
  550.         break;
  551.  
  552.         case 'G':
  553.         if (server_status == 301) {
  554.             /*
  555.             **  Treat as 303 (GET without content).
  556.             */
  557.             FREE(show_POST_url);
  558.             return 303;
  559.         }
  560.         /* fall through to default */
  561.  
  562.         default:
  563.             /*
  564.         **  Get another character.
  565.         */
  566.         if (on_screen == 1) {
  567.             on_screen = 0;
  568.         } else {
  569.             on_screen = 2;
  570.         }
  571.     }
  572.     }
  573. }
  574.