⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendlib.c

📁 mutt-1.5.12 源代码。linux 下邮件接受的工具。
💻 C
📖 第 1 页 / 共 4 页
字号:
  if ((info = mutt_get_content_info (a->filename, a)) == NULL)    return;  mutt_set_encoding (a, info);  mutt_stamp_attachment(a);  FREE (&a->content);  a->content = info;}BODY *mutt_make_message_attach (CONTEXT *ctx, HEADER *hdr, int attach_msg){  char buffer[LONG_STRING];  BODY *body;  FILE *fp;  int cmflags, chflags;  int pgp = WithCrypto? hdr->security : 0;  if (WithCrypto)  {    if ((option(OPTMIMEFORWDECODE) || option(OPTFORWDECRYPT)) &&        (hdr->security & ENCRYPT)) {      if (!crypt_valid_passphrase(hdr->security))        return (NULL);    }  }  mutt_mktemp (buffer);  if ((fp = safe_fopen (buffer, "w+")) == NULL)    return NULL;  body = mutt_new_body ();  body->type = TYPEMESSAGE;  body->subtype = safe_strdup ("rfc822");  body->filename = safe_strdup (buffer);  body->unlink = 1;  body->use_disp = 0;  body->disposition = DISPINLINE;  mutt_parse_mime_message (ctx, hdr);  chflags = CH_XMIT;  cmflags = 0;  /* If we are attaching a message, ignore OPTMIMEFORWDECODE */  if (!attach_msg && option (OPTMIMEFORWDECODE))  {    chflags |= CH_MIME | CH_TXTPLAIN;    cmflags = M_CM_DECODE | M_CM_CHARCONV;    if ((WithCrypto & APPLICATION_PGP))      pgp &= ~PGPENCRYPT;    if ((WithCrypto & APPLICATION_SMIME))      pgp &= ~SMIMEENCRYPT;  }  else if (WithCrypto           && option (OPTFORWDECRYPT) && (hdr->security & ENCRYPT))  {    if ((WithCrypto & APPLICATION_PGP)        && mutt_is_multipart_encrypted (hdr->content))    {      chflags |= CH_MIME | CH_NONEWLINE;      cmflags = M_CM_DECODE_PGP;      pgp &= ~PGPENCRYPT;    }    else if ((WithCrypto & APPLICATION_PGP)             && (mutt_is_application_pgp (hdr->content) & PGPENCRYPT))    {      chflags |= CH_MIME | CH_TXTPLAIN;      cmflags = M_CM_DECODE | M_CM_CHARCONV;      pgp &= ~PGPENCRYPT;    }    else if ((WithCrypto & APPLICATION_SMIME)              && mutt_is_application_smime (hdr->content) & SMIMEENCRYPT)    {      chflags |= CH_MIME | CH_TXTPLAIN;      cmflags = M_CM_DECODE | M_CM_CHARCONV;      pgp &= ~SMIMEENCRYPT;    }  }  mutt_copy_message (fp, ctx, hdr, cmflags, chflags);    fflush(fp);  rewind(fp);  body->hdr = mutt_new_header();  body->hdr->offset = 0;  /* we don't need the user headers here */  body->hdr->env = mutt_read_rfc822_header(fp, body->hdr, 0, 0);  if (WithCrypto)    body->hdr->security = pgp;  mutt_update_encoding (body);  body->parts = body->hdr->content;  fclose(fp);    return (body);}BODY *mutt_make_file_attach (const char *path){  BODY *att;  CONTENT *info;  att = mutt_new_body ();  att->filename = safe_strdup (path);  /* Attempt to determine the appropriate content-type based on the filename   * suffix.   */#if 0    if ((n = mutt_lookup_mime_type (buf, sizeof (buf), xbuf, sizeof (xbuf), path)) != TYPEOTHER       || *xbuf != '\0')  {    att->type = n;    att->subtype = safe_strdup (buf);    att->xtype = safe_strdup (xbuf);  }#else    mutt_lookup_mime_type (att, path);#endif    if ((info = mutt_get_content_info (path, att)) == NULL)  {    mutt_free_body (&att);    return NULL;  }  if (!att->subtype)  {    if (info->lobin == 0 || (info->lobin + info->hibin + info->ascii)/ info->lobin >= 10)    {      /*       * Statistically speaking, there should be more than 10% "lobin"        * chars if this is really a binary file...       */      att->type = TYPETEXT;      att->subtype = safe_strdup ("plain");    }    else    {      att->type = TYPEAPPLICATION;      att->subtype = safe_strdup ("octet-stream");    }  }   mutt_update_encoding (att);  return (att);}static int get_toplevel_encoding (BODY *a){  int e = ENC7BIT;  for (; a; a = a->next)  {    if (a->encoding == ENCBINARY)      return (ENCBINARY);    else if (a->encoding == ENC8BIT)      e = ENC8BIT;  }  return (e);}BODY *mutt_make_multipart (BODY *b){  BODY *new;  new = mutt_new_body ();  new->type = TYPEMULTIPART;  new->subtype = safe_strdup ("mixed");  new->encoding = get_toplevel_encoding (b);  mutt_generate_boundary (&new->parameter);  new->use_disp = 0;    new->disposition = DISPINLINE;  new->parts = b;  return new;}/* remove the multipart body if it exists */BODY *mutt_remove_multipart (BODY *b){  BODY *t;  if (b->parts)  {    t = b;    b = b->parts;    t->parts = NULL;    mutt_free_body (&t);  }  return b;}char *mutt_make_date (char *s, size_t len){  time_t t = time (NULL);  struct tm *l = localtime (&t);  time_t tz = mutt_local_tz (t);  tz /= 60;  snprintf (s, len,  "Date: %s, %d %s %d %02d:%02d:%02d %+03d%02d\n",	    Weekdays[l->tm_wday], l->tm_mday, Months[l->tm_mon],	    l->tm_year + 1900, l->tm_hour, l->tm_min, l->tm_sec,	    (int) tz / 60, (int) abs (tz) % 60);  return (s);}/* wrapper around mutt_write_address() so we can handle very large   recipient lists without needing a huge temporary buffer in memory */void mutt_write_address_list (ADDRESS *adr, FILE *fp, int linelen, int display){  ADDRESS *tmp;  char buf[LONG_STRING];  int count = 0;  int len;  while (adr)  {    tmp = adr->next;    adr->next = NULL;    buf[0] = 0;    rfc822_write_address (buf, sizeof (buf), adr, display);    len = mutt_strlen (buf);    if (count && linelen + len > 74)    {      fputs ("\n\t", fp);      linelen = len + 8; /* tab is usually about 8 spaces... */    }    else    {      if (count && adr->mailbox)      {	fputc (' ', fp);	linelen++;      }      linelen += len;    }    fputs (buf, fp);    adr->next = tmp;    if (!adr->group && adr->next && adr->next->mailbox)    {      linelen++;      fputc (',', fp);    }    adr = adr->next;    count++;  }  fputc ('\n', fp);}/* arbitrary number of elements to grow the array by */#define REF_INC 16#define TrimRef 10/* need to write the list in reverse because they are stored in reverse order * when parsed to speed up threading */static void write_references (LIST *r, FILE *f){  LIST **ref = NULL;  int refcnt = 0, refmax = 0;  for ( ; (TrimRef == 0 || refcnt < TrimRef) && r ; r = r->next)  {    if (refcnt == refmax)      safe_realloc (&ref, (refmax += REF_INC) * sizeof (LIST *));    ref[refcnt++] = r;  }  while (refcnt-- > 0)  {    fputc (' ', f);    fputs (ref[refcnt]->data, f);  }  FREE (&ref);}/* Note: all RFC2047 encoding should be done outside of this routine, except * for the "real name."  This will allow this routine to be used more than * once, if necessary. *  * Likewise, all IDN processing should happen outside of this routine. * * mode == 1  => "lite" mode (used for edit_hdrs) * mode == 0  => normal mode.  write full header + MIME headers * mode == -1 => write just the envelope info (used for postponing messages) *  * privacy != 0 => will omit any headers which may identify the user. *               Output generated is suitable for being sent through * 		 anonymous remailer chains. * */int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach, 			      int mode, int privacy){  char buffer[LONG_STRING];  char *p;  LIST *tmp = env->userhdrs;  int has_agent = 0; /* user defined user-agent header field exists */    if (mode == 0 && !privacy)    fputs (mutt_make_date (buffer, sizeof(buffer)), fp);  /* OPTUSEFROM is not consulted here so that we can still write a From:   * field if the user sets it with the `my_hdr' command   */  if (env->from && !privacy)  {    buffer[0] = 0;    rfc822_write_address (buffer, sizeof (buffer), env->from, 0);    fprintf (fp, "From: %s\n", buffer);  }  if (env->to)  {    fputs ("To: ", fp);    mutt_write_address_list (env->to, fp, 4, 0);  }  else if (mode > 0)    fputs ("To: \n", fp);  if (env->cc)  {    fputs ("Cc: ", fp);    mutt_write_address_list (env->cc, fp, 4, 0);  }  else if (mode > 0)    fputs ("Cc: \n", fp);  if (env->bcc)  {    if(mode != 0 || option(OPTWRITEBCC))    {      fputs ("Bcc: ", fp);      mutt_write_address_list (env->bcc, fp, 5, 0);    }  }  else if (mode > 0)    fputs ("Bcc: \n", fp);  if (env->subject)    fprintf (fp, "Subject: %s\n", env->subject);  else if (mode == 1)    fputs ("Subject: \n", fp);  /* save message id if the user has set it */  if (env->message_id && !privacy)    fprintf (fp, "Message-ID: %s\n", env->message_id);  if (env->reply_to)  {    fputs ("Reply-To: ", fp);    mutt_write_address_list (env->reply_to, fp, 10, 0);  }  else if (mode > 0)    fputs ("Reply-To: \n", fp);  if (env->mail_followup_to)  {    fputs ("Mail-Followup-To: ", fp);    mutt_write_address_list (env->mail_followup_to, fp, 18, 0);  }  if (mode <= 0)  {    if (env->references)    {      fputs ("References:", fp);      write_references (env->references, fp);      fputc('\n', fp);    }    /* Add the MIME headers */    fputs ("MIME-Version: 1.0\n", fp);    mutt_write_mime_header (attach, fp);  }  if (env->in_reply_to)  {    fputs ("In-Reply-To:", fp);    write_references (env->in_reply_to, fp);    fputc ('\n', fp);  }    /* Add any user defined headers */  for (; tmp; tmp = tmp->next)  {    if ((p = strchr (tmp->data, ':')))    {      p++; SKIPWS (p);      if (!*p) 	continue;  /* don't emit empty fields. */      /* check to see if the user has overridden the user-agent field */      if (!ascii_strncasecmp ("user-agent", tmp->data, 10))      {	has_agent = 1;	if (privacy)	  continue;      }      fputs (tmp->data, fp);      fputc ('\n', fp);    }  }  if (mode == 0 && !privacy && option (OPTXMAILER) && !has_agent)  {    /* Add a vanity header */    fprintf (fp, "User-Agent: Mutt/%s-%s\n", MUTT_VERSION, ReleaseDate);  }  return (ferror (fp) == 0 ? 0 : -1);}static void encode_headers (LIST *h){  char *tmp;  char *p;  int i;    for (; h; h = h->next)  {    if (!(p = strchr (h->data, ':')))      continue;    i = p - h->data;    ++p; SKIPWS (p);    tmp = safe_strdup (p);    if (!tmp)      continue;        rfc2047_encode_string (&tmp);    safe_realloc (&h->data, mutt_strlen (h->data) + 2 + mutt_strlen (tmp) + 1);    sprintf (h->data + i, ": %s", NONULL (tmp));  /* __SPRINTF_CHECKED__ */        FREE (&tmp);  }}const char *mutt_fqdn(short may_hide_host){  char *p = NULL, *q;    if(Fqdn && Fqdn[0] != '@')  {    p = Fqdn;        if(may_hide_host && option(OPTHIDDENHOST))    {      if((p = strchr(Fqdn, '.')))	p++;      /* sanity check: don't hide the host if       * the fqdn is something like detebe.org.       */            if(!p || !(q = strchr(p, '.')))	p = Fqdn;    }  }  return p;}char *mutt_gen_msgid (void){  char buf[SHORT_STRING];  time_t now;  struct tm *tm;  const char *fqdn;  now = time (NULL);  tm = gmtime (&now);  if(!(fqdn = mutt_fqdn(0)))    fqdn = NONULL(Hostname);  snprintf (buf, sizeof (buf), "<%d%02d%02d%02d%02d%02d.G%c%u@%s>",	    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,	    tm->tm_min, tm->tm_sec, MsgIdPfx, (unsigned int)getpid (), fqdn);  MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;  return (safe_strdup (buf));}static RETSIGTYPE alarm_handler (int sig){  SigAlrm = 1;}/* invoke sendmail in a subshell   path	(in)		path to program to execute   args	(in)		arguments to pass to program   msg (in)		temp file containing message to send   tempfile (out)	if sendmail is put in the background, this points   			to the temporary file containing the stdout of the			child process */static intsend_msg (const char *path, char **args, const char *msg, char **tempfile){  sigset_t set;  int fd, st;  pid_t pid, ppid;  mutt_block_signals_system ();  sigemptyset (&set);  /* we also don't want to be stopped right now */  sigaddset (&set, SIGTSTP);  sigprocmask (SIG_BLOCK, &set, NULL);  if (SendmailWait >= 0)  {    char tmp[_POSIX_PATH_MAX];    mutt_mktemp (tmp);    *tempfile = safe_strdup (tmp);  }  if ((pid = fork ()) == 0)  {    struct sigaction act, oldalrm;    /* save parent's ID before setsid() */    ppid = getppid ();    /* we want the delivery to continue even after the main process dies,     * so we put ourselves into another session right away     */    setsid ();      /* next we close all open files */#if defined(OPEN_MAX)    for (fd = 0; fd < OPEN_MAX; fd++)      close (fd);#elif defined(_POSIX_OPEN_MAX)    for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)      close (fd);#else    close (0);    close (1);    close (2);#endif    /* now the second fork() */    if ((pid = fork ()) == 0)    {      /* "msg" will be opened as stdin */      if (open (msg, O_RDONLY, 0) < 0)      {	unlink (msg);	_exit (S_ERR);      }      unlink (msg);      if (SendmailWait >= 0)      {	/* *tempfile will be opened as stdout */	if (open (*tempfile, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0600) < 0)	  _exit (S_ERR);	/* redirect stderr to *tempfile too */	if (dup (1) < 0)	  _exit (S_ERR);      }      else       {	if (open ("/dev/null", O_WRONLY | O_APPEND) < 0)	/* stdout */	  _exit (S_ERR);	if (open ("/dev/null", O_RDWR | O_APPEND) < 0)		/* stderr */	  _exit (S_ERR);      }      execv (path, args);      _exit (S_ERR);    }    else if (pid == -1)    {      unlink (msg);      FREE (tempfile);		/* __FREE_CHECKED__ */      _exit (S_ERR);    }    /* SendmailWait > 0: interrupt waitpid() after SendmailWait seconds     * SendmailWait = 0: wait forever     * SendmailWait < 0: don't wait     */    if (SendmailWait > 0)    {      SigAlrm = 0;      act.sa_handler = alarm_handler;#ifdef SA_INTERRUPT      /* need to make sure waitpid() is interrupted on SIGALRM */      act.sa_flags = SA_INTERRUPT;#else      act.sa_flags = 0;#endif      sigemptyset (&act.sa_mask);      sigaction (SIGALRM, &act, &oldalrm);      alarm (SendmailWait);    }    else if (SendmailWait < 0)      _exit (0xff & EX_OK);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -