/*****************************************************************************/
/*
                                 mainmenu.c

The soyMAIL main menu is the panel present across the top of most pages,
providing the folder selection [folder][Open], [New 999], [Search], [Options],
[Contacts], [Compose], [Logout], [close] and [help] buttons.  This module also
provides the status panel.


COPYRIGHT
---------
Copyright (C) 2005-2025 Mark G.Daniel
This program, comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under the
conditions of the GNU GENERAL PUBLIC LICENSE, version 3, or any later version.


VERSION HISTORY
---------------
18-APR-2014  MGD  add form field STAND_ALONE to track standlone mode
27-FEB-2010  MGD  add [^] to [contacts] button
07-OCT-2006  MGD  soyMAIL autogenous authentication and logout
12-JUN-2006  MGD  add form field CLIENT_TIME and JavaScript clientTime()
                    to provide local (to browser) message origination time
16-MAR-2006  MGD  revised [logout] support
27-FEB-2006  MGD  [close] now has a JavaScript confirm for compose page
25-FEB-2006  MGD  [logout] now has associated JavaScript confirm explaining 
                    the logout basics and allowing an opportunity to cancel
                  bugfix; MainMenuRequest() FOLDER_LIST_CHANGE detect 't'
14-FEB-2006  MGD  main menu [compose] button now has an associated [^] popup
                  main menu [close] button for child windows
01-FEB-2005  MGD  initial
*/

/*****************************************************************************/

#ifdef SOYMAIL_VMS_V7
#undef _VMS_V6_SOURCE
#define _VMS_V6_SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000
#endif

#pragma nomember_alignment

/* standard C header files */
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* VMS related header files */
#include <descrip.h>
#include <mailmsgdef.h>
#include <ssdef.h>
#include <stsdef.h>

/* application header file */
#include "soymail.h"
#include "callmail.h"
#include "cereal.h"
#include "cgilib.h"
#include "config.h"
#include "login.h"
#include "mainmenu.h"
#include "newmail.h"
#include "other.h"
#include "message.h"

#define FI_LI "MAINMENU", __LINE__

/* global storage */

/* external storage */

extern BOOL  Debug,
             WatchEnabled;

extern int  VmsVersion;

extern char  *CgiEnvironmentPtr;

extern char  DocType[],
             SoftwareCopy[],
             SoftwareEnv[],
             SoftwareVn[];

extern CONFIG_DATA  SoyMailConfig;
extern USER_OPTIONS  UserOptions;
extern VMS_MAIL_USER  VmsMailUser;

/* ad hoc prototypes */

int lib$day_of_week(__unknown_params);
int sys$fao(__unknown_params);

/****************************************************************************/
/*
Parse the buttons from the main menu.
*/ 

BOOL MainMenuRequest (REQUEST_DATA *rdptr)

{
   /* must be static because it's pointed to for possible use later */
   static char  bbuf [64];

   char  *cptr, *sptr, *zptr;
   USER_OPTIONS  *uoptr;
   REQUEST_DATA  *sdptr;

   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuRequest()");

   /* a bit of sanity checking */
   if (!rdptr->PrivateAccess) ErrorExit (SS$_BUGCHECK, FI_LI);

   uoptr = &rdptr->UserOptions;
   sdptr = (REQUEST_DATA*)rdptr->StateDataPtr;

   CGIVARNULL (cptr, "FORM_FOLDER_LIST_CHANGE");
   if (cptr && *cptr == 't')
   {
      /* folder selection (change) will propagate a search, [open] doesn't */
      MessageFolderPage (rdptr);
      return (TRUE);
   }

   CGIVARNULL (cptr, "FORM_NEWMAIL_REFRESH");
   if (cptr && *cptr == 'N')
   {
      NewMailMessage (rdptr);
      return (TRUE);
   }

   CGIVARNULL (cptr, "FORM_MAIN_BTN_CONTACTS_POPUP");
   if (cptr)
   {
      ContactsPageRequest (rdptr);
      return (TRUE);
   }

   CGIVARNULL (cptr, "FORM_MAIN_BTN_COMPOSE_POPUP");
   if (cptr)
   {
      ComposePageRequest (rdptr);
      return (TRUE);
   }

   CGIVARNULL (cptr, "FORM_MAIN_BTN");
   if (!cptr) return (FALSE);

   rdptr->SearchData.InProgress = FALSE;

   /* we insert new message count into "new" button! */
   for (sptr = cptr; *sptr && !isdigit(*sptr); sptr++);
   if (*sptr)
   {
      cptr = sptr;
      while (*cptr && isdigit(*cptr)) cptr++;
      while (*cptr && isspace(*cptr)) cptr++;
      zptr = (sptr = bbuf) + sizeof(bbuf)-1;
      while (*cptr && sptr < zptr) *sptr++ = *cptr++;
      if (sptr >= zptr) ErrorExit (SS$_BUGCHECK, FI_LI);
      sptr--;
      while (sptr > bbuf && isdigit(*sptr)) sptr--;
      *++sptr = '\0';
      cptr = bbuf;
   }

   if (LangSame ("open", cptr))
   {
      MessageFolderPage (rdptr);
      return (TRUE);
   }

   if (LangSame ("new", cptr))
   {
      NewMailMessage (rdptr);
      return (TRUE);
   }

   /* searching can be disabled */
   if (LangSame ("search", cptr) ||
       LangSame ("search_new", cptr))
      return (SearchPageRequest (rdptr));

   if (LangSame ("options", cptr))
   {
      OptionsPageRequest (rdptr);
      return (TRUE);
   }

   if (LangSame ("contacts", cptr))
   {
      ContactsPageRequest (rdptr);
      return (TRUE);
   }

   if (LangSame ("compose", cptr))
   {
      ComposePageRequest (rdptr);
      return (TRUE);
   }

   if (LangSame ("logout", cptr))
   {
      if (SoyMailConfig.LogoutDisabled)
      {
         StatusMessage (FI_LI, 1, LangFor("logout_disabled"));
         return (FALSE);
      }
      if (SoyMailConfig.LoginSecret)
      {
         /* using soyMAIL's autogenous authentication, force re-login */
         LoginPage (rdptr, "", NULL, 0);
         RequestAccessLog (NULL, "logout");
      }
      else
      {
         strcat (rdptr->FormAction, SOY_LOGOUT_SENTINAL);
         CgiLibResponseRedirect (rdptr->FormAction);
      }
      return (TRUE);
   }

   if (LangSame ("help", cptr))
   {
      HelpPageRequest (rdptr);
      return (TRUE);
   }

   return (FALSE);
}

/*****************************************************************************/
/*
*/

void MainMenuPageBegin
(
REQUEST_DATA *rdptr,
int ThisPageIdent
)
{
   static char  OperaChild0 [] = 
"<input type=\"hidden\" name=\"opera_child\" id=\"opera_child\" value=\"0\">\n",
                OperaChild1 [] =
"<input type=\"hidden\" name=\"opera_child\" id=\"opera_child\" value=\"1\">\n";

   int  NewMessages,
        SoyMailHistory;
   char  *cptr,
         *OperaChildPtr,
         *UserNamePtr;
   USER_OPTIONS  *uoptr;
   REQUEST_DATA  *sdptr;
   VMS_MAIL_MSG  *vmptr;
   VMS_MAIL_USER  *muptr;

   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuPageBegin()");

   sdptr = (REQUEST_DATA*)rdptr->StateDataPtr;
   uoptr = &rdptr->UserOptions;
   muptr = &VmsMailUser;
   vmptr = &muptr->VmsMailMsg;

   if (!rdptr->ThisPageIdent) rdptr->ThisPageIdent = ThisPageIdent;

   if (rdptr->CharSetPtr)
      cptr = rdptr->CharSetPtr;
   else
   if (uoptr->ComposeCharSets[0])
      cptr = uoptr->ComposeCharSets;
   else
      cptr = "utf-8";

   if (strsame (cptr, "ascii", -1) ||
       strsame (cptr, "us-ascii", -1))
      cptr = "utf-8";

   CgiLibResponseSetCharset (cptr);

   CgiLibResponseHeader (200, "text/html", "%s", rdptr->LoginSetCookiePtr);

   fprintf (stdout,
"%s\n\
<html>\n\
<head>\n\
%s\
<meta name=\"generator\" content=\"%s\">\n\
<meta name=\"environment\" content=\"%s\">\n\
<meta name=\"copyright\" content=\"%s - Apache-2.0 licensed\">\n\
<meta name=\"language\" content=\"%s %s\">\n",
            DocType,
            CgiLibXUACompatible(NULL),
            SoftwareEnv,
            CgiEnvironmentPtr,
            SoftwareCopy,
            LangFor("lang_name"),
            LangFor("lang_author"));

   /* used to support some of the enhanced functionality */
   SoyMailJavaScriptStuff (rdptr);

   /* returns a pointer to the '<body onload=()>' requirement */
   cptr = NewMailRefreshXMLHttpRequest (rdptr);

   SoyMailStyleStuff (rdptr, uoptr->Theme);

   fprintf (stdout,
"<title>%s</title>\n\
</head>\n\n",
            SoyMailPageTitle(rdptr));

   if (ThisPageIdent == PAGE_HELP)
      fputs ("<body onload=\"soyBodyLoad();refreshClock();window.focus();\">\n", stdout);
   else
   if (ThisPageIdent == PAGE_OPTIONS)
      fputs ("<body onload=\"soyBodyLoad();refreshClock();resizeOptionsPanels();\">\n", stdout);
   else
   {
      fprintf (stdout, "<body onload=\"soyBodyLoad();refreshClock();");

      /* use the pointer to the '<body onload=()>' requirement */
      if (cptr) fputs (cptr, stdout);

      if (ThisPageIdent == PAGE_COMPOSE ||
          ThisPageIdent == PAGE_CONTACTS)
      {
         fputs ("window.addEventListener(\'beforeunload\',\
doBeforeUnload);", stdout);
      }

      if (rdptr->NewMailPlay)
      {
         fputs ("playYouGotMail();", stdout);
         rdptr->NewMailPlay = FALSE;
      }

      if (ThisPageIdent == PAGE_COMPOSE)
      {
         fputs ("contactListResize();", stdout);
         if (rdptr->ComposeData.EditorType == COMPOSE_EDITOR_HTML)
            fputs ("htmlEditorLoad();", stdout);
      }

      if (rdptr->FieldFocusPtr) fputs (rdptr->FieldFocusPtr, stdout);

      fputs ("\" onunload=\"\
clearWorking();\" \
onfocus=\"refreshSoyMailForm();\" \
onblur=\"refreshSoyMailForm();\">\n",
             stdout);
   }

   /* keeps track of previous soyMAIL usage history for (re-)login purposes */
   CGIVAR (cptr, "FORM_SOYMAIL_HISTORY");
   SoyMailHistory = atoi(cptr) + 1;

   OperaChildPtr = "";
   if (rdptr->UserAgent == SOY_USER_AGENT_OPERA)
   {
      OperaChildPtr = OperaChild0;
      if (!strcmp (rdptr->CgiRequestMethodPtr, "POST"))
      {
         CGIVARNULL (cptr, "FORM_OPERA_CHILD");
         if (!cptr || *cptr == '0') OperaChildPtr = OperaChild1;
      }
   }

   if (rdptr->CharSetPtr)
      cptr = rdptr->CharSetPtr;
   else
   if (uoptr->ComposeCharSets[0])
      cptr = uoptr->ComposeCharSets;
   else
      cptr = "utf-8";

   if (strsame (cptr, "ascii", -1) ||
       strsame (cptr, "us-ascii", -1))
      cptr = "utf-8";

   fprintf (stdout,
"<form \
name=\"soymail_form\" \
id=\"soymail_form\" \
action=\"%s\" \
method=\"POST\" \
accept-charset=\"%s\" \
enctype=\"multipart/form-data\" \
onsubmit=\"clientTime()\">\n\
<input type=\"hidden\" name=\"soymail_history\" \
id=\"soymail_history\" value=\"%d\">\n\
<input type=\"hidden\" name=\"newmail_refresh\" \
id=\"newmail_refresh\" value=\"\">\n\
<input type=\"hidden\" name=\"client_time\" id=\"client_time\" value=\"\">\n\
%s\
\n\
<table id=\"main_menu\" width=\"100%%\" cellpadding=\"0\" cellspacing=\"5\" \
align=\"center\">\n\
<tr><td align=\"center\">\n\n",
            rdptr->FormAction,
            cptr,
            SoyMailHistory,
            OperaChildPtr);
}

/*****************************************************************************/
/*
*/

void MainMenuPageEnd (REQUEST_DATA *rdptr)

{
   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuPageEnd()");

   fprintf (stdout, "</td></tr>\n</table>\n\n");

   RequestPrivateStateSave (rdptr);

   fputs ("</form>\n<span id=\"you_got_mail\"></span>\n", stdout);
   StatTimer (TRUE);
   fputs ("</body>\n</html>\n", stdout);

}

/*****************************************************************************/
/*
*/

void MainMenuPlainPageBegin (REQUEST_DATA *rdptr)

{
   char  *cptr,
         *UserNamePtr;
   USER_OPTIONS  *uoptr;
   VMS_MAIL_MSG  *vmptr;
   VMS_MAIL_USER  *muptr;

   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuPlainPageBegin()");

   uoptr = &rdptr->UserOptions;
   muptr = &VmsMailUser;
   vmptr = &muptr->VmsMailMsg;

   if (rdptr->CharSetPtr)
      cptr = rdptr->CharSetPtr;
   else
   if (uoptr->ComposeCharSets[0])
      cptr = uoptr->ComposeCharSets;
   else
      cptr = "utf-8";

   if (strsame (cptr, "ascii", -1) ||
       strsame (cptr, "us-ascii", -1))
      cptr = "utf-8";

   CgiLibResponseSetCharset (cptr);

   CgiLibResponseHeader (200, "text/html", "%s", rdptr->LoginSetCookiePtr);

   fprintf (stdout,
"%s\n\
<html>\n\
<head>\n\
%s\
<meta name=\"generator\" content=\"%s\">\n\
<meta name=\"environment\" content=\"%s\">\n\
<meta name=\"copyright\" content=\"%s - Apache-2.0 licensed\">\n\
<meta name=\"language\" content=\"%s %s\">\n\
\
<style type=\"text/css\">\n\
body { font-family:%s; font-size:%dpt; }\n\
* { font-family:inherit; font-size:inherit; }\n\
.p0 { margin-top:0; margin-bottom:0; }\n\
.p10 { margin-top:10px; margin-bottom:0; }\n\
</style>\n\
\
<title>%s</title>\n\
</head>\n\n\
<body>\n\
\n\
<table width=\"100%%\" cellpadding=\"5\" align=\"center\">\n\
<tr><td>\n\n",
            DocType,
            CgiLibXUACompatible(NULL),
            SoftwareEnv,
            CgiEnvironmentPtr,
            SoftwareCopy,
            LangFor("lang_name"),
            LangFor("lang_author"),
            uoptr->FontFamily,
            uoptr->FontSize,
            SoyMailPageTitle(rdptr));
}

/*****************************************************************************/
/*
*/

void MainMenuPlainPageEnd (REQUEST_DATA *rdptr)

{
   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuPlainPageEnd()");

   fprintf (stdout,
"</td></tr>\n\
</table>\n\n\
</body>\n\
</html>\n");
}

/*****************************************************************************/
/*
Display the top-level main menu.
*/

void MainMenuBar (REQUEST_DATA *rdptr)

{
   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuBar()");

   MainMenuBarBegin (rdptr);
   if (rdptr->PrevPageIdent == PAGE_HELP)
      FolderOpenHelp (rdptr);
   else
   {
      fprintf (stdout, "<td nowrap align=\"left\" valign=\"middle\">\n\n");
      FolderListInit (rdptr);
      FolderOpenSelect (rdptr);
      fprintf (stdout, "</td>\n");
   }
   MainMenuBarButtons (rdptr);
   MainMenuBarEnd (rdptr);
}

/*****************************************************************************/
/*
*/

void MainMenuBarBegin (REQUEST_DATA *rdptr)

{
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuBarBegin()");

   fprintf (stdout,
"<!-- main menu begin -->\n\
<table width=\"100%%\" cellpadding=\"1\" cellspacing=\"0\" \
align=\"center\" class=\"css_panel_1\">\n\
<tr><td>\n\
<table class=\"css_panel_2\" width=\"100%%\" align=\"center\" \
cellpadding=\"4\" cellspacing=\"2\">\n");

   if (SoyMailConfig.PageTitle1 &&
       SoyMailConfig.PageTitle2)
   {
      if (strchr (SoyMailConfig.PageTitle1, '<'))
         cptr = " style=\"padding-left:0.5em;\"";
      else
         cptr = " style=\"font-size:120%;padding-left:0.5em;\"";

      if (strchr (SoyMailConfig.PageTitle2, '<'))
         sptr = " style=\"padding-right:0.5em;\"";
      else
         sptr = " style=\"font-size:120%;padding-right:0.5em;\"";

      fprintf (stdout,
"<tr><td align=\"left\" colspan=\"2\" width=\"100%%\"%s>%s</td>\
<td align=\"right\" width=\"100%%\"%s>%s</td></tr>\n",
               cptr, SoyMailConfig.PageTitle1,
               sptr, SoyMailConfig.PageTitle2);
   }
   else
   if (SoyMailConfig.PageTitle1)
   {
      if (strchr (SoyMailConfig.PageTitle1, '<'))
         cptr = "";
      else
         cptr = " style=\"font-size:120%;text-align:center;\"";

      fprintf (stdout, "<tr><td colspan=\"3\"%s>%s</td></tr>\n",
               cptr, SoyMailConfig.PageTitle1);
   }

   fprintf (stdout,
"<tr>\n\
<td nowrap align=\"center\" valign=\"middle\" width=\"1%%\">&nbsp;&nbsp;");

   SoyMailLogo (rdptr);

   fprintf (stdout, "&nbsp;&nbsp;</td>\n\n");
}

/*****************************************************************************/
/*
*/

void MainMenuBarEnd (REQUEST_DATA *rdptr)

{
   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuBarEnd()");

   fprintf (stdout,
"</tr>\n\
</table>\n\
</td></tr>\n\
</table>\n\
<!-- main menu end -->\n\n");

}

/*****************************************************************************/
/*
*/

void MainMenuBarButtons (REQUEST_DATA *rdptr)

{
   BOOL  LogoutButton;
   char  *cptr, *sptr,
         *LogoutConfirm;
   VMS_MAIL_USER  *muptr;

   /*********/
   /* begin */
   /*********/

   if (WatchEnabled) WatchThis (FI_LI, "MainMenuBarButtons()");

   muptr = &VmsMailUser;

   fprintf (stdout,
"<!-- main menu buttons begin -->\n\
<td nowrap align=\"right\" valign=\"middle\">\n\
<input class=\"css_button_panel_2\"%s type=\"submit\" \
name=\"main_btn\" value=\"%s\">&nbsp;\n\
<input class=\"css_button_panel_2\" type=\"submit\" \
name=\"main_btn\" value=\"%s\">&nbsp;\n\
<input class=\"css_button_panel_2\" type=\"submit\" \
name=\"main_btn\" value=\"%s\">",
           rdptr->ThisPageIdent == PAGE_HELP || SoyMailConfig.SearchEnabled ?
              "" : " style=\"font-style:italic;\"",
           LangFor("search"),
           LangFor("options"),
           LangFor("contacts"));

   if (rdptr->ThisPageIdent != PAGE_HELP)
      fprintf (stdout,
"<script language=\"JavaScript\">\
document.write(\'<input class=\"css_button_panel_2\" \
style=\"border-left-width:0px;\" \
type=\"submit\" name=\"main_btn_contacts_popup\" value=\"^\" \
onclick=\"targetBlank();\">\');\
</script>");

   fprintf (stdout,
"&nbsp;\n\
<input class=\"css_button_panel_2\" type=\"submit\" \
name=\"main_btn\" value=\"%s\">",
           LangFor("compose"));

   if (rdptr->ThisPageIdent != PAGE_HELP)
      fprintf (stdout,
"<script language=\"JavaScript\">\
document.write(\'<input class=\"css_button_panel_2\" \
style=\"border-left-width:0px;\" \
type=\"submit\" name=\"main_btn_compose_popup\" value=\"^\" \
onclick=\"targetBlank();\">\');\
</script>");

   fprintf (stdout, "&nbsp;\n");

   if (rdptr->ThisPageIdent == PAGE_HELP)
   {
      fprintf (stdout,
"<input class=\"css_button_panel_2\" type=\"submit\" \
name=\"main_btn\" value=\"%s\">&nbsp;\n",
               LangFor("logout"));
   }
   else
   {
      LogoutButton = FALSE;
      if (SoyMailConfig.LoginSecret)
      {
         /* using soyMAIL's own internal authentication */
         LogoutButton = TRUE;
         LogoutConfirm = LANG_FOR_CONFIRM("login_confirm_logout");
      }
      else
      if (CgiLibEnvironmentIsOsu())
      {
         /* OSU does not appear to provide AUTH_TYPE */
         LogoutButton = TRUE;
         LogoutConfirm = LANG_FOR_CONFIRM("logout_confirm");
      }
      else
      {
         /* if it's BASIC or DIGEST authentication */
         CGIVARNULL (cptr, "AUTH_TYPE");
         if (!cptr) cptr = "";
         if (strsame(cptr,"basic",-1) || strsame(cptr,"digest",-1))
         {
            LogoutButton = TRUE;
            LogoutConfirm = LANG_FOR_CONFIRM("logout_confirm");
         }
      }

      if (LogoutButton)
      {
         if (SoyMailConfig.LogoutDisabled)
            fprintf (stdout,
"<input class=\"css_button_panel_2\" style=\"font-style:italic;\" \
type=\"submit\" id=\"main_btn_logout\" name=\"main_btn\" value=\"%s\" \
onclick=\"return false\">&nbsp;\n",
                     LangFor("logout"));
          else
            fprintf (stdout,
"<input class=\"css_button_panel_2\" \
type=\"submit\" name=\"main_btn\" value=\"%s\" \
onclick=\"return confirm(\'%s\')\">&nbsp;\n",
                     LangFor("logout"),
                     LogoutConfirm);
      }
   }

   fprintf (stdout,
"<script language=\"JavaScript\">\
if (childWindow()) \
document.write(\'<input class=\"css_button_panel_2\" \
type=\"submit\" name=\"main_btn\" value=\"%s\" \
onclick=\"window.close();return false;\">&nbsp;\\n\');\
</script>",
           LangFor("close"));

   fprintf (stdout,
"<input class=\"css_button_panel_2\" type=\"submit\" \
name=\"main_btn\" value=\"%s\" \
onclick=\"document.soymail_form.target=\'soymail_help\'\">&nbsp;\n\
</td>\n\
<!-- main menu buttons end -->\n\
\n",
           LangFor("help"));

}

/*****************************************************************************/

