* new version 2.20.9.
[alpine.git] / imap / src / c-client / smanager.c
blob437e10794620fc57d1cb0837148e89a91de41b8a
1 /* ========================================================================
2 * Copyright 2008-2011 Mark Crispin
3 * ========================================================================
4 */
6 /*
7 * Program: Subscription Manager
9 * Author: Mark Crispin
11 * Date: 3 December 1992
12 * Last Edited: 8 April 2011
14 * Previous versions of this file were
16 * Copyright 1988-2006 University of Washington
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
28 #include <stdio.h>
29 #include <ctype.h>
30 #include "c-client.h"
32 /* Subscribe to mailbox
33 * Accepts: mailbox name
34 * Returns: T on success, NIL on failure
37 long sm_subscribe (char *mailbox)
39 FILE *f;
40 char *s,db[MAILTMPLEN],tmp[MAILTMPLEN];
41 /* canonicalize INBOX */
42 if (!compare_cstring (mailbox,"INBOX")) mailbox = "INBOX";
43 SUBSCRIPTIONFILE (db); /* open subscription database */
44 if ((f = fopen (db,"r")) != NULL) { /* make sure not already there */
45 while (fgets (tmp,MAILTMPLEN,f)) {
46 if ((s = strchr (tmp,'\n')) != NULL) *s = '\0';
47 if (!strcmp (tmp,mailbox)) {/* already subscribed? */
48 sprintf (tmp,"Already subscribed to mailbox %.80s",mailbox);
49 MM_LOG (tmp,ERROR);
50 fclose (f);
51 return NIL;
54 fclose (f);
56 if (!(f = fopen (db,"a"))) { /* append new entry */
57 MM_LOG ("Can't append to subscription database",ERROR);
58 return NIL;
60 fprintf (f,"%s\n",mailbox);
61 return (fclose (f) == EOF) ? NIL : T;
64 /* Unsubscribe from mailbox
65 * Accepts: mailbox name
66 * Returns: T on success, NIL on failure
69 long sm_unsubscribe (char *mailbox)
71 FILE *f,*tf;
72 char *s,tmp[MAILTMPLEN],old[MAILTMPLEN],newname[MAILTMPLEN];
73 int found = NIL;
74 /* canonicalize INBOX */
75 if (!compare_cstring (mailbox,"INBOX")) mailbox = "INBOX";
76 SUBSCRIPTIONFILE (old); /* make file names */
77 SUBSCRIPTIONTEMP (newname);
78 if (!(f = fopen (old,"r"))) /* open subscription database */
79 MM_LOG ("No subscriptions",ERROR);
80 else if (!(tf = fopen (newname,"w"))) {
81 MM_LOG ("Can't create subscription temporary file",ERROR);
82 fclose (f);
84 else {
85 while (fgets (tmp,MAILTMPLEN,f)) {
86 if ((s = strchr (tmp,'\n')) != NULL) *s = '\0';
87 if (strcmp (tmp,mailbox)) fprintf (tf,"%s\n",tmp);
88 else found = T; /* found the name */
90 fclose (f);
91 if (fclose (tf) == EOF)
92 MM_LOG ("Can't write subscription temporary file",ERROR);
93 else if (!found) {
94 sprintf (tmp,"Not subscribed to mailbox %.80s",mailbox);
95 MM_LOG (tmp,ERROR); /* error if at end */
97 else if (!unlink (old) && !rename (newname,old)) return LONGT;
98 else MM_LOG ("Can't update subscription database",ERROR);
100 return NIL;
103 /* Read subscription database
104 * Accepts: pointer to destination buffer of size MAILTMPLEN
105 * pointer to subscription database handle (handle NIL if first time)
106 * Returns: character string for subscription database or NIL if done
109 char *sm_read (char *sbname,void **sdb)
111 FILE *f = (FILE *) *sdb;
112 char *s;
113 if (!f) { /* first time through? */
114 SUBSCRIPTIONFILE (sbname); /* open subscription database */
115 /* make sure not already there */
116 if ((f = fopen (sbname,"r")) != NULL) *sdb = (void *) f;
117 else return NIL;
119 if (fgets (sbname,MAILTMPLEN,f)) {
120 if ((s = strchr (sbname,'\n')) != NULL) *s = '\0';
121 return sbname;
123 fclose (f); /* all done */
124 *sdb = NIL; /* zap sdb */
125 return NIL;