1 /* ========================================================================
2 * Copyright 2008-2011 Mark Crispin
3 * ========================================================================
7 * Program: Subscription Manager
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
32 /* Subscribe to mailbox
33 * Accepts: mailbox name
34 * Returns: T on success, NIL on failure
37 long sm_subscribe (char *mailbox
)
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
);
56 if (!(f
= fopen (db
,"a"))) { /* append new entry */
57 MM_LOG ("Can't append to subscription database",ERROR
);
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
)
72 char *s
,tmp
[MAILTMPLEN
],old
[MAILTMPLEN
],newname
[MAILTMPLEN
];
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
);
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 */
91 if (fclose (tf
) == EOF
)
92 MM_LOG ("Can't write subscription temporary file",ERROR
);
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
);
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
;
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
;
119 if (fgets (sbname
,MAILTMPLEN
,f
)) {
120 if ((s
= strchr (sbname
,'\n')) != NULL
) *s
= '\0';
123 fclose (f
); /* all done */
124 *sdb
= NIL
; /* zap sdb */