home *** CD-ROM | disk | FTP | other *** search
/ zipcon.net / www.zipcon.net.tar / www.zipcon.net / pub / linux / pine-4.43-maildir.patch < prev    next >
Text File  |  2002-02-28  |  46KB  |  1,403 lines

  1. diff -Nur pine4.43/README.maildir pine4.43-maildir/README.maildir
  2. --- pine4.43/README.maildir    Thu Jan  1 01:00:00 1970
  3. +++ pine4.43-maildir/README.maildir    Wed Dec  5 09:32:47 2001
  4. @@ -0,0 +1,93 @@
  5. +Pine 4.0x Maildir c-client module
  6. +---------------------------------
  7. +
  8. +Written by Mattias Larsson <ml@techno.org> <mta@freeit.com>
  9. +
  10. +This is the second release of my Maildir driver for Pine 4. It is based
  11. +on Eric Greens IMAP2bis Maildir driver.
  12. +
  13. +PLEASE NOTE that this driver has only been tested in the IMAP4rev1 daemon
  14. +before. It was just put into Pine 4.00, and it looks like it works, but it
  15. +has not been extensively tested. It has been running for 8 months in
  16. +production in our IMAP4 daemon though.
  17. +
  18. +PLEASE NOTE:
  19. +
  20. +This driver needs to store the UID's of the message in the file somehow. In
  21. +the earlier versions of this driver this was done by adding a new maildir
  22. +flag (the ,3 flag), however, this solution was not very good since most
  23. +other clients could not handle it. Thus I had to find another solution. In
  24. +this driver I use a pretty radical method. Any file in the maildir with the
  25. +execute bit set has its UID set in the mtime of the file. So you should not
  26. +edit the files in here, or in any otherway update the mtime, because then
  27. +the UID gets changed. Most clients should not do this, so I think this
  28. +solution is compatible with most other clients (if you find one that isn't,
  29. +let me know). If you for some reason have to edit a file in the Maildir,
  30. +delete the ".uidvalidity" file in the root of the Maildir (ie, the dir where
  31. +you find cur/ new/ and tmp/). Alternatively, edit maildir.c and define the
  32. +NO_UID_VALIDITY option, which will cause the Maildir to get set new UID's on
  33. +every start up. Note that if you are running IMAP and Netscape as a client,
  34. +you can't change the UID's, because Netscape do not support the UID validity
  35. +flag in the IMAP protocol. So use this with care. 
  36. +
  37. +Please edit the maildir.c file in any case. There are 3 options you can
  38. +set yourself. The default configuration is not suitable for ISPs. If you are
  39. +an ISP, or system with many users, you might want to consider setting some
  40. +of the options different.
  41. +
  42. +Ohh, if there are problems compiling it, let me know, and please let me know
  43. +what you did to fix it. This thing was developed on Solaris (using both GCC
  44. +and SunCC, should work with both), but I haven't tried it on any other
  45. +platform. It is also known to compile cleanly on Linux RH5.1
  46. +
  47. +CONFIGURATION
  48. +-------------
  49. +
  50. +There are a few configurable options. You find these at the top of the
  51. +maildir.c file (and it can be found in imap/src/osdep/unix if I'm not all
  52. +mistaken). Right now, two options are configurable. By default it is
  53. +configured for ISP use, something that you might want to change if you use
  54. +it at home.
  55. +
  56. +HOW TO USE
  57. +----------
  58. +
  59. +Use it as any other c-client driver. There is some option you want to change
  60. +if you want all folders to be created as Maildirs (and I can't remember what
  61. +the option is from the top of my head). Read the pine documentation.
  62. +
  63. +CHANGES
  64. +-------
  65. +
  66. +Rel 4.  Coredump problem fixed. In release 3 I decided to user the sparep
  67. +    in the message cache so no patching of mail.h would be necessary,
  68. +    however, PINE uses this pointer internally for other things, causing
  69. +    coredumps when used with the Rel 3. patch.
  70. +
  71. +Rel 3.    New way of storing UID's (compatible with ,2 clients).
  72. +    Multiple inbox patches applied
  73. +
  74. +Rel 2.  Pine 4 changes.
  75. +
  76. +Rel 1.  Imap4rev 1 driver
  77. +
  78. +FINAL NOTES
  79. +-----------
  80. +
  81. +I'll try to maintain and release new versions as soon as I have time over,
  82. +which unfortunately does not happen very often in this business ;)
  83. +
  84. +You can (might) find newer versions of this driver at:
  85. +
  86. +http://www.freeit.com/mta/
  87. +
  88. +
  89. +Regards,
  90. +Daniel Mattias Larsson
  91. +
  92. +e-mail: ml@techno.org
  93. +ph: +46-707-268785
  94. +snail-mail:
  95. +Industrivagen 4
  96. +SE-194 77 Upplands Vasby
  97. +SWEDEN
  98. diff -Nur pine4.43/imap/src/c-client/mail.h pine4.43-maildir/imap/src/c-client/mail.h
  99. --- pine4.43/imap/src/c-client/mail.h    Tue Nov 13 20:50:10 2001
  100. +++ pine4.43-maildir/imap/src/c-client/mail.h    Wed Dec  5 09:32:47 2001
  101. @@ -692,6 +692,7 @@
  102.    unsigned int spare2 : 1;    /* second spare bit */
  103.    unsigned int spare3 : 1;    /* third spare bit */
  104.    void *sparep;            /* spare pointer */
  105. +  void *maildirp;        /* for the Maildir driver */
  106.    unsigned long user_flags;    /* user-assignable flags */
  107.  } MESSAGECACHE;
  108.  
  109. diff -Nur pine4.43/imap/src/osdep/unix/Makefile pine4.43-maildir/imap/src/osdep/unix/Makefile
  110. --- pine4.43/imap/src/osdep/unix/Makefile    Wed Nov  7 01:23:48 2001
  111. +++ pine4.43-maildir/imap/src/osdep/unix/Makefile    Wed Dec  5 09:54:38 2001
  112. @@ -21,7 +21,7 @@
  113.  # Command line build parameters
  114.  
  115.  EXTRAAUTHENTICATORS=
  116. -EXTRADRIVERS=mbox
  117. +EXTRADRIVERS=maildir mbox
  118.  PASSWDTYPE=std
  119.  SSLTYPE=none
  120.  
  121. @@ -105,7 +105,7 @@
  122.  # Standard distribution build parameters
  123.  
  124.  DEFAULTAUTHENTICATORS=md5 pla log
  125. -DEFAULTDRIVERS=imap nntp pop3 mh mx mbx tenex mtx mmdf unix news phile
  126. +DEFAULTDRIVERS=maildir imap nntp pop3 mh mx mbx tenex mtx mmdf unix news phile
  127.  
  128.  
  129.  # Normally no need to change any of these
  130. @@ -114,7 +114,7 @@
  131.  BINARIES=mail.o misc.o newsrc.o smanager.o osdep.o utf8.o siglocal.o \
  132.   dummy.o pseudo.o netmsg.o flstring.o fdstring.o \
  133.   rfc822.o nntp.o smtp.o imap4r1.o pop3.o \
  134. - unix.o mbox.o mbx.o mmdf.o tenex.o mtx.o news.o phile.o mh.o mx.o
  135. + unix.o mbox.o mbx.o mmdf.o tenex.o mtx.o news.o phile.o mh.o mx.o maildir.o
  136.  CFLAGS=-g
  137.  
  138.  CAT=cat
  139. @@ -808,7 +808,7 @@
  140.  tenex.o: mail.h misc.h osdep.h tenex.h dummy.h
  141.  unix.o: mail.h misc.h osdep.h unix.h pseudo.h dummy.h
  142.  utf8.o: mail.h misc.h osdep.h utf8.h
  143. -
  144. +maildir.o: mail.h misc.h osdep.h maildir.h dummy.h
  145.  
  146.  # OS-dependent
  147.  
  148. diff -Nur pine4.43/imap/src/osdep/unix/dummy.c pine4.43-maildir/imap/src/osdep/unix/dummy.c
  149. --- pine4.43/imap/src/osdep/unix/dummy.c    Sat Oct 27 08:15:23 2001
  150. +++ pine4.43-maildir/imap/src/osdep/unix/dummy.c    Wed Dec  5 10:04:15 2001
  151. @@ -341,13 +341,17 @@
  152.    char tmp[MAILTMPLEN];
  153.                  /* don't \NoSelect dir if it has a driver */
  154.    if ((attributes & LATT_NOSELECT) && (d = mail_valid (NIL,name,NIL)) &&
  155. -      (d != &dummydriver)) attributes &= ~LATT_NOSELECT;
  156. +      (d != &dummydriver)) {
  157. +    attributes &= ~LATT_NOSELECT; 
  158. +    attributes |= LATT_NOINFERIORS;
  159. +  }      
  160.    if (!contents ||        /* notify main program */
  161.        (!(attributes & LATT_NOSELECT) && (csiz = strlen (contents)) &&
  162.         !stat (dummy_file (tmp,name),&sbuf) && (csiz <= sbuf.st_size) &&
  163.         SAFE_SCAN_CONTENTS (d,tmp,contents,csiz,sbuf.st_size)))
  164.      mm_list (stream,delimiter,name,attributes);
  165. -  return T;
  166. +  if (attributes & LATT_NOINFERIORS) return NIL;
  167. +  else return T;
  168.  }
  169.  
  170.  /* Dummy create mailbox
  171. diff -Nur pine4.43/imap/src/osdep/unix/maildir.c pine4.43-maildir/imap/src/osdep/unix/maildir.c
  172. --- pine4.43/imap/src/osdep/unix/maildir.c    Thu Jan  1 01:00:00 1970
  173. +++ pine4.43-maildir/imap/src/osdep/unix/maildir.c    Wed Dec  5 09:32:47 2001
  174. @@ -0,0 +1,1143 @@
  175. +/*
  176. + * Maildir Module for PINE 4.0x - fourth release, use with CARE! 
  177. + *
  178. + * Author:      Mattias Larsson <ml@techno.org>
  179. + *
  180. + * Version:     21.07.98
  181. + *
  182. + * Please read the README.maildir file before using this module!
  183. + *
  184. + * If you have any questions, please e-mail ml@techno.org 
  185. + *
  186. + * Multiple inboxes patch by Dean Gaudet <dgaudet@arctic.org>
  187. + *
  188. + * =================================================
  189. + *
  190. + * Based on the IMAP2 maildir routines by:
  191. + *
  192. + * Author:      Eric Green
  193. + *              Bloodhounds International Inc.
  194. + *              thrytis@imaxx.net
  195. + *
  196. + * Additional contributions from:
  197. + *              Aidas Kasparas (kaspar@soften.ktu.lt)
  198. + *
  199. + * Date:        27 April 1997
  200. + * Last Edited: 13 June 1997
  201. + *
  202. + * Based (heavily) on mh.c and other c-client library files by Mark Crispin:
  203. + *
  204. + *              Mark Crispin
  205. + *              Networks and Distributed Computing
  206. + *              Computing & Communications
  207. + *              University of Washington
  208. + *              Administration Building, AG-44
  209. + *              Seattle, WA  98195
  210. + *              Internet: MRC@CAC.Washington.EDU
  211. + *
  212. + * Copyright 1995 by the University of Washington
  213. + *
  214. + *  Permission to use, copy, modify, and distribute this software and its
  215. + * documentation for any purpose and without fee is hereby granted, provided
  216. + * that the above copyright notice appears in all copies and that both the
  217. + * above copyright notice and this permission notice appear in supporting
  218. + * documentation, and that the name of the University of Washington not be
  219. + * used in advertising or publicity pertaining to distribution of the software
  220. + * without specific, written prior permission.  This software is made
  221. + * available "as is", and
  222. + * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  223. + * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  224. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  225. + * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  226. + * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  227. + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  228. + * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  229. + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  230. + *
  231. + */
  232. +
  233. +/* CONFIGURABLE OPTIONS - PLEASE CHECK THESE OUT */
  234. +
  235. +#define NO_MAILDIR_FIDDLE    /* disallow Maildir with Maildir in the
  236. +                name. This is useful in an ISP setup
  237. +                using the IMAP daemon. #undef it if you
  238. +                are running a normal pine and know what
  239. +                you are doing */
  240. +
  241. +#define NO_ABSOLUTE_PATHS    /* if you define this, all paths
  242. +                use your HOMEDIR is the root instead
  243. +                of the actual root of the machine. This
  244. +                is also useful in an ISP setup with
  245. +                IMAP */
  246. +
  247. +#undef NO_UID_VALIDITIY        /* define this if you want the UID's not
  248. +                to be persistent over sessions. Use this
  249. +                if you use another client to read the
  250. +                maildir that screws up the special way
  251. +                in which we store UIDs. Do not enable
  252. +                unless you are sure you need it. */
  253. +    
  254. +/* END CONFIGURATION */
  255. +
  256. +#define MTA_DEBUG    /* debugging sent to stdout */
  257. +#undef MTA_DEBUG
  258. +
  259. +#include <stdio.h>
  260. +#include <ctype.h>
  261. +#include <errno.h>
  262. +extern int errno;        /* just in case */
  263. +#include "mail.h"
  264. +#include "osdep.h"
  265. +#include <pwd.h>
  266. +#include <sys/stat.h>
  267. +#include <sys/time.h>
  268. +#include <sys/types.h>
  269. +#include <utime.h>
  270. +#include "maildir.h"
  271. +#include "misc.h"
  272. +#include "dummy.h"
  273. +
  274. +/* Driver dispatch used by MAIL */
  275. +
  276. +DRIVER maildirdriver = {
  277. +  "maildir",            /* driver name */
  278. +                /* driver flags */
  279. +  DR_MAIL|DR_LOCAL|DR_NOFAST|DR_NAMESPACE,
  280. +  (DRIVER *) NIL,        /* next driver */
  281. +  maildir_valid,            /* mailbox is valid for us */
  282. +  maildir_parameters,        /* manipulate parameters */
  283. +  NIL,            /* scan mailboxes */
  284. +  maildir_list,            /* find mailboxes */
  285. +  maildir_lsub,            /* find subscribed mailboxes */
  286. +  maildir_sub,            /* subscribe to mailbox */
  287. +  maildir_unsub,        /* unsubscribe from mailbox */
  288. +  maildir_create,            /* create mailbox */
  289. +  maildir_delete,            /* delete mailbox */
  290. +  maildir_rename,            /* rename mailbox */
  291. +  NIL,                /* status of mailbox */
  292. +  maildir_open,            /* open mailbox */
  293. +  maildir_close,        /* close mailbox */
  294. +  maildir_fast,            /* fetch message "fast" attributes */
  295. +  NIL,                /* fetch message flags */
  296. +  NIL,                /* fetch overview */
  297. +  NIL,                /* fetch message envelopes */
  298. +  maildir_fetchheader,        /* fetch message header */
  299. +  maildir_fetchtext,        /* fetch message body */
  300. +  NIL,                /* fetch partial message text */
  301. +  NIL,                /* unique identifier */
  302. +  NIL,                /* message number */
  303. +  NIL,                /* modify flags */
  304. +  maildir_flagmsg,        /* per-message modify flags */
  305. +  NIL,                /* search for message based on criteria */
  306. +  NIL,                /* sort messages */
  307. +  NIL,                /* thread messages */
  308. +  maildir_ping,            /* ping mailbox to see if still alive */
  309. +  maildir_check,        /* check for new messages */
  310. +  maildir_expunge,            /* expunge deleted messages */
  311. +  maildir_copy,            /* copy messages to another mailbox */
  312. +  maildir_append,            /* append string message to mailbox */
  313. +  maildir_gc                /* garbage collect stream */
  314. +};
  315. +
  316. +                /* prototype stream */
  317. +MAILSTREAM maildirproto = {&maildirdriver};
  318. +
  319. +/* Check validity of mailbox
  320. + */
  321. +DRIVER *maildir_valid (char *name)
  322. +{
  323. +  return maildir_isvalid(name,T) ? &maildirdriver : NIL;
  324. +}
  325. +
  326. +int maildir_isvalid (char *name,long justname)
  327. +{
  328. +  char tmp[MAILTMPLEN];
  329. +  struct stat sbuf;
  330. +  
  331. +  if (!name || (!*name) || 
  332. +      ((*name == '#') && 
  333. +       (*(name+1) == 0 ||
  334. +    (*(name+1) != 'm' && *(name+1) != 'M') ||
  335. +    (*(name+2) != 'd' && *(name+1) != 'D') ||
  336. +    *(name+3) != '/')) || (*name == '.'))
  337. +    return NIL;
  338. +  
  339. +  /* okay, anything containing the name Maildir will be ignored
  340. +     this is to prevent anyone from fiddling with their incoming Maildir
  341. +     directly, it should be accessed via the INBOX alias */
  342. +
  343. +  #ifdef NO_MAILDIR_FIDDLE
  344. +  if (strstr(name, "Maildir")) {
  345. +    return NIL; 
  346. +    }
  347. +  #endif
  348. +                 /* If we are requested only to check 
  349. +                     if the name is appropriate then we
  350. +                     have done! */
  351. +  if (justname && *name == '#') return T;
  352. +  
  353. +
  354. +                /* must be valid local mailbox */
  355. +  if ((*name != '*') && (*name != '{') &&
  356. +      maildir_file (tmp,name) &&
  357. +                /* assume its maildir if its a dir */
  358. +      stat (tmp,&sbuf) == 0 && S_ISDIR (sbuf.st_mode))
  359. +    return T;
  360. +
  361. +                /* INBOX is for default Maildir */
  362. +  if (!strcmp (ucase (strcpy (tmp,name)), "INBOX") &&
  363. +      (stat (maildir_file (tmp,name),&sbuf) == 0) &&
  364. +      S_ISDIR (sbuf.st_mode))
  365. +    return T;
  366. +
  367. +  return NIL;
  368. +}
  369. +
  370. +/* Maildir mail generate file string
  371. + */
  372. +
  373. +char *maildir_file (char *dst,char *name)
  374. +{
  375. +  char tmp[MAILTMPLEN];
  376. +  
  377. +  if (strlen (name) > 3 &&    /* safe do other comparisons */
  378. +      (*name == '#') &&
  379. +      (name[1] == 'm' || name[1] == 'M') &&
  380. +      (name[2] == 'd' || name[2] == 'D') &&
  381. +      (name[3] == '/'))
  382. +    name += 4;
  383. +
  384. +#ifdef NO_ABSOLUTE_PATHS  
  385. +  if (*name == '/') {    
  386. +  /* we do not want to accept / absolute paths, so lets strip the first
  387. +     / ... */
  388. +    sprintf(dst,"%s/%s/cur", myhomedir(), name+1);
  389. +
  390. +/*    strncpy (dst, name, MAILTMPLEN - 2);
  391. +    strncat (dst, "/cur", MAILTMPLEN - 2);
  392. +    dst[MAILTMPLEN - 1] = '\0'; */
  393. +  }
  394. +  else
  395. +    sprintf (dst,"%s/%s/cur",myhomedir (),
  396. +        strcmp (ucase (strcpy (tmp, name)), "INBOX") ? name : MAILDIRPATH);
  397. +#else
  398. +  if (*name == '/') {    
  399. +    strncpy (dst, name, MAILTMPLEN - 2);
  400. +    strncat (dst, "/cur", MAILTMPLEN - 2);
  401. +    dst[MAILTMPLEN - 1] = '\0';
  402. +  }
  403. +  else
  404. +    sprintf (dst,"%s/%s/cur",myhomedir (),
  405. +        strcmp (ucase (strcpy (tmp, name)), "INBOX") ? name : MAILDIRPATH);
  406. +
  407. +#endif 
  408. +  
  409. +  #ifdef MTA_DEBUG
  410. +  printf("maildir_file '%s'\n", dst);
  411. +  #endif
  412. +  return dst;
  413. +}
  414. +
  415. +/* Maildir open
  416. + */
  417. +MAILSTREAM *maildir_open (MAILSTREAM *stream)
  418. +{
  419. + char tmp[MAILTMPLEN],tmp2[MAILTMPLEN];
  420. +
  421. + if (!stream) return &maildirproto;
  422. + if (LOCAL) {        /* recycle stream */
  423. +    maildir_close (stream, 0);
  424. +    stream->dtb = &maildirdriver;
  425. +    mail_free_cache (stream);
  426. +    stream->uid_last = 0;       /* default UID validity */
  427. +    stream->uid_validity = time (0);
  428. +    }
  429. +
  430. +    stream->uid_validity = 0; /* was time(0) */
  431. +
  432. +  if (stream->uid_last < time(0))
  433. +      stream->uid_last = time (0);  
  434. +  
  435. +    stream->local = fs_get (sizeof (MAILDIRLOCAL));
  436. +  LOCAL->inbox = !strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX") ||
  437. +      !strcmp (stream->mailbox,maildir_file (tmp2,"INBOX"));
  438. +  LOCAL->dir = cpystr (maildir_file (tmp,stream->mailbox)); /* copy dir name */
  439. +                                /* make temporary buffer */
  440. +  LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1);
  441. +  LOCAL->scantime = 0;          /* not scanned yet */
  442. +  stream->sequence++;
  443. +  stream->nmsgs = stream->recent = 0;
  444. +
  445. +  maildir_ping_core (stream);
  446. +  maildir_ping (stream);
  447. +/*  if (maildir_ping (stream) && !(stream->nmsgs || stream->silent))
  448. +    printf("Mailbox is empty\n");
  449. +*/
  450. +  return stream;
  451. +
  452. +}
  453. +
  454. +/* Maildir ping mailbox
  455. + */
  456. +
  457. +long maildir_ping_core (MAILSTREAM *stream)
  458. +{
  459. +  char tmp[MAILTMPLEN];
  460. +  MESSAGECACHE *elt;
  461. +  struct stat sbuf, sbuf2;
  462. +  DIR *dir;
  463. +  struct direct *d;
  464. +  int reloadall = NIL;
  465. +  int uidinvalid = NIL;
  466. +  unsigned long old;
  467. +  long i;
  468. +  long nmsgs = stream->nmsgs;
  469. +  long recent = stream->recent;
  470. +  long nfiles = stream->nmsgs;
  471. +  int silent = stream->silent;
  472. +  char *s, *s2;
  473. +  mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
  474. +
  475. +/*  maildir_copynew (LOCAL->dir);
  476. + */
  477. +  
  478. +  if (stat (LOCAL->dir,&sbuf) < 0) {
  479. +    sprintf (tmp,"Unable to open maildir: %s",strerror (errno));
  480. +    mm_log (tmp,ERROR);
  481. +    return NIL;
  482. +  }
  483. +
  484. +  /* okay, lets try to figure out the Maildir UID validity. This is done
  485. +     by checking the last modification time of the file .uidvalidity
  486. +     in the rootdir of the Maildir. Any program reordering the files
  487. +     in the directory have to touch this file */
  488. +
  489. +  sprintf(tmp, "%s/../.uidvalidity", LOCAL->dir);
  490. +  
  491. +  if (stat (tmp,&sbuf2) < 0) {
  492. +    /* no uid validity file found, if uid_validity == 0, we have
  493. +       to set it to something other than 0, and then create the
  494. +       .uidvalidity file for future accesses */
  495. +
  496. +    if (stream->uid_validity == 0) {
  497. +        FILE *fl;
  498. +        struct utimbuf tbuf;
  499. +        
  500. +        stream->uid_validity = time(0);
  501. +        tbuf.actime = stream->uid_validity;
  502. +        tbuf.modtime = stream->uid_validity;
  503. +
  504. +        if ((fl = fopen(tmp, "w"))) {
  505. +            fclose(fl);
  506. +            chmod (tmp, S_IRUSR|S_IWUSR);
  507. +            utime(tmp, &tbuf);
  508. +            }
  509. +        }
  510. +        uidinvalid = T; /* UID's are invalid, update them */
  511. +    } else {
  512. +    /* valid file, lets set UID if uid_validity = 0 */
  513. +    if (stream->uid_validity == 0) {
  514. +    stream->uid_validity = sbuf2.st_mtime;
  515. +        }
  516. +    }
  517. +
  518. +  #ifdef NO_UID_VALIDITY
  519. +  uidinvalid = T; /* force the UIDs to be invalid and reset every time,
  520. +               useful in an environment without imap servers and
  521. +               clients that screw up the UIDs.. i'd leave it to
  522. +               OFF until I really need it though... */
  523. +  #endif
  524. +
  525. +  stream->silent = T;  
  526. +  if (sbuf.st_ctime != LOCAL->scantime) {
  527. +    /* update the message list */
  528. +    struct direct **names = NIL;
  529. +    nfiles = scandir (LOCAL->dir,&names,maildir_select,maildir_namesort);
  530. +
  531. +    for (i = 0; i < nfiles; i++) {
  532. +
  533. +    /* check if file has executable bit set */
  534. +    sprintf(tmp, "%s/%s", LOCAL->dir, names[i]->d_name);
  535. +      stat (tmp,&sbuf2);
  536. +    if (sbuf2.st_mode & S_IXUSR) {
  537. +        /* executable bit set, modtime is uid */
  538. +        if (sbuf2.st_mtime > stream->uid_last)
  539. +            stream->uid_last = sbuf2.st_mtime+1;
  540. +        }
  541. +    /* this is kept for backwards compatibility */
  542. +        if ((s = strstr (names[i]->d_name,":3,")))
  543. +        s += 3;
  544. +        if (s && (s2 = strstr (s, ",U"))) {
  545. +              s2 += 2;
  546. +        sscanf(s2, "%d", &old);
  547. +        if (old > stream->uid_last) {
  548. +            stream->uid_last = old+1;
  549. +            }
  550. +        }    
  551. +    
  552. +    }
  553. +
  554. +    mm_critical (stream);    /* go critical */
  555. +    old = stream->uid_last;
  556. +    LOCAL->scantime = sbuf.st_ctime;
  557. +
  558. +                /* check if old files same */
  559. +    for (i = 0; i < stream->nmsgs; i++) {
  560. +
  561. +      if (strcmp ((char *) mail_elt (stream, i + 1)->maildirp,
  562. +          names[i]->d_name)) {
  563. +    reloadall = T;
  564. +    break;
  565. +      }
  566. +    }
  567. +
  568. +    if (reloadall) { /* files are out of order, rebuild cache */
  569. +
  570. +      i = 1;
  571. +      while (i <= stream->nmsgs)
  572. +                /* clean out cache */
  573. +    if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT))) {
  574. +      fs_give ((void **) &elt->maildirp);
  575. +      mail_expunged (stream,i);
  576. +    }
  577. +    else
  578. +      i++;
  579. +      
  580. +      mm_log ("Warning: Mailbox has changed in an unexpected way.  Reloading.",
  581. +          WARN);
  582. +      stream->nmsgs = 0;
  583. +    }
  584. +    nmsgs = stream->nmsgs;
  585. +    
  586. +    stream->nmsgs = nfiles; /* hm? */    
  587. +
  588. +    for (i = nmsgs; i < nfiles; i++) {
  589. +
  590. +      mail_exists(stream, i+1);
  591. +                /* if newly seen, add to list */
  592. +      (elt = mail_elt (stream, i + 1))->maildirp = (long) cpystr (names[i]->d_name);
  593. +       elt->valid = T;
  594. +
  595. +      /* grab the flags */
  596. +      if ((s = strstr (names[i]->d_name,":3,"))) {
  597. +    s += 3;
  598. +    if (strchr (s,'F'))
  599. +      elt->flagged = T;
  600. +    if (strchr (s,'R'))
  601. +      elt->answered = T;
  602. +    if (strchr (s,'S'))
  603. +      elt->seen = T;
  604. +    if (strchr (s,'T'))
  605. +      elt->deleted = T;
  606. +      } else if ((s = strstr (names[i]->d_name,":2,"))) {
  607. +    /* this is the :2, id where all files go nowadays */
  608. +    s += 3;
  609. +    if (strchr (s,'F'))
  610. +      elt->flagged = T;
  611. +    if (strchr (s,'R'))
  612. +      elt->answered = T;
  613. +    if (strchr (s,'S'))
  614. +      elt->seen = T;
  615. +    if (strchr (s,'T'))
  616. +      elt->deleted = T;
  617. +    sprintf(tmp, "%s/%s", LOCAL->dir, names[i]->d_name);
  618. +      stat (tmp,&sbuf2);
  619. +    if (sbuf2.st_mode & S_IXUSR) {
  620. +        /* executable bit set, modtime is uid */
  621. +        elt->private.uid = sbuf2.st_mtime;
  622. +        }
  623. +    /* and if we could not retrieve UID from modtime, or if
  624. +       UIDs are invalid, go here */
  625. +    if (elt->private.uid == 0 || uidinvalid) {
  626. +            stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
  627. +        maildir_flagmsg(stream, elt);
  628. +        }    
  629. +    s = 0; /* make sure next if statement does not trigger */      
  630. +          }
  631. +
  632. +      if (s)
  633. +      if ((s2 = strstr (s, ",U"))) {
  634. +          s2 += 2;
  635. +    sscanf(s2, "%d", &elt->private.uid);
  636. +    if (elt->private.uid == 0 || uidinvalid) {
  637. +            stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
  638. +        maildir_flagmsg(stream, elt);
  639. +        }    
  640. +    
  641. +          } else { /* assign new UID */
  642. +        stream->uid_last = (elt = mail_elt (stream,i+1))->private.uid = stream->uid_last+1;
  643. +    elt->recent = T;
  644. +    recent++;
  645. +    maildir_flagmsg(stream, elt); /* store the UID that we assigned to it */
  646. +    }    
  647. +
  648. +
  649. +
  650. +    }
  651. +
  652. +    mm_nocritical (stream);    /* release critical */
  653. +                /* free the names stuff */
  654. +    for (i = 0; i < nfiles; i++)
  655. +      fs_give ((void **) &names[i]);
  656. +    if (names)
  657. +      fs_give ((void **) &names);
  658. +  }
  659. +  stream->silent = silent;
  660. +  mail_exists(stream,nfiles);
  661. +/*  if (!reloadall)  */
  662. +      mail_recent (stream,recent);
  663. +
  664. +  return T;            /* return that we are alive */
  665. +}
  666. +
  667. +long maildir_ping (MAILSTREAM *stream)
  668. +{
  669. +  maildir_copynew (LOCAL->dir);
  670. +  return maildir_ping_core (stream);
  671. +}
  672. +
  673. +void maildir_copynew (const char *mailbox)
  674. +{
  675. +  char tmp[MAILTMPLEN],file[MAILTMPLEN],newfile[MAILTMPLEN];
  676. +  DIR *dir;
  677. +  struct dirent *d;
  678. +  struct stat sbuf;
  679. +  
  680. +  sprintf (tmp,"%s/../new",mailbox);
  681. +  if (!(dir = opendir (tmp)))
  682. +    return;
  683. +
  684. +  while (d = readdir (dir)) {
  685. +    if (d->d_name[0] == '.')
  686. +      continue;            /* skip .files */
  687. +
  688. +    sprintf (file,"%s/%s",tmp,d->d_name);
  689. +                /* make sure this is a normal file */
  690. +    if (stat (file,&sbuf) == 0 && S_ISREG (sbuf.st_mode)) {
  691. +      
  692. +      if (strstr (d->d_name,":3,")) /* this message already has flags */
  693. +    sprintf (newfile,"%s/%s",mailbox,d->d_name);
  694. +      else
  695. +    sprintf (newfile,"%s/%s:3,",mailbox,d->d_name);
  696. +      
  697. +                /* move the new mail to the cur dir */
  698. +      if (link (file,newfile) == -1)
  699. +    mm_log("Unable to read new mail!",WARN);
  700. +      else
  701. +    unlink (file);    
  702. +    }
  703. +  }
  704. +  closedir (dir);
  705. +}
  706. +
  707. +int maildir_select (struct direct *name)
  708. +{
  709. +  if (name->d_name[0] != '.')
  710. +    return T;
  711. +
  712. +  return NIL;
  713. +}
  714. +
  715. +int maildir_namesort (struct direct **d1,struct direct **d2)
  716. +{
  717. +/* this maildir module is kind of lame and expects files it just moved
  718. + * from new/ to cur/ to show up at the end of the sorting... this mostly
  719. + * works fine when the timestamp is less than 1000000000 -- you can just
  720. + * strcmp.  but when the timestamp went past that point we need to do
  721. + * this convoluted sort.
  722. + */
  723. +  unsigned long t1, t2;
  724. +
  725. +  t1 = strtoul((*d1)->d_name, NULL, 10);
  726. +  t2 = strtoul((*d2)->d_name, NULL, 10);
  727. +  if (t1 == t2) {
  728. +    return strcmp ((*d1)->d_name,(*d2)->d_name);
  729. +  }
  730. +  else if (t1 > t2) {
  731. +    return 1;
  732. +  }
  733. +  return -1;
  734. +}
  735. +
  736. +
  737. +/* Maildir garbage collect stream
  738. + */
  739. +
  740. +void maildir_gc (MAILSTREAM *stream,long gcflags)
  741. +{
  742. +  unsigned long i;
  743. +  
  744. +  if (gcflags & GC_TEXTS) {    /* garbage collect texts? */
  745. +                /* flush texts from cache */
  746. +/*    if (LOCAL->hdr) fs_give ((void **) &LOCAL->hdr);
  747. +//    if (stream->text) fs_give ((void **) &stream->text);
  748. +//    stream->msgno = 0;        invalidate stream text
  749. +*/
  750. +  }
  751. +}
  752. +
  753. +/* Maildir close
  754. + */
  755. +
  756. +void maildir_close (MAILSTREAM *stream, long options)
  757. +{
  758. +  MESSAGECACHE *elt;
  759. +  int i;
  760. +  mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
  761. +/*  CL_EXPUNGE OPTION SUPPORT HERE SOMEWHERE! */
  762. +                /* clean out the cached paths */
  763. +  for (i = 1; i <= stream->nmsgs; i++)
  764. +    if ((elt = (MESSAGECACHE *) (*mc) (stream,i,CH_ELT)) && elt->maildirp) {
  765. +      fs_give ((void **) &elt->maildirp);
  766. +      elt->maildirp = 0; /* otherwise pine coredumps */  
  767. +    }  
  768. +  
  769. +  if (LOCAL) {            /* only if a stream is open */
  770. +    if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
  771. +    maildir_gc (stream,GC_TEXTS); /* free local cache */
  772. +                /* free local scratch buffer */
  773. +    if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
  774. +                /* nuke the local data */
  775. +    fs_give ((void **) &stream->local);
  776. +    stream->dtb = NIL;        /* log out the DTB */
  777. +  }
  778. +}
  779. +
  780. +void maildir_check (MAILSTREAM *stream)
  781. +{
  782. +  /* Perhaps in the future this will preserve flags */
  783. +  if (maildir_ping (stream)) mm_log ("Check completed",(long) NIL);   
  784. +}
  785. +
  786. +long maildir_fetchtext (MAILSTREAM *stream,unsigned long msgno,STRING *bs, long flags)
  787. +{
  788. +  unsigned long i;
  789. +  MESSAGECACHE *elt;
  790. +                                /* UID call "impossible" */
  791. +  if (flags & FT_UID) return NIL;
  792. +  elt = mail_elt (stream,msgno);/* get elt */
  793. +                                /* snarf message if don't have it yet */
  794. +  if (!elt->private.msg.text.text.data) {
  795. +    maildir_fetchheader (stream,msgno,&i,flags);
  796. +    if (!elt->private.msg.text.text.data) return NIL;
  797. +  }
  798. +  if (!(flags & FT_PEEK)) {     /* mark as seen */
  799. +    mail_elt (stream,msgno)->seen = T;
  800. +    maildir_flagmsg (stream, mail_elt(stream,msgno));
  801. +    mm_flags (stream,msgno);
  802. +  }
  803. +  if (!elt->private.msg.text.text.data) return NIL;
  804. +  INIT (bs,mail_string,elt->private.msg.text.text.data,
  805. +        elt->private.msg.text.text.size);
  806. +  return T;
  807. +}
  808. +
  809. +
  810. +/* Maildir fetch message header
  811. + */
  812. +
  813. +char *maildir_fetchheader (MAILSTREAM *stream,unsigned long msgno,
  814. +        unsigned long *length, long flags)
  815. +{
  816. +  unsigned long i,hdrsize;
  817. +  int fd;
  818. +  char *t;
  819. +  char tmp[MAILTMPLEN];
  820. +  char *s,*b;
  821. +  struct stat sbuf;
  822. +  struct tm *tm;
  823. +  MESSAGECACHE *elt;
  824. +  *length = 0;                  /* default to empty */
  825. +  if (flags & FT_UID) return "";/* UID call "impossible" */
  826. +  elt = mail_elt (stream,msgno);/* get elt */
  827. +  if (!elt->private.msg.header.text.data) {
  828. +
  829. +/*    maildir_gc (stream,GC_TEXTS);  invalidate current cache */
  830. +                /* build message file name */
  831. +    sprintf (tmp,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  832. +    if ((fd = open (tmp,O_RDONLY,NIL)) >= 0) {
  833. +      fstat (fd,&sbuf);        /* get size of message */
  834. +                /* make plausible IMAPish date string */
  835. +      tm = gmtime (&sbuf.st_mtime);
  836. +      elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  837. +      elt->year = tm->tm_year + 1900 - BASEYEAR;
  838. +      elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  839. +      elt->seconds = tm->tm_sec;
  840. +      elt->zhours = 0; elt->zminutes = 0;
  841. +                /* slurp message */
  842. +      read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  843. +      s[sbuf.st_size] = '\0';    /* tie off file */
  844. +      close (fd);        /* close file */
  845. +
  846. +      for (i = 0,b = s; *b && !(i && (*b == '\n')); i = (*b++ == '\n'));
  847. +      hdrsize = (*b ? ++b:b)-s;    /* number of header bytes */
  848. +
  849. +      elt->rfc822_size =          /* size of entire message in CRLF form */
  850. +       (elt->private.msg.header.text.size =
  851. +        strcrlfcpy ((char **) &elt->private.msg.header.text.data,&i,s,
  852. +                    hdrsize)) +
  853. +          (elt->private.msg.text.text.size =
  854. +           strcrlfcpy ((char **) &elt->private.msg.text.text.data,&i,b,
  855. +                       sbuf.st_size - hdrsize));
  856. +      fs_give ((void **) &s);
  857. +    } else return "";
  858. +
  859. +  }
  860. +
  861. +  *length = elt->private.msg.header.text.size;
  862. +  return (char *) elt->private.msg.header.text.data;
  863. +}
  864. +
  865. +void maildir_fast (MAILSTREAM *stream,char *sequence,long flags)
  866. +{
  867. +  unsigned long i,j;
  868. +                /* ugly and slow */
  869. +  if (stream && LOCAL && ((flags & FT_UID) ?
  870. +              mail_uid_sequence (stream,sequence) :
  871. +              mail_sequence (stream,sequence)))
  872. +    for (i = 1; i <= stream->nmsgs; i++)
  873. +      if (mail_elt (stream,i)->sequence) maildir_fetchheader (stream,i,&j,NIL);
  874. +}
  875. +
  876. +/* Maildir find list of subscribed mailboxes
  877. + * Accepts: mail stream
  878. + *        pattern to search
  879. + */
  880. +
  881. +void maildir_list (MAILSTREAM *stream,char *ref, char *pat)
  882. +{
  883. +  return;
  884. +}
  885. +
  886. +void *maildir_parameters (long function,void *value)
  887. +{
  888. +  return NIL;
  889. +}
  890. +
  891. +long maildir_create (MAILSTREAM *stream,char *mailbox)
  892. +{
  893. +  char tmp[MAILTMPLEN];
  894. +  char err[MAILTMPLEN];
  895. +  char *s, *s2;
  896. +  int fnlen, i;
  897. +  char *subdir_names[] = {"/cur","/new","/tmp",NULL};
  898. +
  899. +                /* must not already exist */
  900. +  if (access (maildir_file (tmp,mailbox),F_OK) == 0) {
  901. +    sprintf (err,"Can't create mailbox %s: mailbox already exists",mailbox);
  902. +    mm_log (err,ERROR);
  903. +    return NIL;
  904. +  }
  905. +  
  906. +  maildir_file (tmp,mailbox);    /* get file name */
  907. +  fnlen = strlen (tmp);
  908. +  /*syslog(LOG_INFO, "fname: '%s'", tmp);*/
  909. +  tmp[fnlen - 4] = '\0';    /* making main directory's name */
  910. +  fnlen -= 4;
  911. +
  912. +  /* okay, try to add support for adding hiearchys of directories, this
  913. +     is done by scanning for /'s.... */
  914. +     
  915. +  /*syslog(LOG_INFO, "tmp '%s'", tmp);*/
  916. +  s = tmp;
  917. +
  918. +  while ((s = strstr(s, "/")) != 0) {
  919. +    /*syslog(LOG_INFO, "Before make: '%s'", s);*/
  920. +      *s = '\0';
  921. +      /*syslog(LOG_INFO, "Trying to make: '%s'", tmp);*/
  922. +    if (mkdir (tmp,0700) && *s != '\0') /* trying to make the dir */
  923. +      if (errno != EEXIST) {
  924. +        sprintf (err,"Can't create mailbox %s: %s %s",
  925. +             mailbox,tmp,strerror (errno));
  926. +             mm_log (err,ERROR);
  927. +             return NIL;
  928. +      }
  929. +      *s = '/';
  930. +    s++;
  931. +      } 
  932. +
  933. +  if (mkdir (tmp,0700)) {    /* try to make new dir */
  934. +    sprintf (err,"Can't create mailbox %s: %s %s",
  935. +         mailbox,tmp,strerror (errno));
  936. +    mm_log (err,ERROR);
  937. +    return NIL;
  938. +  }
  939. +
  940. +  /*syslog(LOG_INFO, "create maildir");*/
  941. +  for (i = 0; subdir_names[i]; i++) {
  942. +    strcpy (tmp + fnlen,subdir_names[i]);
  943. +
  944. +    if (mkdir (tmp,0700)) {    /* try to make new dir */
  945. +      sprintf (err,"Can't create mailbox %s: %s %s",
  946. +           mailbox,tmp,strerror (errno));
  947. +      mm_log (err,ERROR);
  948. +      return NIL;
  949. +    }
  950. +  }
  951. +
  952. +  return T;            /* return success */
  953. +}
  954. +
  955. +void maildir_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
  956. +{
  957. +  char oldfile[MAILTMPLEN],newfile[MAILTMPLEN],fn[MAILTMPLEN];
  958. +  struct utimbuf tbuf;
  959. +  char *s;
  960. +
  961. +                                /* build the new filename */
  962. +  sprintf (oldfile,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  963. +  if ((s = strchr ((char *) elt->maildirp,':'))) *s = '\0';
  964. +  sprintf (fn,"%s:2,%s%s%s%s",(char *) elt->maildirp,elt->flagged ? "F" : "",
  965. +           elt->answered ? "R" : "",elt->seen ? "S" : "",
  966. +           elt->deleted ? "T" : "");
  967. +  sprintf (newfile,"%s/%s",LOCAL->dir,fn);
  968. +                                /* rename the file with new flags */
  969. +  if (rename (oldfile,newfile) < 0) {
  970. +    sprintf(oldfile,"Unable to write flags to disk: %s",strerror (errno));
  971. +    mm_log(oldfile,ERROR);
  972. +    return;
  973. +  }
  974. +                                /* update the file name in cache */
  975. +  fs_give ((void **) &elt->maildirp);
  976. +  elt->maildirp = (long) cpystr (fn);
  977. +
  978. +  /* fix the UID on the file */
  979. +  tbuf.actime = elt->private.uid;
  980. +  tbuf.modtime = elt->private.uid;
  981. +  chmod (newfile, S_IRUSR|S_IWUSR|S_IXUSR);
  982. +  utime (newfile, &tbuf);
  983. +
  984. +}
  985. +
  986. +void maildir_expunge (MAILSTREAM *stream)
  987. +{
  988. +  MESSAGECACHE *elt;
  989. +  unsigned long i = 1;
  990. +  unsigned long n = 0;
  991. +  unsigned long recent = stream->recent;
  992. +  
  993. +  maildir_gc (stream,GC_TEXTS);    /* invalidate texts */
  994. +  mm_critical (stream);        /* go critical */
  995. +  while (i <= stream->nmsgs) {    /* for each message */
  996. +                /* if deleted, need to trash it */
  997. +    if ((elt = mail_elt (stream,i))->deleted) {
  998. +      sprintf (LOCAL->buf,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  999. +      if (unlink (LOCAL->buf)) {/* try to delete the message */
  1000. +    sprintf (LOCAL->buf,"Expunge of message %ld failed, aborted: %s",i,
  1001. +         strerror (errno));
  1002. +    mm_log (LOCAL->buf,WARN);
  1003. +    break;
  1004. +      }
  1005. +                /* free the cached filename */
  1006. +      if (elt->maildirp) {
  1007. +          fs_give ((void **) &elt->maildirp);
  1008. +          elt->maildirp = 0; /* otherwise pine coredumps */
  1009. +        }
  1010. +      if (elt->recent) --recent;/* if recent, note one less recent message */
  1011. +      mail_expunged (stream,i);    /* notify upper levels */
  1012. +      n++;            /* count up one more expunged message */
  1013. +    }
  1014. +    else i++;            /* otherwise try next message */
  1015. +  }
  1016. +  if (n) {            /* output the news if any expunged */
  1017. +    sprintf (LOCAL->buf,"Expunged %ld messages",n);
  1018. +    mm_log (LOCAL->buf,(long) NIL);
  1019. +  }
  1020. +  else mm_log ("No messages deleted, so no update needed",(long) NIL);
  1021. +  mm_nocritical (stream);    /* release critical */
  1022. +                /* notify upper level of new mailbox size */
  1023. +  mail_exists (stream,stream->nmsgs);
  1024. +  mail_recent (stream,recent);
  1025. +}
  1026. +
  1027. +/* dont forget to process options in here */
  1028. +long maildir_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  1029. +{
  1030. +  STRING st;
  1031. +  MESSAGECACHE *elt;
  1032. +  struct stat sbuf;
  1033. +  int fd;
  1034. +  long i;
  1035. +  char *s,tmp[MAILTMPLEN];
  1036. +                /* copy the messages */
  1037. +  if ((options & CP_UID) ? mail_uid_sequence (stream, sequence) : 
  1038. +      mail_sequence (stream,sequence)) 
  1039. +  for (i = 1; i <= stream->nmsgs; i++)
  1040. +    if ((elt = mail_elt (stream,i))->sequence) {
  1041. +      sprintf (LOCAL->buf,"%s/%s",LOCAL->dir,(char *) elt->maildirp);
  1042. +      if ((fd = open (LOCAL->buf,O_RDONLY,NIL)) < 0) return NIL;
  1043. +      fstat (fd,&sbuf);        /* get size of message */
  1044. +                /* slurp message */
  1045. +      read (fd,s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size);
  1046. +      s[sbuf.st_size] = '\0';    /* tie off file */
  1047. +      close (fd);        /* flush message file */
  1048. +      INIT (&st,mail_string,(void *) s,sbuf.st_size);
  1049. +      sprintf (LOCAL->buf,"%s%s%s%s%s)",
  1050. +           elt->seen ? " \\Seen" : "",
  1051. +           elt->deleted ? " \\Deleted" : "",
  1052. +           elt->flagged ? " \\Flagged" : "",
  1053. +           elt->answered ? " \\Answered" : "",
  1054. +           (elt->seen || elt->deleted || elt->flagged || elt->answered) ?
  1055. +           "" : " ");
  1056. +      LOCAL->buf[0] = '(';    /* open list */
  1057. +      mail_date (tmp,elt);    /* generate internal date */
  1058. +      if (!maildir_append (stream,mailbox,LOCAL->buf,tmp,&st)) {
  1059. +    fs_give ((void **) &s);    /* give back temporary space */
  1060. +    return NIL;
  1061. +      }
  1062. +      fs_give ((void **) &s);    /* give back temporary space */
  1063. +    }
  1064. +  return T;            /* return success */
  1065. +}
  1066. +
  1067. +long maildir_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  1068. +           STRING *message)
  1069. +{
  1070. +  int fd;
  1071. +  char c,*s;
  1072. +  char tmp[MAILTMPLEN],file[MAILTMPLEN],path1[MAILTMPLEN],path2[MAILTMPLEN];
  1073. +  MESSAGECACHE elt;
  1074. +  long i;
  1075. +  long size = 0;
  1076. +  long ret = LONGT;
  1077. +  short uf = 0;
  1078. +  
  1079. +  /*  
  1080. +     This is intentionaly made static.  Users can ask to save a LOT of messages
  1081. +     at once and this program can do that within one second. Dan's assumption
  1082. +     that time+pid+hostname always will be unique stops being true in this
  1083. +     case. So we will add yet another number to host part of message file's
  1084. +     name. Hostname is used only to make filename unique and Dan  explicitly
  1085. +     says that "<...>  Other than this [skipping filenames starting at dot] ,
  1086. +     readers should not attempt to parse filenames. <...>". Therefore this 
  1087. +     addition should be no problem. Am I right, Dan?   --AK
  1088. +  */ 
  1089. +  
  1090. +  static unsigned int transact = 0;
  1091. +
  1092. +  if (flags)             /* get flags if given */
  1093. +    uf = maildir_getflags (user_flags (&maildirproto),flags);
  1094. +
  1095. +/*  if (date) {            want to preserve date?
  1096. +    //syslog(LOG_INFO, "date: '%s'", date);
  1097. +                // yes, parse date into an elt
  1098. +    if (!mail_parse_date (&elt,date)) {
  1099. +      sprintf (tmp,"Bad date in append: %s",date);
  1100. +      mm_log (tmp,ERROR);
  1101. +      return NIL;
  1102. +    }
  1103. +  } */
  1104. +                /* N.B.: can't use LOCAL->buf for tmp */
  1105. +                /* make sure valid mailbox */
  1106. +  if (!maildir_isvalid (mailbox, NIL)) {
  1107. +    sprintf (tmp,"Not a valid Maildir mailbox: %s",mailbox);
  1108. +    mm_log (tmp,ERROR);
  1109. +    return NIL;
  1110. +  }
  1111. +                /* build file name we will use */
  1112. +  sprintf (file,"%u.%d.%09u.%s:3,%s%s%s%s",
  1113. +       time (0),getpid (),transact++,mylocalhost (),
  1114. +       uf&fFLAGGED ? "F" : "",uf&fANSWERED ? "R" : "",
  1115. +       uf&fSEEN ? "S" : "",uf&fDELETED ? "T" : "");
  1116. +                /* build tmp file name */
  1117. +  sprintf (path1,"%s/../tmp/%s",maildir_file (tmp,mailbox),file);
  1118. +  
  1119. +  if ((fd = open (path1,O_WRONLY|O_CREAT|O_EXCL,S_IREAD|S_IWRITE)) < 0) {
  1120. +    sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
  1121. +    mm_log (tmp,ERROR);
  1122. +    return NIL;
  1123. +  }
  1124. +  i = SIZE (message);        /* get size of message */
  1125. +  s = (char *) fs_get (i + 1);    /* get space for the data */
  1126. +                /* copy the data w/o CR's */
  1127. +  while (i--) if ((c = SNX (message)) != '\015') s[size++] = c;
  1128. +  mm_critical (stream);        /* go critical */
  1129. +                /* write the data */
  1130. +  if ((write (fd,s,size) < 0) || fsync (fd)) {
  1131. +    unlink (path1);        /* delete message */
  1132. +    sprintf (tmp,"Message append failed: %s",strerror (errno));
  1133. +    mm_log (tmp,ERROR);
  1134. +    ret = NIL;
  1135. +  }
  1136. +                /* build final filename to use */
  1137. +  sprintf (path2,"%s/../new/%s",maildir_file (tmp,mailbox),file);
  1138. +  if (link (path1,path2) < 0) {
  1139. +    sprintf (tmp,"Message append failed: %s",strerror (errno));
  1140. +    mm_log (tmp,ERROR);
  1141. +    ret = NIL;
  1142. +  }
  1143. +  unlink (path1);
  1144. +  
  1145. +  close (fd);            /* close the file */
  1146. +  mm_nocritical (stream);    /* release critical */
  1147. +  fs_give ((void **) &s);    /* flush the buffer */
  1148. +  return ret;
  1149. +}
  1150. +
  1151. +short bezerk_getflags (MAILSTREAM *stream,char *flag)
  1152. +{
  1153. +  char *t,tmp[MAILTMPLEN],err[MAILTMPLEN];
  1154. +  short f = 0;
  1155. +  short i,j;
  1156. +  if (flag && *flag) {        /* no-op if no flag string */
  1157. +                /* check if a list and make sure valid */
  1158. +    if ((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) {
  1159. +      mm_log ("Bad flag list",ERROR);
  1160. +      return NIL;
  1161. +    }
  1162. +                /* copy the flag string w/o list construct */
  1163. +    strncpy (tmp,flag+i,(j = strlen (flag) - (2*i)));
  1164. +    tmp[j] = '\0';
  1165. +    t = ucase (tmp);        /* uppercase only from now on */
  1166. +
  1167. +    while (t && *t) {        /* parse the flags */
  1168. +      if (*t == '\\') {        /* system flag? */
  1169. +    switch (*++t) {        /* dispatch based on first character */
  1170. +    case 'S':        /* possible \Seen flag */
  1171. +      if (t[1] == 'E' && t[2] == 'E' && t[3] == 'N') i = fSEEN;
  1172. +      t += 4;        /* skip past flag name */
  1173. +      break;
  1174. +    case 'D':        /* possible \Deleted flag */
  1175. +      if (t[1] == 'E' && t[2] == 'L' && t[3] == 'E' && t[4] == 'T' &&
  1176. +          t[5] == 'E' && t[6] == 'D') i = fDELETED;
  1177. +      t += 7;        /* skip past flag name */
  1178. +      break;
  1179. +    case 'F':        /* possible \Flagged flag */
  1180. +      if (t[1] == 'L' && t[2] == 'A' && t[3] == 'G' && t[4] == 'G' &&
  1181. +          t[5] == 'E' && t[6] == 'D') i = fFLAGGED;
  1182. +      t += 7;        /* skip past flag name */
  1183. +      break;
  1184. +    case 'A':        /* possible \Answered flag */
  1185. +      if (t[1] == 'N' && t[2] == 'S' && t[3] == 'W' && t[4] == 'E' &&
  1186. +          t[5] == 'R' && t[6] == 'E' && t[7] == 'D') i = fANSWERED;
  1187. +      t += 8;        /* skip past flag name */
  1188. +      break;
  1189. +    default:        /* unknown */
  1190. +      i = 0;
  1191. +      break;
  1192. +    }
  1193. +                /* add flag to flags list */
  1194. +    if (i && ((*t == '\0') || (*t++ == ' '))) f |= i;
  1195. +      }
  1196. +      else {            /* no user flags yet */
  1197. +    t = strtok (t," ");    /* isolate flag name */
  1198. +    sprintf (err,"Unknown flag: %.80s",t);
  1199. +    t = strtok (NIL," ");    /* get next flag */
  1200. +    mm_log (err,ERROR);
  1201. +      }
  1202. +    }
  1203. +  }
  1204. +  return f;
  1205. +}
  1206. +
  1207. +short maildir_getflags (MAILSTREAM *stream,char *flag)
  1208. +{
  1209. +  return bezerk_getflags (stream,flag); /* nothing exciting, reuse old code */
  1210. +}
  1211. +
  1212. +long maildir_delete (MAILSTREAM *stream,char *mailbox)
  1213. +{
  1214. +  DIR *dirp;
  1215. +  struct direct *d;
  1216. +  int i,j;
  1217. +  char tmp[MAILTMPLEN],err[MAILTMPLEN];
  1218. +  char *subdir_names[] = {"cur/","new/","tmp/",NULL};
  1219. +
  1220. +                /* check if mailbox even exists */
  1221. +  if (!maildir_isvalid (mailbox,NIL)) {
  1222. +/*    sprintf (tmp,"Can't delete mailbox %s: no such mailbox",mailbox);
  1223. +    mm_log (tmp,ERROR);
  1224. +    return NIL; */
  1225. +    /*syslog(LOG_INFO, "Invalid maildir in delete()"); */
  1226. +    return T; /* well.. a stupid hack to get by a problem in netscape ..
  1227. +            it remembers folders locally, and if a folder is deleted on
  1228. +            another machine, you have no way removing it on any other
  1229. +            netscapes... */
  1230. +  }
  1231. +
  1232. +                /* get name of directory */
  1233. +  i = strlen (maildir_file (tmp,mailbox)) + 1;
  1234. +  for (j = 0; subdir_names[j]; j++) {
  1235. +    strcpy (tmp + i - 4,subdir_names[j]);
  1236. +    if (dirp = opendir (tmp)) {    /* open directory */
  1237. +      while (d = readdir (dirp))    /* empty the directory */
  1238. +    if (strcmp (d->d_name,".") && strcmp (d->d_name,"..")) {
  1239. +      strcpy (tmp + i,d->d_name);
  1240. +      /*syslog(LOG_INFO, "unlink1: '%s'");*/
  1241. +      unlink (tmp);
  1242. +    }
  1243. +      closedir (dirp);        /* flush directory */
  1244. +    }
  1245. +                /* remove the subdir */
  1246. +    tmp[i + 3] = '\0';
  1247. +    /*syslog(LOG_INFO, "tmp: '%s'", tmp);*/
  1248. +    if (rmdir (tmp)) {
  1249. +    /*  sprintf (err,"Can't delete directory %s: %s",tmp,strerror (errno));
  1250. +      mm_log (err,ERROR);*/
  1251. +    }
  1252. +  }
  1253. +
  1254. +                /* try to remove the directory */
  1255. +  *(tmp + i - 5) = '\0';
  1256. +  /*syslog(LOG_INFO, "tmp2: '%s'", tmp);*/
  1257. +  if (rmdir (tmp)) {
  1258. +/*    sprintf (err,"Can't delete mailbox %s: %s",mailbox,strerror (errno));
  1259. +    mm_log (err,ERROR);
  1260. +    return NIL; */
  1261. +  }
  1262. +  return T;            /* return success */
  1263. +}
  1264. +
  1265. +long maildir_rename (MAILSTREAM *stream,char *old,char *new)
  1266. +{
  1267. +  char tmp[MAILTMPLEN],tmpnew[MAILTMPLEN];
  1268. +
  1269. +                /* old mailbox name must be valid */
  1270. +  if (!maildir_isvalid (old,NIL)) {
  1271. +    sprintf (tmp,"Can't rename mailbox %s: no such mailbox",old);
  1272. +    mm_log (tmp,ERROR);
  1273. +    return NIL;
  1274. +  }
  1275. +
  1276. +                /* new mailbox name must not exist */
  1277. +  if (access (maildir_file (tmp,new),F_OK) == 0) {
  1278. +    sprintf (tmp,"Can't rename to mailbox %s: destination already exists",new);
  1279. +    mm_log (tmp,ERROR);
  1280. +    return NIL;
  1281. +  }
  1282. +
  1283. +                /* try to rename the directory */
  1284. +  if (rename (maildir_file (tmp,old),maildir_file (tmpnew,new))) {
  1285. +    sprintf (tmp,"Can't rename mailbox %s to %s: %s",old,new,strerror (errno));
  1286. +    mm_log (tmp,ERROR);
  1287. +    return NIL;
  1288. +  }
  1289. +  return T;            /* return success */
  1290. +}
  1291. +
  1292. +long maildir_sub (MAILSTREAM *stream,char *mailbox)
  1293. +{
  1294. +  char tmp[MAILTMPLEN];
  1295. +  return sm_subscribe (mailbox);
  1296. +}
  1297. +
  1298. +long maildir_unsub (MAILSTREAM *stream,char *mailbox)
  1299. +{
  1300. +  char tmp[MAILTMPLEN];
  1301. +  return sm_unsubscribe (mailbox);
  1302. +}
  1303. +
  1304. +void maildir_lsub (MAILSTREAM *stream,char *ref,char *pat)
  1305. +{
  1306. + void *sdb = NIL;
  1307. +  char *s;
  1308. +                                /* get canonical form of name */
  1309. +  if ((s = sm_read (&sdb))) {
  1310. +    do if (pmatch_full (s,pat,'/')) mm_lsub (stream,'/',s,NIL);
  1311. +    while (s = sm_read (&sdb)); /* until no more subscriptions */
  1312. +  }
  1313. +
  1314. +}
  1315. diff -Nur pine4.43/imap/src/osdep/unix/maildir.h pine4.43-maildir/imap/src/osdep/unix/maildir.h
  1316. --- pine4.43/imap/src/osdep/unix/maildir.h    Thu Jan  1 01:00:00 1970
  1317. +++ pine4.43-maildir/imap/src/osdep/unix/maildir.h    Wed Dec  5 09:32:47 2001
  1318. @@ -0,0 +1,56 @@
  1319. +/*
  1320. + * Please read maildir.c for license and information
  1321. + *
  1322. + */
  1323. +
  1324. +#define MAILDIRPATH "Maildir"
  1325. +    
  1326. +typedef struct maildir_local {
  1327. +  unsigned int inbox : 1;    /* if it is an INBOX or not */
  1328. +  unsigned int dirty : 1;    /* diskcopy needs updating */
  1329. +  char *dir;            /* mail directory name */
  1330. +  char *buf;            /* temporary buffer */
  1331. +  char *hdr;            /* current header */
  1332. +  unsigned long buflen;        /* current size of temporary buffer */
  1333. +  time_t scantime;        /* last time directory scanned */
  1334. +} MAILDIRLOCAL;
  1335. +
  1336. +/* Convenient access to local data */
  1337. +
  1338. +#define LOCAL ((MAILDIRLOCAL *) stream->local)
  1339. +
  1340. +/* Function prototypes */
  1341. +
  1342. +DRIVER *maildir_valid (char *name);
  1343. +int maildir_isvalid (char *name,long justname);
  1344. +MAILSTREAM *maildir_open (MAILSTREAM *stream);
  1345. +void maildir_gc (MAILSTREAM *stream,long gcflags);
  1346. +void maildir_close (MAILSTREAM *stream, long options);
  1347. +long maildir_ping (MAILSTREAM *stream);
  1348. +long maildir_ping_core (MAILSTREAM *stream);
  1349. +void maildir_check (MAILSTREAM *stream);
  1350. +long maildir_fetchtext (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
  1351. +char *maildir_fetchheader (MAILSTREAM *stream,unsigned long msgno,
  1352. +        unsigned long *length, long flags);
  1353. +void maildir_fast (MAILSTREAM *stream,char *sequence,long flags);
  1354. +void maildir_list (MAILSTREAM *stream,char *ref,char *pat);
  1355. +void *maildir_parameters (long function,void *value);
  1356. +long maildir_create (MAILSTREAM *stream,char *mailbox);
  1357. +void maildir_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
  1358. +void maildir_expunge (MAILSTREAM *stream);
  1359. +long maildir_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
  1360. +long maildir_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  1361. +                   STRING *message);
  1362. +short maildir_getflags (MAILSTREAM *stream,char *flag);
  1363. +long maildir_delete (MAILSTREAM *stream,char *mailbox);
  1364. +long maildir_rename (MAILSTREAM *stream,char *old,char *new);
  1365. +long maildir_sub (MAILSTREAM *stream,char *mailbox);
  1366. +long maildir_unsub (MAILSTREAM *stream,char *mailbox);
  1367. +void maildir_lsub (MAILSTREAM *stream,char *ref,char *pat);
  1368. +
  1369. +/* utility functions */
  1370. +char *maildir_file (char *dst,char *name);
  1371. +void maildir_copynew (const char *mailbox);
  1372. +int maildir_select (struct direct *name);
  1373. +int maildir_namesort (struct direct **d1,struct direct **d2);
  1374. +void maildir_listwork(char *name, MAILSTREAM *stream, char *s2, char *subdir, int flag);
  1375. diff -Nur pine4.43/pine/mailcmd.c pine4.43-maildir/pine/mailcmd.c
  1376. --- pine4.43/pine/mailcmd.c    Thu Nov  1 22:28:10 2001
  1377. +++ pine4.43-maildir/pine/mailcmd.c    Wed Dec  5 10:14:32 2001
  1378. @@ -2712,8 +2712,7 @@
  1379.       * the destination folder will need...
  1380.       */
  1381.      context_apply(tmp, context, save_folder, sizeof(tmp));
  1382. -        save_stream = (stream->dtb->flags & DR_LOCAL) && !IS_REMOTE(tmp) ?
  1383. -      stream : context_same_stream(context, save_folder, stream);
  1384. +        save_stream = context_same_stream(context, save_folder, stream);
  1385.      }
  1386.  
  1387.      /* if needed, this'll get set in mm_notify */
  1388. diff -Nur pine4.43/pine/pine.h pine4.43-maildir/pine/pine.h
  1389. --- pine4.43/pine/pine.h    Wed Nov 28 01:12:13 2001
  1390. +++ pine4.43-maildir/pine/pine.h    Wed Dec  5 10:11:40 2001
  1391. @@ -63,7 +63,7 @@
  1392.  #ifndef _PINE_INCLUDED
  1393.  #define _PINE_INCLUDED
  1394.  
  1395. -#define PINE_VERSION        "4.43"
  1396. +#define PINE_VERSION        "4.43L0"
  1397.  #define    PHONE_HOME_VERSION    "-count"
  1398.  #define    PHONE_HOME_HOST        "docserver.cac.washington.edu"
  1399.  
  1400.