📄 mailutil.c
字号:
case 'F': /* possible FLAGGED, FROM */ if (!strcmp (criterion+1,"LAGGED")) f = pgm->flagged = T; else if (!strcmp (criterion+1,"ROM")) f = mail_criteria_string (&pgm->from,&r); break; case 'K': /* possible KEYWORD */ if (!strcmp (criterion+1,"EYWORD")) f = mail_criteria_string (&pgm->keyword,&r); break; case 'L': /* possible LARGER */ if (!strcmp (criterion+1,"ARGER")) f = prune_criteria_number (&pgm->larger,&r); case 'N': /* possible NEW */ if (!strcmp (criterion+1,"EW")) f = pgm->recent = pgm->unseen = T; break; case 'O': /* possible OLD, ON */ if (!strcmp (criterion+1,"LD")) f = pgm->old = T; else if (!strcmp (criterion+1,"N")) f = mail_criteria_date (&pgm->on,&r); break; case 'R': /* possible RECENT */ if (!strcmp (criterion+1,"ECENT")) f = pgm->recent = T; break; case 'S': /* possible SEEN, SENT*, SINCE, SMALLER, SUBJECT */ if (!strcmp (criterion+1,"EEN")) f = pgm->seen = T; else if (!strncmp (criterion+1,"ENT",3)) { if (!strcmp (criterion+4,"BEFORE")) f = mail_criteria_date (&pgm->sentbefore,&r); else if (!strcmp (criterion+4,"ON")) f = mail_criteria_date (&pgm->senton,&r); else if (!strcmp (criterion+4,"SINCE")) f = mail_criteria_date (&pgm->sentsince,&r); } else if (!strcmp (criterion+1,"INCE")) f = mail_criteria_date (&pgm->since,&r); else if (!strcmp (criterion+1,"MALLER")) f = prune_criteria_number (&pgm->smaller,&r); else if (!strcmp (criterion+1,"UBJECT")) f = mail_criteria_string (&pgm->subject,&r); break; case 'T': /* possible TEXT, TO */ if (!strcmp (criterion+1,"EXT")) f = mail_criteria_string (&pgm->text,&r); else if (!strcmp (criterion+1,"O")) f = mail_criteria_string (&pgm->to,&r); break; case 'U': /* possible UN* */ if (criterion[1] == 'N') { if (!strcmp (criterion+2,"ANSWERED")) f = pgm->unanswered = T; else if (!strcmp (criterion+2,"DELETED")) f = pgm->undeleted = T; else if (!strcmp (criterion+2,"DRAFT")) f = pgm->undraft = T; else if (!strcmp (criterion+2,"FLAGGED")) f = pgm->unflagged = T; else if (!strcmp (criterion+2,"KEYWORD")) f = mail_criteria_string (&pgm->unkeyword,&r); else if (!strcmp (criterion+2,"SEEN")) f = pgm->unseen = T; } break; default: /* we will barf below */ break; } if (!f) { /* if can't identify criterion */ sprintf (tmp,"Unknown search criterion: %.30s",criterion); MM_LOG (tmp,ERROR); mail_free_searchpgm (&pgm); break; } } /* no longer need copy of criteria */ fs_give ((void **) &criteria); } return pgm;}/* Parse a number * Accepts: pointer to integer to return * pointer to strtok state * Returns: T if successful, else NIL */int prune_criteria_number (unsigned long *number,char **r){ char *t; STRINGLIST *s = NIL; /* parse the date and return fn if OK */ int ret = (mail_criteria_string (&s,r) && (*number = strtoul ((char *) s->text.data,&t,10)) && !*t) ? T : NIL; if (s) mail_free_stringlist (&s); return ret;}/* Copy mailbox * Accepts: stream open on source * halfopen stream for destination or NIL * destination mailbox name * non-zero to create destination mailbox * non-zero to delete messages from source after copying * merge mode * Returns: T if success, NIL if error */int mbxcopy (MAILSTREAM *source,MAILSTREAM *dest,char *dst,int create,int del, int mode){ char *s,tmp[MAILTMPLEN]; APPENDPACKAGE ap; STRING st; char *ndst = NIL; int ret = NIL; trycreate = NIL; /* no TRYCREATE yet */ if (create) while (!mail_create (dest,dst) && (mode != mAPPEND)) { switch (mode) { case mPROMPT: /* prompt user for new name */ tmp[0] = '\0'; while (!tmp[0]) { /* read name */ fputs ("alternative name: ",stdout); fflush (stdout); fgets (tmp,MAILTMPLEN-1,stdin); if (s = strchr (tmp,'\n')) *s = '\0'; } if (ndst) fs_give ((void **) &ndst); ndst = cpystr (tmp); break; case mSUFFIX: /* try again with new suffix */ if (ndst) fs_give ((void **) &ndst); sprintf (ndst = (char *) fs_get (strlen (dst) + strlen (suffix) + 1), "%s%s",dst,suffix); printf ("retry to create %s\n",ndst); mode = mPROMPT; /* switch to prompt mode if name fails */ break; case NIL: /* not merging */ return NIL; } if (ndst) dst = ndst; /* if alternative name given, use it */ } if (kwcopyp) { int i; size_t len; char *dummymsg = "Date: Thu, 18 May 2006 00:00 -0700\r\nFrom: dummy@example.com\r\nSubject: dummy\r\n\r\ndummy\r\n"; for (i = 0,len = 0; i < NUSERFLAGS; ++i) if (source->user_flags[i]) len += strlen (source->user_flags[i]) + 1; if (len) { /* easy if no user flags to copy... */ char *t; char *tail = "\\Deleted)"; char *flags = (char *) fs_get (1 + len + strlen (tail) + 1); s = flags; *s++ = '('; for (i = 0; i < NUSERFLAGS; ++i) if (t = source->user_flags[i]) { while (*t) *s++ = *t++; *s++ = ' '; } strcpy (s,tail); /* terminate flags list */ if ((dst[0] == '#') && ((dst[1] == 'D') || (dst[1] == 'd')) && ((dst[2] == 'R') || (dst[2] == 'r')) && ((dst[3] == 'I') || (dst[3] == 'i')) && ((dst[4] == 'V') || (dst[4] == 'v')) && ((dst[5] == 'E') || (dst[5] == 'e')) && ((dst[6] == 'R') || (dst[6] == 'r')) && (dst[7] == '.') && (t = strchr (dst+8,'/'))) ++t; else t = dst; INIT (&st,mail_string,dummymsg,strlen (dummymsg)); if (!(mail_append (dest,dst,&st) && (dest = mail_open (dest,t,debugp ? OP_DEBUG : NIL)))) { fs_give ((void **) &flags); return NIL; } mail_setflag (dest,"*",flags); mail_expunge (dest); fs_give ((void **) &flags); } } if (source->nmsgs) { /* non-empty source */ if (verbosep) printf ("%s [%lu message(s)] => %s\n", source->mailbox,source->nmsgs,dst); ap.stream = source; /* prepare append package */ ap.msgno = 0; ap.msgmax = source->nmsgs; ap.flags = ap.date = NIL; ap.message = &st; /* make sure we have all messages */ sprintf (tmp,"1:%lu",ap.msgmax); mail_fetchfast (source,tmp); if (mail_append_multiple (dest,dst,mm_append,(void *) &ap)) { --ap.msgno; /* make sure user knows it won */ if (verbosep) printf ("[Ok %lu messages(s)]\n",ap.msgno); if (del && ap.msgno) { /* delete source messages */ sprintf (tmp,"1:%lu",ap.msgno); mail_flag (source,tmp,"\\Deleted",ST_SET); /* flush moved messages */ mail_expunge (source); } ret = T; } else if ((mode == mAPPEND) && trycreate) ret = mbxcopy (source,dest,dst,create,del,mPROMPT); else if (verbosep) puts ("[Failed]"); } else { /* empty source */ if (verbosep) printf ("%s [empty] => %s\n",source->mailbox,dst); ret = T; } if (ndst) fs_give ((void **) &ndst); return ret;}/* Append callback * Accepts: mail stream * append package * pointer to return flags * pointer to return date * pointer to return message stringstruct * Returns: T on success */long mm_append (MAILSTREAM *stream,void *data,char **flags,char **date, STRING **message){ char *t,*t1,tmp[MAILTMPLEN]; unsigned long u; MESSAGECACHE *elt; APPENDPACKAGE *ap = (APPENDPACKAGE *) data; *flags = *date = NIL; /* assume no flags or date */ if (ap->flags) fs_give ((void **) &ap->flags); if (ap->date) fs_give ((void **) &ap->date); mail_gc (ap->stream,GC_TEXTS); if (++ap->msgno <= ap->msgmax) { /* initialize flag string */ memset (t = tmp,0,MAILTMPLEN); /* output system flags */ if ((elt = mail_elt (ap->stream,ap->msgno))->seen) strcat (t," \\Seen"); if (elt->deleted) strcat (t," \\Deleted"); if (elt->flagged) strcat (t," \\Flagged"); if (elt->answered) strcat (t," \\Answered"); if (elt->draft) strcat (t," \\Draft"); /* any user flags? */ if (!ignorep && (u = elt->user_flags)) do if ((t1 = ap->stream->user_flags[find_rightmost_bit (&u)]) && (MAILTMPLEN - ((t += strlen (t)) - tmp)) > (long) (2 + strlen (t1))){ *t++ = ' '; /* space delimiter */ strcpy (t,t1); /* copy the user flag */ } while (u); /* until no more user flags */ *flags = ap->flags = cpystr (tmp + 1); *date = ap->date = cpystr (mail_date (tmp,elt)); *message = ap->message; /* message stringstruct */ INIT (ap->message,mstring,(void *) ap,elt->rfc822_size); } else *message = NIL; /* all done */ return LONGT;}/* Co-routines from MAIL library *//* Message matches a search * Accepts: MAIL stream * message number */void mm_searched (MAILSTREAM *stream,unsigned long msgno){ /* dummy routine */}/* Message exists (i.e. there are that many messages in the mailbox) * Accepts: MAIL stream * message number */void mm_exists (MAILSTREAM *stream,unsigned long number){ /* dummy routine */}/* Message expunged * Accepts: MAIL stream * message number */void mm_expunged (MAILSTREAM *stream,unsigned long number){ /* dummy routine */}/* Message flags update seen * Accepts: MAIL stream * message number */void mm_flags (MAILSTREAM *stream,unsigned long number){ /* dummy routine */}/* Mailbox found * Accepts: MAIL stream * hierarchy delimiter * mailbox name * mailbox attributes */void mm_list (MAILSTREAM *stream,int delimiter,char *name,long attributes){ /* note destination delimiter */ if (ddelim < 0) ddelim = delimiter; /* if got a selectable name */ else if (!(attributes & LATT_NOSELECT) && *name) fprintf (f,"%c%s\n",delimiter,name);}/* Subscribe mailbox found * Accepts: MAIL stream * hierarchy delimiter * mailbox name * mailbox attributes */void mm_lsub (MAILSTREAM *stream,int delimiter,char *name,long attributes){ /* dummy routine */}/* Mailbox status * Accepts: MAIL stream * mailbox name * mailbox status */void mm_status (MAILSTREAM *stream,char *mailbox,MAILSTATUS *status){ if (status->recent || status->unseen) printf ("%lu new message(s) (%lu unseen),",status->recent,status->unseen); else fputs ("No new messages,",stdout); printf (" %lu total in %s\n",status->messages,mailbox);}/* Notification event * Accepts: MAIL stream * string to log * error flag */void mm_notify (MAILSTREAM *stream,char *string,long errflg){ if (!errflg && (string[0] == '[') && ((string[1] == 'T') || (string[1] == 't')) && ((string[2] == 'R') || (string[2] == 'r')) && ((string[3] == 'Y') || (string[3] == 'y')) && ((string[4] == 'C') || (string[4] == 'c')) && ((string[5] == 'R') || (string[5] == 'r')) && ((string[6] == 'E') || (string[6] == 'e')) && ((string[7] == 'A') || (string[7] == 'a')) && ((string[8] == 'T') || (string[8] == 't')) && ((string[9] == 'E') || (string[9] == 'e')) && (string[10] == ']')) trycreate = T; mm_log (string,errflg); /* just do mm_log action */}/* Log an event for the user to see * Accepts: string to log * error flag */void mm_log (char *string,long errflg){ switch (errflg) { case BYE: case NIL: /* no error */ if (verbosep) fprintf (stderr,"[%s]\n",string); break; case PARSE: /* parsing problem */ case WARN: /* warning */ fprintf (stderr,"warning: %s\n",string); break; case ERROR: /* error */ default: fprintf (stderr,"%s\n",string); break; }}/* Log an event to debugging telemetry * Accepts: string to log */void mm_dlog (char *string){ fprintf (stderr,"%s\n",string);}/* Get user name and password for this host * Accepts: parse of network mailbox name * where to return user name * where to return password * trial count */void mm_login (NETMBX *mb,char *username,char *password,long trial){ char *s,tmp[MAILTMPLEN]; sprintf (s = tmp,"{%s/%s",mb->host,mb->service); if (*mb->user) sprintf (tmp+strlen (tmp),"/user=%s", strcpy (username,mb->user)); if (*mb->authuser) sprintf (tmp+strlen (tmp),"/authuser=%s",mb->authuser); if (*mb->user) strcat (s = tmp,"} password:"); else { printf ("%s} username: ",tmp); fgets (username,NETMAXUSER-1,stdin); username[NETMAXUSER-1] = '\0'; if (s = strchr (username,'\n')) *s = '\0'; s = "password: "; } strcpy (password,getpass (s));}/* About to enter critical code * Accepts: stream */void mm_critical (MAILSTREAM *stream){ critical = T; /* note in critical code */}/* About to exit critical code * Accepts: stream */void mm_nocritical (MAILSTREAM *stream){ critical = NIL; /* note not in critical code */}/* Disk error found * Accepts: stream * system error code * flag indicating that mailbox may be clobbered * Returns: T if user wants to abort */long mm_diskerror (MAILSTREAM *stream,long errcode,long serious){ return T;}/* Log a fatal error event * Accepts: string to log */void mm_fatal (char *string){ fprintf (stderr,"FATAL: %s\n",string);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -