]> andersk Git - moira.git/blob - clients/chpobox/chpobox.c
Enable qsort() on all platforms, not jusr WIN32.
[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 RCSID("$Header$");
40
41 int get_pobox(int argc, char **argv, void *callarg);
42 void usage(void);
43
44 char *whoami;
45
46 static int match;
47
48 int main(int argc, char *argv[])
49 {
50   char *mrarg[3];
51   char *address, *uname;
52   int c, setflag, splitflag, prevflag, status;
53
54   setflag = splitflag = prevflag = 0;
55   address = uname = NULL;
56
57   if ((whoami = strrchr(argv[0], '/')) == NULL)
58     whoami = argv[0];
59   else
60     whoami++;
61
62   if (argc > 5)
63     usage();
64
65   while ((c = getopt(argc, argv, "s:S:pu:")) != -1)
66     switch (c)
67       {
68       case 's':
69         setflag++;
70         address = optarg;
71         break;
72
73       case 'S':
74         splitflag++;
75         address = optarg;
76         break;
77
78       case 'p':
79         prevflag++;
80         break;
81
82       case 'u':
83         uname = optarg;
84         break;
85
86       default:
87         usage();
88         break;
89       }
90   if (argc == 2 && optind == 1 && !uname)
91     uname = argv[optind++];
92
93   if (optind != argc || (prevflag + splitflag + setflag > 1))
94     usage();
95
96   if (!uname)
97     {
98       uname = mrcl_krb_user();
99       if (!uname)
100         exit(1);
101     }
102   mrarg[0] = uname;
103
104   if (mrcl_connect(NULL, "chpobox", 2, 1) != MRCL_SUCCESS)
105     exit(1);
106
107   if (setflag || splitflag)
108     {
109       char *addr;
110       status = mrcl_validate_pobox_smtp(uname, address, &addr);
111       if (mrcl_get_message())
112         mrcl_com_err(whoami);
113       if (status != MRCL_SUCCESS)
114         {
115           printf("\n");
116           goto show;
117         }
118       mrarg[1] = setflag ? "SMTP" : "SPLIT";
119       mrarg[2] = addr;
120       status = mr_query("set_pobox", 3, mrarg, NULL, NULL);
121       free(addr);
122       if (status)
123         {
124           com_err(whoami, status,
125                   "while setting pobox for %s to type %s, box %s",
126                   mrarg[0], mrarg[1], mrarg[2]);
127         }
128     }
129   else if (prevflag)
130     {
131       status = mr_query("set_pobox_pop", 1, mrarg, NULL, NULL);
132       if (status == MR_MACHINE)
133         {
134           fprintf(stderr,
135                   "Moira has no record of a previous POP box for %s\n", uname);
136         }
137       else if (status != 0)
138         com_err(whoami, status, "while setting pobox");
139     }
140
141   /*
142    * get current box
143    */
144 show:
145   status = mr_query("get_pobox", 1, mrarg, get_pobox, NULL);
146   if (status == MR_NO_MATCH)
147     printf("User %s has no pobox.\n", uname);
148   else if (status != 0)
149     mrcl_com_err(whoami);
150   mr_disconnect();
151   exit(0);
152 }
153
154
155 /*
156  * get_pobox gets all your poboxes and displays them.
157  */
158
159 int get_pobox(int argc, char **argv, void *callarg)
160 {
161   if (!strcmp(argv[1], "SMTP"))
162     {
163       printf("User %s, Type %s, Box: %s\n",
164              argv[0], argv[1], argv[2]);
165     }
166   else if (argc == 7)
167     {
168       printf("User %s, Type %s, Box: %s (%s)\n",
169            argv[0], argv[1], argv[2], argv[3]);
170     }
171   else
172     {
173       printf("User %s, Type %s, Box: %s@%s\n",
174              argv[0], argv[1], argv[0], argv[2]);
175     }
176   printf("  Modified by %s on %s with %s\n",
177          argv[argc - 2], argv[argc - 3], argv[argc - 1]);
178   return 0;
179 }
180
181 void usage(void)
182 {
183   fprintf(stderr, "Usage: %s [-s|-S address] [-p] [-u user]\n", whoami);
184   exit(1);
185 }
This page took 0.576962 seconds and 5 git commands to generate.