1 /* mbpath.c -- help the sysadmin to find the path matching the mailbox
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 <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <fcntl.h>
52 #include <sys/stat.h>
53 #include <sys/param.h>
54 
55 #include "util.h"
56 #include "global.h"
57 #include "exitcodes.h"
58 #include "mailbox.h"
59 #include "xmalloc.h"
60 #include "mboxlist.h"
61 
62 /* generated headers are not necessarily in current directory */
63 #include "imap/imap_err.h"
64 
65 extern int optind;
66 extern char *optarg;
67 
68 /* current namespace */
69 static struct namespace mbpath_namespace;
70 
71 static int
usage(void)72 usage(void) {
73   fprintf(stderr,"usage: mbpath [-C <alt_config>] [-q] [-s] [-m] <mailbox name>...\n");
74   fprintf(stderr,"\t-q\tquietly drop any error messages\n");
75   fprintf(stderr,"\t-s\tstop on error\n");
76   fprintf(stderr,"\t-m\toutput the path to the metadata files (if different from the message files)\n");
77   exit(-1);
78 }
79 
80 int
main(int argc,char ** argv)81 main(int argc, char **argv)
82 {
83   mbentry_t *mbentry = NULL;
84   int rc, i, quiet = 0, stop_on_error=0, metadata=0;
85   int opt;              /* getopt() returns an int */
86   char *alt_config = NULL;
87 
88   while ((opt = getopt(argc, argv, "C:qsm")) != EOF) {
89     switch(opt) {
90     case 'C': /* alt config file */
91       alt_config = optarg;
92       break;
93     case 'q':
94       quiet = 1;
95       break;
96     case 's':
97       stop_on_error = 1;
98       break;
99     case 'm':
100         metadata = 1;
101         break;
102 
103     default:
104       usage();
105     }
106   }
107 
108   cyrus_init(alt_config, "mbpath", 0, 0);
109 
110   if ((rc = mboxname_init_namespace(&mbpath_namespace, 1)) != 0) {
111     fatal(error_message(rc), -1);
112   }
113 
114   mboxlist_init(0);
115   mboxlist_open(NULL);
116 
117   for (i = optind; i < argc; i++) {
118     /* Translate mailboxname */
119     char *intname = mboxname_from_external(argv[i], &mbpath_namespace, NULL);
120     if ((rc = mboxlist_lookup(intname, &mbentry, NULL)) == 0) {
121       if (mbentry->mbtype & MBTYPE_REMOTE) {
122         printf("%s!%s\n", mbentry->server, mbentry->partition);
123       } else if (metadata) {
124         const char *path = mbentry_metapath(mbentry, 0, 0);
125         printf("%s\n", path);
126       }
127       else {
128         const char *path = mbentry_datapath(mbentry, 0);
129         printf("%s\n", path);
130       }
131       mboxlist_entry_free(&mbentry);
132     } else {
133       if (!quiet && (rc == IMAP_MAILBOX_NONEXISTENT)) {
134         fprintf(stderr, "Invalid mailbox name: %s\n", argv[i]);
135       }
136       if (stop_on_error) {
137         if (quiet) {
138           fatal("", -1);
139         } else {
140           fatal("Error in processing mailbox. Stopping\n", -1);
141         }
142       }
143     }
144     free(intname);
145   }
146 
147   mboxlist_close();
148   mboxlist_done();
149 
150   cyrus_done();
151 
152   return 0;
153 }
154 
155 /* $Header: /mnt/data/cyrus/cvsroot/src/cyrus/imap/mbpath.c,v 1.23 2010/01/06 17:01:37 murch Exp $ */
156 
157