X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fwho.c;h=9fdd9a90b53838f47377cde67ad00a520bee23eb;hb=fa4703876cbb0330af860bb686d044ddb59dbadb;hp=dadbf041c632f01ba227efa9a28427c77ffbec32;hpb=631d73c1da3de0aceb29a462cbf65bc64605b238;p=citadel.git diff --git a/webcit/who.c b/webcit/who.c index dadbf041c..9fdd9a90b 100644 --- a/webcit/who.c +++ b/webcit/who.c @@ -1,161 +1,126 @@ -/* $Id$ */ - - - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "webcit.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(void) +typedef struct UserStateStruct { + StrBuf *UserName; + StrBuf *Room; + StrBuf *Host; + StrBuf *RealRoom; + StrBuf *RealHost; + long LastActive; + int Session; + int Idle; + int IdleSince; + int SessionCount; +} UserStateStruct; + +void DestroyUserStruct(void *vUser) { - struct whouser *wlist = NULL; - struct whouser *wptr = NULL; - char buf[256], sess, user[256], room[256], host[256]; - int foundit; - - output_headers(7); + 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)); +} - wprintf("\n" - ); - wprintf("
"); - wprintf("Users currently on "); - escputs(serv_info.serv_humannode); - wprintf("
\n"); +int GetWholistSection(HashList *List, time_t now, StrBuf *Buf) +{ + wcsession *WCC = WC; + UserStateStruct *User, *OldUser; + void *VOldUser; + size_t BufLen; + const char *Pos; - wprintf("
\n\n\n"); - wprintf("\n"); - wprintf("\n"); - wprintf(""); - wprintf("\n\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, '|'); + + StrBufSkip_NTokenS(Buf, &Pos, '|', 1); + + User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|'); + StrBufSkip_NTokenS(Buf, &Pos, '|', 3); + + User->RealRoom = NewStrBufPlain(NULL, BufLen); + StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|'); + + 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 (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); } - - 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; - } - } - - while (wlist != NULL) { - wprintf("\n\t\n\t\n\t\n\t\n"); - wptr = wlist->next; - free(wlist); - wlist = wptr; + else + Put(List, + ChrPtr(User->UserName), + StrLength(User->UserName), + User, DestroyUserStruct); } + SortByPayload(List, CompareUserStruct); + return 1; + } + else { + return 0; } - wprintf("
Session IDUser NameRoomFrom host
%d", wlist->sessionnum); - if ((WC->is_aide) && - (wlist->sessionnum != serv_info.serv_pid)) { - wprintf(" sessionnum); - urlescputs(wlist->username); - wprintf("\" onClick=\"return ConfirmKill();\" " - ">(kill)"); - } - if (wlist->sessionnum == serv_info.serv_pid) { - wprintf(" (edit)"); - } - wprintf(""); - /* username */ - escputs(wlist->username); - /* room */ - wprintf(""); - escputs(wlist->roomname); - wprintf(""); - /* hostname */ - escputs(wlist->hostname); - wprintf("
\n

\n"); - wprintf("\n\n
\n"); - wprintf("Close window\n"); - wprintf("
\n
"); - - wDumpContent(1); } - +/* + * end session + */ void terminate_session(void) { - char buf[256]; + char buf[SIZ]; serv_printf("TERM %s", bstr("which_session")); - serv_gets(buf); - whobbs(); + serv_getln(buf, sizeof buf); + url_do_template(); } @@ -164,65 +129,224 @@ void terminate_session(void) */ void edit_me(void) { - char buf[256]; + char buf[SIZ]; - if (!strcasecmp(bstr("sc"), "Change room name")) { + if (havebstr("change_room_name_button")) { serv_printf("RCHG %s", bstr("fake_roomname")); - serv_gets(buf); - http_redirect("/whobbs"); - } else if (!strcasecmp(bstr("sc"), "Change host name")) { + serv_getln(buf, sizeof buf); + http_redirect("who"); + } else if (havebstr("change_host_name_button")) { serv_printf("HCHG %s", bstr("fake_hostname")); - serv_gets(buf); - http_redirect("/whobbs"); - } else if (!strcasecmp(bstr("sc"), "Change user name")) { + serv_getln(buf, sizeof buf); + http_redirect("who"); + } else if (havebstr("change_user_name_button")) { serv_printf("UCHG %s", bstr("fake_username")); - serv_gets(buf); - http_redirect("/whobbs"); - } else if (!strcasecmp(bstr("sc"), "Cancel")) { - http_redirect("/whobbs"); + serv_getln(buf, sizeof buf); + http_redirect("who"); + } else if (havebstr("cancel_button")) { + http_redirect("who"); } else { + output_headers(1, 1, 0, 0, 0, 0); + + wc_printf("
\n"); + wc_printf("
"); + wc_printf(""); + wc_printf(_("Edit your session display")); + wc_printf("
\n"); + wc_printf("
\n
\n"); + + wc_printf(_("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. ")); + wc_printf("
\n"); + + wc_printf("
\n"); + wc_printf("\n", WC->nonce); + + wc_printf("\n"); + + wc_printf("\n\n\n\n"); + + wc_printf("\n\n\n"); - output_headers(3); + if (WC->is_aide) { + wc_printf("\n\n\n"); + } + wc_printf("
"); + wc_printf(_("Room name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change room name")); + wc_printf("
"); + wc_printf(_("Host name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change host name")); + wc_printf("
"); + wc_printf(_("User name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change user name")); + wc_printf("
"); + wc_printf("", + _("Cancel")); + wc_printf("
\n"); + wc_printf("
\n"); + wDumpContent(1); + } +} - wprintf("
"); - wprintf(""); - wprintf("Edit your session display"); - wprintf("
\n"); - wprintf("This screen allows you to change the way your\n"); - wprintf("session appears in the 'Who is online' listing.\n"); - wprintf("To turn off any 'fake' name you've previously\n"); - wprintf("set, simply click the appropriate 'change' button\n"); - wprintf("without typing anything in the corresponding box.\n"); - wprintf("
\n"); +void _terminate_session(void) { + slrp_highest(); + terminate_session(); +} - wprintf("
\n"); +HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP) - wprintf("\n"); +{ + StrBuf *Buf; + HashList *List; + time_t now; - wprintf("\n\n\n\n"); + Buf = NewStrBuf(); - wprintf("\n\n\n"); + 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 (WC->is_aide) { - wprintf("\n\n\n"); - } - wprintf("
Room name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
Host name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
User name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
  "); - wprintf(""); - wprintf("
\n"); + List = NewHash(1, NULL); + GetWholistSection(List, now, Buf); + FreeStrBuf(&Buf); + return List; +} - wprintf("
\n"); - wDumpContent(1); - } + +void DeleteWholistHash(HashList **KillMe) +{ + DeleteHash(KillMe); +} + +void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendTemplate(Target, TP, User->UserName, 0); +} + +void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendTemplate(Target, TP, User->Room, 0); +} + +void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendTemplate(Target, TP, User->Host, 0); +} + +void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendTemplate(Target, TP, User->RealRoom, 0); +} +int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + return StrLength(User->RealRoom) > 0; +} + +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) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendPrintf(Target, "%d", User->LastActive); +} + +void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendPrintf(Target, "%d", User->IdleSince); +} + +void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendPrintf(Target, "%d", User->Session); +} + +int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + return User->Idle; +} + +int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + return User->SessionCount; +} + +void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + StrBufAppendPrintf(Target, "%d", User->SessionCount); +} + +int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP) +{ + UserStateStruct *User = (UserStateStruct*) CTX; + return (User->Session == WC->ctdl_pid); +} + +void +InitModule_WHO +(void) +{ + + + 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, 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); }