Nearly all <FORM> blocks now contain a hidden input
[citadel.git] / webcit / useredit.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup AdminTasks Administrative screen to add/change/delete user accounts
6  * \ingroup CitadelConfig
7  *
8  */
9 /*@{*/
10
11 #include "webcit.h"
12 #include "webserver.h"
13
14
15 /**
16  * \brief show a list of available users to edit them
17  * \param message the header message???
18  * \param preselect which user should be selected in the browser
19  */
20 void select_user_to_edit(char *message, char *preselect)
21 {
22         char buf[SIZ];
23         char username[SIZ];
24
25         output_headers(1, 1, 2, 0, 0, 0);
26         wprintf("<div id=\"banner\">\n");
27         wprintf("<table class=\"useredit_banner\"><tr>"
28                 "<td>"
29                 "<span class=\"titlebar\">"
30                 "<img src=\"static/usermanag_48x.gif\">");
31         wprintf(_("Edit or delete users"));
32         wprintf("</span></td></tr></table>\n"
33                 "</div>\n<div id=\"content\">\n"
34         );
35
36         if (message != NULL) wprintf(message);
37
38         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
39
40         svprintf("BOXTITLE", WCS_STRING, _("Add users"));
41         do_template("beginbox");
42
43         wprintf(_("To create a new user account, enter the desired "
44                 "user name in the box below and click 'Create'."));
45         wprintf("<br /><br />");
46
47         wprintf("<center><form method=\"POST\" action=\"create_user\">\n");
48         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
49         wprintf(_("New user: "));
50         wprintf("<input type=\"text\" name=\"username\"><br />\n"
51                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
52                 "</form></center>\n", _("Create"));
53
54         do_template("endbox");
55
56         wprintf("</td><td>");
57
58         svprintf("BOXTITLE", WCS_STRING, _("Edit or Delete users"));
59         do_template("beginbox");
60
61         wprintf(_("To edit an existing user account, select the user "
62                 "name from the list and click 'Edit'."));
63         wprintf("<br /><br />");
64         
65         wprintf("<center>"
66                 "<form method=\"POST\" action=\"display_edituser\">\n");
67         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
68         wprintf("<select name=\"username\" size=10 style=\"width:100%%\">\n");
69         serv_puts("LIST");
70         serv_getln(buf, sizeof buf);
71         if (buf[0] == '1') {
72                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
73                         extract_token(username, buf, 0, '|', sizeof username);
74                         wprintf("<option");
75                         if (preselect != NULL)
76                            if (!strcasecmp(username, preselect))
77                               wprintf(" selected");
78                         wprintf(">");
79                         escputs(username);
80                         wprintf("\n");
81                 }
82         }
83         wprintf("</select><br />\n");
84
85         wprintf("<input type=\"submit\" name=\"edit_config_button\" value=\"%s\">", _("Edit configuration"));
86         wprintf("<input type=\"submit\" name=\"edit_abe_button\" value=\"%s\">", _("Edit address book entry"));
87         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
88                 "onClick=\"return confirm('%s');\">", _("Delete user"), _("Delete this user?"));
89         wprintf("</form></center>\n");
90         do_template("endbox");
91
92         wprintf("</td></tr></table>\n");
93
94         wDumpContent(1);
95 }
96
97
98
99 /**
100  * \brief Locate the message number of a user's vCard in the current room
101  * \param username the plaintext name of the user
102  * \param usernum the number of the user on the citadel server
103  * \return the message id of his vcard
104  */
105 long locate_user_vcard(char *username, long usernum) {
106         char buf[SIZ];
107         long vcard_msgnum = (-1L);
108         char content_type[SIZ];
109         char partnum[SIZ];
110         int already_tried_creating_one = 0;
111
112         struct stuff_t {
113                 struct stuff_t *next;
114                 long msgnum;
115         };
116
117         struct stuff_t *stuff = NULL;
118         struct stuff_t *ptr;
119
120 TRYAGAIN:
121         /** Search for the user's vCard */
122         serv_puts("MSGS ALL");
123         serv_getln(buf, sizeof buf);
124         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
125                 ptr = malloc(sizeof(struct stuff_t));
126                 ptr->msgnum = atol(buf);
127                 ptr->next = stuff;
128                 stuff = ptr;
129         }
130
131         /** Iterate through the message list looking for vCards */
132         while (stuff != NULL) {
133                 serv_printf("MSG0 %ld|2", stuff->msgnum);
134                 serv_getln(buf, sizeof buf);
135                 if (buf[0]=='1') {
136                         while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
137                                 if (!strncasecmp(buf, "part=", 5)) {
138                                         extract_token(partnum, &buf[5], 2, '|', sizeof partnum);
139                                         extract_token(content_type, &buf[5], 4, '|', sizeof content_type);
140                                         if (  (!strcasecmp(content_type, "text/x-vcard"))
141                                            || (!strcasecmp(content_type, "text/vcard")) ) {
142                                                 vcard_msgnum = stuff->msgnum;
143                                         }
144                                 }
145                         }
146                 }
147
148                 ptr = stuff->next;
149                 free(stuff);
150                 stuff = ptr;
151         }
152
153         /** If there's no vcard, create one */
154         if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
155                 already_tried_creating_one = 1;
156                 serv_puts("ENT0 1|||4");
157                 serv_getln(buf, sizeof buf);
158                 if (buf[0] == '4') {
159                         serv_puts("Content-type: text/x-vcard");
160                         serv_puts("");
161                         serv_puts("begin:vcard");
162                         serv_puts("end:vcard");
163                         serv_puts("000");
164                 }
165                 goto TRYAGAIN;
166         }
167
168         return(vcard_msgnum);
169 }
170
171
172 /**
173  * \brief Display the form for editing a user's address book entry
174  * \param username the name of the user
175  * \param usernum the citadel-uid of the user
176  */
177 void display_edit_address_book_entry(char *username, long usernum) {
178         char roomname[SIZ];
179         char buf[SIZ];
180         char error_message[SIZ];
181         long vcard_msgnum = (-1L);
182
183         /** Locate the user's config room, creating it if necessary */
184         sprintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
185         serv_printf("GOTO %s||1", roomname);
186         serv_getln(buf, sizeof buf);
187         if (buf[0] != '2') {
188                 serv_printf("CRE8 1|%s|5|||1|", roomname);
189                 serv_getln(buf, sizeof buf);
190                 serv_printf("GOTO %s||1", roomname);
191                 serv_getln(buf, sizeof buf);
192                 if (buf[0] != '2') {
193                         sprintf(error_message,
194                                 "<img src=\"static/error.gif\" align=center>"
195                                 "%s<br /><br />\n", &buf[4]);
196                         select_user_to_edit(error_message, username);
197                         return;
198                 }
199         }
200
201         vcard_msgnum = locate_user_vcard(username, usernum);
202
203         if (vcard_msgnum < 0) {
204                 sprintf(error_message,
205                         "<img src=\"static/error.gif\" align=center>%s<br /><br />\n",
206                         _("An error occurred while trying to create or edit this address book entry.")
207                 );
208                 select_user_to_edit(error_message, username);
209                 return;
210         }
211
212         do_edit_vcard(vcard_msgnum, "1", "select_user_to_edit");
213 }
214
215
216
217
218 /**
219  * \brief Edit a user.  
220  * If supplied_username is null, look in the "username"
221  * web variable for the name of the user to edit.
222  * 
223  * If "is_new" is set to nonzero, this screen will set the web variables
224  * to send the user to the vCard editor next.
225  * \param supplied_username user to look up or NULL if to search in the environment
226  * \param is_new should we create the user?
227  */
228 void display_edituser(char *supplied_username, int is_new) {
229         char buf[1024];
230         char error_message[1024];
231         time_t now;
232
233         char username[256];
234         char password[256];
235         unsigned int flags;
236         int timescalled;
237         int msgsposted;
238         int axlevel;
239         long usernum;
240         time_t lastcall;
241         int purgedays;
242         int i;
243
244         if (supplied_username != NULL) {
245                 safestrncpy(username, supplied_username, sizeof username);
246         }
247         else {
248                 safestrncpy(username, bstr("username"), sizeof username);
249         }
250
251         serv_printf("AGUP %s", username);
252         serv_getln(buf, sizeof buf);
253         if (buf[0] != '2') {
254                 sprintf(error_message,
255                         "<img src=\"static/error.gif\" align=center>"
256                         "%s<br /><br />\n", &buf[4]);
257                 select_user_to_edit(error_message, username);
258                 return;
259         }
260
261         extract_token(username, &buf[4], 0, '|', sizeof username);
262         extract_token(password, &buf[4], 1, '|', sizeof password);
263         flags = extract_int(&buf[4], 2);
264         timescalled = extract_int(&buf[4], 3);
265         msgsposted = extract_int(&buf[4], 4);
266         axlevel = extract_int(&buf[4], 5);
267         usernum = extract_long(&buf[4], 6);
268         lastcall = extract_long(&buf[4], 7);
269         purgedays = extract_long(&buf[4], 8);
270
271         if (strlen(bstr("edit_abe_button")) > 0) {
272                 display_edit_address_book_entry(username, usernum);
273                 return;
274         }
275
276         if (strlen(bstr("delete_button")) > 0) {
277                 delete_user(username);
278                 return;
279         }
280
281         output_headers(1, 1, 2, 0, 0, 0);
282         wprintf("<div id=\"banner\">\n");
283         wprintf("<table class=\"useredit_banner\"><tr><td>");
284         wprintf("<span class=\"titlebar\">");
285         wprintf(_("Edit user account: "));
286         escputs(username);
287         wprintf("</span></td></tr></table>\n");
288         wprintf("</div>\n<div id=\"content\">\n");
289
290         wprintf("<div class=\"fix_scrollbar_bug\">"
291                 "<table class=\"useredit_background\"><tr><td>\n");
292         wprintf("<form method=\"POST\" action=\"edituser\">\n"
293                 "<input type=\"hidden\" name=\"username\" value=\"");
294         escputs(username);
295         wprintf("\">\n");
296         wprintf("<input type=\"hidden\" name=\"is_new\" value=\"%d\">\n"
297                 "<input type=\"hidden\" name=\"usernum\" value=\"%ld\">\n",
298                 is_new, usernum);
299         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
300
301         wprintf("<input type=\"hidden\" name=\"flags\" value=\"%d\">\n", flags);
302
303         wprintf("<center><table>");
304
305         wprintf("<tr><td>");
306         wprintf(_("Password"));
307         wprintf("</td><td>"
308                 "<input type=\"password\" name=\"password\" value=\"");
309         escputs(password);
310         wprintf("\" maxlength=\"20\"></td></tr>\n");
311
312         wprintf("<tr><td>");
313         wprintf(_("Permission to send Internet mail"));
314         wprintf("</td><td>");
315         wprintf("<input type=\"checkbox\" name=\"inetmail\" value=\"yes\" ");
316         if (flags & US_INTERNET) {
317                 wprintf("checked ");
318         }
319         wprintf("></td></tr>\n");
320
321         wprintf("<tr><td>");
322         wprintf(_("Number of logins"));
323         wprintf("</td><td>"
324                 "<input type=\"text\" name=\"timescalled\" value=\"");
325         wprintf("%d", timescalled);
326         wprintf("\" maxlength=\"6\"></td></tr>\n");
327
328         wprintf("<tr><td>");
329         wprintf(_("Messages submitted"));
330         wprintf("</td><td>"
331                 "<input type=\"text\" name=\"msgsposted\" value=\"");
332         wprintf("%d", msgsposted);
333         wprintf("\" maxlength=\"6\"></td></tr>\n");
334
335         wprintf("<tr><td>");
336         wprintf(_("Access level"));
337         wprintf("</td><td>"
338                 "<select name=\"axlevel\">\n");
339         for (i=0; i<7; ++i) {
340                 wprintf("<option ");
341                 if (axlevel == i) {
342                         wprintf("selected ");
343                 }
344                 wprintf("value=\"%d\">%d - %s</option>\n",
345                         i, i, axdefs[i]);
346         }
347         wprintf("</select></td></tr>\n");
348
349         wprintf("<tr><td>");
350         wprintf(_("User ID number"));
351         wprintf("</td><td>"
352                 "<input type=\"text\" name=\"usernum\" value=\"");
353         wprintf("%ld", usernum);
354         wprintf("\" maxlength=\"7\"></td></tr>\n");
355
356         now = time(NULL);
357         wprintf("<tr><td>");
358         wprintf(_("Date and time of last login"));
359         wprintf("</td><td>"
360                 "<select name=\"lastcall\">\n");
361
362         wprintf("<option selected value=\"%ld\">", lastcall);
363         escputs(asctime(localtime(&lastcall)));
364         wprintf("</option>\n");
365
366         wprintf("<option value=\"%ld\">", now);
367         escputs(asctime(localtime(&now)));
368         wprintf("</option>\n");
369
370         wprintf("</select></td></tr>");
371
372         wprintf("<tr><td>");
373         wprintf(_("Auto-purge after this many days"));
374         wprintf("</td><td>"
375                 "<input type=\"text\" name=\"purgedays\" value=\"");
376         wprintf("%d", purgedays);
377         wprintf("\" maxlength=\"5\"></td></tr>\n");
378
379         wprintf("</table>\n");
380
381         wprintf("<input type=\"submit\" name=\"ok_button\" value=\"%s\">\n"
382                 "&nbsp;"
383                 "<input type=\"submit\" name=\"cancel\" value=\"%s\">\n"
384                 "<br /><br /></form>\n", _("Save changes"), _("Cancel"));
385
386         wprintf("</center>\n");
387         wprintf("</td></tr></table></div>\n");
388         wDumpContent(1);
389
390 }
391
392
393 /**
394  * \brief do the backend operation of the user edit on the server
395  */
396 void edituser(void) {
397         char message[SIZ];
398         char buf[SIZ];
399         int is_new = 0;
400         unsigned int flags = 0;
401
402         is_new = atoi(bstr("is_new"));
403
404         if (strlen(bstr("ok_button")) == 0) {
405                 safestrncpy(message, _("Changes were not saved."), sizeof message);
406         }
407         else {
408                 flags = atoi(bstr("flags"));
409                 if (!strcasecmp(bstr("inetmail"), "yes")) {
410                         flags |= US_INTERNET;
411                 }
412                 else {
413                         flags &= ~US_INTERNET ;
414                 }
415
416                 serv_printf("ASUP %s|%s|%d|%s|%s|%s|%s|%s|%s|",
417                         bstr("username"),
418                         bstr("password"),
419                         flags,
420                         bstr("timescalled"),
421                         bstr("msgsposted"),
422                         bstr("axlevel"),
423                         bstr("usernum"),
424                         bstr("lastcall"),
425                         bstr("purgedays")
426                 );
427                 serv_getln(buf, sizeof buf);
428                 if (buf[0] != '2') {
429                         sprintf(message,
430                                 "<img src=\"static/error.gif\" align=center>"
431                                 "%s<br /><br />\n", &buf[4]);
432                 }
433                 else {
434                         safestrncpy(message, "", sizeof message);
435                 }
436         }
437
438         /**
439          * If we are in the middle of creating a new user, move on to
440          * the vCard edit screen.
441          */
442         if (is_new) {
443                 display_edit_address_book_entry( bstr("username"), atol(bstr("usernum")) );
444         }
445         else {
446                 select_user_to_edit(message, bstr("username"));
447         }
448 }
449
450 /**
451  * \brief burge a user 
452  * \param username the name of the user to remove
453  */
454 void delete_user(char *username) {
455         char buf[SIZ];
456         char message[SIZ];
457
458         serv_printf("ASUP %s|0|0|0|0|0|", username);
459         serv_getln(buf, sizeof buf);
460         if (buf[0] != '2') {
461                 sprintf(message,
462                         "<img src=\"static/error.gif\" align=center>"
463                         "%s<br /><br />\n", &buf[4]);
464         }
465         else {
466                 safestrncpy(message, "", sizeof message);
467         }
468         select_user_to_edit(message, bstr("username"));
469 }
470                 
471
472
473 /**
474  * \brief create a new user
475  * take the web environment username and create it on the citadel server
476  */
477 void create_user(void) {
478         char buf[SIZ];
479         char error_message[SIZ];
480         char username[SIZ];
481
482         safestrncpy(username, bstr("username"), sizeof username);
483
484         serv_printf("CREU %s", username);
485         serv_getln(buf, sizeof buf);
486
487         if (buf[0] == '2') {
488                 sprintf(WC->ImportantMessage,
489                         _("A new user has been created."));
490                 display_edituser(username, 1);
491         }
492         else {
493                 sprintf(error_message,
494                         "<img src=\"static/error.gif\" align=center>"
495                         "%s<br /><br />\n", &buf[4]);
496                 select_user_to_edit(error_message, NULL);
497         }
498
499 }
500
501
502
503 /*@}*/