* upgrade hash retriever function, it now takes the usual cloud of parameters as...
[citadel.git] / webcit / useredit.c
index 425139a7e67e331ec3e57a3e6229cbd28043b63d..77c98e66772445d25df2629f89cf2a147347765d 100644 (file)
@@ -1,26 +1,26 @@
 /*
  * $Id$
  */
-/**
- * \defgroup AdminTasks Administrative screen to add/change/delete user accounts
- * \ingroup CitadelConfig
- *
- */
-/*@{*/
 
 #include "webcit.h"
 #include "webserver.h"
 
 
 /**
- * \brief show a list of available users to edit them
- * \param message the header message???
- * \param preselect which user should be selected in the browser
+ *  show a list of available users to edit them
+ *  message the header message???
+ *  preselect which user should be selected in the browser
  */
 void select_user_to_edit(char *message, char *preselect)
-{
+{/*
        char buf[SIZ];
        char username[SIZ];
+ */
+       output_headers(1, 0, 0, 0, 1, 0);
+       do_template("edituser_select", NULL);
+        end_burst();
+
+/*
 
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
@@ -37,25 +37,25 @@ void select_user_to_edit(char *message, char *preselect)
        wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
 
        svput("BOXTITLE", WCS_STRING, _("Add users"));
-       do_template("beginbox");
+       do_template("beginbox", NULL);
 
        wprintf(_("To create a new user account, enter the desired "
                "user name in the box below and click 'Create'."));
        wprintf("<br /><br />");
 
         wprintf("<center><form method=\"POST\" action=\"create_user\">\n");
-       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
         wprintf(_("New user: "));
         wprintf("<input type=\"text\" name=\"username\"><br />\n"
                "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
                "</form></center>\n", _("Create"));
 
-       do_template("endbox");
+       do_template("endbox", NULL);
 
        wprintf("</td><td>");
 
        svput("BOXTITLE", WCS_STRING, _("Edit or Delete users"));
-       do_template("beginbox");
+       do_template("beginbox", NULL);
 
        wprintf(_("To edit an existing user account, select the user "
                "name from the list and click 'Edit'."));
@@ -63,7 +63,7 @@ void select_user_to_edit(char *message, char *preselect)
        
         wprintf("<center>"
                "<form method=\"POST\" action=\"display_edituser\">\n");
-       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
         wprintf("<select name=\"username\" size=10 style=\"width:100%%\">\n");
         serv_puts("LIST");
         serv_getln(buf, sizeof buf);
@@ -86,19 +86,365 @@ void select_user_to_edit(char *message, char *preselect)
         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
                "onClick=\"return confirm('%s');\">", _("Delete user"), _("Delete this user?"));
         wprintf("</form></center>\n");
-       do_template("endbox");
+       do_template("endbox", NULL);
 
        wprintf("</td></tr></table>\n");
 
        wDumpContent(1);
+*/
+}
+
+
+typedef struct _UserListEntry {
+       int UID;
+       int AccessLevel;
+       int nLogons;
+       int nPosts;
+
+       StrBuf *UserName;
+       StrBuf *Passvoid;
+       time_t LastLogonT;
+       /* Just available for Single users to view: */
+       unsigned int Flags;
+       int DaysTillPurge;
+} UserListEntry;
+
+
+UserListEntry* NewUserListOneEntry(StrBuf *SerializedUser)
+{
+       UserListEntry *ul;
+
+       if (StrLength(SerializedUser) < 8) 
+               return NULL;
+
+       ul = (UserListEntry*) malloc(sizeof(UserListEntry));
+       ul->UserName = NewStrBuf();
+       ul->Passvoid = NewStrBuf();
+
+       StrBufExtract_token(ul->UserName, SerializedUser, 0, '|');
+       StrBufExtract_token(ul->Passvoid, SerializedUser, 1, '|');
+       ul->Flags = (unsigned int)StrBufExtract_long(SerializedUser, 2, '|');
+       ul->nLogons = StrBufExtract_int(SerializedUser, 3, '|');
+       ul->nPosts = StrBufExtract_int(SerializedUser, 4, '|');
+       ul->AccessLevel = StrBufExtract_int(SerializedUser, 5, '|');
+       ul->UID = StrBufExtract_int(SerializedUser, 6, '|');
+       ul->LastLogonT = StrBufExtract_long(SerializedUser, 7, '|');
+       ul->DaysTillPurge = StrBufExtract_int(SerializedUser, 8, '|');
+       return ul;
+}
+
+void DeleteUserListEntry(void *vUserList)
+{
+       UserListEntry *ul = (UserListEntry*) vUserList;
+       FreeStrBuf(&ul->UserName);
+       FreeStrBuf(&ul->Passvoid);
+       free(ul);
+}
+
+UserListEntry* NewUserListEntry(StrBuf *SerializedUserList)
+{
+       UserListEntry *ul;
+
+       if (StrLength(SerializedUserList) < 8) 
+               return NULL;
+
+       ul = (UserListEntry*) malloc(sizeof(UserListEntry));
+       ul->UserName = NewStrBuf();
+       ul->Passvoid = NewStrBuf();
+
+       StrBufExtract_token(ul->UserName, SerializedUserList, 0, '|');
+       ul->AccessLevel = StrBufExtract_int(SerializedUserList, 1, '|');
+       ul->UID = StrBufExtract_int(SerializedUserList, 2, '|');
+       ul->LastLogonT = StrBufExtract_long(SerializedUserList, 3, '|');
+       ul->nLogons = StrBufExtract_int(SerializedUserList, 4, '|');
+       ul->nPosts = StrBufExtract_int(SerializedUserList, 5, '|');
+       StrBufExtract_token(ul->Passvoid, SerializedUserList, 6, '|');
+       ul->Flags = 0;
+       ul->DaysTillPurge = -1;
+       return ul;
+}
+
+/*
+ * Sort by Username
+ */
+int CompareUserListName(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return strcmp(ChrPtr(u1->UserName), ChrPtr(u2->UserName));
+}
+int CompareUserListNameRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+       return strcmp(ChrPtr(u2->UserName), ChrPtr(u1->UserName));
+}
+
+/*
+ * Sort by AccessLevel
+ */
+int CompareAccessLevel(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u1->AccessLevel > u2->AccessLevel);
+}
+int CompareAccessLevelRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u2->AccessLevel > u1->AccessLevel);
+}
+
+
+/*
+ * Sort by UID
+ */
+int CompareUID(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u1->UID > u2->UID);
+}
+int CompareUIDRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u2->UID > u1->UID);
+}
+
+/*
+ * Sort By Date /// TODO!
+ */
+int CompareLastLogon(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u1->LastLogonT > u2->LastLogonT);
+}
+int CompareLastLogonRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u2->LastLogonT > u1->LastLogonT);
+}
+
+/*
+ * Sort By Number of Logons
+ */
+int ComparenLogons(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u1->nLogons > u2->nLogons);
+}
+int ComparenLogonsRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u2->nLogons > u1->nLogons);
+}
+
+/*
+ * Sort By Number of Posts
+ */
+int ComparenPosts(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u1->nPosts > u2->nPosts);
+}
+int ComparenPostsRev(const void *vUser1, const void *vUser2)
+{
+       UserListEntry *u1 = (UserListEntry*) vUser1;
+       UserListEntry *u2 = (UserListEntry*) vUser2;
+
+       return (u2->nPosts > u1->nPosts);
+}
+
+
+HashList *iterate_load_userlist(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       HashList *Hash;
+       char buf[SIZ];
+       StrBuf *Buf;
+       UserListEntry* ul;
+       char nnn[64];
+       int nUsed;
+       int Order;
+       int len;
+       
+        serv_puts("LIST");
+        serv_getln(buf, sizeof buf);
+        if (buf[0] == '1') {
+               Hash = NewHash(1, NULL);
+
+               Buf = NewStrBuf();
+               while ((len = StrBuf_ServGetln(Buf),
+                       strcmp(ChrPtr(Buf), "000"))) {
+                       ul = NewUserListEntry(Buf);
+                       if (ul == NULL)
+                               continue;
+                       nUsed = GetCount(Hash);
+                       nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
+                       Put(Hash, nnn, nUsed, ul, DeleteUserListEntry); 
+               }
+               FreeStrBuf(&Buf);
+               Order = ibstr("SortOrder");
+               switch (ibstr("SortBy")){
+               case 1: /*NAME*/
+                       SortByPayload(Hash, (Order)? 
+                                     CompareUserListName:
+                                     CompareUserListNameRev);
+                       break;
+               case 2: /*AccessLevel*/
+                       SortByPayload(Hash, (Order)? 
+                                     CompareAccessLevel:
+                                     CompareAccessLevelRev);
+                       break;
+               case 3: /*nLogons*/
+                       SortByPayload(Hash, (Order)? 
+                                     ComparenLogons:
+                                     ComparenLogonsRev);
+                       break;
+               case 4: /*UID*/
+                       SortByPayload(Hash, (Order)? 
+                                     CompareUID:
+                                     CompareUIDRev);
+                       break;
+               case 5: /*LastLogon*/
+                       SortByPayload(Hash, (Order)? 
+                                     CompareLastLogon:
+                                     CompareLastLogonRev);
+                       break;
+               case 6: /* nLogons */
+                       SortByPayload(Hash, (Order)? 
+                                     ComparenLogons:
+                                     ComparenLogonsRev);
+                       break;
+               case 7: /* Posts */
+                       SortByPayload(Hash, (Order)? 
+                                     ComparenPosts:
+                                     ComparenPostsRev);
+                       break;
+               }
+               return Hash;
+        }
+       return NULL;
+}
+
+
+void tmplput_USERLIST_UserName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+/// TODO: X
+       StrBufAppendBuf(Target, ul->UserName, 0);
+}
+
+void tmplput_USERLIST_AccessLevelNo(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->AccessLevel, 0);
+}
+
+void tmplput_USERLIST_AccessLevelStr(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+       
+       StrBufAppendBufPlain(Target, _(axdefs[ul->AccessLevel]), -1, 0);
+}
+
+void tmplput_USERLIST_UID(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->UID, 0);
+}
+
+void tmplput_USERLIST_LastLogonNo(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target,"%ld", ul->LastLogonT, 0);
+}
+void tmplput_USERLIST_LastLogonStr(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+       StrEscAppend(Target, NULL, asctime(localtime(&ul->LastLogonT)), 0, 0);
+}
+
+void tmplput_USERLIST_nLogons(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->nLogons, 0);
+}
+
+void tmplput_USERLIST_nPosts(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->nPosts, 0);
+}
+
+void tmplput_USERLIST_Flags(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->Flags, 0);
+}
+
+void tmplput_USERLIST_DaysTillPurge(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       StrBufAppendPrintf(Target, "%d", ul->DaysTillPurge, 0);
 }
 
+int ConditionalUser(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+       if (havebstr("usernum")) {
+               return ibstr("usernum") == ul->UID;
+       }
+       else if (havebstr("username")) {
+               return strcmp(bstr("username"), ChrPtr(ul->UserName)) == 0;
+       }
+       else 
+               return 0;
+}
 
+int ConditionalFlagINetEmail(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+       return (ul->Flags & US_INTERNET) != 0;
+}
+
+int ConditionalUserAccess(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       UserListEntry *ul = (UserListEntry*) Context;
+
+       if (Tokens->Params[3]->Type == TYPE_LONG)
+               return (Tokens->Params[3]->lvalue == ul->AccessLevel);
+       else
+               return 0;
+}
 
 /**
- * \brief Locate the message number of a user's vCard in the current room
- * \param username the plaintext name of the user
- * \param usernum the number of the user on the citadel server
+ *  Locate the message number of a user's vCard in the current room
+ *  username the plaintext name of the user
+ *  usernum the number of the user on the citadel server
  * \return the message id of his vcard
  */
 long locate_user_vcard(char *username, long usernum) {
@@ -150,7 +496,7 @@ TRYAGAIN:
        }
 
        /** If there's no vcard, create one */
-       if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
+       if ((vcard_msgnum < 0) && (already_tried_creating_one == 0)) {
                already_tried_creating_one = 1;
                serv_puts("ENT0 1|||4");
                serv_getln(buf, sizeof buf);
@@ -169,9 +515,9 @@ TRYAGAIN:
 
 
 /**
- * \brief Display the form for editing a user's address book entry
- * \param username the name of the user
- * \param usernum the citadel-uid of the user
+ *  Display the form for editing a user's address book entry
+ *  username the name of the user
+ *  usernum the citadel-uid of the user
  */
 void display_edit_address_book_entry(char *username, long usernum) {
        char roomname[SIZ];
@@ -212,18 +558,65 @@ void display_edit_address_book_entry(char *username, long usernum) {
 }
 
 
+void display_edituser(char *supplied_username, int is_new) {
+       UserListEntry* UL;
+       StrBuf *Buf;
+       char error_message[1024];
+       char MajorStatus;
+       char username[256];
 
+       if (supplied_username != NULL) {
+               safestrncpy(username, supplied_username, sizeof username);
+       }
+       else {
+               safestrncpy(username, bstr("username"), sizeof username);
+       }
 
-/**
- * \brief Edit a user.  
+       Buf = NewStrBuf();
+       serv_printf("AGUP %s", username);
+       StrBuf_ServGetln(Buf);
+       MajorStatus = ChrPtr(Buf)[0];
+       StrBufCutLeft(Buf, 4);
+       if (MajorStatus != '2') {
+               ///TODO ImportantMessage
+               sprintf(error_message,
+                       "<img src=\"static/error.gif\" align=center>"
+                       "%s<br /><br />\n", ChrPtr(Buf));
+               select_user_to_edit(error_message, username);
+               FreeStrBuf(&Buf);
+               return;
+       }
+       else {
+               UL = NewUserListOneEntry(Buf);
+               if (havebstr("edit_abe_button")) {
+                       display_edit_address_book_entry(username, UL->UID);
+               }
+               else if (havebstr("delete_button")) {
+                       delete_user(username);
+               }
+               else {
+                       output_headers(1, 0, 0, 0, 1, 0);
+                       DoTemplate(HKEY("userlist_detailview"), NULL, (void*) UL, CTX_USERLIST);
+                       end_burst();
+               }
+               DeleteUserListEntry(UL);
+               
+       }
+       FreeStrBuf(&Buf);
+}
+
+
+
+/* *
+ *  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.
- * \param supplied_username user to look up or NULL if to search in the environment
- * \param is_new should we create the user?
- */
+ *  supplied_username user to look up or NULL if to search in the environment
+ *  is_new should we create the user?
+ * /
 void display_edituser(char *supplied_username, int is_new) {
        char buf[1024];
        char error_message[1024];
@@ -296,7 +689,7 @@ void display_edituser(char *supplied_username, int is_new) {
        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=\"nonce\" value=\"%ld\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
 
        wprintf("<input type=\"hidden\" name=\"flags\" value=\"%d\">\n", flags);
 
@@ -395,10 +788,10 @@ void display_edituser(char *supplied_username, int is_new) {
        wDumpContent(1);
 
 }
-
+*/
 
 /**
- * \brief do the backend operation of the user edit on the server
+ *  do the backend operation of the user edit on the server
  */
 void edituser(void) {
        char message[SIZ];
@@ -469,8 +862,8 @@ void edituser(void) {
 }
 
 /*
- * \brief burge a user 
- * \param username the name of the user to remove
+ *  burge a user 
+ *  username the name of the user to remove
  */
 void delete_user(char *username) {
        char buf[SIZ];
@@ -492,7 +885,7 @@ void delete_user(char *username) {
 
 
 /**
- * \brief create a new user
+ *  create a new user
  * take the web environment username and create it on the citadel server
  */
 void create_user(void) {
@@ -529,5 +922,33 @@ void create_user(void) {
 }
 
 
+void _select_user_to_edit(void){select_user_to_edit(NULL, NULL);}
+void _display_edituser(void) {display_edituser(NULL, 0);}
 
-/*@}*/
+void 
+InitModule_USEREDIT
+(void)
+{
+       WebcitAddUrlHandler(HKEY("select_user_to_edit"), _select_user_to_edit, 0);
+       WebcitAddUrlHandler(HKEY("display_edituser"), _display_edituser, 0);
+       WebcitAddUrlHandler(HKEY("edituser"), edituser, 0);
+       WebcitAddUrlHandler(HKEY("create_user"), create_user, 0);
+
+       RegisterNamespace("USERLIST:USERNAME",      0, 1, tmplput_USERLIST_UserName, CTX_USERLIST);
+       RegisterNamespace("USERLIST:ACCLVLNO",      0, 0, tmplput_USERLIST_AccessLevelNo, CTX_USERLIST);
+       RegisterNamespace("USERLIST:ACCLVLSTR",     0, 0, tmplput_USERLIST_AccessLevelStr, CTX_USERLIST);
+       RegisterNamespace("USERLIST:UID",           0, 0, tmplput_USERLIST_UID, CTX_USERLIST);
+       RegisterNamespace("USERLIST:LASTLOGON:STR", 0, 0, tmplput_USERLIST_LastLogonStr, CTX_USERLIST);
+       RegisterNamespace("USERLIST:LASTLOGON:NO",  0, 0, tmplput_USERLIST_LastLogonNo, CTX_USERLIST);
+       RegisterNamespace("USERLIST:NLOGONS",       0, 0, tmplput_USERLIST_nLogons, CTX_USERLIST);
+       RegisterNamespace("USERLIST:NPOSTS",        0, 0, tmplput_USERLIST_nPosts, CTX_USERLIST);
+                                                   
+       RegisterNamespace("USERLIST:FLAGS",         0, 0, tmplput_USERLIST_Flags, CTX_USERLIST);
+       RegisterNamespace("USERLIST:DAYSTILLPURGE", 0, 0, tmplput_USERLIST_DaysTillPurge, CTX_USERLIST);
+
+       RegisterConditional(HKEY("COND:USERNAME"),  0,    ConditionalUser, CTX_USERLIST);
+       RegisterConditional(HKEY("COND:USERACCESS"), 0,   ConditionalUserAccess, CTX_USERLIST);
+       RegisterConditional(HKEY("COND:USERLIST:FLAG:USE_INTERNET"), 0, ConditionalFlagINetEmail, CTX_USERLIST);
+
+       RegisterIterator("USERLIST", 0, NULL, iterate_load_userlist, NULL, DeleteHash, CTX_USERLIST);
+}