]> code.citadel.org Git - citadel.git/blobdiff - webcit/who.c
Remove preprocessor tests for OpenSSL. It's a requirement.
[citadel.git] / webcit / who.c
index f5bac4f9f3c75ec9b8134b3489b77c982cd1f1b9..dee492caf8e641da9c5d82d3117318caa5b5edd8 100644 (file)
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <ctype.h>
-#include <string.h>
+
 #include "webcit.h"
 
-struct whouser {
-       struct whouser *next;
-       int sessionnum;
-       char username[256];
-       char roomname[256];
-       char hostname[256];
-       char clientsoftware[256];
-       };
-       
-/*
- * who is on?
- */
-void whobbs() {
-       struct whouser *wlist = NULL;
-       struct whouser *wptr = NULL;
-       char buf[256],sess,user[256],room[256],host[256];
-       int foundit;
+CtxType CTX_WHO = CTX_NONE;
 
-        printf("HTTP/1.0 200 OK\n");
-        output_headers();
-        wprintf("<HTML><HEAD><TITLE>Who is online?</TITLE>\n");
+typedef struct UserStateStruct {
+       StrBuf *UserName;
+       StrBuf *Room;
+       StrBuf *Host;
+       StrBuf *UserAgent;
+       StrBuf *RealRoom;
+       StrBuf *RealHost;
+       long LastActive;
+       int Session;
+       int Idle;
+       int IdleSince;
+       int SessionCount;
+} UserStateStruct;
 
-       /* Uncomment this line to cause the wholist to auto-refresh */
-       /* wprintf("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"); */
+void DestroyUserStruct(void *vUser)
+{
+       UserStateStruct *User = (UserStateStruct*) vUser;
+       FreeStrBuf(&User->UserName);
+       FreeStrBuf(&User->Room);
+       FreeStrBuf(&User->Host);
+       FreeStrBuf(&User->RealRoom);
+       FreeStrBuf(&User->RealHost);
+       FreeStrBuf(&User->UserAgent);
+       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));
+}
 
-       wprintf("</HEAD><BODY>\n");
 
-        wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=007700><TR><TD>");
-        wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>Users currently on ");
-       escputs(serv_info.serv_humannode);
-        wprintf("</B></FONT></TD></TR></TABLE>\n");
+int GetWholistSection(HashList *List, time_t now, StrBuf *Buf, const char *FilterName, long FNLen)
+{
+       UserStateStruct *User, *OldUser;
+       void *VOldUser;
+       size_t BufLen;
+       const char *Pos;
 
-       wprintf("<CENTER><TABLE border><TR>");
-       wprintf("<TH>Session ID</TH><TH>User Name</TH><TH>Room</TH>");
-       wprintf("<TH>From host</TH></TR>\n");
        serv_puts("RWHO");
-       serv_gets(buf);
-       if (buf[0]=='1') {
-               while(serv_gets(buf), strcmp(buf,"000")) {
-                       sess = extract_int(buf, 0);
-                       extract(user, buf, 1);
-                       extract(room, buf, 2);
-                       extract(host, buf, 3);
-
-                       foundit = 0;
-                       for (wptr = wlist; wptr != NULL; wptr = wptr -> next) {
-                               if (wptr->sessionnum == sess) {
-                                       foundit = 1;
-                                       if (strcasecmp(user, wptr->username)) {
-                                               sprintf(buf, "%cBR%c%s", 
-                                                       LB, RB, user);
-                                               strcat(wptr->username, buf);
-                                               }
-                                       if (strcasecmp(room, wptr->roomname)) {
-                                               sprintf(buf, "%cBR%c%s", 
-                                                       LB, RB, room);
-                                               strcat(wptr->roomname, buf);
-                                               }
-                                       if (strcasecmp(host, wptr->hostname)) {
-                                               sprintf(buf, "%cBR%c%s", 
-                                                       LB, RB, host);
-                                               strcat(wptr->hostname, buf);
-                                               }
-                                       }
-                               }
+       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 = StrBufExtractNext_int(Buf, &Pos, '|');
+
+                       User->UserName = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
+                       
+                       User->Room = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
+
+                       User->Host = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
+
+                       User->UserAgent = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->UserAgent, Buf, &Pos, '|');
+
+                       User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
+                       StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
+
+                       User->RealRoom = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
 
-                       if (foundit == 0) {
-                               wptr = (struct whouser *)
-                                       malloc(sizeof(struct whouser));
-                               wptr->next = wlist;
-                               wlist = wptr;
-                               strcpy(wlist->username, user);
-                               strcpy(wlist->roomname, room);
-                               strcpy(wlist->hostname, host);
-                               wlist->sessionnum = sess;
+                       User->RealHost = NewStrBufPlain(NULL, BufLen);
+                       StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
+                       
+                       User->Idle = (now - User->LastActive) > 900L;
+                       User->IdleSince = (now - User->LastActive) / 60;
+                       User->SessionCount = 1;
+
+                       if (FilterName == NULL) {
+                               if (GetHash(List, 
+                                           SKEY(User->UserName), 
+                                           &VOldUser)) {
+                                       OldUser = VOldUser;
+                                       OldUser->SessionCount++;
+                                       if (!User->Idle) {
+                                               if (User->Session == WC->ctdl_pid) 
+                                                       OldUser->Session = User->Session;
+                                               
+                                               OldUser->Idle = User->Idle;
+                                               OldUser->LastActive = User->LastActive;
+                                       }
+                                       DestroyUserStruct(User);
                                }
+                               else
+                                       Put(List, 
+                                           SKEY(User->UserName), 
+                                           User, DestroyUserStruct);
                        }
-
-               while (wlist != NULL) {
-                       wprintf("<TR><TD>%d</TD><TD>", wlist->sessionnum);
-                       escputs(wlist->username);
-                       wprintf("</TD><TD>");
-                       escputs(wlist->roomname);
-                       wprintf("</TD><TD>");
-                       escputs(wlist->hostname);
-                       wprintf("</TD></TR>\n");
-                       wptr = wlist->next;
-                       free(wlist);
-                       wlist = wptr;
+                       else {
+                               if (strcmp(FilterName, ChrPtr(User->UserName)) == 0)
+                               {
+                                       Put(List, 
+                                           SKEY(User->UserName), 
+                                           User, DestroyUserStruct);
+                               }
+                               else 
+                               {
+                                       DestroyUserStruct(User);
+                               }
                        }
                }
-       wprintf("</TABLE></CENTER>\n");
-        wprintf("</BODY></HTML>\n");
-        wDumpContent();
+               if (FilterName == NULL)
+                       SortByPayload(List, CompareUserStruct);
+               return 1;
+       }
+       else {
+               return 0;
        }
+}
+
+/*
+ * end session
+ */
+void terminate_session(void)
+{
+       char buf[SIZ];
+
+       serv_printf("TERM %s", bstr("which_session"));
+       serv_getln(buf, sizeof buf);
+       url_do_template();
+}
+
+
+void _terminate_session(void) {
+       slrp_highest();
+       terminate_session();
+}
+
+HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
+
+{
+       const char *ch = NULL;
+       int HashUniq = 1;
+       long len;
+       StrBuf *FilterNameStr = NULL;
+       StrBuf *Buf;
+       HashList *List;
+        time_t now;
+
+       Buf = NewStrBuf();
+
+       serv_puts("TIME");
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL)  == 2) {
+               const char *pos = ChrPtr(Buf) + 4;
+               now = StrBufExtractNext_long(Buf, &pos, '|');
+       }
+       else {
+               now = time(NULL);
+       }
+       if (HaveTemplateTokenString(NULL, TP, 2, &ch, &len))
+       {
+               FilterNameStr = NewStrBuf();
+               GetTemplateTokenString(FilterNameStr, TP, 2, &ch, &len);
+               HashUniq = 0;
+       }
+
+       List = NewHash(HashUniq, NULL);
+       GetWholistSection(List, now, Buf, ch, len);
+       FreeStrBuf(&Buf);
+       FreeStrBuf(&FilterNameStr);
+       return List;
+}
+
+
+void DeleteWholistHash(HashList **KillMe)
+{
+       DeleteHash(KillMe);
+}
+
+void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->UserName, 0);
+}
+
+void tmplput_who_UserAgent(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->UserAgent, 0);
+}
+
+void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->Room, 0);
+}
+
+void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->Host, 0);
+}
+
+void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
+}
+int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       return StrLength(User->RealRoom) > 0;
+}
+
+void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendTemplate(Target, TP, User->RealHost, 0);
+}
+int conditional_who_realhost(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       return StrLength(User->RealHost) > 0;
+}
+
+void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendPrintf(Target, "%d", User->LastActive);
+}
+
+void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendPrintf(Target, "%d", User->IdleSince);
+}
+
+void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendPrintf(Target, "%d", User->Session);
+}
+
+int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       return User->Idle;
+}
+
+int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       return User->SessionCount;
+}
+
+void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       StrBufAppendPrintf(Target, "%d", User->SessionCount);
+}
+
+int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
+{
+       UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
+       return (User->Session == WC->ctdl_pid);
+}
+
+void 
+InitModule_WHO
+(void)
+{
+       RegisterCTX(CTX_WHO);
+
+       WebcitAddUrlHandler(HKEY("terminate_session"), "", 0, _terminate_session, 0);
+
+       RegisterIterator("WHOLIST", 1, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
 
+       RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, NULL, CTX_WHO);
+       RegisterNamespace("WHO:USERAGENT",   0, 1, tmplput_who_UserAgent, 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("WHO:IDLE",      1, conditional_who_idle, CTX_WHO);
+       RegisterConditional("WHO:NSESSIONS", 1, conditional_who_nsessions, CTX_WHO);
+       RegisterConditional("WHO:ISME",      1, conditional_who_isme, CTX_WHO);
+       RegisterConditional("WHO:REALROOM",  1, conditional_who_realroom, CTX_WHO);
+       RegisterConditional("WHO:REALHOST",  1, conditional_who_realhost, CTX_WHO);
+}