/[imapfilter]/imapfilter/request.c
ViewVC logotype

Annotation of /imapfilter/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (hide annotations)
Sat Jul 13 14:11:08 2002 UTC (21 years, 9 months ago) by lefcha
Branch: MAIN
Changes since 1.24: +50 -51 lines
File MIME type: text/plain
Added variable to enable direct expunge of marked deleted mail.

1 lefcha 1.1 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4 lefcha 1.3 #include <errno.h>
5 lefcha 1.1
6     #include "config.h"
7     #include "imapfilter.h"
8     #include "data.h"
9    
10    
11 lefcha 1.21 extern int sockpri, sockaux;
12 lefcha 1.1 extern unsigned int options;
13 lefcha 1.18 extern unsigned int capabilities;
14    
15 lefcha 1.21 namesp_t nsppri, nspaux; /* Primary and auxiliary namespace. */
16 lefcha 1.1
17 lefcha 1.3
18 lefcha 1.2 #ifdef DEBUG
19 lefcha 1.1 /*
20     * Test/ping server.
21     */
22 lefcha 1.21 int test(int *sock)
23 lefcha 1.1 {
24 lefcha 1.21 return server_response(sock, imap_noop(sock));
25 lefcha 1.1 }
26 lefcha 1.25
27 lefcha 1.2 #endif
28 lefcha 1.1
29 lefcha 1.10
30 lefcha 1.1 /*
31 lefcha 1.10 * Check server's capabilities.
32 lefcha 1.1 */
33 lefcha 1.21 int check_capabilities(int *sock)
34 lefcha 1.1 {
35 lefcha 1.18 capabilities = CAPABILITY_NONE;
36    
37 lefcha 1.21 return capability_response(sock, imap_capability(sock));
38 lefcha 1.1 }
39    
40    
41     /*
42 lefcha 1.18 * Get namespace of mail server's mailboxes.
43     */
44 lefcha 1.25 int check_namespace(int *sock, namesp_t * nsp)
45 lefcha 1.18 {
46 lefcha 1.21 nsp->prefix[0] = nsp->delim = 0;
47 lefcha 1.18
48     if (!(options & OPTION_NAMESPACE) ||
49     !(capabilities & CAPABILITY_NAMESPACE))
50     return 0;
51     else
52 lefcha 1.21 return namespace_response(sock, imap_namespace(sock), nsp);
53 lefcha 1.18 }
54    
55    
56     /*
57 lefcha 1.10 * Login to server.
58 lefcha 1.8 */
59 lefcha 1.21 int login(int *sock, char *user, char *pass)
60 lefcha 1.8 {
61 lefcha 1.10 log_info(LOG_USERNAME, user);
62 lefcha 1.8
63 lefcha 1.21 return server_response(sock, imap_login(sock, user, pass));
64     }
65    
66    
67    
68     /*
69     * Check if a mailbox exists.
70     */
71 lefcha 1.25 int check_mailbox(int *sock, char *mbox, namesp_t * nsp)
72 lefcha 1.21 {
73 lefcha 1.25 return server_response(sock, imap_examine(sock,
74 lefcha 1.21 apply_namespace(mbox,
75     nsp->prefix,
76     nsp->delim)));
77 lefcha 1.8 }
78    
79 lefcha 1.18
80 lefcha 1.8 /*
81 lefcha 1.3 * Open mailbox in read-write mode.
82 lefcha 1.1 */
83 lefcha 1.25 int select_mailbox(int *sock, char *mbox, namesp_t * nsp)
84 lefcha 1.1 {
85     int r;
86 lefcha 1.5
87 lefcha 1.21 if (mailbox_status(sock, mbox, nsp) == -2)
88 lefcha 1.25 return -2; /* No messages exist. No filters need to be
89     applied. */
90 lefcha 1.5
91 lefcha 1.21 r = select_response(sock, imap_select(sock,
92     apply_namespace(mbox, nsp->prefix,
93     nsp->delim)));
94 lefcha 1.5
95 lefcha 1.4 log_info(LOG_MAILBOX, mbox);
96 lefcha 1.5
97 lefcha 1.1 return r;
98     }
99    
100    
101     /*
102 lefcha 1.3 * Get mailbox's status.
103 lefcha 1.1 */
104 lefcha 1.25 int mailbox_status(int *sock, char *mbox, namesp_t * nsp)
105 lefcha 1.1 {
106 lefcha 1.21 return status_response(sock, imap_status(sock,
107 lefcha 1.25 apply_namespace(mbox, nsp->prefix,
108     nsp->delim),
109     "MESSAGES RECENT UNSEEN"), mbox);
110 lefcha 1.16 }
111 lefcha 1.5
112 lefcha 1.9
113 lefcha 1.1 /*
114     * Close examined/selected mailbox.
115     */
116 lefcha 1.21 int close_mailbox(int *sock)
117 lefcha 1.1 {
118 lefcha 1.21 return server_response(sock, imap_close(sock));
119 lefcha 1.1 }
120    
121    
122     /*
123     * Logout from server.
124     */
125 lefcha 1.21 int logout(int *sock)
126 lefcha 1.1 {
127 lefcha 1.21 return server_response(sock, imap_logout(sock));
128 lefcha 1.1 }
129    
130    
131     /*
132     * Match and apply filters assigned to a mailbox.
133     */
134 lefcha 1.10 int apply_filters(filter_t ** filters)
135 lefcha 1.1 {
136 lefcha 1.3 int i;
137 lefcha 1.12 char *mesgs;
138 lefcha 1.1
139     for (i = 0; filters[i]; i++) {
140 lefcha 1.16 mesgs = NULL;
141 lefcha 1.1
142 lefcha 1.12 if (match_filter(filters[i], &mesgs))
143 lefcha 1.1 continue;
144 lefcha 1.5
145 lefcha 1.4 log_info(LOG_FILTER, filters[i]->key);
146 lefcha 1.1
147 lefcha 1.3 apply_action(mesgs, &(filters[i]->action.type),
148 lefcha 1.25 filters[i]->action.raccount, filters[i]->action.destmbox,
149 lefcha 1.21 filters[i]->action.args);
150 lefcha 1.12
151 lefcha 1.19 xfree(mesgs);
152 lefcha 1.1 }
153    
154     return 0;
155     }
156    
157    
158     /*
159     * Generate the search request by the masks of the filter and try to
160     * match the generated filter.
161     */
162 lefcha 1.12 int match_filter(filter_t * filter, char **mesgs)
163 lefcha 1.1 {
164 lefcha 1.3 char *search;
165 lefcha 1.5
166 lefcha 1.1 if (filter->mode == FILTER_MODE_OR)
167 lefcha 1.5 search = generate_filter_or(filter->masks, filter->masknum,
168     filter->masklen);
169 lefcha 1.1 else
170 lefcha 1.5 search = generate_filter_and(filter->masks, filter->masknum,
171     filter->masklen);
172 lefcha 1.1
173 lefcha 1.21 search_response(&sockpri, imap_search(&sockpri, search), mesgs);
174 lefcha 1.5
175 lefcha 1.19 xfree(search);
176 lefcha 1.5
177 lefcha 1.16 if (!*mesgs)
178 lefcha 1.1 return 1;
179 lefcha 1.5
180 lefcha 1.1 return 0;
181     }
182    
183    
184     /*
185     * Empty the FIFO inventory.
186     */
187     void empty_fifo(mask_t ** mfifo)
188     {
189     mfifo[0] = NULL;
190    
191 lefcha 1.3 queue_fifo(NULL, NULL);
192     dequeue_fifo(NULL);
193 lefcha 1.1 }
194    
195    
196     /*
197     * Add item to FIFO inventory.
198     */
199 lefcha 1.3 void queue_fifo(mask_t ** mfifo, mask_t * mask)
200 lefcha 1.1 {
201 lefcha 1.15 static unsigned int i;
202 lefcha 1.1
203     if (!mfifo) {
204     i = 0;
205     return;
206     }
207     mfifo[i++] = mask;
208     mfifo[i] = NULL;
209     }
210    
211    
212     /*
213     * Get next item from FIFO inventory.
214     */
215 lefcha 1.3 mask_t *dequeue_fifo(mask_t ** mfifo)
216 lefcha 1.1 {
217 lefcha 1.15 static unsigned int j;
218 lefcha 1.1
219     if (!mfifo) {
220     j = 0;
221     return NULL;
222     }
223     return mfifo[j++];
224     }
225    
226    
227     /*
228     * Generate the filter search command from the masks, assuming that
229     * masks are AND-ed.
230     */
231 lefcha 1.9 char *generate_filter_and(mask_t * mask, unsigned int masknum,
232 lefcha 1.10 unsigned int masklen)
233 lefcha 1.1 {
234 lefcha 1.3 const unsigned int searchbuf = masklen + masknum * 6 + 8;
235 lefcha 1.15 unsigned int len = 0;
236 lefcha 1.3 char *search;
237 lefcha 1.9 mask_t *tmp;
238 lefcha 1.5
239 lefcha 1.25 search = (char *)xmalloc(sizeof(char) * searchbuf);
240 lefcha 1.5
241 lefcha 1.3 search[0] = 0;
242 lefcha 1.1
243 lefcha 1.9 tmp = mask;
244 lefcha 1.10 if (!tmp) {
245     strncat(search, "ALL ", searchbuf - len - 1);
246     len += 4;
247     } else
248     while ((tmp = tmp->next)) {
249 lefcha 1.12 if (tmp->type != MASK_TYPE_OR) {
250     strncat(search, "ALL ", searchbuf - len - 1);
251     len += 4;
252     break;
253     }
254 lefcha 1.9 }
255 lefcha 1.1
256 lefcha 1.9 tmp = NULL;
257 lefcha 1.1 while (mask) {
258 lefcha 1.9 tmp = mask;
259     mask = mask->next;
260 lefcha 1.1
261 lefcha 1.9 if (mask && mask->type == MASK_TYPE_OR) {
262 lefcha 1.3 strncat(search, "OR (", searchbuf - len - 1);
263 lefcha 1.1 len += 4;
264    
265 lefcha 1.9 strncat(search, tmp->body, searchbuf - len - 1);
266     len = strlen(search);
267     search[len] = ' ';
268     search[++len] = 0;
269    
270     search[len - 1] = ')';
271     search[len] = ' ';
272     search[++len] = 0;
273    
274     if (!mask->next || mask->next->type != MASK_TYPE_OR) {
275     search[len] = '(';
276     search[++len] = 0;
277     strncat(search, mask->body, searchbuf - len - 1);
278 lefcha 1.1 len = strlen(search);
279 lefcha 1.9 search[len] = ')';
280     search[++len] = ' ';
281 lefcha 1.3 search[++len] = 0;
282 lefcha 1.9 mask = mask->next;
283 lefcha 1.1 }
284 lefcha 1.9 } else {
285     strncat(search, tmp->body, searchbuf - len - 1);
286     len = strlen(search);
287 lefcha 1.3 search[len] = ' ';
288     search[++len] = 0;
289 lefcha 1.1 }
290     }
291 lefcha 1.10
292 lefcha 1.9 search[len - 1] = 0;
293 lefcha 1.5
294 lefcha 1.3 return search;
295 lefcha 1.1 }
296    
297    
298     /*
299     * Generate the filter search command from the masks, assuming that
300 lefcha 1.22 * masks are OR-ed.
301 lefcha 1.1 */
302 lefcha 1.9 char *generate_filter_or(mask_t * mask, unsigned int masknum,
303 lefcha 1.10 unsigned int masklen)
304 lefcha 1.1 {
305 lefcha 1.3 const unsigned int searchbuf = masklen + masknum * 6 + 8;
306 lefcha 1.15 unsigned int len = 0;
307 lefcha 1.3 char *search;
308     mask_t **mfifo; /* Mailbox FIFO queue. */
309 lefcha 1.1 mask_t *mf; /* Mask returned from FIFO. */
310 lefcha 1.5
311 lefcha 1.25 search = (char *)xmalloc(sizeof(char) * searchbuf);
312 lefcha 1.5 mfifo = (mask_t **) xmalloc(sizeof(mask_t *) * (masknum + 1));
313 lefcha 1.1
314 lefcha 1.3 search[0] = 0;
315 lefcha 1.1 empty_fifo(mfifo);
316    
317 lefcha 1.3 strncat(search, "ALL ", searchbuf - len - 1);
318 lefcha 1.1 len += 4;
319    
320     while (mask) {
321 lefcha 1.3 queue_fifo(mfifo, mask);
322 lefcha 1.1 mask = mask->next;
323    
324     while (mask && mask->type == MASK_TYPE_AND) {
325 lefcha 1.3 queue_fifo(mfifo, mask);
326 lefcha 1.1 mask = mask->next;
327     }
328    
329     if (mask) {
330 lefcha 1.3 if (len == 4 && search[0] == 'A')
331     search[0] = len = 0;
332 lefcha 1.5
333 lefcha 1.3 strncat(search, "OR ", searchbuf - len - 1);
334 lefcha 1.1 len += 3;
335     }
336 lefcha 1.3 if (search[0] != 'A') {
337     search[len] = '(';
338     search[++len] = 0;
339     }
340     while ((mf = dequeue_fifo(mfifo))) {
341     strncat(search, mf->body, searchbuf - len - 1);
342 lefcha 1.1 len = strlen(search);
343 lefcha 1.3 search[len] = ' ';
344     search[++len] = 0;
345 lefcha 1.1 }
346    
347 lefcha 1.3 if (strchr(search, '(')) {
348     search[len - 1] = ')';
349     search[len] = ' ';
350     search[++len] = 0;
351     }
352 lefcha 1.1 empty_fifo(mfifo);
353     }
354    
355     search[len - 1] = 0;
356 lefcha 1.10
357 lefcha 1.19 xfree(mfifo);
358 lefcha 1.3
359     return search;
360 lefcha 1.1 }
361    
362    
363     /*
364     * Apply the appropriate action.
365     */
366 lefcha 1.25 int apply_action(char *mesgs, unsigned int *type, account_t * raccount,
367     char *destmbox, char *args)
368 lefcha 1.1 {
369 lefcha 1.3 unsigned int cnt;
370 lefcha 1.5
371 lefcha 1.3 if (!*mesgs)
372 lefcha 1.1 return 0;
373 lefcha 1.25
374 lefcha 1.4 log_info(LOG_ACTION, type);
375     log_info(LOG_DESTINATION_MAILBOX, destmbox);
376 lefcha 1.25
377 lefcha 1.21 cnt = count_messages(mesgs);
378 lefcha 1.25
379 lefcha 1.3 switch (*type) {
380 lefcha 1.1 case FILTER_ACTION_DELETE:
381 lefcha 1.8 info("%d message%s deleted.\n", cnt, plural(cnt));
382 lefcha 1.1 action_delete(mesgs, args);
383     break;
384     case FILTER_ACTION_COPY:
385 lefcha 1.20 info("%d message%s copied to mailbox \"%s\".\n", cnt, plural(cnt),
386 lefcha 1.8 destmbox);
387 lefcha 1.21 action_copy(mesgs, apply_namespace(destmbox, nsppri.prefix,
388     nsppri.delim), args);
389 lefcha 1.1 break;
390     case FILTER_ACTION_MOVE:
391 lefcha 1.20 info("%d message%s moved to mailbox \"%s\".\n", cnt, plural(cnt),
392 lefcha 1.8 destmbox);
393 lefcha 1.21 action_move(mesgs, apply_namespace(destmbox, nsppri.prefix,
394     nsppri.delim), args);
395     break;
396     case FILTER_ACTION_RCOPY:
397     info("%d message%s copied to mailbox \"%s\" at account %s\n", cnt,
398     plural(cnt), destmbox, raccount->key);
399     action_rcopy(mesgs, raccount, destmbox, args);
400     break;
401     case FILTER_ACTION_RMOVE:
402     info("%d messages%s moved to mailbox \"%s\" at account %s\n", cnt,
403     plural(cnt), destmbox, raccount->key);
404     action_rmove(mesgs, raccount, destmbox, args);
405 lefcha 1.1 break;
406     case FILTER_ACTION_LIST:
407 lefcha 1.8 info("%d message%s listed.\n", cnt, plural(cnt));
408 lefcha 1.1 action_list(mesgs, args);
409     break;
410     }
411 lefcha 1.5
412     if (!*args)
413 lefcha 1.4 log_info(LOG_WRITE, NULL);
414 lefcha 1.5
415 lefcha 1.1 return 0;
416     }
417    
418    
419     /*
420     * Delete messages and optionally list some of their headers.
421     */
422     int action_delete(char *mesgs, char *args)
423     {
424 lefcha 1.23 char *tok, *mcp, *m, *cm;
425    
426     action_list(mesgs, args);
427 lefcha 1.25
428 lefcha 1.23 cm = convert_messages(mesgs);
429 lefcha 1.1
430 lefcha 1.23 m = mcp = xstrdup(cm);
431 lefcha 1.25
432 lefcha 1.21 while ((tok = strsep(&m, " ")))
433     server_response(&sockpri, imap_store(&sockpri, tok, "\\Deleted"));
434 lefcha 1.5
435 lefcha 1.25 if (options & OPTION_EXPUNGE)
436     server_response(&sockpri, imap_expunge(&sockpri));
437    
438 lefcha 1.23 xfree(cm);
439 lefcha 1.19 xfree(mcp);
440 lefcha 1.10
441 lefcha 1.1 return 0;
442     }
443    
444    
445     /*
446     * Copy messages to specified mailbox.
447     */
448 lefcha 1.3 int action_copy(char *mesgs, char *destmbox, char *args)
449 lefcha 1.1 {
450 lefcha 1.20 int r = 0;
451 lefcha 1.23 char *tok = NULL, *mcp, *m, *cm;
452 lefcha 1.25
453 lefcha 1.21 action_list(mesgs, args);
454 lefcha 1.25
455 lefcha 1.23 cm = convert_messages(mesgs);
456    
457     m = mcp = xstrdup(cm);
458    
459 lefcha 1.22 while ((tok = strsep(&m, " "))) {
460     if ((r = copy_response(&sockpri,
461     imap_copy(&sockpri, tok, destmbox))) ==
462     RESPONSE_TRYCREATE)
463     if (!server_response(&sockpri, imap_create(&sockpri, destmbox))) {
464     server_response(&sockpri, imap_subscribe(&sockpri, destmbox));
465     r = copy_response(&sockpri, imap_copy(&sockpri, tok, destmbox));
466     }
467     }
468 lefcha 1.23
469     xfree(cm);
470 lefcha 1.19 xfree(mcp);
471 lefcha 1.8
472 lefcha 1.20 return r;
473 lefcha 1.1 }
474    
475    
476     /*
477     * Move messages to specified mailbox.
478     */
479 lefcha 1.3 int action_move(char *mesgs, char *destmbox, char *args)
480 lefcha 1.1 {
481 lefcha 1.20 if (!action_copy(mesgs, destmbox, args))
482     action_delete(mesgs, "\0");
483 lefcha 1.16
484 lefcha 1.1 return 0;
485     }
486    
487    
488     /*
489 lefcha 1.21 * Copy messages to the specified mailbox of another mail server.
490     */
491 lefcha 1.25 int action_rcopy(char *mesgs, account_t * destacc, char *destmbox, char *args)
492 lefcha 1.21 {
493     int r;
494     char *tok, *m, *mcp, *ndm;
495     unsigned int n, t = 0;
496     char buf[RESPONSE_BUF];
497 lefcha 1.25
498 lefcha 1.21 if (init_connection(&sockaux, destacc->server, destacc->port,
499     destacc->ssl))
500     return ERROR_NETWORK;
501 lefcha 1.25
502 lefcha 1.21 r = greeting_response(&sockaux);
503 lefcha 1.25
504 lefcha 1.21 if (r == RESPONSE_BYE || check_capabilities(&sockaux))
505     return ERROR_NETWORK;
506 lefcha 1.25
507 lefcha 1.21 #ifdef DEBUG
508     test(&sockaux);
509     #endif
510 lefcha 1.25
511 lefcha 1.21 if (r != RESPONSE_PREAUTH) {
512     if (destacc->passwdattr == PASSWORD_NONE) {
513     printf("Enter password for %s@%s: ", destacc->username,
514     destacc->server);
515     get_password(destacc->password, PASSWORD_LEN);
516     destacc->passwdattr = PASSWORD_PLAIN;
517     }
518     if (login(&sockaux, destacc->username,
519     destacc->password) == RESPONSE_NO) {
520     error("imapfilter: username %s or password rejected at %s\n",
521     destacc->username, destacc->server);
522     return ERROR_NETWORK;
523     }
524     }
525     check_namespace(&sockaux, &nspaux);
526    
527     /* apply_namespace() returns a pointer to a static buffer. */
528     ndm = apply_namespace(destmbox, nspaux.prefix, nspaux.delim);
529 lefcha 1.25
530 lefcha 1.21 r = check_mailbox(&sockaux, ndm, &nspaux);
531    
532     if (r == RESPONSE_OK)
533     close_mailbox(&sockaux);
534     else if (r == RESPONSE_NO) {
535     server_response(&sockaux, imap_create(&sockaux, ndm));
536     server_response(&sockaux, imap_subscribe(&sockaux, ndm));
537     }
538     m = mcp = xstrdup(mesgs);
539    
540     while ((tok = strsep(&m, " "))) {
541     fetchsize_response(&sockpri, &n,
542 lefcha 1.25 imap_fetch(&sockpri, tok, "RFC822.SIZE"));
543    
544 lefcha 1.21 t = imap_append(&sockaux, ndm, n);
545    
546 lefcha 1.24 fetch_response(&sockpri, 1, NULL, 0);
547 lefcha 1.21 do {
548     r = fetch_response(&sockpri, 0, buf, imap_fetch(&sockpri, tok,
549 lefcha 1.25 "RFC822.HEADER"));
550    
551 lefcha 1.21 socket_write(&sockaux, buf);
552     } while (r == RESPONSE_NONE);
553 lefcha 1.25
554 lefcha 1.21 socket_write(&sockaux, "\r\n");
555    
556     fetch_response(&sockpri, 1, NULL, 0);
557     do {
558     r = fetch_response(&sockpri, 0, buf, imap_fetch(&sockpri, tok,
559     "BODY[TEXT]"));
560 lefcha 1.25
561 lefcha 1.21 socket_write(&sockaux, buf);
562     } while (r == RESPONSE_NONE);
563 lefcha 1.25
564 lefcha 1.24 socket_write(&sockaux, "\r\n\r\n");
565 lefcha 1.25
566 lefcha 1.24 append_response(&sockaux, t);
567 lefcha 1.21 }
568    
569     logout(&sockaux);
570 lefcha 1.25
571 lefcha 1.21 action_list(mesgs, args);
572 lefcha 1.25
573 lefcha 1.21 xfree(mcp);
574    
575     return 0;
576     }
577    
578    
579     /*
580     * Move messages to the specified mailbox of another mail server.
581     */
582 lefcha 1.25 int action_rmove(char *mesgs, account_t * destacc, char *destmbox, char *args)
583 lefcha 1.21 {
584     if (!action_rcopy(mesgs, destacc, destmbox, args))
585     action_delete(mesgs, "\0");
586 lefcha 1.25
587 lefcha 1.21 return 0;
588     }
589    
590    
591     /*
592 lefcha 1.1 * List user selected headers of messages.
593     */
594     int action_list(char *mesgs, char *args)
595     {
596 lefcha 1.21 int r;
597     char *tok, *mcp, *m;
598     char s[ARGS_LEN + 27];
599     char hdrs[RESPONSE_BUF];
600 lefcha 1.5
601 lefcha 1.2 if (!*args)
602     return 0;
603 lefcha 1.1
604 lefcha 1.5 m = mcp = xstrdup(mesgs);
605 lefcha 1.6
606 lefcha 1.21 snprintf(s, ARGS_LEN + 27 - 1, "BODY.PEEK[HEADER.FIELDS (%s)]", args);
607 lefcha 1.1
608 lefcha 1.21 while ((tok = strsep(&m, " "))) {
609     /* Reset internal fetch counter. */
610     fetch_response(&sockpri, 1, NULL, 0);
611    
612     do {
613     r = fetch_response(&sockpri, 0, hdrs, imap_fetch(&sockpri, tok, s));
614 lefcha 1.25
615 lefcha 1.21 if (*hdrs) {
616     if (options & OPTION_HEADERS)
617     info("%s\n", hdrs);
618     log_info(LOG_WRITE, hdrs);
619     } else {
620     log_info(LOG_WRITE, NULL);
621     }
622     } while (r == RESPONSE_NONE);
623     }
624 lefcha 1.1
625 lefcha 1.19 xfree(mcp);
626 lefcha 1.1
627     return 0;
628     }
629    
630    
631     /*
632 lefcha 1.21 * Count how many messages matched the filter.
633     */
634     unsigned int count_messages(char *mesgs)
635     {
636     unsigned int cnt = 0;
637     char *c = mesgs;
638 lefcha 1.25
639 lefcha 1.21 while ((c = strchr(c, ' '))) {
640     cnt++;
641     c++;
642     }
643    
644     return ++cnt;
645     }
646    
647    
648     /*
649 lefcha 1.11 * Convert messages with contiguous sequence number to the corresponding
650 lefcha 1.5 * number range, eg. 1 2 3 5 7 8 --> 1:3 5 7:8
651 lefcha 1.1 */
652 lefcha 1.23 char *convert_messages(char *mesgs)
653 lefcha 1.1 {
654 lefcha 1.21 unsigned int maxlen;
655 lefcha 1.1 unsigned int start, end, tmp;
656 lefcha 1.23 char *c, *cp, *tail;
657 lefcha 1.25
658 lefcha 1.21 start = end = tmp = 0;
659 lefcha 1.13 maxlen = strlen(mesgs) + 1;
660     tail = NULL;
661 lefcha 1.1
662 lefcha 1.23 c = cp = xstrdup(mesgs);
663 lefcha 1.1
664 lefcha 1.25 start = (unsigned int)strtoul(mesgs, &tail, 10);
665 lefcha 1.1 end = start;
666    
667     do {
668     if (tail) {
669 lefcha 1.25 tmp = (unsigned int)strtoul(tail, &tail, 10);
670 lefcha 1.21 if (!tmp)
671 lefcha 1.15 tail = NULL;
672 lefcha 1.1 }
673     if (tmp == end + 1)
674     end++;
675     else {
676 lefcha 1.13 if (start == end) {
677 lefcha 1.23 xstrncpy(c, ultostr(start, 10), maxlen);
678     c += strlen(c);
679 lefcha 1.13 } else {
680 lefcha 1.23 xstrncpy(c, ultostr(start, 10), maxlen - 1);
681     c += strlen(c);
682     *c = ':';
683     *++c = 0;
684     xstrncpy(c, ultostr(end, 10), maxlen);
685     c += strlen(c);
686 lefcha 1.13 }
687 lefcha 1.1
688 lefcha 1.23 if (tail && c - cp < maxlen) {
689     *c = ' ';
690     *++c = 0;
691 lefcha 1.13 }
692 lefcha 1.1 start = end = tmp;
693     }
694     } while (tmp);
695    
696 lefcha 1.23 return cp;
697 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26