imapext-2007

view src/c-client/smanager.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Subscription Manager
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 3 December 1992
26 * Last Edited: 6 December 2006
27 */
30 #include <stdio.h>
31 #include <ctype.h>
32 #include "c-client.h"
34 /* Subscribe to mailbox
35 * Accepts: mailbox name
36 * Returns: T on success, NIL on failure
37 */
39 long sm_subscribe (char *mailbox)
40 {
41 FILE *f;
42 char *s,db[MAILTMPLEN],tmp[MAILTMPLEN];
43 /* canonicalize INBOX */
44 if (!compare_cstring (mailbox,"INBOX")) mailbox = "INBOX";
45 SUBSCRIPTIONFILE (db); /* open subscription database */
46 if (f = fopen (db,"r")) { /* make sure not already there */
47 while (fgets (tmp,MAILTMPLEN,f)) {
48 if (s = strchr (tmp,'\n')) *s = '\0';
49 if (!strcmp (tmp,mailbox)) {/* already subscribed? */
50 sprintf (tmp,"Already subscribed to mailbox %.80s",mailbox);
51 MM_LOG (tmp,ERROR);
52 fclose (f);
53 return NIL;
54 }
55 }
56 fclose (f);
57 }
58 if (!(f = fopen (db,"a"))) { /* append new entry */
59 MM_LOG ("Can't append to subscription database",ERROR);
60 return NIL;
61 }
62 fprintf (f,"%s\n",mailbox);
63 return (fclose (f) == EOF) ? NIL : T;
64 }
66 /* Unsubscribe from mailbox
67 * Accepts: mailbox name
68 * Returns: T on success, NIL on failure
69 */
71 long sm_unsubscribe (char *mailbox)
72 {
73 FILE *f,*tf;
74 char *s,tmp[MAILTMPLEN],old[MAILTMPLEN],newname[MAILTMPLEN];
75 int found = NIL;
76 /* canonicalize INBOX */
77 if (!compare_cstring (mailbox,"INBOX")) mailbox = "INBOX";
78 SUBSCRIPTIONFILE (old); /* make file names */
79 SUBSCRIPTIONTEMP (newname);
80 if (!(f = fopen (old,"r"))) /* open subscription database */
81 MM_LOG ("No subscriptions",ERROR);
82 else if (!(tf = fopen (newname,"w"))) {
83 MM_LOG ("Can't create subscription temporary file",ERROR);
84 fclose (f);
85 }
86 else {
87 while (fgets (tmp,MAILTMPLEN,f)) {
88 if (s = strchr (tmp,'\n')) *s = '\0';
89 if (strcmp (tmp,mailbox)) fprintf (tf,"%s\n",tmp);
90 else found = T; /* found the name */
91 }
92 fclose (f);
93 if (fclose (tf) == EOF)
94 MM_LOG ("Can't write subscription temporary file",ERROR);
95 else if (!found) {
96 sprintf (tmp,"Not subscribed to mailbox %.80s",mailbox);
97 MM_LOG (tmp,ERROR); /* error if at end */
98 }
99 else if (!unlink (old) && !rename (newname,old)) return LONGT;
100 else MM_LOG ("Can't update subscription database",ERROR);
101 }
102 return NIL;
103 }
105 /* Read subscription database
106 * Accepts: pointer to subscription database handle (handle NIL if first time)
107 * Returns: character string for subscription database or NIL if done
108 */
110 static char sbname[MAILTMPLEN];
112 char *sm_read (void **sdb)
113 {
114 FILE *f = (FILE *) *sdb;
115 char *s;
116 if (!f) { /* first time through? */
117 SUBSCRIPTIONFILE (sbname); /* open subscription database */
118 /* make sure not already there */
119 if (f = fopen (sbname,"r")) *sdb = (void *) f;
120 else return NIL;
121 }
122 if (fgets (sbname,MAILTMPLEN,f)) {
123 if (s = strchr (sbname,'\n')) *s = '\0';
124 return sbname;
125 }
126 fclose (f); /* all done */
127 *sdb = NIL; /* zap sdb */
128 return NIL;
129 }

UW-IMAP'd extensions by yuuji