X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fwho.c;h=db86d609d4e134e0a39c690deff0615d6f2edce6;hb=f5c1330914acc193f96892efc191a32ee537dfb5;hp=a1b2ca13b538a3c8629a4ade68eec20dde1b9310;hpb=a1b04c57eca8827bf0ef3875265a6a356107091e;p=citadel.git diff --git a/webcit/who.c b/webcit/who.c index a1b2ca13b..db86d609d 100644 --- a/webcit/who.c +++ b/webcit/who.c @@ -1,138 +1,123 @@ -/* $Id$ */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include + #include "webcit.h" +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) +{ + 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) +{ + wcsession *WCC = WC; + UserStateStruct *User, *OldUser; + void *VOldUser; + size_t BufLen; + const char *Pos; -/* - * who is on? - */ -void whobbs(void) -{ - char buf[SIZ], sess, user[SIZ], room[SIZ], host[SIZ], - realroom[SIZ], realhost[SIZ]; - - output_headers(7); - - wprintf("\n" - ); - - wprintf("
"); - wprintf("\""); - wprintf("  Users currently on "); - escputs(serv_info.serv_humannode); - wprintf(""); - offer_start_page(); - wprintf("
\n"); - - wprintf("
\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); - extract(realroom, buf, 9); - extract(realhost, buf, 10); - - wprintf("\n\t\n\t\n\t\n\t\n"); + 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", sess); - if ((WC->is_aide) && - (sess != serv_info.serv_pid)) { - wprintf(" (kill)"); - } - if (sess == serv_info.serv_pid) { - wprintf(" (edit)"); - } - wprintf(""); - - /* (link to page this user) */ - wprintf("" - " "); - - - /* username (link to user bio/photo page) */ - wprintf(""); - escputs(user); - wprintf(""); - - /* room */ - wprintf(""); - escputs(room); - if (strlen(realroom) > 0) { - wprintf("
"); - escputs(realroom); - wprintf(""); - } - wprintf("
"); - - /* hostname */ - escputs(host); - if (strlen(realhost) > 0) { - wprintf("
"); - escputs(realhost); - wprintf(""); + StrBuf_ServGetln(Buf); + if (GetServerStatus(Buf, NULL) == 1) { + while (BufLen = StrBuf_ServGetln(Buf), 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); } - wprintf("
\n" - "Click on a name to read user info. Click on " - "\"(p)\" to send " - "a page (instant message) to that user.
\n"); - wDumpContent(1); } - +/* + * end session + */ void terminate_session(void) { char buf[SIZ]; serv_printf("TERM %s", bstr("which_session")); - serv_gets(buf); - whobbs(); + serv_getln(buf, sizeof buf); + url_do_template(); } @@ -143,63 +128,222 @@ void edit_me(void) { 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); - output_headers(3); + wc_printf("
\n"); + wc_printf("
"); + wc_printf(""); + wc_printf(_("Edit your session display")); + wc_printf("
\n"); + wc_printf("
\n
\n"); - 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"); + 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"); - wprintf("
\n"); + wc_printf("\n"); + wc_printf("\n", WC->nonce); - wprintf("\n"); + wc_printf("
\n"); - wprintf("\n\n\n\n"); + wc_printf("\n\n\n\n"); - wprintf("\n\n\n"); + wc_printf("\n\n\n"); if (WC->is_aide) { - wprintf("\n\n\n"); + wc_printf("\n\n\n"); } - wprintf("
Room name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
"); + wc_printf(_("Room name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change room name")); + wc_printf("
Host name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
"); + wc_printf(_("Host name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change host name")); + wc_printf("
User name:"); - wprintf("\n"); - wprintf(""); - wprintf(""); - wprintf("
"); + wc_printf(_("User name:")); + wc_printf(""); + wc_printf("\n"); + wc_printf(""); + wc_printf("", + _("Change user name")); + wc_printf("
  "); - wprintf(""); - wprintf("
\n"); - - wprintf("
\n"); + wc_printf(" "); + wc_printf("", + _("Cancel")); + wc_printf("\n"); + wc_printf("\n"); wDumpContent(1); } } + +void _terminate_session(void) { + slrp_highest(); + terminate_session(); +} + +HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP) + +{ + 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); + } + + List = NewHash(1, NULL); + GetWholistSection(List, now, Buf); + FreeStrBuf(&Buf); + return List; +} + + +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); +}