* add contexttype, so the subst can precheck the context pointer for us, and bad...
[citadel.git] / webcit / who.c
index 91fe3d5a01d90d4c2ac246ef2215105b361100d1..02f3630e90e716aa982ecd07950fecfa762a26a3 100644 (file)
 /*
  * $Id$
- *
- * Display a list of all users currently logged on to the Citadel server.
  */
 
 #include "webcit.h"
 
+typedef struct UserStateStruct {
+       StrBuf *UserName;
+       StrBuf *Room;
+       StrBuf *Host;
+       StrBuf *RealRoom;
+       StrBuf *RealHost;
+       long LastActive;
+       int Session;
+       int Idle;
+       int SessionCount;
+} UserStateStruct;
+
+void DestroyUserStruct(void *vUser)
+{
+       UserStateStruct *User = (UserStateStruct*) vUser;
+       FreeStrBuf(&User->UserName);
+       FreeStrBuf(&User->Room);
+       FreeStrBuf(&User->Host);
+       FreeStrBuf(&User->RealRoom);
+       FreeStrBuf(&User->RealHost);
+       free(User);
+}
+
+int CompareUserStruct(const void *VUser1, const void *VUser2)
+{
+       const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
+       const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
+       
+       if (User1->Idle != User2->Idle)
+               return User1->Idle > User2->Idle;
+       return strcasecmp(ChrPtr(User1->UserName), 
+                         ChrPtr(User2->UserName));
+}
+
+
+int GetWholistSection(HashList *List, time_t now)
+{
+       StrBuf *Buf, *XBuf;
+       struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
+       UserStateStruct *User, *OldUser;
+       void *VOldUser;
+       size_t BufLen;
+       char buf[SIZ];
+
+       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")) {
+                       if (BufLen <= 0)
+                           continue;
+                       User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
+                       User->Session = StrBufExtract_int(Buf, 0, '|');
+
+                       StrBufExtract_token(XBuf, Buf, 1, '|');
+                       User->UserName = NewStrBufDup(XBuf);
+
+                       StrBufExtract_token(XBuf, Buf, 2, '|');
+                       User->Room = NewStrBufDup(XBuf);
+
+                       StrBufExtract_token(XBuf, Buf, 3, '|');
+                       User->Host = NewStrBufDup(XBuf);
+
+                       StrBufExtract_token(XBuf, Buf, 9, '|');
+                       User->RealRoom = NewStrBufDup(XBuf);
+
+                       StrBufExtract_token(XBuf, Buf, 10, '|');
+                       User->RealHost = NewStrBufDup(XBuf);
+                       
+                       User->LastActive = StrBufExtract_long(Buf, 5, '|');
+                       User->Idle = (now - User->LastActive) > 900L;
+                       User->SessionCount = 1;
+
+                       if (GetHash(List, 
+                                   ChrPtr(User->UserName), 
+                                   StrLength(User->UserName), 
+                                   &VOldUser)) {
+                               OldUser = VOldUser;
+                               OldUser->SessionCount++;
+                               if (!User->Idle) {
+                                       if (User->Session == WCC->ctdl_pid) 
+                                               OldUser->Session = User->Session;
+
+                                       OldUser->Idle = User->Idle;
+                                       OldUser->LastActive = User->LastActive;
+                               }
+                               DestroyUserStruct(User);
+                       }
+                       else
+                               Put(List, 
+                                   ChrPtr(User->UserName), 
+                                   StrLength(User->UserName), 
+                                   User, DestroyUserStruct);
+               }
+               SortByPayload(List, CompareUserStruct);
 
+               FreeStrBuf(&XBuf);
+               FreeStrBuf(&Buf);
+               return 1;
+       }
+       else
+               return 0;
+}
 
 /*
  * Display inner div of Wholist
- */
+ * /
 void who_inner_div(void) {
-       char buf[SIZ], user[SIZ], room[SIZ], host[SIZ],
-               realroom[SIZ], realhost[SIZ];
-       int sess;
-       time_t last_activity;
+       UserStateStruct *User;
+       void *VUser;
+       char buf[SIZ];
+       struct wcsession *WCC = WC;
+       HashList *List;
+       HashPos  *it;
+       const char *UserName;
+       long len;
        time_t now;
        int bg = 0;
-
-       wprintf("<table border=\"0\" cellspacing=\"0\" width=\"100%%\" bgcolor=\"#FFFFFF\">"
+       wprintf("<table class=\"altern\">"
                "<tr>\n");
-       wprintf("<th colspan=\"3\"> </th>\n");
+       wprintf("<th class=\"edit_col\"> </th>\n");
+       wprintf("<th colspan=\"2\"> </th>\n");
        wprintf("<th>%s</th>\n", _("User name"));
        wprintf("<th>%s</th>", _("Room"));
-       wprintf("<th>%s</th>\n</tr>\n", _("From host"));
+       wprintf("<th class=\"host_col\">%s</th>\n</tr>\n", _("From host"));
 
        serv_puts("TIME");
        serv_getln(buf, sizeof buf);
+
        if (buf[0] == '2') {
                now = extract_long(&buf[4], 0);
        }
@@ -35,97 +141,99 @@ void who_inner_div(void) {
                now = time(NULL);
        }
 
-       serv_puts("RWHO");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '1') {
-               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                       sess = extract_int(buf, 0);
-                       extract_token(user, buf, 1, '|', sizeof user);
-                       extract_token(room, buf, 2, '|', sizeof room);
-                       extract_token(host, buf, 3, '|', sizeof host);
-                       extract_token(realroom, buf, 9, '|', sizeof realroom);
-                       extract_token(realhost, buf, 10, '|', sizeof realhost);
-                       last_activity = extract_long(buf, 5);
+       List = NewHash(1, NULL);
 
+       if (GetWholistSection(List, now)) {
+               it = GetNewHashPos();
+               while (GetNextHashPos(List, it, &len, &UserName, &VUser)) {
+                       User = VUser;
                        bg = 1 - bg;
-                       wprintf("<tr bgcolor=\"#%s\">",
-                               (bg ? "DDDDDD" : "FFFFFF")
+                       wprintf("<tr class=\"%s\">",
+                               (bg ? "even" : "odd")
                        );
 
 
-                       wprintf("<td>");
-                       if ((WC->is_aide) &&
-                           (sess != WC->ctdl_pid)) {
-                               wprintf(" <a href=\"/terminate_session?which_session=%d", sess);
+                       wprintf("<td class=\"edit_col\">");
+                       if ((WCC->is_aide) &&
+                           (User->Session != WCC->ctdl_pid)) {
+                               wprintf(" <a href=\"terminate_session?which_session=%d", User->Session);
                                wprintf("\" onClick=\"return ConfirmKill();\">%s</a>", _("(kill)"));
                        }
-                       if (sess == WC->ctdl_pid) {
-                               wprintf(" <a href=\"/edit_me\">%s</a>", _("(edit)"));
+                       if (User->Session == WCC->ctdl_pid) {
+                               wprintf(" <a href=\"edit_me\">%s</a>", _("(edit)"));
                        }
                        wprintf("</td>");
 
-                       /* (link to page this user) */
-                       wprintf("<td><a href=\"/display_page?recp=");
-                       urlescputs(user);
+                       / * (link to page this user) * /
+                       wprintf("<td width=\"5%%\"><a href=\"display_page?recp=");
+                       UrlescPutStrBuf(User->UserName);
                        wprintf("\">"
                                "<img align=\"middle\" "
-                               "src=\"/static/citadelchat_24x.gif\" "
+                               "src=\"static/citadelchat_24x.gif\" "
                                "alt=\"(p)\""
                                " border=\"0\" /></a> ");
                        wprintf("</td>");
 
-                       /* (idle flag) */
-                       wprintf("<td>");
-                       if ((now - last_activity) > 900L) {
+                       / * (idle flag) * /
+                       wprintf("<td width=\"5%%\">");
+                       if (User->Idle) {
                                wprintf(" "
                                        "<img align=\"middle\" "
-                                       "src=\"/static/inactiveuser_24x.gif\" "
-                                       "alt=\"(idle)\" border=\"0\" />");
+                                       "src=\"static/inactiveuser_24x.gif\" "
+                                       "alt=\"(%s %ld %s)\" border=\"0\" />",
+                                       _("idle since"),
+                                       (now - User->LastActive) / 60,
+                                       _("Minutes")
+                                       );
+                               
                        }
                        else {
                                wprintf(" "
                                        "<img align=\"middle\" "
-                                       "src=\"/static/activeuser_24x.gif\" "
+                                       "src=\"static/activeuser_24x.gif\" "
                                        "alt=\"(active)\" border=\"0\" />");
                        }
                        wprintf("</td>\n<td>");
 
-
-
-                       /* username (link to user bio/photo page) */
-                       wprintf("<a href=\"/showuser?who=");
-                       urlescputs(user);
+                       / * username (link to user bio/photo page) * /
+                       wprintf("<a href=\"showuser?who=");
+                       UrlescPutStrBuf(User->UserName);
                        wprintf("\">");
-                       escputs(user);
+                       StrEscPuts(User->UserName);
+                       if (User->SessionCount > 1)
+                               wprintf(" [%d] ", User->SessionCount);
                        wprintf("</a>");
 
-                       /* room */
+                       / * room * /
                        wprintf("</td>\n\t<td>");
-                       escputs(room);
-                       if (strlen(realroom) > 0) {
+                       StrEscPuts(User->Room);
+                       if (StrLength(User->RealRoom) > 0) {
                                wprintf("<br /><i>");
-                               escputs(realroom);
+                               StrEscPuts(User->RealRoom);
                                wprintf("</i>");
                        }
-                       wprintf("</td>\n\t<td>");
+                       wprintf("</td>\n\t<td class=\"host_col\">");
 
-                       /* hostname */
-                       escputs(host);
-                       if (strlen(realhost) > 0) {
+                       / * hostname * /
+                       StrEscPuts(User->Host);
+                       if (StrLength(User->RealHost) > 0) {
                                wprintf("<br /><i>");
-                               escputs(realhost);
+                               StrEscPuts(User->RealHost);
                                wprintf("</i>");
                        }
                        wprintf("</td>\n</tr>");
                }
+               DeleteHashPos(&it);
        }
        wprintf("</table>");
+       DeleteHash(&List);
 }
+*/
 
 
 /*
- * who is on?
- */
+ * Display a list of users currently logged in to the system
+ * /
 void who(void)
 {
        char title[256];
@@ -140,56 +248,64 @@ void who(void)
        );
 
        wprintf("<div id=\"banner\">\n");
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
-       wprintf("<IMG SRC=\"/static/usermanag_48x.gif\" ALT=\" \" "
-               "ALIGN=MIDDLE "
-               ">");
-       wprintf("<SPAN CLASS=\"titlebar\"> ");
-
+       wprintf("<div class=\"room_banner\">");
+       wprintf("<img src=\"static/usermanag_48x.gif\">");
+       wprintf("<h1>");
        snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
        escputs(title);
-
-       wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
+       wprintf("</h1></div>");
+       wprintf("<ul class=\"room_actions\">\n");
+       wprintf("<li class=\"start_page\">");
        offer_start_page();
-       wprintf("</TD></TR></TABLE>\n");
-       wprintf("</div>\n");
+       wprintf("</li></ul>");
+       wprintf("</div>");
 
-       wprintf("<div id=\"content\">\n");
-
-       wprintf("<div style=\"display:inline\" id=\"fix_scrollbar_bug\">");
+       wprintf("<div id=\"content\" class=\"fix_scrollbar_bug who_is_online\">\n");
+       wprintf("<div class=\"box\">");
+       wprintf("<div class=\"boxlabel\">");    
+       snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
+       escputs(title);
+       wprintf("</div>");      
+       wprintf("<div class=\"boxcontent\">");
+        wprintf("<div id=\"who_inner\" >");
        who_inner_div();
-       wprintf("</div>\n");
+       wprintf("</div>");
 
-       wprintf("<div id=\"instructions\" align=center>");
+       wprintf("<div class=\"instructions\">");
        wprintf(_("Click on a name to read user info.  Click on %s "
                "to send an instant message to that user."),
-               "<img align=\"middle\" src=\"/static/citadelchat_16x.gif\" alt=\"(p)\" border=\"0\">"
+               "<img align=\"middle\" src=\"static/citadelchat_16x.gif\" alt=\"(p)\" border=\"0\">"
        );
-       wprintf("</div>\n");
+       wprintf("</div></div>\n");
 
-       /* JavaScript to make the ajax refresh happen:
+       / *
+        * JavaScript to make the ajax refresh happen:
         * See http://www.sergiopereira.com/articles/prototype.js.html for info on Ajax.PeriodicalUpdater
         * It wants: 1. The div being updated
         *           2. The URL of the update source
         *           3. Other flags (such as the HTTP method and the refresh frequency)
-        */
+        * /
        wprintf(
                "<script type=\"text/javascript\">                                      "
-               " new Ajax.PeriodicalUpdater('fix_scrollbar_bug', '/who_inner_html',    "
+               " new Ajax.PeriodicalUpdater('who_inner', 'who_inner_html',     "
                "                            { method: 'get', frequency: 30 }  );       "
                "</script>                                                              \n"
        );
        wDumpContent(1);
 }
+*/
 
-
+/*
+ * end session
+ */
 void terminate_session(void)
 {
        char buf[SIZ];
 
        serv_printf("TERM %s", bstr("which_session"));
        serv_getln(buf, sizeof buf);
-       who();
+       ///who();
+       url_do_template();
 }
 
 
@@ -200,28 +316,28 @@ void edit_me(void)
 {
        char buf[SIZ];
 
-       if (strlen(bstr("change_room_name_button")) > 0) {
+       if (havebstr("change_room_name_button")) {
                serv_printf("RCHG %s", bstr("fake_roomname"));
                serv_getln(buf, sizeof buf);
-               http_redirect("/who");
-       } else if (strlen(bstr("change_host_name_button")) > 0) {
+               http_redirect("who");
+       } else if (havebstr("change_host_name_button")) {
                serv_printf("HCHG %s", bstr("fake_hostname"));
                serv_getln(buf, sizeof buf);
-               http_redirect("/who");
-       } else if (strlen(bstr("change_user_name_button")) > 0) {
+               http_redirect("who");
+       } else if (havebstr("change_user_name_button")) {
                serv_printf("UCHG %s", bstr("fake_username"));
                serv_getln(buf, sizeof buf);
-               http_redirect("/who");
-       } else if (strlen(bstr("cancel_button")) > 0) {
-               http_redirect("/who");
+               http_redirect("who");
+       } else if (havebstr("cancel_button")) {
+               http_redirect("who");
        } else {
                output_headers(1, 1, 0, 0, 0, 0);
 
                wprintf("<div id=\"banner\">\n");
-               wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
-               wprintf("<SPAN CLASS=\"titlebar\">");
+               wprintf("<table class=\"who_banner\"><tr><td>");
+               wprintf("<span class=\"titlebar\">");
                wprintf(_("Edit your session display"));
-               wprintf("</SPAN></TD></TR></TABLE>\n");
+               wprintf("</span></td></tr></table>\n");
                wprintf("</div>\n<div id=\"content\">\n");
 
                wprintf(_("This screen allows you to change the way your "
@@ -231,43 +347,154 @@ void edit_me(void)
                        "without typing anything in the corresponding box. "));
                wprintf("<br />\n");
 
-               wprintf("<FORM METHOD=\"POST\" ACTION=\"/edit_me\">\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("<table border=0 width=100%%>\n");
 
-               wprintf("<TR><TD><B>");
+               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\">",
+               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("</td>\n</tr>\n");
 
-               wprintf("<TR><TD><B>");
+               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\">",
+               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");
+               wprintf("</td>\n</tr>\n");
 
                if (WC->is_aide) {
-                       wprintf("<TR><TD><B>");
+                       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\">",
+                       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("</td>\n</tr>\n");
                }
-               wprintf("<TR><TD> </TD><TD> </TD><TD ALIGN=center>");
-               wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">",
+               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");
+               wprintf("</td></tr></table>\n");
+               wprintf("</form></center>\n");
                wDumpContent(1);
        }
 }
+
+/*
+ * Wholist section
+ * /
+void wholist_section(void) {
+       UserStateStruct *User;
+       void *VUser;
+       HashList *List;
+       HashPos  *it;
+       const char *UserName;
+       long len;
+       char buf[SIZ];
+        time_t now;
+
+       serv_puts("TIME");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '2') {
+               now = extract_long(&buf[4], 0);
+       }
+       else {
+               now = time(NULL);
+       }
+
+       List = NewHash(1, NULL);
+
+       if (GetWholistSection(List, now)) {
+               SortByPayload(List, CompareUserStruct);
+               it = GetNewHashPos();
+               while (GetNextHashPos(List, it, &len, &UserName, &VUser)) {
+                       User = VUser;
+                       if (strcmp(ChrPtr(User->UserName), NLI)) {
+                               wprintf("<li class=\"");
+                               if (User->Idle) {
+                                       wprintf("inactiveuser");
+                               }
+                               else {
+                                       wprintf("activeuser");
+                               }
+                               wprintf("\"><a href=\"showuser?who=");
+                               UrlescPutStrBuf(User->UserName);
+                               wprintf("\">");
+                               StrEscPuts(User->UserName);
+                               wprintf("</a></li>");
+                       }
+               }
+               DeleteHashPos(&it);
+       }
+       DeleteHash(&List);
+}
+*/
+
+void _terminate_session(void) {
+       slrp_highest();
+       terminate_session();
+}
+
+HashList *GetWholistHash(WCTemplateToken *Token)
+{
+       HashList *List;
+       char buf[SIZ];
+        time_t now;
+
+       serv_puts("TIME");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '2') {
+               now = extract_long(&buf[4], 0);
+       }
+       else {
+               now = time(NULL);
+       }
+
+       List = NewHash(1, NULL);
+       GetWholistSection(List, now);
+       return List;
+}
+
+void WholistSubst(StrBuf *TemplBuffer, void *vContext, WCTemplateToken *Token)
+{
+       UserStateStruct *User = (UserStateStruct*) vContext;
+
+       SVPutBuf("WHO:NAME", User->UserName, 1);
+       SVPutBuf("WHO:ROOM", User->Room, 1);
+       SVPutBuf("WHO:HOST", User->Host, 1);
+       SVPutBuf("WHO:REALROOM", User->RealRoom, 1);
+       SVPutBuf("WHO:REALHOST", User->RealHost, 1);
+       svputlong("WHO:LASTACTIVE", User->LastActive);
+       ///svputlong("WHO:IDLESINCE",(now - User->LastActive) / 60);//// todo
+       svputlong("WHO:SESSION", User->Session);
+       svputlong("WHO:IDLE", User->Idle);
+       svputlong("WHO:NSESSIONS", User->SessionCount);
+       svputlong("WHO:ISME", (User->Session == WC->ctdl_pid));
+}
+
+void DeleteWholistHash(HashList **KillMe)
+{
+       DeleteHash(KillMe);
+}
+
+void 
+InitModule_WHO
+(void)
+{
+///    WebcitAddUrlHandler(HKEY("who"), who, 0);
+//     WebcitAddUrlHandler(HKEY("who_inner_html"), who_inner_div, AJAX);
+//     WebcitAddUrlHandler(HKEY("wholist_section"), wholist_section, AJAX);
+       WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
+       WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
+
+       RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, WholistSubst, DeleteWholistHash, CTX_WHO);
+}