/* authenticate.c */

#include "ml.h"

Menu login_menu[] = {
  { NULL, "login_done", NUL_TERM,
      login_done, NULL, 0, NULL, NULL, 0 },
  { NULL, "login_abort", NUL_TERM,
      login_abort, NULL, 0, NULL, NULL, 0 },
  { NULL,   "login_HELP",  NUL_TERM, 
      login_help, NULL, 0, NULL, NULL, 0 },
};



#ifdef __STDC__
Remote_Auth *new_remote_auth(void)
#else
Remote_Auth *new_remote_auth()
#endif
{
  Remote_Auth *remote_auth = (Remote_Auth *) fs_get(sizeof(Remote_Auth));

  remote_auth->config       = NULL;
  remote_auth->hostname     = NULL;
  remote_auth->authtype     = NULL;
  remote_auth->username     = NULL;
  remote_auth->password     = NULL;
  remote_auth->workspace    = NULL;
  remote_auth->done         = 0;
  remote_auth->timer        = (XtIntervalId) 0;
  remote_auth->next         = NULL;

  return(remote_auth);
}

#ifdef __STDC__
void free_remote_auth(Remote_Auth *remote_auth)
#else
void free_remote_auth(remote_auth)
     Remote_Auth *remote_auth;
#endif
{
  if(remote_auth) {
    if(remote_auth->password) {
      wipeout(remote_auth->password);
      fs_give((void **) &remote_auth->password);
    }
    if(remote_auth->workspace)
      fs_give((void **) &remote_auth->workspace);
    if(remote_auth->username)
      fs_give((void **) &remote_auth->username);
    if(remote_auth->hostname)
      fs_give((void **) &remote_auth->hostname);
    if(remote_auth->authtype)
      fs_give((void **) &remote_auth->authtype);
    
    fs_give((void **) &remote_auth);
  }
  return;
}








#ifdef __STDC__
Remote_Auth * login(Widget w, Server_Config *config, char *where, 
		    char *type, char *defuser, char *defpass)
#else
Remote_Auth * login(w, config, where, type, defuser, defpass)
     Widget w;
     Server_Config *config;
     char *where;
     char *type;
     char *defuser;
     char *defpass;
#endif
{
  Arg args[ARGLISTSIZE];
  char buff[FILEBUFFLEN];
  XtTranslations translations;
  int n = 0;
  int i;
  char *unknown = MLGetLocalized(XtNstrUnknown,StrUnknown);
  Widget form, menubar, label1, label2, label3;
  XmString xstr = NULL;
  Remote_Auth *remote_auth = new_remote_auth();
  
  remote_auth->config = config;
  remote_auth->hostname = cpystr((where) ? where : unknown);
  remote_auth->workspace = (char *) fs_get(FILEBUFFLEN + 1);
  remote_auth->workspace[0] = NUL_TERM;
  remote_auth->authtype = cpystr((type) ? type : EMPTYSTR );

  if(preferences.autoPlace == TRUE) {
    XtSetArg(args[n], XtNx, -1000); n ++;
    XtSetArg(args[n], XtNy, -1000); n ++;
  }

  XtSetArg (args[n], XmNdeleteResponse, XmDO_NOTHING);  n ++;        
  remote_auth->shell = XtCreatePopupShell("authenticate",
				     topLevelShellWidgetClass, w,
				     args, n);
  AddDestroyCallback (remote_auth->shell);
  n = 0;

  setup_editres(remote_auth->shell);

  if(pirate_icon != (Pixmap) None)
    XtVaSetValues(remote_auth->shell,
		  XmNiconPixmap,pirate_icon,
		  NULL);

  form = XmCreateForm(remote_auth->shell, "authenticate_form", args, n); n = 0;
  
  XtSetArg(args[n], XmNtopAttachment,   XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNleftAttachment,  XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n ++;
  menubar = XmCreateMenuBar(form,"authenticate_menubar", args, n); n = 0;
  XtManageChild(menubar);

  create_buttons(NULL, menubar, 
		 login_menu, XtNumber(login_menu),
		 BTN_ON, (XtPointer) remote_auth, ROOTMENULEVEL);
  
  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNtopWidget, menubar);             n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM);  n ++;
  XtSetArg(args[n], XmNborderWidth, 0);                 n ++;

  sprintf(buff, MLGetLocalized(XtNmsgAuthenticateTo,MsgAuthenticateTo), 
	  (where) ? where : unknown);
  xstr = XmStringCreateSimple(buff);

  XtSetArg(args[n], XmNlabelString,xstr);                       n ++;
  label1 = XmCreateLabel(form,"authenticate_where_lbl",args,n); n = 0;
  XtManageChild(label1);
  XmStringFree(xstr);

  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNtopWidget, label1); n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n ++;

  XtSetArg(args[n], XmNborderWidth, 0); n ++;

  label2 = XmCreateLabel(form,"authenticate_name_lbl", args, n); n = 0;
  XtManageChild(label2);

  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNtopWidget,     label1); n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNleftWidget,label2); n ++;
  
  remote_auth->name = 
    XmCreateTextField(form,"authenticate_name_textfld", args, n); n = 0;
  XtManageChild(remote_auth->name);
  if(defuser)
    AppendText(remote_auth->name,defuser);

  translations = XtParseTranslationTable(GLOBAL_text_field_translations);
  XtOverrideTranslations(remote_auth->name,translations);
  translations = 
    XtParseTranslationTable(GLOBAL_nonterminal_text_field_translations);
  XtOverrideTranslations(remote_auth->name,translations);

  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNtopWidget, remote_auth->name); n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n ++;
  
  XtSetArg(args[n], XmNborderWidth, 0); n ++;

  label3 = XmCreateLabel(form,"authenticate_pass_lbl", args, n); n = 0;
  XtManageChild(label3);

  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNtopWidget, remote_auth->name); n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n ++;
  XtSetArg(args[n], XmNleftWidget,label3); n ++;

  remote_auth->pass 
    = XmCreateTextField(form,"authenticate_pass_textfld", args, n); n = 0;
  XtManageChild(remote_auth->pass);
  if(defpass) {
    strcpy(remote_auth->workspace,defpass);
    for(i = 0; i < (int) strlen(remote_auth->workspace); i ++)
      AppendText(remote_auth->pass,PASSWD_STR);
  }

  XtAddCallback(remote_auth->pass, XmNactivateCallback, 
		(XtCallbackProc) login_done, remote_auth);

  XtAddCallback(remote_auth->pass, XmNmodifyVerifyCallback, 
		(XtCallbackProc) passwd_edit, remote_auth);

  translations = XtParseTranslationTable(GLOBAL_text_field_translations);
  XtOverrideTranslations(remote_auth->pass,translations);
  translations = 
    XtParseTranslationTable(GLOBAL_terminal_text_field_translations);
  XtOverrideTranslations(remote_auth->pass,translations);

  XtAddCallback(remote_auth->shell, XmNpopdownCallback,
		(XtCallbackProc) auth_destroy, remote_auth);


  XtManageChild(form);
  XtManageChild(remote_auth->shell);
  XmProcessTraversal(remote_auth->pass, XmTRAVERSE_CURRENT);
  XtPopup(remote_auth->shell,XtGrabExclusive); 
  position_popup_widget(remote_auth->shell, TRUE);

  remote_auth->timer = XtAppAddTimeOut(context,
				       LOGINREFRESH,
				       (XtTimerCallbackProc)auth_refresh,
				       remote_auth);

  push_cursor(PIRATE_CURSOR);

  modal_main_loop(&remote_auth->done);
  XtRemoveTimeOut(remote_auth->timer);
  remote_auth->timer = (XtIntervalId) 0;
				       
  XtPopdown(remote_auth->shell);
  XtDestroyWidget(remote_auth->shell);

  pop_cursor();
  return(remote_auth);
}


#ifdef __STDC__
void login_done(Widget w, Remote_Auth *remote_auth, XtPointer xp)
#else
void login_done(w,remote_auth,xp)
     Widget w;
     Remote_Auth *remote_auth;
     XtPointer xp;
#endif
{
  remote_auth->username = XmTextGetString(remote_auth->name);
  remote_auth->password = scramble(remote_auth->workspace);
  wipeout(remote_auth->workspace);
  fs_give((void **) &remote_auth->workspace);
  remote_auth->done = MODAL_LOOP_DONE;
  return;
}


#ifdef __STDC__
void auth_refresh(Remote_Auth *remote_auth, XtIntervalId *id)
#else
void auth_refresh(remote_auth,id)
     Remote_Auth *remote_auth;
     XtIntervalId *id;
#endif
{
  de_iconify(remote_auth->shell);
  remote_auth->timer = XtAppAddTimeOut(context,
				       LOGINREFRESH,
				      (XtTimerCallbackProc) auth_refresh,
				      remote_auth);
  return;
}


#ifdef __STDC__
void auth_destroy(Widget w, Remote_Auth *remote_auth, XtPointer xp)
#else
void auth_destroy(w,remote_auth,xp)
     Widget w;
     Remote_Auth *remote_auth;
     XtPointer xp;
#endif
{
  remote_auth->done = MODAL_LOOP_DONE;
  return;
}



#ifdef __STDC__
void login_abort(Widget w, Remote_Auth *remote_auth, XtPointer xp)
#else
void login_abort(w, remote_auth, xp)
     Widget w;
     Remote_Auth *remote_auth;
     XtPointer xp;
#endif
{
  remote_auth->username = NULL;
  remote_auth->password = NULL;
  wipeout(remote_auth->workspace);
  fs_give((void **) &remote_auth->workspace);
  remote_auth->done = MODAL_LOOP_DONE;
  return;
}

#ifdef __STDC__
void login_help(Widget w, Remote_Auth *remote_auth, XtPointer xp)
#else
void login_help(w,remote_auth, xp)
     Widget w;
     Remote_Auth *remote_auth;
     XtPointer xp;
#endif
{
  help(remote_auth->shell, AUTHENTICATEHELPFILE);
  return;
}




syntax highlighted by Code2HTML, v. 0.9.1