WHQ: templatize editing of user; fix handling of output after edit.
[citadel.git] / webcit / who.c
index 9e1e3c90e797f39573186947d651f83e3ca7109a..1c2984584ada5534ebe22ed4f95780ef461cb351 100644 (file)
@@ -1,6 +1,3 @@
-/*
- * $Id$
- */
 
 #include "webcit.h"
 
@@ -40,42 +37,47 @@ int CompareUserStruct(const void *VUser1, const void *VUser2)
 }
 
 
-int GetWholistSection(HashList *List, time_t now)
+int GetWholistSection(HashList *List, time_t now, StrBuf *Buf)
 {
-       StrBuf *Buf, *XBuf;
        wcsession *WCC = WC;
        UserStateStruct *User, *OldUser;
        void *VOldUser;
        size_t BufLen;
-       char buf[SIZ];
+       const char *Pos;
 
        serv_puts("RWHO");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '1') {
-               Buf = NewStrBuf();
-               XBuf = NewStrBuf();
-               while (BufLen = StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) == 1) {
+               while (BufLen = StrBuf_ServGetln(Buf), 
+                      ((BufLen >= 0) && 
+                       ((BufLen != 3) || strcmp(ChrPtr(Buf), "000"))))
+               {
                        if (BufLen <= 0)
                            continue;
+                       Pos = NULL;
                        User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
-                       User->Session = StrBufExtract_int(Buf, 0, '|');
+                       User->Session = StrBufExtractNext_int(Buf, &Pos, '|');
 
-                       StrBufExtract_token(XBuf, Buf, 1, '|');
-                       User->UserName = NewStrBufDup(XBuf);
+                       User->UserName = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
+                       
+                       User->Room = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
 
-                       StrBufExtract_token(XBuf, Buf, 2, '|');
-                       User->Room = NewStrBufDup(XBuf);
+                       User->Host = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
 
-                       StrBufExtract_token(XBuf, Buf, 3, '|');
-                       User->Host = NewStrBufDup(XBuf);
+                       StrBufSkip_NTokenS(Buf, &Pos, '|', 1);
 
-                       StrBufExtract_token(XBuf, Buf, 9, '|');
-                       User->RealRoom = NewStrBufDup(XBuf);
+                       User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
+                       StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
 
-                       StrBufExtract_token(XBuf, Buf, 10, '|');
-                       User->RealHost = NewStrBufDup(XBuf);
+                       User->RealRoom = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
+
+                       User->RealHost = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
                        
-                       User->LastActive = StrBufExtract_long(Buf, 5, '|');
                        User->Idle = (now - User->LastActive) > 900L;
                        User->IdleSince = (now - User->LastActive) / 60;
                        User->SessionCount = 1;
@@ -102,13 +104,11 @@ int GetWholistSection(HashList *List, time_t now)
                                    User, DestroyUserStruct);
                }
                SortByPayload(List, CompareUserStruct);
-
-               FreeStrBuf(&XBuf);
-               FreeStrBuf(&Buf);
                return 1;
        }
-       else
+       else {
                return 0;
+       }
 }
 
 /*
@@ -131,77 +131,25 @@ void edit_me(void)
 {
        char buf[SIZ];
 
+       output_headers(1, 0, 0, 0, 0, 0);
        if (havebstr("change_room_name_button")) {
                serv_printf("RCHG %s", bstr("fake_roomname"));
                serv_getln(buf, sizeof buf);
-               http_redirect("who");
+               do_template("who");
        } else if (havebstr("change_host_name_button")) {
                serv_printf("HCHG %s", bstr("fake_hostname"));
                serv_getln(buf, sizeof buf);
-               http_redirect("who");
+               do_template("who");
        } else if (havebstr("change_user_name_button")) {
                serv_printf("UCHG %s", bstr("fake_username"));
                serv_getln(buf, sizeof buf);
-               http_redirect("who");
+               do_template("who");
        } else if (havebstr("cancel_button")) {
-               http_redirect("who");
+               do_template("who");
        } else {
-               output_headers(1, 1, 0, 0, 0, 0);
-
-               wprintf("<div id=\"banner\">\n");
-               wprintf("<table class=\"who_banner\"><tr><td>");
-               wprintf("<span class=\"titlebar\">");
-               wprintf(_("Edit your session display"));
-               wprintf("</span></td></tr></table>\n");
-               wprintf("</div>\n<div id=\"content\">\n");
-
-               wprintf(_("This screen allows you to change the way your "
-                       "session appears in the 'Who is online' listing. "
-                       "To turn off any 'fake' name you've previously "
-                       "set, simply click the appropriate 'change' button "
-                       "without typing anything in the corresponding box. "));
-               wprintf("<br />\n");
-
-               wprintf("<form method=\"POST\" action=\"edit_me\">\n");
-               wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
-
-               wprintf("<table border=0 width=100%%>\n");
-
-               wprintf("<tr><td><b>");
-               wprintf(_("Room name:"));
-               wprintf("</b></td>\n<td>");
-               wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
-               wprintf("</td>\n<td align=center>");
-               wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
-                       _("Change room name"));
-               wprintf("</td>\n</tr>\n");
-
-               wprintf("<tr><td><b>");
-               wprintf(_("Host name:"));
-               wprintf("</b></td><td>");
-               wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
-               wprintf("</td>\n<td align=center>");
-               wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
-                       _("Change host name"));
-               wprintf("</td>\n</tr>\n");
-
-               if (WC->is_aide) {
-                       wprintf("<tr><td><b>");
-                       wprintf(_("User name:"));
-                       wprintf("</b></td><td>");
-                       wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
-                       wprintf("</td>\n<td align=center>");
-                       wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
-                               _("Change user name"));
-                       wprintf("</td>\n</tr>\n");
-               }
-               wprintf("<tr><td> </td><td> </td><td align=center>");
-               wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
-                       _("Cancel"));
-               wprintf("</td></tr></table>\n");
-               wprintf("</form></center>\n");
-               wDumpContent(1);
+               do_template("who_edit");
        }
+       end_burst();
 }
 
 void _terminate_session(void) {
@@ -212,21 +160,25 @@ void _terminate_session(void) {
 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
 
 {
+       StrBuf *Buf;
        HashList *List;
-       char buf[SIZ];
         time_t now;
 
+       Buf = NewStrBuf();
+
        serv_puts("TIME");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '2') {
-               now = extract_long(&buf[4], 0);
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL)  == 2) {
+               const char *pos = ChrPtr(Buf) + 4;
+               now = StrBufExtractNext_long(Buf, &pos, '|');
        }
        else {
                now = time(NULL);
        }
 
        List = NewHash(1, NULL);
-       GetWholistSection(List, now);
+       GetWholistSection(List, now, Buf);
+       FreeStrBuf(&Buf);
        return List;
 }
 
@@ -270,6 +222,11 @@ void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
        UserStateStruct *User = (UserStateStruct*) CTX;
        StrBufAppendTemplate(Target, TP, User->RealHost, 0);
 }
+int conditional_who_realhost(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX;
+       return StrLength(User->RealHost) > 0;
+}
 
 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
 {
@@ -317,24 +274,27 @@ void
 InitModule_WHO
 (void)
 {
-       WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
-       WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
+       
+
+       WebcitAddUrlHandler(HKEY("terminate_session"), "", 0, _terminate_session, 0);
+       WebcitAddUrlHandler(HKEY("edit_me"), "", 0, edit_me, 0);
 
        RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
 
-       RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, CTX_WHO);
-       RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, CTX_WHO);
-       RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, CTX_WHO);
-       RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, CTX_WHO);
-       RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, CTX_WHO);
-       RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, CTX_WHO);
-       RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, CTX_WHO);
-       RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, CTX_WHO);
-       RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
-       RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
+       RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, NULL, CTX_WHO);
+       RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, NULL, CTX_WHO);
+       RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, NULL, CTX_WHO);
+       RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, NULL, CTX_WHO);
+       RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, NULL, CTX_WHO);
+       RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, NULL, CTX_WHO);
+       RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, NULL, CTX_WHO);
+       RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, NULL, CTX_WHO);
+       RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
+       RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
 
        RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
        RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
        RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
        RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
+       RegisterConditional(HKEY("WHO:REALHOST"),  1, conditional_who_realhost, CTX_WHO);
 }