]> code.citadel.org Git - citadel.git/blob - webcit/marchlist.c
fix various incidents reported by CLANG Static analyzer:
[citadel.git] / webcit / marchlist.c
1 #include "webcit.h"
2 #include "webserver.h"
3
4 /*
5  * Free a session's march list
6  */
7 void free_march_list(wcsession *wcf)
8 {
9         struct march *mptr;
10
11         while (wcf->march != NULL) {
12                 mptr = wcf->march->next;
13                 free(wcf->march);
14                 wcf->march = mptr;
15         }
16
17 }
18
19
20
21 /*
22  * remove a room from the march list
23  */
24 void remove_march(const StrBuf *aaa)
25 {
26         struct march *mptr, *mptr2;
27
28         if (WC->march == NULL)
29                 return;
30
31         if (!strcasecmp(WC->march->march_name, ChrPtr(aaa))) {
32                 mptr = WC->march->next;
33                 free(WC->march);
34                 WC->march = mptr;
35                 return;
36         }
37         mptr2 = WC->march;
38         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
39                 if (!strcasecmp(mptr->march_name, ChrPtr(aaa))) {
40                         mptr2->next = mptr->next;
41                         free(mptr);
42                         mptr = mptr2;
43                 } else {
44                         mptr2 = mptr;
45                 }
46         }
47 }
48
49
50
51 /**
52  * \brief Locate the room on the march list which we most want to go to.  
53  * Each room
54  * is measured given a "weight" of preference based on various factors.
55  * \param desired_floor the room number on the citadel server
56  * \return the roomname
57  */
58 char *pop_march(int desired_floor)
59 {
60         static char TheRoom[128];
61         int TheWeight = 0;
62         int weight;
63         struct march *mptr = NULL;
64
65         strcpy(TheRoom, "_BASEROOM_");
66         if (WC->march == NULL)
67                 return (TheRoom);
68
69         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
70                 weight = 0;
71                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
72                         weight = weight + 10000;
73                 if (mptr->march_floor == desired_floor)
74                         weight = weight + 5000;
75
76                 weight = weight + ((128 - (mptr->march_floor)) * 128);
77                 weight = weight + (128 - (mptr->march_order));
78
79                 if (weight > TheWeight) {
80                         TheWeight = weight;
81                         strcpy(TheRoom, mptr->march_name);
82                 }
83         }
84         return (TheRoom);
85 }
86
87
88
89 /*
90  * Goto next room having unread messages.
91  *
92  * We want to skip over rooms that the user has already been to, and take the
93  * user back to the lobby when done.  The room we end up in is placed in
94  * newroom - which is set to 0 (the lobby) initially.
95  * We start the search in the current room rather than the beginning to prevent
96  * two or more concurrent users from dragging each other back to the same room.
97  */
98 void gotonext(void)
99 {
100         char buf[256];
101         struct march *mptr = NULL;
102         struct march *mptr2 = NULL;
103         char room_name[128];
104         StrBuf *next_room;
105         int ELoop = 0;
106
107         /*
108          * First check to see if the march-mode list is already allocated.
109          * If it is, pop the first room off the list and go there.
110          */
111         if (havebstr("startmsg")) {
112                 readloop(readnew, eUseDefault);
113                 return;
114         }
115
116         if (WC->march == NULL) {
117                 serv_puts("LKRN");
118                 serv_getln(buf, sizeof buf);
119                 if (buf[0] == '1')
120                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
121                                 if (IsEmptyStr(buf)) {
122                                         if (ELoop > 10000)
123                                                 return;
124                                         if (ELoop % 100 == 0)
125                                                 sleeeeeeeeeep(1);
126                                         ELoop ++;
127                                         continue;                                       
128                                 }
129                                 extract_token(room_name, buf, 0, '|', sizeof room_name);
130                                 if (strcasecmp(room_name, ChrPtr(WC->CurRoom.name))) {
131                                         mptr = (struct march *) malloc(sizeof(struct march));
132                                         mptr->next = NULL;
133                                         safestrncpy(mptr->march_name, room_name, sizeof mptr->march_name);
134                                         mptr->march_floor = extract_int(buf, 2);
135                                         mptr->march_order = extract_int(buf, 3);
136                                         if (WC->march == NULL) 
137                                                 WC->march = mptr;
138                                         else 
139                                                 mptr2->next = mptr;
140                                         mptr2 = mptr;
141                                 }
142                                 buf[0] = '\0';
143                         }
144                 /*
145                  * add _BASEROOM_ to the end of the march list, so the user will end up
146                  * in the system base room (usually the Lobby>) at the end of the loop
147                  */
148                 mptr = (struct march *) malloc(sizeof(struct march));
149                 mptr->next = NULL;
150                 mptr->march_order = 0;
151                 mptr->march_floor = 0;
152                 strcpy(mptr->march_name, "_BASEROOM_");
153                 if (WC->march == NULL) {
154                         WC->march = mptr;
155                 } else {
156                         mptr2 = WC->march;
157                         while (mptr2->next != NULL)
158                                 mptr2 = mptr2->next;
159                         mptr2->next = mptr;
160                 }
161                 /*
162                  * ...and remove the room we're currently in, so a <G>oto doesn't make us
163                  * walk around in circles
164                  */
165                 remove_march(WC->CurRoom.name);
166         }
167         if (WC->march != NULL) {
168                 next_room = NewStrBufPlain(pop_march(-1), -1);/*TODO: migrate march to strbuf */
169         } else {
170                 next_room = NewStrBufPlain(HKEY("_BASEROOM_"));
171         }
172
173
174         smart_goto(next_room);
175         FreeStrBuf(&next_room);
176 }
177
178 /*
179  * un-goto the previous room
180  */
181 void ungoto(void)
182 {
183         StrBuf *Buf;
184
185         if (havebstr("startmsg")) {
186                 readloop(readnew, eUseDefault);
187                 return;
188         }
189
190         if (!strcmp(WC->ugname, "")) {
191                 smart_goto(WC->CurRoom.name);
192                 return;
193         }
194         serv_printf("GOTO %s", WC->ugname);
195         Buf = NewStrBuf();
196         StrBuf_ServGetln(Buf);
197         if (GetServerStatus(Buf, NULL) != 2) {
198                 smart_goto(WC->CurRoom.name);
199                 FreeStrBuf(&Buf);
200                 return;
201         }
202         if (WC->uglsn >= 0L) {
203                 serv_printf("SLRP %ld", WC->uglsn);
204                 StrBuf_ServGetln(Buf);
205         }
206         FlushStrBuf(Buf);
207         StrBufAppendBufPlain(Buf, WC->ugname, -1, 0);
208         strcpy(WC->ugname, "");
209         smart_goto(Buf);
210         FreeStrBuf(&Buf);
211 }
212
213
214
215 void _gotonext(void) {
216         slrp_highest();
217         gotonext();
218 }
219
220
221 void dotskip(void) {
222         smart_goto(sbstr("room"));
223 }
224
225
226
227
228 void 
229 InitModule_MARCHLIST
230 (void)
231 {
232
233         WebcitAddUrlHandler(HKEY("gotonext"), "", 0, _gotonext, NEED_URL);
234         WebcitAddUrlHandler(HKEY("skip"), "", 0, gotonext, NEED_URL);
235         WebcitAddUrlHandler(HKEY("ungoto"), "", 0, ungoto, NEED_URL);
236 }