use the same way to display all banners and services contents
[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("<img src=\"static/usermanag_48x.gif\">");
28         wprintf("<h1>");
29         wprintf(_("Edit or delete users"));
30         wprintf("</h1>");
31         wprintf("</div>");
32
33         wprintf("<div id=\"content\" class=\"service\">\n");
34
35         if (message != NULL) wprintf(message);
36
37         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
38
39         svprintf("BOXTITLE", WCS_STRING, _("Add users"));
40         do_template("beginbox");
41
42         wprintf(_("To create a new user account, enter the desired "
43                 "user name in the box below and click 'Create'."));
44         wprintf("<br /><br />");
45
46         wprintf("<center><form method=\"POST\" action=\"create_user\">\n");
47         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
48         wprintf(_("New user: "));
49         wprintf("<input type=\"text\" name=\"username\"><br />\n"
50                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
51                 "</form></center>\n", _("Create"));
52
53         do_template("endbox");
54
55         wprintf("</td><td>");
56
57         svprintf("BOXTITLE", WCS_STRING, _("Edit or Delete users"));
58         do_template("beginbox");
59
60         wprintf(_("To edit an existing user account, select the user "
61                 "name from the list and click 'Edit'."));
62         wprintf("<br /><br />");
63         
64         wprintf("<center>"
65                 "<form method=\"POST\" action=\"display_edituser\">\n");
66         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
67         wprintf("<select name=\"username\" size=10 style=\"width:100%%\">\n");
68         serv_puts("LIST");
69         serv_getln(buf, sizeof buf);
70         if (buf[0] == '1') {
71                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
72                         extract_token(username, buf, 0, '|', sizeof username);
73                         wprintf("<option");
74                         if (preselect != NULL)
75                            if (!strcasecmp(username, preselect))
76                               wprintf(" selected");
77                         wprintf(">");
78                         escputs(username);
79                         wprintf("\n");
80                 }
81         }
82         wprintf("</select><br />\n");
83
84         wprintf("<input type=\"submit\" name=\"edit_config_button\" value=\"%s\">", _("Edit configuration"));
85         wprintf("<input type=\"submit\" name=\"edit_abe_button\" value=\"%s\">", _("Edit address book entry"));
86         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
87                 "onClick=\"return confirm('%s');\">", _("Delete user"), _("Delete this user?"));
88         wprintf("</form></center>\n");
89         do_template("endbox");
90
91         wprintf("</td></tr></table>\n");
92
93         wDumpContent(1);
94 }
95
96
97
98 /**
99  * \brief Locate the message number of a user's vCard in the current room
100  * \param username the plaintext name of the user
101  * \param usernum the number of the user on the citadel server
102  * \return the message id of his vcard
103  */
104 long locate_user_vcard(char *username, long usernum) {
105         char buf[SIZ];
106         long vcard_msgnum = (-1L);
107         char content_type[SIZ];
108         char partnum[SIZ];
109         int already_tried_creating_one = 0;
110
111         struct stuff_t {
112                 struct stuff_t *next;
113                 long msgnum;
114         };
115
116         struct stuff_t *stuff = NULL;
117         struct stuff_t *ptr;
118
119 TRYAGAIN:
120         /** Search for the user's vCard */
121         serv_puts("MSGS ALL");
122         serv_getln(buf, sizeof buf);
123         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
124                 ptr = malloc(sizeof(struct stuff_t));
125                 ptr->msgnum = atol(buf);
126                 ptr->next = stuff;
127                 stuff = ptr;
128         }
129
130         /** Iterate through the message list looking for vCards */
131         while (stuff != NULL) {
132                 serv_printf("MSG0 %ld|2", stuff->msgnum);
133                 serv_getln(buf, sizeof buf);
134                 if (buf[0]=='1') {
135                         while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
136                                 if (!strncasecmp(buf, "part=", 5)) {
137                                         extract_token(partnum, &buf[5], 2, '|', sizeof partnum);
138                                         extract_token(content_type, &buf[5], 4, '|', sizeof content_type);
139                                         if (  (!strcasecmp(content_type, "text/x-vcard"))
140                                            || (!strcasecmp(content_type, "text/vcard")) ) {
141                                                 vcard_msgnum = stuff->msgnum;
142                                         }
143                                 }
144                         }
145                 }
146
147                 ptr = stuff->next;
148                 free(stuff);
149                 stuff = ptr;
150         }
151
152         /** If there's no vcard, create one */
153         if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
154                 already_tried_creating_one = 1;
155                 serv_puts("ENT0 1|||4");
156                 serv_getln(buf, sizeof buf);
157                 if (buf[0] == '4') {
158                         serv_puts("Content-type: text/x-vcard");
159                         serv_puts("");
160                         serv_puts("begin:vcard");
161                         serv_puts("end:vcard");
162                         serv_puts("000");
163                 }
164                 goto TRYAGAIN;
165         }
166
167         return(vcard_msgnum);
168 }
169
170
171 /**
172  * \brief Display the form for editing a user's address book entry
173  * \param username the name of the user
174  * \param usernum the citadel-uid of the user
175  */
176 void display_edit_address_book_entry(char *username, long usernum) {
177         char roomname[SIZ];
178         char buf[SIZ];
179         char error_message[SIZ];
180         long vcard_msgnum = (-1L);
181
182         /** Locate the user's config room, creating it if necessary */
183         sprintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
184         serv_printf("GOTO %s||1", roomname);
185         serv_getln(buf, sizeof buf);
186         if (buf[0] != '2') {
187                 serv_printf("CRE8 1|%s|5|||1|", roomname);
188                 serv_getln(buf, sizeof buf);
189                 serv_printf("GOTO %s||1", roomname);
190                 serv_getln(buf, sizeof buf);
191                 if (buf[0] != '2') {
192                         sprintf(error_message,
193                                 "<img src=\"static/error.gif\" align=center>"
194                                 "%s<br /><br />\n", &buf[4]);
195                         select_user_to_edit(error_message, username);
196                         return;
197                 }
198         }
199
200         vcard_msgnum = locate_user_vcard(username, usernum);
201
202         if (vcard_msgnum < 0) {
203                 sprintf(error_message,
204                         "<img src=\"static/error.gif\" align=center>%s<br /><br />\n",
205                         _("An error occurred while trying to create or edit this address book entry.")
206                 );
207                 select_user_to_edit(error_message, username);
208                 return;
209         }
210
211         do_edit_vcard(vcard_msgnum, "1", "select_user_to_edit");
212 }
213
214
215
216
217 /**
218  * \brief Edit a user.  
219  * If supplied_username is null, look in the "username"
220  * web variable for the name of the user to edit.
221  * 
222  * If "is_new" is set to nonzero, this screen will set the web variables
223  * to send the user to the vCard editor next.
224  * \param supplied_username user to look up or NULL if to search in the environment
225  * \param is_new should we create the user?
226  */
227 void display_edituser(char *supplied_username, int is_new) {
228         char buf[1024];
229         char error_message[1024];
230         time_t now;
231
232         char username[256];
233         char password[256];
234         unsigned int flags;
235         int timescalled;
236         int msgsposted;
237         int axlevel;
238         long usernum;
239         time_t lastcall;
240         int purgedays;
241         int i;
242
243         if (supplied_username != NULL) {
244                 safestrncpy(username, supplied_username, sizeof username);
245         }
246         else {
247                 safestrncpy(username, bstr("username"), sizeof username);
248         }
249
250         serv_printf("AGUP %s", username);
251         serv_getln(buf, sizeof buf);
252         if (buf[0] != '2') {
253                 sprintf(error_message,
254                         "<img src=\"static/error.gif\" align=center>"
255                         "%s<br /><br />\n", &buf[4]);
256                 select_user_to_edit(error_message, username);
257                 return;
258         }
259
260         extract_token(username, &buf[4], 0, '|', sizeof username);
261         extract_token(password, &buf[4], 1, '|', sizeof password);
262         flags = extract_int(&buf[4], 2);
263         timescalled = extract_int(&buf[4], 3);
264         msgsposted = extract_int(&buf[4], 4);
265         axlevel = extract_int(&buf[4], 5);
266         usernum = extract_long(&buf[4], 6);
267         lastcall = extract_long(&buf[4], 7);
268         purgedays = extract_long(&buf[4], 8);
269
270         if (!IsEmptyStr(bstr("edit_abe_button"))) {
271                 display_edit_address_book_entry(username, usernum);
272                 return;
273         }
274
275         if (!IsEmptyStr(bstr("delete_button"))) {
276                 delete_user(username);
277                 return;
278         }
279
280         output_headers(1, 1, 2, 0, 0, 0);
281         wprintf("<div id=\"banner\">\n");
282         wprintf("<h1>");
283         wprintf(_("Edit user account: "));
284         escputs(username);
285         wprintf("</h1>");
286         wprintf("</div>");
287
288         wprintf("<div id=\"content\" class=\"service\">\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 (IsEmptyStr(bstr("ok_button"))) {
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 /*@}*/