home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / osdep / amiga / unix.h < prev    next >
Text File  |  1998-10-22  |  12KB  |  291 lines

  1. /*
  2.  * Program:    UNIX mail routines, Amiga version
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    20 December 1989
  13.  * Last Edited:    22 October 1998
  14.  *
  15.  * Copyright 1998 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. /*                DEDICATION
  38.  *
  39.  *  This file is dedicated to my dog, Unix, also known as Yun-chan and
  40.  * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
  41.  * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
  42.  * a two-month bout with cirrhosis of the liver.
  43.  *
  44.  *  He was a dear friend, and I miss him terribly.
  45.  *
  46.  *  Lift a leg, Yunie.  Luv ya forever!!!!
  47.  */
  48.  
  49. /* Validate line
  50.  * Accepts: pointer to candidate string to validate as a From header
  51.  *        return pointer to end of date/time field
  52.  *        return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
  53.  *        return pointer to offset from t of time zone (if non-zero)
  54.  * Returns: t,ti,zn set if valid From string, else ti is NIL
  55.  */
  56.  
  57. #define VALID(s,x,ti,zn) {                        \
  58.   int remote = 0;                            \
  59.   ti = 0;                                \
  60.   if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') &&    \
  61.       (s[4] == ' ')) {                            \
  62.     for (x = s + 5; *x && *x != '\012'; x++);                \
  63.     if (*x) {                                \
  64.       if (x[-1] == '\015') --x;                        \
  65.       if (x - s >= 41) {                        \
  66.     for (zn = -1; x[zn] != ' '; zn--);                \
  67.     if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') &&    \
  68.         (x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') &&    \
  69.         (x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') &&    \
  70.         (x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
  71.       {                                \
  72.         while (x[zn-13] == ' ') zn--;                \
  73.         x += zn - 12;                        \
  74.         remote = 1;                            \
  75.       }                                \
  76.       }                                    \
  77.       if (x - s >= 27) {                        \
  78.     if (x[-5] == ' ') {                        \
  79.       if (x[-8] == ':') zn = 0,ti = -5;                \
  80.       else if (x[-9] == ' ') ti = zn = -9;                \
  81.       else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-')))    \
  82.         ti = zn = -11;                        \
  83.     }                                \
  84.     else if (x[-4] == ' ') {                    \
  85.       if (x[-9] == ' ') zn = -4,ti = -9;                \
  86.       else if ( (x[-13] == ' ') && (x[-16] == ' ')            \
  87.         && (x[-20] ==' ') &&                    \
  88.         ( ((x[-22] == ' ') && (x[-23] == ',')) ||        \
  89.           ((x[-23] == ' ') && (x[-24] == ',')) ) ) {        \
  90.         char weekday[4]={0,}, month[4]={0,}, time[11]={0,};        \
  91.         char tzone[4]={0,};                     \
  92.         char realtime[80];                        \
  93.         int day,year,start=-26;                    \
  94.         if (x[-23] == ' ') x--;                    \
  95.           sscanf(&x[start],"%3c, %d %s %d %s %s",            \
  96.         weekday,&day,month,&year,time,tzone);            \
  97.           sprintf(realtime,"%s %s %2d %s %d %s",            \
  98.         weekday,month,day,time,                 \
  99.         ( (year < 100) ? year+1900 : year),tzone);        \
  100.           if (remote)                        \
  101.         strcat(realtime," remote from ");            \
  102.           else                            \
  103.         strcat(realtime,"\n");                    \
  104.           strncpy(&x[start],realtime,strlen(realtime));        \
  105.           zn = -2;                            \
  106.           ti = -7;                            \
  107.       }                                \
  108.     }                                \
  109.     else if (x[-6] == ' ') {                    \
  110.       if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-')))    \
  111.         zn = -6,ti = -11;                        \
  112.     }                                \
  113.     else if (x[-9] == ' ') {                    \
  114.         if ( ( (x[-12] == ' ') && (x[-16] == ' ') &&        \
  115.           ( ((x[-18] == ' ') && (x[-19] == ',') )  ||        \
  116.             ((x[-19] == ' ') && (x[-20] == ',')) )        \
  117.         ||                            \
  118.         ((x[-14] == ' ') && (x[-18] == ' ') &&            \
  119.           ( ((x[-20] == ' ') && (x[-21] == ',') )  ||        \
  120.             ((x[-21] == ' ') && (x[-22] == ',')) ) ) ) ) {    \
  121.           char weekday[4]={0,}, month[4]={0,},time[11]={0,};    \
  122.           int day,year,start=-24;                    \
  123.           char realtime[80];                    \
  124.           if (x[-12] == ' ') x++;                    \
  125.           if (x[-19] == ' ') x++;                    \
  126.           sscanf(&x[start],"%3c, %d %3c %d %s",weekday,        \
  127.              &day,month,&year,time);                \
  128.           sprintf(realtime,"%s %s %2d %s %d",weekday,month,day,time,\
  129.          ( (year < 100) ? year+1900 : year));            \
  130.           if (remote)                        \
  131.         strcat(realtime," remote from ");            \
  132.           else                            \
  133.         strcat(realtime,"\n");                    \
  134.           strncpy(&x[start],realtime,strlen(realtime));        \
  135.           ti=-5;                            \
  136.           zn=0;                            \
  137.         }                                \
  138.     }                                \
  139.     if (ti && !((x[ti - 3] == ':') &&                \
  140.             (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') &&    \
  141.             (x[ti - 3] == ' ') && (x[ti - 7] == ' ') &&        \
  142.             (x[ti - 11] == ' '))) ti = 0;            \
  143.       }                                    \
  144.     }                                    \
  145.   }                                    \
  146. }
  147.  
  148. /* You are not expected to understand this macro, but read the next page if
  149.  * you are not faint of heart.
  150.  *
  151.  * Known formats to the VALID macro are:
  152.  *        From user Wed Dec  2 05:53 1992
  153.  * BSD        From user Wed Dec  2 05:53:22 1992
  154.  * SysV        From user Wed Dec  2 05:53 PST 1992
  155.  * rn        From user Wed Dec  2 05:53:22 PST 1992
  156.  *        From user Wed Dec  2 05:53 -0700 1992
  157.  * emacs    From user Wed Dec  2 05:53:22 -0700 1992
  158.  *        From user Wed Dec  2 05:53 1992 PST
  159.  *        From user Wed Dec  2 05:53:22 1992 PST
  160.  *        From user Wed Dec  2 05:53 1992 -0700
  161.  * Solaris    From user Wed Dec  2 05:53:22 1992 -0700
  162.  * Amiga    From user Wed, 6 Dec 92 05:53:22 who did this !!!
  163.  *        CHANGED in place to
  164.  *        From user Wed Dec  2 05:53:22 1992
  165.  *
  166.  * Plus all of the above with `` remote from xxx'' after it. Thank you very
  167.  * much, smail and Solaris, for making my life considerably more complicated.
  168.  */
  169.  
  170. /*
  171.  * What?  You want to understand the VALID macro anyway?  Alright, since you
  172.  * insist.  Actually, it isn't really all that difficult, provided that you
  173.  * take it step by step.
  174.  *
  175.  * Line 1    Initializes the return ti value to failure (0);
  176.  * Lines 2-3    Validates that the 1st-5th characters are ``From ''.
  177.  * Lines 4-6    Validates that there is an end of line and points x at it.
  178.  * Lines 7-14    First checks to see if the line is at least 41 characters long.
  179.  *        If so, it scans backwards to find the rightmost space.  From
  180.  *        that point, it scans backwards to see if the string matches
  181.  *        `` remote from''.  If so, it sets x to point to the space at
  182.  *        the start of the string.
  183.  * Line 15    Makes sure that there are at least 27 characters in the line.
  184.  * Lines 16-21    Checks if the date/time ends with the year (there is a space
  185.  *        five characters back).  If there is a colon three characters
  186.  *        further back, there is no timezone field, so zn is set to 0
  187.  *        and ti is set in front of the year.  Otherwise, there must
  188.  *        either to be a space four characters back for a three-letter
  189.  *        timezone, or a space six characters back followed by a + or -
  190.  *        for a numeric timezone; in either case, zn and ti become the
  191.  *        offset of the space immediately before it.
  192.  * Lines 22-24    Are the failure case for line 14.  If there is a space four
  193.  *        characters back, it is a three-letter timezone; there must be a
  194.  *        space for the year nine characters back.  zn is the zone
  195.  *        offset; ti is the offset of the space.
  196.  * Lines 25-28    Are the failure case for line 20.  If there is a space six
  197.  *        characters back, it is a numeric timezone; there must be a
  198.  *        space eleven characters back and a + or - five characters back.
  199.  *        zn is the zone offset; ti is the offset of the space.
  200.  * Line 29-32    If ti is valid, make sure that the string before ti is of the
  201.  *        form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
  202.  *        invalidate ti.  There must be a colon three characters back
  203.  *        and a space six or nine    characters back (depending upon
  204.  *        whether or not the character six characters back is a colon).
  205.  *        There must be a space three characters further back (in front
  206.  *        of the day), one seven characters back (in front of the month),
  207.  *        and one eleven characters back (in front of the day of week).
  208.  *        ti is set to be the offset of the space before the time.
  209.  *
  210.  * Why a macro?  It gets invoked a *lot* in a tight loop.  On some of the
  211.  * newer pipelined machines it is faster being open-coded than it would be if
  212.  * subroutines are called.
  213.  *
  214.  * Why does it scan backwards from the end of the line, instead of doing the
  215.  * much easier forward scan?  There is no deterministic way to parse the
  216.  * ``user'' field, because it may contain unquoted spaces!  Yes, I tested it to
  217.  * see if unquoted spaces were possible.  They are, and I've encountered enough
  218.  * evil mail to be totally unwilling to trust that ``it will never happen''.
  219.  */
  220.  
  221. /* Build parameters */
  222.  
  223. #define KODRETRY 15        /* kiss-of-death retry in seconds */
  224. #define LOCKTIMEOUT 5        /* lock timeout in minutes */
  225. #define CHUNK 16384        /* read-in chunk size */
  226.  
  227.  
  228. /* UNIX I/O stream local data */
  229.  
  230. typedef struct unix_local {
  231.   unsigned int dirty : 1;    /* disk copy needs updating */
  232.   int fd;            /* mailbox file descriptor */
  233.   int ld;            /* lock file descriptor */
  234.   char *lname;            /* lock file name */
  235.   off_t filesize;        /* file size parsed */
  236.   time_t filetime;        /* last file time */
  237.   time_t lastsnarf;        /* last snarf time (for mbox driver) */
  238.   char *buf;            /* temporary buffer */
  239.   unsigned long buflen;        /* current size of temporary buffer */
  240.   char *line;            /* returned line */
  241. } UNIXLOCAL;
  242.  
  243.  
  244. /* Convenient access to local data */
  245.  
  246. #define LOCAL ((UNIXLOCAL *) stream->local)
  247.  
  248. /* Function prototypes */
  249.  
  250. DRIVER *unix_valid (char *name);
  251. long unix_isvalid (char *name,char *tmp);
  252. long unix_isvalid_fd (int fd,char *tmp);
  253. void *unix_parameters (long function,void *value);
  254. void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
  255. void unix_list (MAILSTREAM *stream,char *ref,char *pat);
  256. void unix_lsub (MAILSTREAM *stream,char *ref,char *pat);
  257. long unix_create (MAILSTREAM *stream,char *mailbox);
  258. long unix_delete (MAILSTREAM *stream,char *mailbox);
  259. long unix_rename (MAILSTREAM *stream,char *old,char *newname);
  260. MAILSTREAM *unix_open (MAILSTREAM *stream);
  261. void unix_close (MAILSTREAM *stream,long options);
  262. char *unix_header (MAILSTREAM *stream,unsigned long msgno,
  263.            unsigned long *length,long flags);
  264. long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
  265. char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
  266.               unsigned long *length,long flags);
  267. void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
  268. long unix_ping (MAILSTREAM *stream);
  269. void unix_check (MAILSTREAM *stream);
  270. void unix_check (MAILSTREAM *stream);
  271. void unix_expunge (MAILSTREAM *stream);
  272. long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
  273. long unix_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  274.           STRING *message);
  275. long unix_append_putc (int fd,char *s,long *i,char c);
  276.  
  277. void unix_abort (MAILSTREAM *stream);
  278. char *unix_file (char *dst,char *name);
  279. int unix_lock (char *file,int flags,int mode,char *lock,int op);
  280. void unix_unlock (int fd,MAILSTREAM *stream,char *lock);
  281. int unix_parse (MAILSTREAM *stream,char *lock,int op);
  282. char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size);
  283. unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr);
  284. unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
  285.                 long flag);
  286. long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp);
  287. long unix_write_message (FILE *f,MAILSTREAM *stream,MESSAGECACHE *elt,
  288.              unsigned long *size);
  289. long unix_fwrite (FILE *f,char *s,unsigned long i,unsigned long *size);
  290. long unix_punt_scratch (FILE *f);
  291.