home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / lib / mail.sl < prev    next >
Encoding:
Text File  |  1994-12-12  |  3.8 KB  |  196 lines

  1. % Mail for VMS and Unix systems
  2. %
  3. % On VMS, this uses callable mail interface.  
  4. % For Unix, uses /usr/ucb/mail
  5. %
  6. % Calls mailhook if defined.  "mail_hook" can either load a file
  7. % to override some of the functions here.  This is because one cannot
  8. % define a function from within a function unless one writes the function
  9. % as a string and evals the string.  For example:
  10. %
  11. %  define create_sum_function ()
  12. %  { 
  13. %     eval ("define sum (x, y) { x + y; }");
  14. %  }
  15. %
  16.  
  17.  
  18. #ifdef UNIX
  19. %%% University of Calififornia, Berkeley mail program
  20. %%% "/usr/ucb/mail" on most unix, "/usr/sbin/Mail" on others
  21.  
  22. !if (is_defined("UCB_Mailer"))
  23. {
  24.    variable UCB_Mailer;
  25.    UCB_Mailer = "/usr/ucb/mail";
  26.    if (1 != file_status(UCB_Mailer))
  27.      {
  28.     UCB_Mailer = "/usr/sbin/Mail";
  29.     if (1 != file_status (UCB_Mailer))
  30.       {
  31.          error ("UCB Mail program not found!");
  32.       }
  33.      }
  34. }
  35.  
  36. define unix_send_mail (to, cc, subj)
  37. {
  38.    subj = str_quote_string (subj, "'", '\'');
  39.    % This is a total crock.  For ucb mail, we need to 'quote' lines beginning
  40.    % with '~'.  Lets do it now:
  41.    check_region (0);
  42.    dupmark();  pop();
  43.    narrow ();
  44.    bob();
  45.    while (bol_fsearch("~")) insert (" ");
  46.    bob ();
  47.    if (strlen(cc))
  48.      {
  49.     push_spot ();
  50.     insert (Sprintf("~c %s\n", cc, 1));
  51.      }
  52.    eob();
  53.    widen();
  54.    pipe_region (Sprintf("%s -s '%s' '%s'", UCB_Mailer, subj, to, 3));
  55.    if (strlen(cc))
  56.      {
  57.     pop_spot ();
  58.     delete_line();
  59.      }
  60.    
  61. }
  62.  
  63. #endif
  64.  
  65.  
  66. variable Mail_Previous_Buffer = Null_String;
  67. variable Mail_Previous_Windows = 1;
  68.  
  69. define send ()
  70. {
  71.    variable mail_cmd, to = Null_String, subj = Null_String;
  72.    variable dir, file, cc = Null_String, sent;
  73.   
  74.    push_spot ();
  75.    bob ();
  76.    
  77.    !if (bol_fsearch ("---text follows this line---"))
  78.      {
  79.     pop_spot ();
  80.     error ("Failed to find text divider.");
  81.      }
  82.    
  83.    push_mark ();
  84.    bob (); narrow ();
  85.    if (re_fsearch("^To:[ \t]*\\(.*\\)"))
  86.      {
  87.     to = regexp_nth_match (1);
  88.      }
  89.    if (re_fsearch("^Cc:[ \t]*\\(.*\\)"))
  90.      {
  91.     cc = regexp_nth_match (1);
  92.      }
  93.    if (re_fsearch("^Subject:[ \t]*\\(.*\\)"))
  94.      {
  95.     subj = regexp_nth_match (1);
  96.      }
  97.    
  98.    eob();
  99.    widen ();
  100.  
  101.    !if (strlen(to))
  102.      {
  103.     pop_spot ();
  104.     error ("Bad To: field.");
  105.      }
  106. #ifdef VMS
  107.    if (strlen(cc))
  108.      {
  109.     to = strcat (strcat(to, ","), cc);
  110.      }
  111. #endif
  112.       
  113.    !if (down(1))
  114.      {
  115.     pop_spot();
  116.     error ("No message!");
  117.      }
  118.    
  119.    push_mark ();
  120.    eob ();
  121.    flush("Sending...");
  122.    
  123. #ifdef UNIX
  124.    sent = unix_send_mail (to, cc, subj);
  125. #endif
  126. #ifdef VMS
  127.    narrow();
  128.    sent = vms_send_mail (to, subj);
  129.    widen ();
  130. #endif
  131.    if (sent) 
  132.      {
  133.     flush ("Sending...done");
  134.     set_buffer_modified_flag (0);
  135.     getbuf_info(); pop(); pop(); =dir; =file;
  136.     delete_file (make_autosave_filename(dir, file)); pop();
  137.      } 
  138.    else
  139.      {
  140.     message ("No message sent.");
  141.      } 
  142.    
  143.    pop_spot ();
  144.    whatbuf();
  145.    if (bufferp(Mail_Previous_Buffer)) sw2buf (Mail_Previous_Buffer);
  146.    if (1 == Mail_Previous_Windows) onewindow();
  147.    bury_buffer(());
  148. }
  149.  
  150. add_completion("send");
  151.  
  152. define mail_format_buffer ()
  153. {
  154.    variable mail_map = "mail_map";
  155.    erase_buffer();
  156.    text_mode();
  157.    insert("To: \nCc: \nSubject: \n---text follows this line---\n");
  158.    bob(); eol();
  159.    set_buffer_modified_flag(0);
  160.    set_buffer_undo(1);
  161.    !if (keymap_p(mail_map)) make_keymap(mail_map);
  162.    use_keymap(mail_map);
  163. }
  164.  
  165. define mail()
  166. {
  167.    variable mail_buf, old, status;
  168.    mail_buf = "*mail*";
  169.    variable file, dir;
  170.       
  171.    old = bufferp(mail_buf);
  172.    Mail_Previous_Windows = nwindows();
  173.    Mail_Previous_Buffer = pop2buf_whatbuf(mail_buf);
  174.    
  175.    %% if buffer is not old, turn autosave on
  176.    if (old == 0)
  177.      {
  178. #ifdef VMS
  179.     dir = "sys$login:";
  180. #else
  181.     dir = dircat(getenv("HOME"), Null_String);
  182. #endif
  183.     file = "__jed_mail__";
  184.     setbuf_info(file, dir, mail_buf, 2);
  185.      }
  186.    else
  187.      {
  188.     status = getbuf_info() & 1; pop(); pop(); pop();
  189.     if (status) return();
  190.      }
  191.    
  192.    mail_format_buffer ();
  193.    runhooks("mail_hook");
  194. }
  195.  
  196.