1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include "webcit.h"
CtxType CTX_WHO = CTX_NONE;
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;
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));
}
int GetWholistSection(HashList *List, time_t now, StrBuf *Buf, const char *FilterName, long FNLen)
{
wcsession *WCC = WC;
UserStateStruct *User, *OldUser;
void *VOldUser;
size_t BufLen;
const char *Pos;
serv_puts("RWHO");
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, '|');
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 == WCC->ctdl_pid)
OldUser->Session = User->Session;
OldUser->Idle = User->Idle;
OldUser->LastActive = User->LastActive;
}
DestroyUserStruct(User);
}
else
Put(List,
SKEY(User->UserName),
User, DestroyUserStruct);
}
else {
if (strcmp(FilterName, ChrPtr(User->UserName)) == 0)
{
Put(List,
SKEY(User->UserName),
User, DestroyUserStruct);
}
else
{
DestroyUserStruct(User);
}
}
}
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();
}
/*
* Change your session info (fake roomname and hostname)
*/
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);
do_template("who");
} else if (havebstr("change_host_name_button")) {
serv_printf("HCHG %s", bstr("fake_hostname"));
serv_getln(buf, sizeof buf);
do_template("who");
} else if (havebstr("change_user_name_button")) {
serv_printf("UCHG %s", bstr("fake_username"));
serv_getln(buf, sizeof buf);
do_template("who");
} else if (havebstr("cancel_button")) {
do_template("who");
} else {
do_template("who_edit");
}
end_burst();
}
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);
WebcitAddUrlHandler(HKEY("edit_me"), "", 0, edit_me, 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);
}
|