0
|
1 /* m_gmsg.c - read a folder */
|
|
2 #ifndef lint
|
12
|
3 static char ident[] = "@(#)$Id: m_gmsg.c,v 1.1.1.1 2005/04/18 14:46:06 kono Exp $";
|
0
|
4 #endif /* lint */
|
|
5
|
|
6 #include "../h/mh.h"
|
|
7 #include "../h/local.h"
|
|
8 #include <stdio.h>
|
|
9
|
|
10
|
|
11 #define NINFO (MAXFOLDER / 5) /* PLEASE be non-trivial... */
|
|
12 struct info {
|
|
13 int msgno;
|
|
14 int stats;
|
|
15 };
|
|
16
|
|
17 static int len=0;
|
|
18 static struct info *head;
|
|
19
|
|
20 static m_getatr();
|
|
21 static int m_setatr();
|
|
22 /* */
|
|
23
|
|
24 struct msgs *m_gmsg (name)
|
|
25 register char *name;
|
|
26 {
|
|
27 #ifdef COMPAT
|
|
28 register int cur,
|
|
29 fd;
|
|
30 #endif /* COMPAT */
|
|
31 register int i,
|
|
32 j;
|
|
33 register struct info *rover,
|
|
34 *tail;
|
|
35 #ifdef COMPAT
|
|
36 register char *cp;
|
|
37 char buffer[BUFSIZ];
|
|
38 #endif /* COMPAT */
|
|
39 register struct msgs *mp;
|
|
40 #ifdef SYS5DIR
|
|
41 register struct dirent *dp;
|
|
42 DIR * dd;
|
|
43 #else /* SYS5DIR */
|
|
44 register struct direct *dp;
|
|
45 register DIR * dd;
|
|
46 #endif /* SYS5DIR */
|
|
47 struct stat st;
|
|
48
|
|
49 if ((dd = opendir (name = m_mailpath (name))) == NULL) {
|
|
50 free (name);
|
|
51 return NULL;
|
|
52 }
|
|
53 if (stat (name, &st) == NOTOK) {
|
|
54 free (name);
|
|
55 return NULL;
|
|
56 }
|
|
57
|
|
58 mp = (struct msgs *) malloc (MHSIZE (mp, 0, 0));
|
|
59 if (mp == NULL)
|
|
60 adios (NULLCP, "unable to allocate folder storage");
|
|
61 mp -> lowmsg = mp -> hghmsg = mp -> nummsg = 0;
|
|
62 mp -> curmsg = 0;
|
|
63 mp -> lowsel = mp -> hghsel = mp -> numsel = 0;
|
|
64 mp -> foldpath = name;
|
|
65 mp -> msgflags = 0;
|
|
66 if (st.st_uid != getuid () || access (name, 02) == NOTOK)
|
|
67 mp -> msgflags |= READONLY;
|
|
68 #ifdef COMPAT
|
|
69 cur = 0;
|
|
70 #endif /* COMPAT */
|
|
71 j = strlen (SBACKUP);
|
|
72 if (head == NULL)
|
|
73 if ((head = (struct info *)
|
|
74 malloc ((unsigned) ((len = NINFO) * sizeof *head))) == NULL)
|
|
75 adios (NULLCP, "unable to allocate info storage");
|
|
76 tail = (rover = head) + len;
|
|
77
|
|
78 while (dp = readdir (dd)) {
|
|
79 #if 0
|
|
80 /* There might exists a sub-folder whose name is number.
|
|
81 However, this check is too slow for us to adopt it. */
|
|
82 char *buf;
|
|
83 buf = concat(name, "/", dp -> d_name, NULLCP);
|
|
84 if ((i = m_atoi (dp -> d_name))
|
|
85 && stat (buf, &st) != NOTOK
|
|
86 && (st.st_mode & S_IFMT) != S_IFDIR) {
|
|
87 #else
|
|
88 if (i = m_atoi (dp -> d_name)) {
|
|
89 #endif
|
|
90 if (rover >= tail) {
|
|
91 register int curlen = tail - head;
|
|
92
|
|
93 if ((tail = (struct info *) realloc ((char *) head,
|
|
94 (unsigned) ((len += NINFO) * sizeof *head)))
|
|
95 == NULL)
|
|
96 adios (NULLCP, "unable to allocate info storage");
|
|
97 else
|
|
98 rover = tail + curlen, head = tail, tail += len;
|
|
99 }
|
|
100 if (i > mp -> hghmsg)
|
|
101 mp -> hghmsg = i;
|
|
102 mp -> nummsg++;
|
|
103 if (mp -> lowmsg == 0 || i < mp -> lowmsg)
|
|
104 mp -> lowmsg = i;
|
|
105 rover -> msgno = i;
|
|
106 rover -> stats = EXISTS;
|
|
107 #ifdef notdef
|
|
108 rover -> stats &= ~DELETED;
|
|
109 #endif /* notdef */
|
|
110 rover++;
|
|
111 }
|
|
112 else
|
|
113 switch (dp -> d_name[0]) {
|
|
114 case '.':
|
|
115 continue;
|
|
116
|
|
117 case ',':
|
|
118 #ifdef notdef
|
|
119 if ((i = m_atoi (dp -> d_name + 1)) {
|
|
120 register struct info *l;
|
|
121
|
|
122 for (l = head; l < rover; l++)
|
|
123 if (l -> msgno == i) {
|
|
124 if (!(l -> stats & EXISTS))
|
|
125 l -> stats |= DELETED;
|
|
126 break;
|
|
127 }
|
|
128 }
|
|
129 #endif /* notdef */
|
|
130 continue;
|
|
131
|
|
132 #ifdef MHE
|
|
133 case '+':
|
|
134 continue;
|
|
135 #endif /* MHE */
|
|
136
|
|
137 #ifdef UCI
|
|
138 case '_':
|
|
139 case '#':
|
|
140 continue;
|
|
141 #endif /* UCI */
|
|
142
|
|
143 default:
|
|
144 #ifdef COMPAT
|
|
145 if (strcmp (dp -> d_name, current) == 0) {
|
|
146 cur++;
|
|
147 continue;
|
|
148 }
|
|
149 #endif /* COMPAT */
|
|
150 if (strcmp (dp -> d_name, LINK) == 0
|
|
151 || strncmp (dp -> d_name, SBACKUP, j) == 0)
|
|
152 continue;
|
|
153 mp -> msgflags |= OTHERS;
|
|
154 continue;
|
|
155 }
|
|
156 #if 0 /* for a sub-folder whose name is number */
|
|
157 free(buf);
|
|
158 #endif
|
|
159 }
|
|
160
|
|
161 closedir (dd);
|
|
162
|
|
163 /* */
|
|
164
|
|
165 #ifdef COMPAT
|
|
166 (void) sprintf (buffer, "%s-%s", current, name);
|
|
167 if (cp = m_find (buffer)) {
|
|
168 i = m_atoi (cp);
|
|
169 (void) m_delete(buffer);
|
|
170 if (i > 0)
|
|
171 mp -> curmsg = i;
|
|
172 }
|
|
173 if (mp -> curmsg == 0 && cur && (fd = open (current, 0)) != NOTOK) {
|
|
174 if ((i = read (fd, buffer, sizeof buffer)) > 0) {
|
|
175 if (cp = index (buffer, '\n'))
|
|
176 *cp = 0;
|
|
177 if ((i = m_atoi (buffer)) > 0)
|
|
178 mp -> curmsg = i;
|
|
179 }
|
|
180 (void) close (fd);
|
|
181 }
|
|
182 if (cur && !(mp -> msgflags & READONLY)){ /* sneaky... */
|
|
183 (void) sprintf (buffer, "%s/%s", name, current);
|
|
184 (void) unlink (buffer);
|
|
185 }
|
|
186 #endif /* COMPAT */
|
|
187
|
|
188 #ifndef MTR
|
|
189 mp -> lowoff = 1;
|
|
190 #else /* MTR */
|
|
191 mp -> lowoff = mp -> lowmsg;
|
|
192 #endif /* MTR */
|
|
193 mp -> hghoff = mp -> hghmsg + 1;/* for "new" in m_convert */
|
|
194
|
|
195 mp = (struct msgs *)
|
|
196 realloc ((char *) mp, MHSIZE (mp, mp -> lowoff, mp -> hghoff));
|
|
197 if (mp == NULL)
|
|
198 adios (NULLCP, "unable to allocate folder storage");
|
|
199 #ifndef MTR
|
|
200 for (i = mp -> lowmsg; i <= mp -> hghmsg; i++)
|
|
201 mp -> msgstats[i] = 0;
|
|
202 #else /* MTR */
|
|
203 mp -> msgstats = (int *)
|
|
204 calloc ((unsigned) 1, MHSIZEX (mp, mp -> lowmsg, mp -> hghmsg));
|
|
205 if (mp -> msgstats == NULL)
|
|
206 adios (NULLCP, "unable to allocate messages storage");
|
|
207 mp -> msgstats = (mp -> msgbase = mp -> msgstats) - mp -> lowoff;
|
|
208 if (mp -> msgstats < (int *)0) /* non portable */
|
|
209 adios (NULLCP, "m_gmsg() botch -- you lose big");
|
|
210 #endif /* MTR */
|
|
211 for (tail = head; tail < rover; tail++)
|
|
212 mp -> msgstats[tail -> msgno] = tail -> stats;
|
|
213 m_getatr (mp);
|
|
214
|
|
215 return mp;
|
|
216 }
|
|
217
|
|
218 /* */
|
|
219
|
|
220 static m_getatr (mp)
|
|
221 register struct msgs *mp;
|
|
222 {
|
|
223 int alen,
|
|
224 bits,
|
|
225 i,
|
|
226 j,
|
|
227 plen,
|
|
228 state;
|
|
229 register char *cp;
|
|
230 char name[NAMESZ],
|
|
231 field[BUFSIZ * 2],
|
|
232 *tmpfld = NULLCP;
|
|
233 register struct node *np;
|
|
234 register FILE * fp;
|
|
235
|
|
236 bits = FFATTRSLOT;
|
|
237
|
|
238 mp -> msgattrs[i = 0] = getcpy (current);
|
|
239 mp -> msgattrs[++i] = '\0';
|
|
240 mp -> attrstats = 0; /* initially, all public */
|
|
241
|
|
242 m_getdefs ();
|
|
243 if (mh_seq == '\0' || *mh_seq == '\0')
|
|
244 goto private_only;
|
|
245
|
|
246 (void) sprintf (field, "%s/%s", mp -> foldpath, mh_seq);
|
|
247 if (fp = fopen (field, "r")) {
|
|
248 for (state = FLD;;) {
|
|
249 switch (state = m_getfld (state, name, field, sizeof field, fp)) {
|
|
250 case FLD:
|
|
251 case FLDPLUS:
|
|
252 case FLDEOF:
|
|
253 tmpfld = add(field, NULLCP);
|
|
254 while (state == FLDPLUS) { /* for very very long line */
|
|
255 state = m_getfld(state, name, field, sizeof field, fp);
|
|
256 tmpfld = add(field, tmpfld);
|
|
257 }
|
|
258 (void) m_setatr (mp, getcpy (name), trimcpy (tmpfld));
|
|
259 free(tmpfld);
|
|
260 if (state == FLDEOF)
|
|
261 break;
|
|
262 continue;
|
|
263
|
|
264 case BODY:
|
|
265 case BODYEOF:
|
|
266 adios (NULLCP,
|
|
267 "no blank lines are permitted in %s/%s",
|
|
268 mp -> foldpath, mh_seq);/* fall */
|
|
269
|
|
270 case FILEEOF:
|
|
271 break;
|
|
272
|
|
273 default:
|
|
274 adios (NULLCP, "%s/%s is poorly formatted",
|
|
275 mp -> foldpath, mh_seq);
|
|
276 }
|
|
277 break;
|
|
278 }
|
|
279 (void) fclose (fp);
|
|
280 }
|
|
281
|
|
282 private_only: ;
|
|
283 alen = strlen ("atr-");
|
|
284 plen = strlen (mp -> foldpath) + 1;
|
|
285
|
|
286 for (np = m_defs; np; np = np -> n_next)
|
|
287 if (ssequal ("atr-", np -> n_name)
|
|
288 && (j = strlen (np -> n_name) - plen) > alen
|
|
289 && *(np -> n_name + j) == '-'
|
|
290 && strcmp (mp -> foldpath, np -> n_name + j + 1) == 0) {
|
|
291 cp = getcpy (np -> n_name + alen);
|
|
292 *(cp + j - alen) = '\0';
|
|
293 if ((i = m_setatr (mp, cp, getcpy (np -> n_field))) != NOTOK)
|
|
294 mp -> attrstats |= 1 << (bits + i);/* private */
|
|
295 }
|
|
296 }
|
|
297
|
|
298 /* */
|
|
299
|
|
300 static int m_setatr (mp, name, field)
|
|
301 register struct msgs *mp;
|
|
302 register char *name,
|
|
303 *field;
|
|
304 {
|
|
305 int bits,
|
|
306 hack;
|
|
307 register int i,
|
|
308 j,
|
|
309 k;
|
|
310 register char *cp,
|
|
311 **ap;
|
|
312
|
|
313 bits = FFATTRSLOT;
|
|
314 hack = strcmp (current, name) == 0;/* hack... */
|
|
315 /* if we're going to use UNSEEN, it should be set here! */
|
|
316
|
|
317 for (i = 0; mp -> msgattrs[i]; i++)
|
|
318 if (strcmp (mp -> msgattrs[i], name) == 0) {
|
|
319 for (j = mp -> lowmsg; j <= mp -> hghmsg; j++)
|
|
320 mp -> msgstats[j] &= ~(1 << (bits + i));
|
|
321 break;
|
|
322 }
|
|
323 if (i >= NATTRS) {
|
|
324 free (name);
|
|
325 free (field);
|
|
326 return NOTOK;
|
|
327 }
|
|
328
|
|
329 if (mp -> msgattrs[i] == NULL) {
|
|
330 mp -> msgattrs[i] = name;
|
|
331 mp -> msgattrs[i + 1] = NULL;
|
|
332 }
|
|
333 else
|
|
334 free (name);
|
|
335
|
|
336 for (ap = brkstring (field, " ", "\n");
|
|
337 *ap;
|
|
338 ap++) {
|
|
339 if (cp = index (*ap, '-'))
|
|
340 *cp++ = '\0';
|
|
341 if ((j = m_atoi (*ap)) > 0) {
|
|
342 #ifdef notdef
|
|
343 if (hack && j >= mp -> lowmsg && j <= mp -> hghmsg
|
|
344 && (mp -> msgstats[j] & EXISTS))
|
|
345 mp -> curmsg = j;
|
|
346 #else /* not notdef */
|
|
347 if (hack)
|
|
348 mp -> curmsg = j;
|
|
349 #endif /* not notdef */
|
|
350 for (k = cp ? m_atoi (cp) : j; j <= k; j++)
|
|
351 if (j >= mp -> lowmsg && j <= mp -> hghmsg
|
|
352 && (mp -> msgstats[j] & EXISTS))
|
|
353 mp -> msgstats[j] |= 1 << (bits + i);
|
|
354 }
|
|
355 }
|
|
356 free (field);
|
|
357
|
|
358 return i;
|
|
359 }
|