]> andersk Git - moira.git/blob - clients/chpobox/chpobox.c
Command line printer manipulation client, and build goo.
[moira.git] / clients / chpobox / chpobox.c
1 /* $Id$
2  *
3  * Talk to the Moira database to change a person's home mail machine. This may
4  * be an Athena machine, or a completely arbitrary address.
5  *
6  * chpobox with no modifiers reports the current mailbox.
7  *
8  * chpobox -s address means set the mailbox to this address.
9  *
10  * chpobox -p restores the pobox to a previous POP/IMAP box, if there was one.
11  *
12  * chpobox -S address means split mail between POP/IMAP and SMTP
13  *
14  * chpobox -u [user] is needed if you are logged in as one user, but
15  * are trying to change the email address of another.  You must have
16  * Kerberos tickets as the person whose address you are trying to
17  * change, or the attempt will fail.
18  *
19  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
20  * For copying and distribution information, please see the file
21  * <mit-copyright.h>.
22  */
23
24 #include <mit-copyright.h>
25 #include <moira.h>
26 #include <moira_site.h>
27 #include <mrclient.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef HAVE_GETOPT_H
36 #include <getopt.h>
37 #endif
38
39 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
40
41 RCSID("$Header$");
42
43 int get_pobox(int argc, char **argv, void *callarg);
44 void usage(void);
45
46 char *whoami;
47
48 static int match;
49
50 int main(int argc, char *argv[])
51 {
52   char *mrarg[3];
53   char *address, *uname;
54   char **arg = argv;
55   char *server = NULL;
56   int c, setflag, splitflag, prevflag, status;
57
58   setflag = splitflag = prevflag = 0;
59   address = uname = NULL;
60
61   if ((whoami = strrchr(argv[0], '/')) == NULL)
62     whoami = argv[0];
63   else
64     whoami++;
65
66   if (argc > 5)
67     usage();
68
69   while (++arg - argv < argc)
70     {
71       if (**arg == '-')
72         {
73           if (argis("s", "set")) {
74             if (arg - argv < argc - 1) {
75               arg++;
76               setflag++;
77               address = *arg;
78             } else
79               usage();
80           }
81           else if (argis("S", "split")) {
82             if (arg - argv < argc - 1) {
83               arg++;
84               splitflag++;
85               address = *arg;
86             } else
87               usage();
88           }
89           else if (argis("p", "previous"))
90             prevflag++;
91           else if (argis("u", "username")) {
92             if (arg - argv < argc - 1) {
93               arg++;
94               uname = *arg;
95             } else
96               usage();
97           }
98           else if (argis("db", "database")) {
99             if (arg - argv < argc - 1) {
100               arg++;
101               server = *arg;
102             } else
103               usage();
104           }
105         }
106       else if (uname == NULL)
107         uname = *arg;
108       else
109         usage();
110     }
111
112   if (prevflag + splitflag + setflag > 1)
113     usage();
114
115   if (!uname)
116     {
117       uname = mrcl_krb_user();
118       if (!uname)
119         exit(1);
120     }
121   mrarg[0] = uname;
122
123   if (mrcl_connect(server, "chpobox", 2, 1) != MRCL_SUCCESS)
124     exit(1);
125
126   if (setflag || splitflag)
127     {
128       char *addr;
129       status = mrcl_validate_pobox_smtp(uname, address, &addr);
130       if (mrcl_get_message())
131         mrcl_com_err(whoami);
132       if (status != MRCL_SUCCESS)
133         {
134           printf("\n");
135           goto show;
136         }
137       mrarg[1] = setflag ? "SMTP" : "SPLIT";
138       mrarg[2] = addr;
139       status = mr_query("set_pobox", 3, mrarg, NULL, NULL);
140       free(addr);
141       if (status)
142         {
143           com_err(whoami, status,
144                   "while setting pobox for %s to type %s, box %s",
145                   mrarg[0], mrarg[1], mrarg[2]);
146         }
147     }
148   else if (prevflag)
149     {
150       status = mr_query("set_pobox_pop", 1, mrarg, NULL, NULL);
151       if (status == MR_MACHINE)
152         {
153           fprintf(stderr,
154                   "Moira has no record of a previous POP box for %s\n", uname);
155         }
156       else if (status != 0)
157         com_err(whoami, status, "while setting pobox");
158     }
159
160   /*
161    * get current box
162    */
163 show:
164   status = mr_query("get_pobox", 1, mrarg, get_pobox, NULL);
165   if (status == MR_NO_MATCH)
166     printf("User %s has no pobox.\n", uname);
167   else if (status != 0)
168     mrcl_com_err(whoami);
169   mr_disconnect();
170   exit(0);
171 }
172
173
174 /*
175  * get_pobox gets all your poboxes and displays them.
176  */
177
178 int get_pobox(int argc, char **argv, void *callarg)
179 {
180   if (!strcmp(argv[1], "SMTP"))
181     {
182       printf("User %s, Type %s, Box: %s\n",
183              argv[0], argv[1], argv[2]);
184     }
185   else if (argc == 7)
186     {
187       printf("User %s, Type %s, Box: %s (%s)\n",
188            argv[0], argv[1], argv[2], argv[3]);
189     }
190   else
191     {
192       printf("User %s, Type %s, Box: %s@%s\n",
193              argv[0], argv[1], argv[0], argv[2]);
194     }
195   printf("  Modified by %s on %s with %s\n",
196          argv[argc - 2], argv[argc - 3], argv[argc - 1]);
197   return 0;
198 }
199
200 void usage(void)
201 {
202   fprintf(stderr, "Usage: %s [-s|-S address] [-p] [-u user] [-db database]\n", whoami);
203   exit(1);
204 }
This page took 0.295961 seconds and 5 git commands to generate.