* Moved to the new string tokenizer API
[citadel.git] / webcit / useredit.c
index e47ed8dbb6c4ee40a17f3a19d3bd38a91f3b8331..835105bf37b00de501ec2533dc65ca1aa587eab7 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * $Id$
+ *
  * Administrative screen to add/change/delete user accounts
  *
  */
 
 
 
-void select_user_to_edit(char *message)
+void select_user_to_edit(char *message, char *preselect)
 {
        char buf[SIZ];
        char username[SIZ];
 
-       output_headers(3);      /* No room banner on this screen */
+       output_headers(1, 1, 2, 0, 1, 0, 0);
+       wprintf("<div id=\"banner\">\n");
+       wprintf("<table width=100%% border=0 bgcolor=#444455><tr>"
+               "<td>"
+               "<span class=\"titlebar\">"
+               "<img src=\"/static/users-icon.gif\">"
+               "Edit or delete users"
+               "</span></td></tr></table>\n"
+               "</div>\n<div id=\"content\">\n"
+       );
 
        if (message != NULL) wprintf(message);
 
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>"
-               "Add/change/delete user accounts"
-               "</B></FONT></TD></TR></TABLE>\n");
+       wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP><TD>\n");
+
+       svprintf("BOXTITLE", WCS_STRING, "Add users");
+       do_template("beginbox");
+
+       wprintf("To create a new user account, enter the desired "
+               "user name in the box below and click 'Create'.<br /><br />");
+
+        wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/create_user\">\n");
+        wprintf("New user: ");
+        wprintf("<input type=text name=username><br />\n"
+               "<input type=submit value=\"Create\">"
+               "</FORM></CENTER>\n");
+
+       do_template("endbox");
+
+       wprintf("</TD><TD>");
 
-       wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
-               "<TD>To edit an existing user account, select the user "
-               "name from the list and click 'Edit'.<BR><BR>");
+       svprintf("BOXTITLE", WCS_STRING, "Edit or Delete users");
+       do_template("beginbox");
+
+       wprintf("To edit an existing user account, select the user "
+               "name from the list and click 'Edit'.<br /><br />");
        
-        wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_edituser\">\n");
-        wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
+        wprintf("<CENTER>"
+               "<FORM METHOD=\"POST\" ACTION=\"/display_edituser\">\n");
+        wprintf("<SELECT NAME=\"username\" SIZE=10 STYLE=\"width:100%%\">\n");
         serv_puts("LIST");
         serv_gets(buf);
         if (buf[0] == '1') {
                 while (serv_gets(buf), strcmp(buf, "000")) {
-                        extract(username, buf, 0);
-                        wprintf("<OPTION>");
+                        extract_token(username, buf, 0, '|', sizeof username);
+                        wprintf("<OPTION");
+                       if (preselect != NULL)
+                          if (!strcasecmp(username, preselect))
+                             wprintf(" SELECTED");
+                       wprintf(">");
                         escputs(username);
                         wprintf("\n");
                 }
         }
-        wprintf("</SELECT><BR>\n");
+        wprintf("</SELECT><br />\n");
 
         wprintf("<input type=submit name=sc value=\"Edit configuration\">");
         wprintf("<input type=submit name=sc value=\"Edit address book entry\">");
+        wprintf("<input type=submit name=sc value=\"Delete user\" "
+               "onClick=\"return confirm('Delete this user?');\">");
         wprintf("</FORM></CENTER>\n");
-
-       wprintf("</TD><TD>"
-               "To create a new user account, enter the desired "
-               "user name in the box below and click 'Create'.<BR><BR>");
-
-        wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/create_user\">\n");
-        wprintf("New user: ");
-        wprintf("<input type=text name=username><BR>\n"
-               "<input type=submit value=\"Create\">"
-               "</FORM></CENTER>\n");
+       do_template("endbox");
 
        wprintf("</TD></TR></TABLE>\n");
 
@@ -84,12 +108,10 @@ void select_user_to_edit(char *message)
 
 
 /* 
- * Display the form for editing a user's address book entry
+ * Locate the message number of a user's vCard in the current room
  */
-void display_edit_address_book_entry(char *username, long usernum) {
-       char roomname[SIZ];
+long locate_user_vcard(char *username, long usernum) {
        char buf[SIZ];
-       char error_message[SIZ];
        long vcard_msgnum = (-1L);
        char content_type[SIZ];
        char partnum[SIZ];
@@ -103,25 +125,6 @@ void display_edit_address_book_entry(char *username, long usernum) {
        struct stuff_t *stuff = NULL;
        struct stuff_t *ptr;
 
-
-       /* Locate the user's config room, creating it if necessary */
-       sprintf(roomname, "%010ld.My Citadel Config", usernum);
-       serv_printf("GOTO %s", roomname);
-       serv_gets(buf);
-       if (buf[0] != '2') {
-               serv_printf("CRE8 1|%s|5|", roomname);
-               serv_gets(buf);
-               serv_printf("GOTO %s", roomname);
-               serv_gets(buf);
-               if (buf[0] != '2') {
-                       sprintf(error_message,
-                               "<IMG SRC=\"static/error.gif\" VALIGN=CENTER>"
-                               "%s<BR><BR>\n", &buf[4]);
-                       select_user_to_edit(error_message);
-                       return;
-               }
-       }
-
 TRYAGAIN:
        /* Search for the user's vCard */
        serv_puts("MSGS ALL");
@@ -133,15 +136,15 @@ TRYAGAIN:
                stuff = ptr;
        }
 
-       /* Iterate throught the message list looking for vCards */
+       /* Iterate through the message list looking for vCards */
        while (stuff != NULL) {
                serv_printf("MSG0 %ld|2", stuff->msgnum);
                serv_gets(buf);
                if (buf[0]=='1') {
                        while(serv_gets(buf), strcmp(buf, "000")) {
                                if (!strncasecmp(buf, "part=", 5)) {
-                                       extract(partnum, &buf[5], 2);
-                                       extract(content_type, &buf[5], 4);
+                                       extract_token(partnum, &buf[5], 2, '|', sizeof partnum);
+                                       extract_token(content_type, &buf[5], 4, '|', sizeof content_type);
                                        if (!strcasecmp(content_type,
                                           "text/x-vcard")) {
                                                vcard_msgnum = stuff->msgnum;
@@ -155,8 +158,6 @@ TRYAGAIN:
                stuff = ptr;
        }
 
-       lprintf(9, "vcard_msgnum == %ld\n", vcard_msgnum);
-
        /* If there's no vcard, create one */
        if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
                already_tried_creating_one = 1;
@@ -172,15 +173,50 @@ TRYAGAIN:
                goto TRYAGAIN;
        }
 
+       return(vcard_msgnum);
+}
+
+
+/* 
+ * Display the form for editing a user's address book entry
+ */
+void display_edit_address_book_entry(char *username, long usernum) {
+       char roomname[SIZ];
+       char buf[SIZ];
+       char error_message[SIZ];
+       long vcard_msgnum = (-1L);
+
+       /* Locate the user's config room, creating it if necessary */
+       sprintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
+       serv_printf("GOTO %s||1", roomname);
+       serv_gets(buf);
+       if (buf[0] != '2') {
+               serv_printf("CRE8 1|%s|5|||1|", roomname);
+               serv_gets(buf);
+               serv_printf("GOTO %s||1", roomname);
+               serv_gets(buf);
+               if (buf[0] != '2') {
+                       sprintf(error_message,
+                               "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                               "%s<br /><br />\n", &buf[4]);
+                       select_user_to_edit(error_message, username);
+                       return;
+               }
+       }
+
+       vcard_msgnum = locate_user_vcard(username, usernum);
+
        if (vcard_msgnum < 0) {
                sprintf(error_message,
-                       "<IMG SRC=\"static/error.gif\" VALIGN=CENTER>"
-                       "Could not create/edit vCard<BR><BR>\n");
-               select_user_to_edit(error_message);
+                       "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                       "Could not create/edit vCard"
+                       "<br /><br />\n"
+               );
+               select_user_to_edit(error_message, username);
                return;
        }
 
-       display_addressbook(vcard_msgnum, 0);
+       do_edit_vcard(vcard_msgnum, "1", "/select_user_to_edit");
 }
 
 
@@ -189,8 +225,11 @@ TRYAGAIN:
 /*
  * Edit a user.  If supplied_username is null, look in the "username"
  * web variable for the name of the user to edit.
+ * 
+ * If "is_new" is set to nonzero, this screen will set the web variables
+ * to send the user to the vCard editor next.
  */
-void display_edituser(char *supplied_username) {
+void display_edituser(char *supplied_username, int is_new) {
        char buf[SIZ];
        char error_message[SIZ];
        time_t now;
@@ -217,14 +256,14 @@ void display_edituser(char *supplied_username) {
        serv_gets(buf);
        if (buf[0] != '2') {
                sprintf(error_message,
-                       "<IMG SRC=\"static/error.gif\" VALIGN=CENTER>"
-                       "%s<BR><BR>\n", &buf[4]);
-               select_user_to_edit(error_message);
+                       "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                       "%s<br /><br />\n", &buf[4]);
+               select_user_to_edit(error_message, username);
                return;
        }
 
-       extract(username, &buf[4], 0);
-       extract(password, &buf[4], 1);
+       extract_token(username, &buf[4], 0, '|', sizeof username);
+       extract_token(password, &buf[4], 1, '|', sizeof password);
        flags = extract_int(&buf[4], 2);
        timescalled = extract_int(&buf[4], 3);
        msgsposted = extract_int(&buf[4], 4);
@@ -238,17 +277,29 @@ void display_edituser(char *supplied_username) {
                return;
        }
 
-       output_headers(3);      /* No room banner on this screen */
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>"
+       if (!strcmp(bstr("sc"), "Delete user")) {
+               delete_user(username);
+               return;
+       }
+
+       output_headers(1, 1, 2, 0, 0, 0, 0);
+       wprintf("<div id=\"banner\">\n");
+       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
+       wprintf("<SPAN CLASS=\"titlebar\">"
                "Edit user account: ");
        escputs(username);
-       wprintf("</B></FONT></TD></TR></TABLE>\n");
+       wprintf("</SPAN></TD></TR></TABLE>\n");
+       wprintf("</div>\n<div id=\"content\">\n");
 
+       wprintf("<div id=\"fix_scrollbar_bug\">"
+               "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
        wprintf("<FORM METHOD=\"POST\" ACTION=\"/edituser\">\n"
                "<INPUT TYPE=\"hidden\" NAME=\"username\" VALUE=\"");
        escputs(username);
        wprintf("\">\n");
+       wprintf("<INPUT TYPE=\"hidden\" NAME=\"is_new\" VALUE=\"%d\">\n"
+               "<INPUT TYPE=\"hidden\" NAME=\"usernum\" VALUE=\"%ld\">\n",
+               is_new, usernum);
 
        wprintf("<INPUT TYPE=\"hidden\" NAME=\"flags\" VALUE=\"%d\">\n", flags);
 
@@ -308,17 +359,12 @@ void display_edituser(char *supplied_username) {
        wprintf("</TABLE>\n");
 
        wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"OK\">\n"
+               "&nbsp;"
                "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n"
-               "<BR><BR></FORM>\n");
-
-       wprintf("<A HREF=\"/dotgoto&room=%010ld.My%%20Citadel%%20Config\">",
-               usernum);
-       wprintf("Click here to access the configuration room for ");
-       escputs(username);
-       wprintf("</A><BR>\n"
-               "(Contact information, Internet e-mail addresses, etc.)<BR>"
-               "</CENTER>\n");
+               "<br /><br /></FORM>\n");
 
+       wprintf("</CENTER>\n");
+       wprintf("</td></tr></table></div>\n");
        wDumpContent(1);
 
 }
@@ -328,6 +374,9 @@ void display_edituser(char *supplied_username) {
 void edituser(void) {
        char message[SIZ];
        char buf[SIZ];
+       int is_new = 0;
+
+       is_new = atoi(bstr("is_new"));
 
        if (strcasecmp(bstr("action"), "OK")) {
                strcpy(message, "Edit user cancelled.");
@@ -349,18 +398,44 @@ void edituser(void) {
                serv_gets(buf);
                if (buf[0] != '2') {
                        sprintf(message,
-                               "<IMG SRC=\"static/error.gif\" VALIGN=CENTER>"
-                               "%s<BR><BR>\n", &buf[4]);
+                               "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                               "%s<br /><br />\n", &buf[4]);
                }
                else {
                        strcpy(message, "");
                }
        }
 
-       select_user_to_edit(message);
+       /* If we are in the middle of creating a new user, move on to
+        * the vCard edit screen.
+        */
+       if (is_new) {
+               display_edit_address_book_entry( bstr("username"), atol(bstr("usernum")) );
+       }
+       else {
+               select_user_to_edit(message, bstr("username"));
+       }
 }
 
 
+void delete_user(char *username) {
+       char buf[SIZ];
+       char message[SIZ];
+
+       serv_printf("ASUP %s|0|0|0|0|0|", username);
+       serv_gets(buf);
+       if (buf[0] != '2') {
+               sprintf(message,
+                       "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                       "%s<br /><br />\n", &buf[4]);
+       }
+       else {
+               strcpy(message, "");
+       }
+       select_user_to_edit(message, bstr("username"));
+}
+               
+
 
 
 void create_user(void) {
@@ -374,13 +449,15 @@ void create_user(void) {
        serv_gets(buf);
 
        if (buf[0] == '2') {
-               display_edituser(username);
+               /* sprintf(error_message, "<b>User has been created.</b>");
+               select_user_to_edit(error_message, username); */
+               display_edituser(username, 1);
        }
        else {
                sprintf(error_message,
-                       "<IMG SRC=\"static/error.gif\" VALIGN=CENTER>"
-                       "%s<BR><BR>\n", &buf[4]);
-               select_user_to_edit(error_message);
+                       "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
+                       "%s<br /><br />\n", &buf[4]);
+               select_user_to_edit(error_message, NULL);
        }
 
 }