1 /* ctl_userseen.c - tool to remove seen records for owners.
2  *
3  * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  */
42 
43 #include <config.h>
44 
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <fcntl.h>
53 #include <sys/stat.h>
54 
55 #include "global.h"
56 #include "libcyr_cfg.h"
57 #include "mailbox.h"
58 #include "mboxlist.h"
59 #include "seen.h"
60 #include "util.h"
61 #include "xmalloc.h"
62 
63 /* config.c stuff */
64 static int do_remove = 0;
65 
usage(void)66 static void usage(void)
67 {
68     fprintf(stderr, "cyr_userseen [-C <altconfig>] -d\n");
69     exit(-1);
70 }
71 
72 /* Callback for use by delete_seen */
deluserseen(const mbentry_t * mbentry,void * rock)73 static int deluserseen(const mbentry_t *mbentry, void *rock __attribute__((unused)))
74 {
75     struct mailbox *mailbox = NULL;
76     int r = 0;
77 
78     r = mailbox_open_irl(mbentry->name, &mailbox);
79     if (r) goto done;
80 
81     char *userid = mboxname_to_userid(mbentry->name);
82     if (userid) {
83         printf("removing seen for %s on %s\n", userid, mailbox->name);
84         if (do_remove) seen_delete_mailbox(userid, mailbox);
85         free(userid);
86     }
87 
88     mailbox_close(&mailbox);
89 
90 done:
91     return r;
92 }
93 
main(int argc,char * argv[])94 int main(int argc, char *argv[])
95 {
96     extern char *optarg;
97     int opt;
98     char *alt_config = NULL;
99 
100     while ((opt = getopt(argc, argv, "C:d")) != EOF) {
101         switch (opt) {
102         case 'C': /* alt config file */
103             alt_config = optarg;
104             break;
105 
106         case 'd':
107             do_remove = 1;
108             break;
109 
110         default:
111             usage();
112             break;
113         }
114     }
115 
116     cyrus_init(alt_config, "cyr_userseen", 0, 0);
117 
118     /* build a list of mailboxes - we're using internal names here */
119     mboxlist_allmbox("", deluserseen, NULL, /*flags*/0);
120 
121     cyrus_done();
122 
123     return 0;
124 }
125