home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / MailEnclosure / MESendModules / Source / UnixScript / UnixScript.m < prev   
Encoding:
Text File  |  1995-06-12  |  2.2 KB  |  91 lines

  1. #import "UnixScript.h"
  2. #import "FileName.h"
  3. #import "support.h"
  4. #import "EnhancedApp.h"
  5. #include <sys/file.h>
  6.  
  7. const char *TAR_COMMAND = "tar chf - * | compress > ";
  8. const char *SCRIPT_HEADER = "\n#BEGIN_HERE \n\
  9. # This shell script unpacks a uuencoded, compressed, tar file. \n\
  10. # You can run it in one of two ways: \n\
  11. #    1) save the entire text to a file and then: \n\
  12. #        awk \"/^#BEGIN_HERE/,/^exit/\" < file | /bin/sh \n\
  13. #    2) Or, remove everything before #BEGIN_HERE, save to file and then: \n\
  14. #        /bin/sh < file \n\
  15. if test -d attachments \n\
  16. then \n\
  17.   echo \"An attachments directory exits. Remove, rename or move it.\" \n\
  18. else \n\
  19. mkdir attachments; cd attachments \n\
  20. uudecode << \\ALL_DONE\n";
  21.  
  22. const char *SCRIPT_TRAILER = "ALL_DONE\n\
  23. zcat .attach.Z | tar xf - \n\
  24. rm .attach.Z \n\
  25. fi \n\
  26. exit 0 \n";
  27.  
  28. @implementation UnixScript
  29.  
  30. - initForMUA: (const char *)anApp  andMTA: (const char *)aMailer
  31. {
  32.    [super initForMUA: anApp andMTA: aMailer];
  33.    tmpDir = [[StringStorage alloc] init:"/tmp/.attachments"];
  34.    [tmpDir mktemp];
  35.    mkdir([tmpDir stringValue],0700);
  36.    attachFile = [[StringStorage alloc] init: [tmpDir stringValue]];
  37.    [attachFile appendStringValue: ".attach.Z"];
  38.    return self;
  39. }
  40.  
  41. - free
  42. {
  43.    int x, cnt;
  44.  
  45.    unlink([attachFile stringValue]);         /* unlink the compressed tar file */
  46.  
  47.    chdir([tmpDir stringValue]);             /* clean up the enclosure directory */
  48.  
  49.    for(x = 0, cnt = [attachments count]; x < cnt; x++)
  50.        unlink([[attachments objectAt: x] basename]);
  51.  
  52.    chdir([NXApp appDirectory]);
  53.    rmdir([tmpDir stringValue]);             /* remove it */
  54.  
  55.    [tmpDir free];
  56.    [attachFile free];
  57.    return [super free];
  58. }
  59.  
  60. - buildBody
  61. {
  62.    char tarCmd[FILENAME_MAX + 30];
  63.    int x, cnt;
  64.    id aFile;
  65.  
  66.    [super buildBody];
  67.    
  68.    fwrite(SCRIPT_HEADER,1,strlen(SCRIPT_HEADER), fp);
  69.  
  70.    chdir([tmpDir stringValue]);
  71.    for(x = 0, cnt = [attachments count]; x < cnt; x++)
  72.    {
  73.       aFile = [attachments objectAt: x];
  74.       symlink([aFile stringValue],[aFile basename]);
  75.    }
  76.  
  77.    strcpy(tarCmd, TAR_COMMAND);
  78.    strcat(tarCmd, [attachFile stringValue]);
  79.    system(tarCmd);
  80.    uuencode(".attach.Z", [attachFile stringValue], fp, 0666);
  81.    fwrite(SCRIPT_TRAILER,1,strlen(SCRIPT_TRAILER),fp);
  82.    return self;
  83. }
  84.  
  85. + (BOOL)supportsAttachments
  86. {
  87.    return YES;
  88. }
  89.  
  90. @end
  91.