Back to index

webcit  8.12-dfsg
who.c
Go to the documentation of this file.
00001 
00002 #include "webcit.h"
00003 
00004 typedef struct UserStateStruct {
00005        StrBuf *UserName;
00006        StrBuf *Room;
00007        StrBuf *Host;
00008        StrBuf *RealRoom;
00009        StrBuf *RealHost;
00010        long LastActive;
00011        int Session;
00012        int Idle;
00013        int IdleSince;
00014        int SessionCount;
00015 } UserStateStruct;
00016 
00017 void DestroyUserStruct(void *vUser)
00018 {
00019        UserStateStruct *User = (UserStateStruct*) vUser;
00020        FreeStrBuf(&User->UserName);
00021        FreeStrBuf(&User->Room);
00022        FreeStrBuf(&User->Host);
00023        FreeStrBuf(&User->RealRoom);
00024        FreeStrBuf(&User->RealHost);
00025        free(User);
00026 }
00027 
00028 int CompareUserStruct(const void *VUser1, const void *VUser2)
00029 {
00030        const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
00031        const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
00032        
00033        if (User1->Idle != User2->Idle)
00034               return User1->Idle > User2->Idle;
00035        return strcasecmp(ChrPtr(User1->UserName), 
00036                        ChrPtr(User2->UserName));
00037 }
00038 
00039 
00040 int GetWholistSection(HashList *List, time_t now, StrBuf *Buf)
00041 {
00042        wcsession *WCC = WC;
00043        UserStateStruct *User, *OldUser;
00044        void *VOldUser;
00045        size_t BufLen;
00046        const char *Pos;
00047 
00048        serv_puts("RWHO");
00049        StrBuf_ServGetln(Buf);
00050        if (GetServerStatus(Buf, NULL) == 1) {
00051               while (BufLen = StrBuf_ServGetln(Buf), 
00052                      ((BufLen >= 0) && 
00053                      ((BufLen != 3) || strcmp(ChrPtr(Buf), "000"))))
00054               {
00055                      if (BufLen <= 0)
00056                          continue;
00057                      Pos = NULL;
00058                      User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
00059                      User->Session = StrBufExtractNext_int(Buf, &Pos, '|');
00060 
00061                      User->UserName = NewStrBufPlain(NULL, BufLen);
00062                      StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
00063                      
00064                      User->Room = NewStrBufPlain(NULL, BufLen);
00065                      StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
00066 
00067                      User->Host = NewStrBufPlain(NULL, BufLen);
00068                      StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
00069 
00070                      StrBufSkip_NTokenS(Buf, &Pos, '|', 1);
00071 
00072                      User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
00073                      StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
00074 
00075                      User->RealRoom = NewStrBufPlain(NULL, BufLen);
00076                      StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
00077 
00078                      User->RealHost = NewStrBufPlain(NULL, BufLen);
00079                      StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
00080                      
00081                      User->Idle = (now - User->LastActive) > 900L;
00082                      User->IdleSince = (now - User->LastActive) / 60;
00083                      User->SessionCount = 1;
00084 
00085                      if (GetHash(List, 
00086                                 ChrPtr(User->UserName), 
00087                                 StrLength(User->UserName), 
00088                                 &VOldUser)) {
00089                             OldUser = VOldUser;
00090                             OldUser->SessionCount++;
00091                             if (!User->Idle) {
00092                                    if (User->Session == WCC->ctdl_pid) 
00093                                           OldUser->Session = User->Session;
00094 
00095                                    OldUser->Idle = User->Idle;
00096                                    OldUser->LastActive = User->LastActive;
00097                             }
00098                             DestroyUserStruct(User);
00099                      }
00100                      else
00101                             Put(List, 
00102                                 ChrPtr(User->UserName), 
00103                                 StrLength(User->UserName), 
00104                                 User, DestroyUserStruct);
00105               }
00106               SortByPayload(List, CompareUserStruct);
00107               return 1;
00108        }
00109        else {
00110               return 0;
00111        }
00112 }
00113 
00114 /*
00115  * end session
00116  */
00117 void terminate_session(void)
00118 {
00119        char buf[SIZ];
00120 
00121        serv_printf("TERM %s", bstr("which_session"));
00122        serv_getln(buf, sizeof buf);
00123        url_do_template();
00124 }
00125 
00126 
00127 /*
00128  * Change your session info (fake roomname and hostname)
00129  */
00130 void edit_me(void)
00131 {
00132        char buf[SIZ];
00133 
00134        if (havebstr("change_room_name_button")) {
00135               serv_printf("RCHG %s", bstr("fake_roomname"));
00136               serv_getln(buf, sizeof buf);
00137               http_redirect("who");
00138        } else if (havebstr("change_host_name_button")) {
00139               serv_printf("HCHG %s", bstr("fake_hostname"));
00140               serv_getln(buf, sizeof buf);
00141               http_redirect("who");
00142        } else if (havebstr("change_user_name_button")) {
00143               serv_printf("UCHG %s", bstr("fake_username"));
00144               serv_getln(buf, sizeof buf);
00145               http_redirect("who");
00146        } else if (havebstr("cancel_button")) {
00147               http_redirect("who");
00148        } else {
00149               output_headers(1, 1, 0, 0, 0, 0);
00150 
00151               wc_printf("<div id=\"banner\">\n");
00152               wc_printf("<table class=\"who_banner\"><tr><td>");
00153               wc_printf("<span class=\"titlebar\">");
00154               wc_printf(_("Edit your session display"));
00155               wc_printf("</span></td></tr></table>\n");
00156               wc_printf("</div>\n<div id=\"content\">\n");
00157 
00158               wc_printf(_("This screen allows you to change the way your "
00159                      "session appears in the 'Who is online' listing. "
00160                      "To turn off any 'fake' name you've previously "
00161                      "set, simply click the appropriate 'change' button "
00162                      "without typing anything in the corresponding box. "));
00163               wc_printf("<br>\n");
00164 
00165               wc_printf("<form method=\"POST\" action=\"edit_me\">\n");
00166               wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
00167 
00168               wc_printf("<table border=0 width=100%%>\n");
00169 
00170               wc_printf("<tr><td><b>");
00171               wc_printf(_("Room name:"));
00172               wc_printf("</b></td>\n<td>");
00173               wc_printf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
00174               wc_printf("</td>\n<td align=center>");
00175               wc_printf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
00176                      _("Change room name"));
00177               wc_printf("</td>\n</tr>\n");
00178 
00179               wc_printf("<tr><td><b>");
00180               wc_printf(_("Host name:"));
00181               wc_printf("</b></td><td>");
00182               wc_printf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
00183               wc_printf("</td>\n<td align=center>");
00184               wc_printf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
00185                      _("Change host name"));
00186               wc_printf("</td>\n</tr>\n");
00187 
00188               if (WC->is_aide) {
00189                      wc_printf("<tr><td><b>");
00190                      wc_printf(_("User name:"));
00191                      wc_printf("</b></td><td>");
00192                      wc_printf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
00193                      wc_printf("</td>\n<td align=center>");
00194                      wc_printf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
00195                             _("Change user name"));
00196                      wc_printf("</td>\n</tr>\n");
00197               }
00198               wc_printf("<tr><td> </td><td> </td><td align=center>");
00199               wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
00200                      _("Cancel"));
00201               wc_printf("</td></tr></table>\n");
00202               wc_printf("</form></center>\n");
00203               wDumpContent(1);
00204        }
00205 }
00206 
00207 void _terminate_session(void) {
00208        slrp_highest();
00209        terminate_session();
00210 }
00211 
00212 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
00213 
00214 {
00215        StrBuf *Buf;
00216        HashList *List;
00217         time_t now;
00218 
00219        Buf = NewStrBuf();
00220 
00221        serv_puts("TIME");
00222        StrBuf_ServGetln(Buf);
00223        if (GetServerStatus(Buf, NULL)  == 2) {
00224               const char *pos = ChrPtr(Buf) + 4;
00225               now = StrBufExtractNext_long(Buf, &pos, '|');
00226        }
00227        else {
00228               now = time(NULL);
00229        }
00230 
00231        List = NewHash(1, NULL);
00232        GetWholistSection(List, now, Buf);
00233        FreeStrBuf(&Buf);
00234        return List;
00235 }
00236 
00237 
00238 void DeleteWholistHash(HashList **KillMe)
00239 {
00240        DeleteHash(KillMe);
00241 }
00242 
00243 void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
00244 {
00245        UserStateStruct *User = (UserStateStruct*) CTX;
00246        StrBufAppendTemplate(Target, TP, User->UserName, 0);
00247 }
00248 
00249 void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
00250 {
00251        UserStateStruct *User = (UserStateStruct*) CTX;
00252        StrBufAppendTemplate(Target, TP, User->Room, 0);
00253 }
00254 
00255 void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
00256 {
00257        UserStateStruct *User = (UserStateStruct*) CTX;
00258        StrBufAppendTemplate(Target, TP, User->Host, 0);
00259 }
00260 
00261 void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
00262 {
00263        UserStateStruct *User = (UserStateStruct*) CTX;
00264        StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
00265 }
00266 int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
00267 {
00268        UserStateStruct *User = (UserStateStruct*) CTX;
00269        return StrLength(User->RealRoom) > 0;
00270 }
00271 
00272 void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
00273 {
00274        UserStateStruct *User = (UserStateStruct*) CTX;
00275        StrBufAppendTemplate(Target, TP, User->RealHost, 0);
00276 }
00277 int conditional_who_realhost(StrBuf *Target, WCTemplputParams *TP)
00278 {
00279        UserStateStruct *User = (UserStateStruct*) CTX;
00280        return StrLength(User->RealHost) > 0;
00281 }
00282 
00283 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
00284 {
00285        UserStateStruct *User = (UserStateStruct*) CTX;
00286        StrBufAppendPrintf(Target, "%d", User->LastActive);
00287 }
00288 
00289 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
00290 {
00291        UserStateStruct *User = (UserStateStruct*) CTX;
00292        StrBufAppendPrintf(Target, "%d", User->IdleSince);
00293 }
00294 
00295 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
00296 {
00297        UserStateStruct *User = (UserStateStruct*) CTX;
00298        StrBufAppendPrintf(Target, "%d", User->Session);
00299 }
00300 
00301 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
00302 {
00303        UserStateStruct *User = (UserStateStruct*) CTX;
00304        return User->Idle;
00305 }
00306 
00307 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
00308 {
00309        UserStateStruct *User = (UserStateStruct*) CTX;
00310        return User->SessionCount;
00311 }
00312 
00313 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
00314 {
00315        UserStateStruct *User = (UserStateStruct*) CTX;
00316        StrBufAppendPrintf(Target, "%d", User->SessionCount);
00317 }
00318 
00319 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
00320 {
00321        UserStateStruct *User = (UserStateStruct*) CTX;
00322        return (User->Session == WC->ctdl_pid);
00323 }
00324 
00325 void 
00326 InitModule_WHO
00327 (void)
00328 {
00329        
00330 
00331        WebcitAddUrlHandler(HKEY("terminate_session"), "", 0, _terminate_session, 0);
00332        WebcitAddUrlHandler(HKEY("edit_me"), "", 0, edit_me, 0);
00333 
00334        RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
00335 
00336        RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, NULL, CTX_WHO);
00337        RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, NULL, CTX_WHO);
00338        RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, NULL, CTX_WHO);
00339        RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, NULL, CTX_WHO);
00340        RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, NULL, CTX_WHO);
00341        RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, NULL, CTX_WHO);
00342        RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, NULL, CTX_WHO);
00343        RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, NULL, CTX_WHO);
00344        RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
00345        RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
00346 
00347        RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
00348        RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
00349        RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
00350        RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
00351        RegisterConditional(HKEY("WHO:REALHOST"),  1, conditional_who_realhost, CTX_WHO);
00352 }