home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscRegistration.m < prev    next >
Encoding:
Text File  |  1994-03-21  |  6.9 KB  |  224 lines

  1. //
  2. //    MiscRegistration.m -- a class to sit underneath a Registration panel
  3. //        Written by Don Yacktman (c) 1994 by Don Yacktman.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. #import <misckit/misckit.h>
  15.  
  16. #define _notRegistered [strings valueForStringKey:"NotRegistered"]
  17. #define _unreg [strings valueForStringKey:"Unreg"]
  18. #define _reg [strings valueForStringKey:"Reg"]
  19. #define _OK [strings valueForStringKey:"OK"]
  20. #define _cantReg [strings valueForStringKey:"CantReg"]
  21. #define _badRegNum [strings valueForStringKey:"BadRegNum"]
  22.  
  23. @implementation MiscRegistration
  24.  
  25. + initialize
  26. {
  27.     static NXDefaultsVector MiscRegistrationDefaults = {
  28.         { MISC_KEY_DEFAULT, MISC_DEFAULT_KEY },
  29.         { NULL }
  30.     };
  31.     NXRegisterDefaults([NXApp appName], MiscRegistrationDefaults);
  32.     return self;
  33. }
  34.  
  35. - init                // make sure that *Panel is nil.
  36. {
  37.     self = [super init];
  38.     _alertOnNoWrite = YES;
  39.     registerPanel = nil;
  40.     // get name of key file (inside app wrapper)
  41.     _keyFileName = [MiscString newWithString:[NXApp appDirectory]];
  42.     [_keyFileName cat:"/"];
  43.     [_keyFileName cat:MISC_KEY_FILE_NAME];
  44.     _key = [[MiscString alloc] init];
  45.     _serialNum = [MiscString newWithString:MISC_UNREG_SERIAL_NUM];
  46.     [self readKey];
  47.     return self;
  48. }
  49.  
  50. - setController:sender { controller = sender; return self; }
  51.  
  52. - readKey
  53. {
  54.     if (![self readKeyFromFile]) {
  55.         // if read from file failed, then we need to try the one from
  56.         // the defaults system.
  57.         id keyString = [MiscString newWithString:
  58.                 NXGetDefaultValue([NXApp appName], MISC_KEY_DEFAULT)];
  59.         [_key setStringValue:[keyString stringValueAndFree]];
  60.         _alertOnNoWrite = NO;
  61.         [self writeKeyToFile]; // attempt to remember the key
  62.         _alertOnNoWrite = YES;
  63.     }
  64.     [self registered]; // make sure we are up to date on key verification
  65.     // ***** even if not nil returned, may be invalid key...
  66.     return self;
  67. }
  68.  
  69. - writeKey
  70. {
  71.     [self writeKeyToFile];
  72.     // **** this should only be written if the key is valid and single user,
  73.     // in which case we do NOT write to key file!
  74.     NXWriteDefault([NXApp appName], MISC_KEY_DEFAULT, [_key stringValue]);
  75.     return self;
  76. }
  77.  
  78. - readKeyFromFile
  79. {
  80.     NXTypedStream *typedStream;
  81.     FILE *testFile;
  82.     char *temp;
  83.  
  84.     // first check for existence of file
  85.     testFile = fopen([_keyFileName stringValue], "r");
  86.     if (testFile == NULL) {
  87.         if (_notRegistered)
  88.             [_key setStringValue:_notRegistered];
  89.         else [_key setStringValue:"No key."];
  90.         return nil;
  91.     }
  92.     fclose(testFile);
  93.     typedStream = NXOpenTypedStreamForFile([_keyFileName stringValue],
  94.             NX_READONLY);
  95.     NXReadTypes(typedStream, "*", &temp);
  96.     if (temp) {
  97.         [_key setStringValue:temp];
  98.     } else {
  99.         if (_notRegistered) {
  100.             [_key setStringValue:_notRegistered];
  101.         } else { [_key setStringValue:"No key."]; }
  102.         return nil;
  103.     }
  104.     NXCloseTypedStream(typedStream);
  105.     return self;
  106. }
  107.  
  108. - writeKeyToFile
  109. {
  110.     NXTypedStream *typedStream;
  111.     id chmodString = [MiscString newWithString:"chmod 644 "]; // only owner...
  112.     const char *temp = [_key stringValue];
  113.  
  114.     typedStream = NXOpenTypedStreamForFile([_keyFileName stringValue],
  115.             NX_WRITEONLY);
  116.     if (!typedStream) {
  117.         // ***** should only put this up for network licenses...
  118.         if (_alertOnNoWrite)
  119.             NXRunAlertPanel("writeKey", _cantReg, NULL, NULL, _OK);
  120.         return nil;
  121.     }
  122.     NXWriteTypes(typedStream, "*", &temp);
  123.     NXCloseTypedStream(typedStream);
  124.     [chmodString concatenate:_keyFileName];
  125.     system([chmodString stringValueAndFree]);
  126.     return self;
  127. }
  128.  
  129. - registerPanel        // return the registerPanel, load it if needed.
  130. {
  131.     if( !registerPanel) {
  132.         [NXApp loadNibSection:"Register.nib" owner:self withNames:NO];
  133.         if (!strings) { // attempt to load a ".strings" file *****
  134.         }
  135.         if (!strings) { // failing that, use the controller's string table
  136.             strings = [controller strings];
  137.         }
  138.     }
  139.     return registerPanel;
  140. }
  141.  
  142. - registration:sender
  143. {
  144.     [self registerPanel]; // make sure nib is loaded
  145.     [self fillRegistrationText:regText];
  146.     [[self registerPanel] makeFirstResponder:regNumText];
  147.     if ([_key emptyString])
  148.         [_key setStringValue:_notRegistered];
  149.     [regNumText setStringValue:[_key stringValue]];
  150.     [regNumText selectText:self];
  151.     [NXApp runModalFor:[self registerPanel]];
  152.     return self;
  153. }
  154.  
  155. - (BOOL)registered
  156. {    // determine if the user has registered.    
  157.     if (![_key emptyString] && [self keyOK]) return YES;
  158.     return NO;
  159. }
  160.  
  161. - (BOOL)keyOK
  162. {    // override to check the "_key" variable (a MiscString) for OK values...
  163.     // you should set the stringValue of _serialNum to the serial number
  164.     // that corresponds to the key.  (ie, the serial num should be embedded
  165.     // in the key in some way along with everything else.)
  166.     return YES;
  167.     // Why don't I provide a mechanism here?  Because the code would allow
  168.     // any fool to come up with fake keys.  So you have to write your own
  169.     // code for this method for your security.  If you would like me to
  170.     // write you a simple key encoding mechanism, I'd be happy to do so;
  171.     // just contact me:  Don_Yacktman@byu.edu.  I will charge you for the
  172.     // service, but I don't ask much.  (It's based upon how long it takes
  173.     // me to write it, and since I already have some algorithms, I could use
  174.     // a simpler method that's a permutation of what I already have and
  175.     // your cost wouldn't be much more that about $15.  Contact me...)
  176.     // One other note:  A hacker could always come in and disable the
  177.     // keys.  We don't try to circumvent this at all, so you might want
  178.     // to use a more complex integrity method to fool hackers.  Again, I
  179.     // can give suggestions to the truly paranoid, for a fee.  However,
  180.     // unless you're really paranoid, there is no need for this since a
  181.     // hacker will eventually crack whatever scheme you use, and this
  182.     // scheme is sufficient to keep everyone else honest, I believe.
  183. }
  184.  
  185. - registerApp:sender        // accept registration...or not...
  186. {
  187.     [_key setStringValue:[regNumText stringValue]]; // grab the key they typed
  188.     [self writeKey];
  189.     [self fillRegistrationText:regText];
  190.     [registerPanel orderOut:self];
  191.     [NXApp stopModal];
  192.     if (![self registered])
  193.         NXRunAlertPanel(NULL, _badRegNum, NULL, NULL, NULL);
  194.     return self;
  195. }
  196.  
  197. - cancelRegistration:sender        // cancel registration modal loop...
  198. {
  199.     [registerPanel orderOut:self];
  200.     [NXApp stopModal];
  201.     return self;
  202. }
  203.  
  204. - fillRegistrationText:textField
  205. {    // sets up a text field with the registration number...
  206.     if ([self registered]) {
  207.         const char *regFormat = _reg;
  208.         id tempStr;
  209.         if (!regFormat) regFormat = "Copy #%s is registered.";
  210.         tempStr = [[MiscString alloc] initFromFormat:regFormat,
  211.                 [_serialNum stringValue]];
  212.         [[textField setStringValue:[tempStr stringValueAndFree]]
  213.                 setTextGray:NX_DKGRAY];
  214.     } else {
  215.         [[textField setStringValue:_unreg] setTextGray:NX_WHITE];
  216.     }
  217.     return self;
  218. }
  219.  
  220. - (const char *)serialNumber { return [_serialNum stringValue]; }
  221. - (const char *)registrationKey { return [_key stringValue]; };
  222.  
  223. @end
  224.