Back to index

webcit  8.12-dfsg
marchlist.c
Go to the documentation of this file.
00001 #include "webcit.h"
00002 #include "webserver.h"
00003 
00004 /*
00005  * Free a session's march list
00006  */
00007 void free_march_list(wcsession *wcf)
00008 {
00009        struct march *mptr;
00010 
00011        while (wcf->march != NULL) {
00012               mptr = wcf->march->next;
00013               free(wcf->march);
00014               wcf->march = mptr;
00015        }
00016 }
00017 
00018 
00019 
00020 /*
00021  * remove a room from the march list
00022  */
00023 void remove_march(const StrBuf *aaa)
00024 {
00025        struct march *mptr, *mptr2;
00026 
00027        if (WC->march == NULL)
00028               return;
00029 
00030        if (!strcasecmp(WC->march->march_name, ChrPtr(aaa))) {
00031               mptr = WC->march->next;
00032               free(WC->march);
00033               WC->march = mptr;
00034               return;
00035        }
00036        mptr2 = WC->march;
00037        for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
00038               if (!strcasecmp(mptr->march_name, ChrPtr(aaa))) {
00039                      mptr2->next = mptr->next;
00040                      free(mptr);
00041                      mptr = mptr2;
00042               } else {
00043                      mptr2 = mptr;
00044               }
00045        }
00046 }
00047 
00048 
00049 
00057 char *pop_march(int desired_floor)
00058 {
00059        static char TheRoom[128];
00060        int TheWeight = 0;
00061        int weight;
00062        struct march *mptr = NULL;
00063 
00064        strcpy(TheRoom, "_BASEROOM_");
00065        if (WC->march == NULL)
00066               return (TheRoom);
00067 
00068        for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
00069               weight = 0;
00070               if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
00071                      weight = weight + 10000;
00072               if (mptr->march_floor == desired_floor)
00073                      weight = weight + 5000;
00074 
00075               weight = weight + ((128 - (mptr->march_floor)) * 128);
00076               weight = weight + (128 - (mptr->march_order));
00077 
00078               if (weight > TheWeight) {
00079                      TheWeight = weight;
00080                      strcpy(TheRoom, mptr->march_name);
00081 /* TODOO: and now????
00082                      TheFloor = mptr->march_floor;
00083                      TheOrder = mptr->march_order;
00084 */
00085               }
00086        }
00087        return (TheRoom);
00088 }
00089 
00090 
00091 
00092 /*
00093  * Goto next room having unread messages.
00094  *
00095  * We want to skip over rooms that the user has already been to, and take the
00096  * user back to the lobby when done.  The room we end up in is placed in
00097  * newroom - which is set to 0 (the lobby) initially.
00098  * We start the search in the current room rather than the beginning to prevent
00099  * two or more concurrent users from dragging each other back to the same room.
00100  */
00101 void gotonext(void)
00102 {
00103        char buf[256];
00104        struct march *mptr = NULL;
00105        struct march *mptr2 = NULL;
00106        char room_name[128];
00107        StrBuf *next_room;
00108        int ELoop = 0;
00109 
00110        /*
00111         * First check to see if the march-mode list is already allocated.
00112         * If it is, pop the first room off the list and go there.
00113         */
00114        if (havebstr("startmsg")) {
00115               readloop(readnew, eUseDefault);
00116               return;
00117        }
00118 
00119        if (WC->march == NULL) {
00120               serv_puts("LKRN");
00121               serv_getln(buf, sizeof buf);
00122               if (buf[0] == '1')
00123                      while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00124                             if (IsEmptyStr(buf)) {
00125                                    if (ELoop > 10000)
00126                                           return;
00127                                    if (ELoop % 100 == 0)
00128                                           sleeeeeeeeeep(1);
00129                                    ELoop ++;
00130                                    continue;                                 
00131                             }
00132                             extract_token(room_name, buf, 0, '|', sizeof room_name);
00133                             if (strcasecmp(room_name, ChrPtr(WC->CurRoom.name))) {
00134                                    mptr = (struct march *) malloc(sizeof(struct march));
00135                                    mptr->next = NULL;
00136                                    safestrncpy(mptr->march_name, room_name, sizeof mptr->march_name);
00137                                    mptr->march_floor = extract_int(buf, 2);
00138                                    mptr->march_order = extract_int(buf, 3);
00139                                    if (WC->march == NULL) 
00140                                           WC->march = mptr;
00141                                    else 
00142                                           mptr2->next = mptr;
00143                                    mptr2 = mptr;
00144                             }
00145                             buf[0] = '\0';
00146                      }
00147               /*
00148                * add _BASEROOM_ to the end of the march list, so the user will end up
00149                * in the system base room (usually the Lobby>) at the end of the loop
00150                */
00151               mptr = (struct march *) malloc(sizeof(struct march));
00152               mptr->next = NULL;
00153               mptr->march_order = 0;
00154               mptr->march_floor = 0;
00155               strcpy(mptr->march_name, "_BASEROOM_");
00156               if (WC->march == NULL) {
00157                      WC->march = mptr;
00158               } else {
00159                      mptr2 = WC->march;
00160                      while (mptr2->next != NULL)
00161                             mptr2 = mptr2->next;
00162                      mptr2->next = mptr;
00163               }
00164               /*
00165                * ...and remove the room we're currently in, so a <G>oto doesn't make us
00166                * walk around in circles
00167                */
00168               remove_march(WC->CurRoom.name);
00169        }
00170        if (WC->march != NULL) {
00171               next_room = NewStrBufPlain(pop_march(-1), -1);/*TODO: migrate march to strbuf */
00172        } else {
00173               next_room = NewStrBufPlain(HKEY("_BASEROOM_"));
00174        }
00175 
00176 
00177        smart_goto(next_room);
00178        FreeStrBuf(&next_room);
00179 }
00180 
00181 /*
00182  * un-goto the previous room
00183  */
00184 void ungoto(void)
00185 {
00186        StrBuf *Buf;
00187 
00188        if (havebstr("startmsg")) {
00189               readloop(readnew, eUseDefault);
00190               return;
00191        }
00192 
00193        if (!strcmp(WC->ugname, "")) {
00194               smart_goto(WC->CurRoom.name);
00195               return;
00196        }
00197        serv_printf("GOTO %s", WC->ugname);
00198        Buf = NewStrBuf();
00199        StrBuf_ServGetln(Buf);
00200        if (GetServerStatus(Buf, NULL) != 2) {
00201               smart_goto(WC->CurRoom.name);
00202               FreeStrBuf(&Buf);
00203               return;
00204        }
00205        if (WC->uglsn >= 0L) {
00206               serv_printf("SLRP %ld", WC->uglsn);
00207               StrBuf_ServGetln(Buf);
00208        }
00209        FlushStrBuf(Buf);
00210        StrBufAppendBufPlain(Buf, WC->ugname, -1, 0);
00211        strcpy(WC->ugname, "");
00212        smart_goto(Buf);
00213        FreeStrBuf(&Buf);
00214 }
00215 
00216 
00217 
00218 void tmplput_ungoto(StrBuf *Target, WCTemplputParams *TP)
00219 {
00220        wcsession *WCC = WC;
00221 
00222        if ((WCC!=NULL) && 
00223            (!IsEmptyStr(WCC->ugname)))
00224               StrBufAppendBufPlain(Target, WCC->ugname, -1, 0);
00225 }
00226 
00227 void _gotonext(void) {
00228        slrp_highest();
00229        gotonext();
00230 }
00231 
00232 
00233 
00234 
00235 int ConditionalHaveUngoto(StrBuf *Target, WCTemplputParams *TP)
00236 {
00237        wcsession *WCC = WC;
00238        
00239        return ((WCC!=NULL) && 
00240               (!IsEmptyStr(WCC->ugname)) && 
00241               (strcasecmp(WCC->ugname, ChrPtr(WCC->CurRoom.name)) == 0));
00242 }
00243 
00244 
00245 void 
00246 InitModule_MARCHLIST
00247 (void)
00248 {
00249        RegisterConditional(HKEY("COND:UNGOTO"), 0, ConditionalHaveUngoto, CTX_NONE);
00250        RegisterNamespace("ROOM:UNGOTO", 0, 0, tmplput_ungoto, NULL, CTX_NONE);
00251 
00252        WebcitAddUrlHandler(HKEY("gotonext"), "", 0, _gotonext, NEED_URL);
00253        WebcitAddUrlHandler(HKEY("skip"), "", 0, gotonext, NEED_URL);
00254        WebcitAddUrlHandler(HKEY("ungoto"), "", 0, ungoto, NEED_URL);
00255 }