]> andersk Git - moira.git/blob - clients/moira/pobox.c
fix a bug in ChangeUserPOBox caused by the fact that get_pobox now
[moira.git] / clients / moira / pobox.c
1 /* $Id$
2  *
3  *      This is the file pobox.c for the Moira Client, which allows users
4  *      to quickly and easily maintain most parts of the Moira database.
5  *      It Contains: Functions for handling the poboxes.
6  *
7  *      Created:        7/10/88
8  *      By:             Chris D. Peterson
9  *
10  * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11  * For copying and distribution information, please see the file
12  * <mit-copyright.h>.
13  */
14
15 #include <mit-copyright.h>
16 #include <moira.h>
17 #include <moira_site.h>
18 #include <mrclient.h>
19 #include "defs.h"
20 #include "f_defs.h"
21 #include "globals.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 RCSID("$Header$");
28
29 #define FOREIGN_BOX ("SMTP")
30 #define LOCAL_BOX ("POP")
31
32 /*      Function Name: PrintPOBox
33  *      Description: Yet another specialized print function.
34  *      Arguments: info - all info about this PO_box.
35  *      Returns: MR_CONT
36  */
37
38 static void PrintPOBox(char **info)
39 {
40   char buf[BUFSIZ];
41
42   sprintf(buf, "User: %-10s Box: %-35s Type: %s", info[PO_NAME],
43           info[PO_BOX], info[PO_TYPE]);
44   Put_message(buf);
45   if (info[6])
46     {
47       sprintf(buf, "Address: %s", info[PO_ADDR]);
48       Put_message(buf);
49       sprintf(buf, MOD_FORMAT, info[5], info[4], info[6]);
50       Put_message(buf);
51     }
52   else
53     {
54       sprintf(buf, MOD_FORMAT, info[4], info[3], info[5]);
55       Put_message(buf);
56     }
57 }
58
59 /*      Function Name: RealPrintPOMachines
60  *      Description: Actually does the printing for PrintPOMachines.
61  *      Arguments: info, - name of machines in info[1].
62  *      Returns: none.
63  */
64
65 static void RealPrintPOMachines(char **info)
66 {
67   Print(1, info + 1, NULL);
68 }
69
70 /*      Function Name: PrintPOMachines
71  *      Description: Prints all current post offices.
72  *      Arguments: none.
73  *      Returns: SUB_ERROR if the machines could not be printed.
74  */
75
76 static int PrintPOMachines(void)
77 {
78   int status;
79   static char *args[] = {"pop", NULL};
80   struct mqelem *top = NULL;
81
82   if ((status = do_mr_query("get_server_locations", CountArgs(args), args,
83                             StoreInfo, &top)))
84     {
85       com_err(program_name, status, " in get_server_locations.");
86       return SUB_ERROR;
87     }
88
89   top = QueueTop(top);
90   Loop(top, RealPrintPOMachines);
91   FreeQueue(top);
92   return SUB_NORMAL;
93 }
94
95 /*      Function Name: GetUserPOBox
96  *      Description: prints the users POBox information.
97  *      Arguments: argc, argv - name of the user in argv[1].
98  *      Returns: DM_NORMAL.
99  */
100
101 int GetUserPOBox(int argc, char **argv)
102 {
103   int status;
104   struct mqelem *top = NULL;
105   char buf[BUFSIZ];
106
107   if (!ValidName(argv[1]))
108     return DM_NORMAL;
109
110   switch ((status = do_mr_query("get_pobox", 1, argv + 1, StoreInfo, &top)))
111     {
112     case MR_NO_MATCH:
113       Put_message("This user has no P.O. Box.");
114       break;
115     case MR_SUCCESS:
116       sprintf(buf, "Current pobox for user %s: \n", argv[1]);
117       Put_message("");
118       top = QueueTop(top);
119       Loop(top, PrintPOBox);    /* should only return 1 box. */
120       FreeQueue(top);
121       break;
122     default:
123       com_err(program_name, status, " in get_pobox.");
124     }
125   return DM_NORMAL;
126 }
127
128 /*      Function Name: GetNewLocalPOBox
129  *      Description: get the machine for a new local pop Box for the user.
130  *      Arguments: local_user - name of the local user.
131  *      Returns: machine - name of the machine for then new pop box, or NULL.
132  */
133
134 static char *GetNewLocalPOBox(char *local_user)
135 {
136   char temp_buf[BUFSIZ];
137
138   sprintf(temp_buf, "%s %s", "Pick one of the following",
139           "machines for this user's Post Office.");
140   Put_message(temp_buf);
141   Put_message("");
142   if (PrintPOMachines() == SUB_NORMAL)
143     {
144       Put_message("");
145       if (!Prompt_input("Which Machine? ", temp_buf, BUFSIZ))
146         return (char *) SUB_ERROR;
147       return canonicalize_hostname(strdup(temp_buf));
148     }
149   Put_message("Could not get machines to choose from, quitting.");
150   return (char *) SUB_ERROR;
151 }
152
153 /*      Function Name: SetUserPOBox
154  *      Description: Addes or Chnages the P.O. Box for a user.
155  *      Arguments: argc, argv - the login name of the user in argv[1].
156  *      Returns: DM_NORMAL.
157  */
158
159 int SetUserPOBox(int argc, char **argv)
160 {
161   int status;
162   char *type, temp_buf[BUFSIZ], *local_user, *args[10], *box;
163   char *temp_box;
164   struct mqelem *top = NULL;
165   local_user = argv[1];
166
167   if (!ValidName(local_user))
168     return DM_NORMAL;
169
170   /* Print the current PO Box info */
171   switch ((status = do_mr_query("get_pobox", 1, argv + 1, StoreInfo, &top)))
172     {
173     case MR_SUCCESS:
174       sprintf(temp_buf, "Current pobox for user %s: \n", local_user);
175       Put_message("");
176       top = QueueTop(top);
177       Loop(top, PrintPOBox);    /* should only return 1 box. */
178       FreeQueue(top);
179       break;
180     case MR_NO_MATCH:
181       Put_message("This user has no P.O. Box.");
182       break;
183     default:
184       com_err(program_name, status, " in get_pobox.");
185       return DM_NORMAL;
186     }
187
188   sprintf(temp_buf, "Assign %s a local PO Box (y/n)", local_user);
189   switch (YesNoQuestion(temp_buf, TRUE))
190     {
191     case TRUE:
192       type = LOCAL_BOX;
193       switch (YesNoQuestion("Use Previous Local Box (y/n)", TRUE))
194         {
195         case TRUE:
196           switch ((status = do_mr_query("set_pobox_pop", 1,
197                                         &local_user, NULL, NULL)))
198             {
199             case MR_SUCCESS:
200               return DM_NORMAL;
201             case MR_MACHINE:
202               sprintf(temp_buf, "%s did not have a previous local PO Box.",
203                       local_user);
204               Put_message(temp_buf);
205               if ((box = GetNewLocalPOBox(local_user)) == (char *) SUB_ERROR)
206                 return DM_NORMAL;
207               break;
208             default:
209               com_err(program_name, status, " in set_pobox_pop.");
210               return DM_NORMAL;
211             }
212           break;
213         case FALSE:
214           if ((box = GetNewLocalPOBox(local_user)) == (char *) SUB_ERROR)
215             return DM_NORMAL;
216           break;
217         default:
218           return DM_NORMAL;
219         }
220       break;
221     case FALSE:
222       type = FOREIGN_BOX;
223       sprintf(temp_buf, "Set up a foreign PO Box for %s (y/n)", local_user);
224       switch (YesNoQuestion(temp_buf, TRUE))
225         {
226         case TRUE:
227           if (!Prompt_input("Foreign PO Box for this user? ", temp_buf, BUFSIZ))
228             return DM_NORMAL;
229           if (mrcl_validate_pobox_smtp(local_user, temp_buf, &box) !=
230               MRCL_SUCCESS)
231             return DM_NORMAL;
232           break;
233         case FALSE:
234         default:
235           return DM_NORMAL;     /* ^C hit. */
236         }
237       break;
238     default:                    /* ^C hit. */
239       Put_message("Aborted.");
240       return DM_NORMAL;
241     }
242
243   args[PO_NAME] = local_user;
244   args[PO_TYPE] = type;
245   args[PO_BOX] = box;
246   args[3] = NULL;
247   if ((status = do_mr_query("set_pobox", CountArgs(args), args,
248                             NULL, NULL)))
249     com_err(program_name, status, " in ChangeUserPOBox");
250   else
251     Put_message("PO Box assigned.");
252   free(box);
253
254   return DM_NORMAL;
255 }
256
257 /*      Function Name: RemoveUserPOBox
258  *      Description: Removes this users POBox.
259  *      Arguments: argc, argv - name of user in argv[1].
260  *      Returns: DM_NORMAL.
261  */
262
263 int RemoveUserPOBox(int argc, char **argv)
264 {
265   int status;
266   char temp_buf[BUFSIZ];
267
268   if (!ValidName(argv[1]))
269     return DM_NORMAL;
270
271   sprintf(temp_buf, "Are you sure that you want to remove %s's PO Box (y/n)",
272           argv[1]);
273
274   if (Confirm(temp_buf))
275     {
276       if ((status = do_mr_query("delete_pobox", 1, argv + 1,
277                                 NULL, NULL)))
278         com_err(program_name, status, " in delete_pobox.");
279       else
280         Put_message("PO Box removed.");
281     }
282   return DM_NORMAL;
283 }
This page took 0.059125 seconds and 5 git commands to generate.