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 / c-client / smanager.c < prev    next >
C/C++ Source or Header  |  1998-09-16  |  4KB  |  137 lines

  1. /*
  2.  * Program:    Subscription Manager
  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:    3 December 1992
  13.  * Last Edited:    14 April 1997
  14.  *
  15.  * Copyright 1997 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. #include <stdio.h>
  38. #include <ctype.h>
  39. #include "mail.h"
  40. #include "osdep.h"
  41. #include "misc.h"
  42.  
  43. /* Subscribe to mailbox
  44.  * Accepts: mailbox name
  45.  * Returns: T on success, NIL on failure
  46.  */
  47.  
  48. long sm_subscribe (char *mailbox)
  49. {
  50.   FILE *f;
  51.   char *s,db[MAILTMPLEN],tmp[MAILTMPLEN];
  52.   SUBSCRIPTIONFILE (db);    /* open subscription database */
  53.   if (f = fopen (db,"r")) {    /* make sure not already there */
  54.     while (fgets (tmp,MAILTMPLEN,f)) {
  55.       if (s = strchr (tmp,'\n')) *s = '\0';
  56.       if (!strcmp (tmp,mailbox)) {/* already subscribed? */
  57.     sprintf (tmp,"Already subscribed to mailbox %s",mailbox);
  58.     mm_log (tmp,ERROR);
  59.     fclose (f);
  60.     return NIL;
  61.       }
  62.     }
  63.     fclose (f);
  64.   }
  65.   if (!(f = fopen (db,"a"))) {    /* append new entry */
  66.     mm_log ("Can't create subscription database",ERROR);
  67.     return NIL;
  68.   }
  69.   fprintf (f,"%s\n",mailbox);
  70.   return (fclose (f) == EOF) ? NIL : T;
  71. }
  72.  
  73. /* Unsubscribe from mailbox
  74.  * Accepts: mailbox name
  75.  * Returns: T on success, NIL on failure
  76.  */
  77.  
  78. long sm_unsubscribe (char *mailbox)
  79. {
  80.   FILE *f,*tf;
  81.   char *s,tmp[MAILTMPLEN],old[MAILTMPLEN],newname[MAILTMPLEN];
  82.   long ret = NIL;
  83.   SUBSCRIPTIONFILE (old);    /* open subscription database */
  84.   if (!(f = fopen (old,"r"))) {    /* can we? */
  85.     mm_log ("No subscriptions",ERROR);
  86.     return NIL;
  87.   }
  88.   SUBSCRIPTIONTEMP (newname);    /* make tmp file name */
  89.   if (!(tf = fopen (newname,"w"))) {
  90.     mm_log ("Can't create subscription temporary file",ERROR);
  91.     fclose (f);
  92.     return NIL;
  93.   }
  94.   while (fgets (tmp,MAILTMPLEN,f)) {
  95.     if (s = strchr (tmp,'\n')) *s = '\0';
  96.     if (strcmp (tmp,mailbox)) fprintf (tf,"%s\n",tmp);
  97.     else ret = T;        /* found the name */
  98.   }
  99.   fclose (f);
  100.   if (fclose (tf) == EOF) {
  101.     mm_log ("Can't write subscription temporary file",ERROR);
  102.     return NIL;
  103.   }
  104.   if (!ret) {
  105.     sprintf (tmp,"Not subscribed to mailbox %s",mailbox);
  106.     mm_log (tmp,ERROR);        /* error if at end */
  107.   }
  108.   else rename (newname,old);
  109.   return ret;
  110. }
  111.  
  112. /* Read subscription database
  113.  * Accepts: pointer to subscription database handle (handle NIL if first time)
  114.  * Returns: character string for subscription database or NIL if done
  115.  */
  116.  
  117. static char sbname[MAILTMPLEN];
  118.  
  119. char *sm_read (void **sdb)
  120. {
  121.   FILE *f = (FILE *) *sdb;
  122.   char *s;
  123.   if (!f) {            /* first time through? */
  124.     SUBSCRIPTIONFILE (sbname);    /* open subscription database */
  125.                 /* make sure not already there */
  126.     if (f = fopen (sbname,"r")) *sdb = (void *) f;
  127.     else return NIL;
  128.   }
  129.   if (fgets (sbname,MAILTMPLEN,f)) {
  130.     if (s = strchr (sbname,'\n')) *s = '\0';
  131.     return sbname;
  132.   }
  133.   fclose (f);            /* all done */
  134.   *sdb = NIL;            /* zap sdb */
  135.   return NIL;
  136. }
  137.