home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / UPassword.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  5.8 KB  |  248 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // UPassword.cp
  3.  
  4. #include "UPassword.h"
  5. #include "UPrefsDatabase.h"
  6. #include "Tools.h"
  7. #include "UNntp.h"
  8. #include "ViewTools.h"
  9.  
  10. #include <RsrcGlobals.h>
  11.  
  12. #include <Packages.h>
  13. #include <UDialog.h>
  14.  
  15. #pragma segment MyDialogs
  16.  
  17. #define qDebugPassword qDebug & 1
  18.  
  19. CStr255 gCurrentPassword;
  20.  
  21. void CryptPassword(CStr255 &password)
  22. {
  23.     CStr255 s("01"); // version number
  24.     for (short i = 1; i <= password.Length(); i++)
  25.     {
  26.         s += char(97 + (Random() & 15));
  27.         s += char(password[i] ^ 9);
  28.         s += char(107 + (Random() & 15));
  29.     }
  30. #if qDebugPassword
  31.     fprintf(stderr, "Password '%s'", (char*)password);
  32.     fprintf(stderr, " crypted to '%s'\n", (char*)s);
  33. #endif
  34.     password = s;
  35. }
  36.  
  37. Boolean DeCryptPassword(CStr255 &password)
  38. {
  39.     CStr255 s(password);
  40.     password = "";
  41.     if (s.Length() < 2)
  42.         return false; // missing version number
  43.     if (s[1] != '0' || s[2] != '1')
  44.         return false; // unknown version
  45.     for (short i = 4; i <= s.Length(); i += 3)
  46.     {
  47.         password += char(s[i] ^ 9);
  48.     }
  49. #if qDebugPassword
  50.     fprintf(stderr, "Password '%s'", (char*)password);
  51.     fprintf(stderr, " decrypted from '%s'\n", (char*)s);
  52. #endif
  53.     BlockSet(Ptr(&s), sizeof(s), 0);
  54.     return true;
  55. }
  56.  
  57. void GetUserNameAndPassword(CStr255 &username, CStr255 &password)
  58. {
  59.     password = "";
  60.     gPrefs->GetStringPrefs('UNam', username);
  61.     if (gCurrentPassword.Length())
  62.         password = gCurrentPassword;
  63.  
  64.     if (gPrefs->PrefExists('Gate'))
  65.         gPrefs->GetStringPrefs('Gate', password);
  66.  
  67.     if (!username.Length())
  68.     {
  69.         gPrefs->GetStringPrefs('@adr', username);
  70.         short pos = username.Pos("@", 1);
  71.         if (pos)
  72.             username.Delete(pos, username.Length() - pos + 1);
  73.     }
  74.     if (DeCryptPassword(password))
  75.         if (username.Length() && password.Length())
  76.             return;
  77.  
  78.     TWindow *window = gViewServer->NewTemplateWindow(kAskPasswordView, nil);
  79.     FailNIL(window);
  80. // set all the control-states
  81.  
  82.     window->Center(true, true, window->IsModal() );
  83.     gPrefs->GetSilentWindowPosPrefs('WPas', window);
  84.     CStr255 s;
  85.     
  86. // Names
  87.     TEditText *usernameET = (TEditText*)window->FindSubView(kUsernameView);
  88.     TPasswordView *passwordView = (TPasswordView*)window->FindSubView(kPasswordView);
  89.  
  90.     usernameET->SetText(username, kRedraw);
  91.     DeCryptPassword(password);
  92.     passwordView->SetPassword(password);
  93.     BlockSet(Ptr(&password), sizeof(password), 0);
  94. #if qDebug
  95.     if (!IsObject(passwordView))
  96.         ProgramBreak("passwordView is not object");
  97.     if (!IsObject(usernameET))
  98.         ProgramBreak("usernameET is not object");
  99. #endif
  100.  
  101. // show the thing
  102.     if (!username.Length())
  103.         window->SetWindowTarget(usernameET);
  104.     window->Open();
  105.     IDType dismisser = MyPoseModally(window);
  106.     gPrefs->SetWindowPosPrefs('WPas', window);
  107.     if (dismisser != 'ok  ')
  108.     {
  109.         window->CloseByUser();
  110.         BlockSet(Ptr(&username), sizeof(username), 0);
  111.         BlockSet(Ptr(&password), sizeof(password), 0);
  112.         Failure(0, 0);
  113.     }
  114.     
  115.     usernameET->GetText(username);
  116.     gPrefs->SetStringPrefs('UNam', username);
  117.     passwordView->GetPassword(password);
  118.     gCurrentPassword = password;
  119.     CryptPassword(gCurrentPassword);
  120.     TCheckBox *rememberCB = (TCheckBox*) window->FindSubView('Husk');
  121.     if (rememberCB->IsOn())
  122.         gPrefs->SetStringPrefs('Gate', gCurrentPassword);
  123.     else if (gPrefs->PrefExists('Gate'))
  124.     {
  125.         CStr255 s(password);
  126.         BlockSet(Ptr(&s[1]), 255, 0);
  127.         gPrefs->SetStringPrefs('Gate', s); // clear memory
  128.         gPrefs->DeletePrefs('Gate');
  129.     }
  130.     window->CloseByUser();
  131. }
  132.  
  133. void InvalidateCurrentPassword()
  134. {
  135.     long numActive = gNntpCache->GetNoActiveConnections();
  136.     if (numActive)
  137.     {
  138.         CStr255 s;
  139.         NumToString(numActive, s);
  140.         ParamText(gEmptyString, gEmptyString, gEmptyString, s);
  141.         StdAlert(phUnableToForgetAllPasswords);
  142.         return;
  143.     }
  144.     gNntpCache->FlushCache();
  145.     ForgetCurrentPassword();
  146. }
  147.  
  148. void ForgetCurrentPassword()
  149. {
  150.     BlockSet(Ptr(&gCurrentPassword[1]), 255, 0);
  151.     gPrefs->SetStringPrefs('Gate', gCurrentPassword); // clear memory
  152.     gPrefs->DeletePrefs('Gate');
  153.     BlockSet(Ptr(&gCurrentPassword), sizeof(gCurrentPassword), 0);
  154.     gCurrentPassword.Length() = 0;
  155. }
  156.  
  157. Boolean HasPassword()
  158. {
  159.     return gCurrentPassword.Length() > 0 || gPrefs->PrefExists('Gate');
  160. }
  161.  
  162. void InitUPassword()
  163. {
  164.     gCurrentPassword = "";
  165. }
  166.  
  167. //----------------------------------------------------------------
  168.  
  169. pascal void TPasswordView::Initialize()
  170. {
  171.     inherited::Initialize();
  172.     BlockSet(Ptr(&fPassword), sizeof(fPassword), 0);
  173. }
  174.  
  175. pascal void TPasswordView::DoPostCreate(TDocument* itsDocument)
  176. {
  177.     inherited::DoPostCreate(itsDocument);
  178.     TEFeatureFlag(teFOutlineHilite, TEBitClear, fHTE);
  179. }
  180.  
  181. pascal void TPasswordView::Free()
  182. {
  183.     BlockSet(Ptr(&fPassword), sizeof(fPassword), 0);
  184.     inherited::Free();
  185. }
  186.  
  187. void TPasswordView::SetPassword(const CStr255 &password)
  188. {
  189.     CStr255 dot;
  190.     MyGetIndString(dot, kPasswordDot);
  191.     dot += '•'; // at least one char
  192.     dot.Length() = 1;
  193.     CStr255 s(gEmptyString);
  194.     for (short i = 1; i <= password.Length(); i++)
  195.         s += dot;
  196.     fPassword = password;
  197.     SetText(s);
  198. }
  199.  
  200. void TPasswordView::GetPassword(CStr255 &password)
  201. {
  202.     password = fPassword;
  203. }
  204.  
  205. pascal void TPasswordView::DoKeyEvent(TToolboxEvent* event)
  206. {
  207.     unsigned char ch = event->fCharacter;
  208.     if (ch == chBackspace)
  209.     {
  210.         if ((**fHTE).selStart == (**fHTE).selEnd)
  211.         {
  212.             fPassword.Length()--;
  213.             inherited::DoKeyEvent(event);
  214.         }
  215.         else
  216.         {
  217.             fPassword.Length() = 0;
  218.             SetText(gEmptyString);
  219.         }
  220.     }
  221.     else if (ch >= chSpace)
  222.     {
  223.         CStr255 dot;
  224.         MyGetIndString(dot, kPasswordDot);
  225.         dot += '•'; // at least one char
  226.         fPassword += event->fCharacter;
  227.         event->fCharacter = dot[1];
  228.         inherited::DoKeyEvent(event);
  229.     }
  230.     else if (ch == chTab || ch == chEscape || ch == chReturn || ch == chEnter)
  231.         TEventHandler::DoKeyEvent(event); // bypass TTEView
  232.     else
  233.         return; // ignore arrows etc.
  234. }
  235.  
  236. pascal void TPasswordView::DoMouseCommand(VPoint& /* theMouse */, TToolboxEvent* /* event */, CPoint /* hysteresis */)
  237. {
  238.     SetSelection(0, 1000, kRedraw);
  239. }
  240.  
  241. pascal void TPasswordView::DoSetupMenus()
  242. {
  243.     inherited::DoSetupMenus();
  244.     Enable(cCopy, false);
  245.     Enable(cPaste, false);
  246.     Enable(cCut, false);
  247. }
  248.