Remove APPDIR support in database_cleanup.sh
[citadel] / webcit / bbsview_renderer.c
1 /* 
2  * BBS View renderer module for WebCit
3  *
4  * Note: we briefly had a dynamic UI for this.  I thought it was cool, but
5  * it was not received well by the user community.  If you want to play
6  * with it, go get commit dcf99fe61379b78436c387ea3f89ebfd4ffaf635 of
7  * bbsview_renderer.c and have fun.
8  *
9  * Copyright (c) 1996-2012 by the citadel.org team
10  *
11  * This program is open source software.  You can redistribute it and/or
12  * modify it under the terms of the GNU General Public License, version 3.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #define RANGE 5
21
22 #include "webcit.h"
23 #include "webserver.h"
24 #include "dav.h"
25
26 /*
27  * Data which gets passed around between the various functions in this module
28  *
29  */
30 struct bbsview {
31         long *msgs;             /* Array of msgnums for messages we are displaying */
32         int num_msgs;           /* Number of msgnums stored in 'msgs' */
33         long lastseen;          /* The number of the last seen message in this room */
34         int alloc_msgs;         /* Currently allocated size of array */
35         int requested_page;     /* Which page number did the user request? */
36         int num_pages;          /* Total number of pages in this room */
37         long start_reading_at;  /* Start reading at the page containing this message */
38 };
39
40
41 /*
42  * Attempt to determine the closest thing to the "last seen message number" using the
43  * results of the GTSN command
44  */
45 long bbsview_get_last_seen(void)
46 {
47         char buf[SIZ] = "0";
48
49         serv_puts("GTSN");
50         serv_getln(buf, sizeof buf);
51         if (buf[0] == '2') {
52                 char *colon_pos;
53                 char *comma_pos;
54
55                 comma_pos = strchr(buf, ',');   /* kill first comma and everything to its right */
56                 if (comma_pos) {
57                         *comma_pos = 0;
58                 }
59
60                 colon_pos = strchr(buf, ':');   /* kill first colon and everything to its left */
61                 if (colon_pos) {
62                         strcpy(buf, ++colon_pos);
63                 }
64         }
65
66         return(atol(buf));
67 }
68
69
70
71 /*
72  * Entry point for message read operations.
73  */
74 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
75                                    void **ViewSpecific, 
76                                    long oper, 
77                                    char *cmd, 
78                                    long len,
79                                    char *filter,
80                                    long flen)
81 {
82         struct bbsview *BBS = malloc(sizeof(struct bbsview));
83         memset(BBS, 0, sizeof(struct bbsview));
84         *ViewSpecific = BBS;
85
86         Stat->startmsg = (-1);                                  /* not used here */
87         Stat->sortit = 1;                                       /* not used here */
88         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
89         BBS->requested_page = 0;
90         BBS->lastseen = bbsview_get_last_seen();
91         BBS->start_reading_at = 0;
92
93         /* By default, the requested page is the first one. */
94         if (havebstr("start_reading_at")) {
95                 BBS->start_reading_at = lbstr("start_reading_at");
96                 BBS->requested_page = (-4);
97         }
98
99         /* However, if we are asked to start with a specific message number, make sure
100          * we start on the page containing that message
101          */
102
103         /* Or, if a specific page was requested, make sure we go there */
104         else if (havebstr("page")) {
105                 BBS->requested_page = ibstr("page");
106         }
107
108         /* Otherwise, if this is a "read new" operation, make sure we start on the page
109          * containing the first new message
110          */
111         else if (oper == 3) {
112                 BBS->requested_page = (-3);
113         }
114
115         if (havebstr("maxmsgs")) {
116                 Stat->maxmsgs = ibstr("maxmsgs");
117         }
118         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
119         
120         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
121         rlid[2].cmd(cmd, len);
122         
123         return 200;
124 }
125
126
127 /*
128  * This function is called for every message in the list.
129  */
130 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
131                               void **ViewSpecific, 
132                               message_summary* Msg, 
133                               int is_new, 
134                               int i)
135 {
136         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
137
138         if (BBS->alloc_msgs == 0) {
139                 BBS->alloc_msgs = 1000;
140                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
141                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
142         }
143
144         /* Check our buffer size */
145         if (BBS->num_msgs >= BBS->alloc_msgs) {
146                 BBS->alloc_msgs *= 2;
147                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
148                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
149         }
150
151         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
152
153         return 200;
154 }
155
156
157 int bbsview_sortfunc(const void *s1, const void *s2) {
158         long l1;
159         long l2;
160
161         l1 = *(long *)(s1);
162         l2 = *(long *)(s2);
163
164         if (l1 > l2) return(+1);
165         if (l1 < l2) return(-1);
166         return(0);
167 }
168
169
170 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
171                                void **ViewSpecific, 
172                                long oper)
173 {
174         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
175         int i;
176         int seq;
177         const StrBuf *Mime;
178         int start_index = 0;
179         int end_index = 0;
180         int go_to_the_very_end = 0;
181
182         if (Stat->nummsgs > 0) {
183                 syslog(LOG_DEBUG, "sorting %d messages\n", BBS->num_msgs);
184                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
185         }
186
187         if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
188                 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
189         }
190         else {
191                 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
192         }
193
194         /* If the requested page number is -4,
195          * it means "whichever page on which msg#xxxxx starts"
196          * Change to the page number which contains that message.
197          */
198         if (BBS->requested_page == (-4)) {
199                 if (BBS->num_msgs == 0) {
200                         BBS->requested_page = 0;
201                 }
202                 else {
203                         for (i=0; i<BBS->num_msgs; ++i) {
204                                 if (
205                                         (BBS->msgs[i] >= BBS->start_reading_at)
206                                         && (BBS->requested_page == (-4))
207                                 ) {
208                                         BBS->requested_page = (i / Stat->maxmsgs) ;
209                                 }
210                         }
211                 }
212         }
213
214         /* If the requested page number is -3,
215          * it means "whichever page on which new messages start"
216          * Change that to an actual page number now.
217          */
218         if (BBS->requested_page == (-3)) {
219                 if (BBS->num_msgs == 0) {
220                         /*
221                          * The room is empty; just start at the top and leave it there.
222                          */
223                         BBS->requested_page = 0;
224                 }
225                 else if (
226                         (BBS->num_msgs > 0) 
227                         && (BBS->lastseen <= BBS->msgs[0])
228                 ) {
229                         /*
230                          * All messages are new; this is probably the user's first visit to the room,
231                          * so start at the last page instead of showing ancient history.
232                          */
233                         BBS->requested_page = BBS->num_pages - 1;
234                         go_to_the_very_end = 1;
235                 }
236                 else {
237                         /*
238                          * Some messages are old and some are new.  Go to the start of new messages.
239                          */
240                         for (i=0; i<BBS->num_msgs; ++i) {
241                                 if (
242                                         (BBS->msgs[i] > BBS->lastseen)
243                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
244                                 ) {
245                                         BBS->requested_page = (i / Stat->maxmsgs) ;
246                                 }
247                         }
248                 }
249         }
250
251         /* Still set to -3 ?  If so, that probably means that there are no new messages,
252          * so we'll go to the *end* of the final page.
253          */
254         if (BBS->requested_page == (-3)) {
255                 if (BBS->num_msgs == 0) {
256                         BBS->requested_page = 0;
257                 }
258                 else {
259                         BBS->requested_page = BBS->num_pages - 1;
260                 }
261         }
262
263         /* keep the requested page within bounds */
264         if (BBS->requested_page < 0) BBS->requested_page = 0;
265         if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
266
267         start_index = BBS->requested_page * Stat->maxmsgs;
268         if (start_index < 0) start_index = 0;
269         end_index = start_index + Stat->maxmsgs - 1;
270
271         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
272
273                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
274                         /* display the selected range of messages */
275
276                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
277                                 if (
278                                         (BBS->msgs[i] > BBS->lastseen)
279                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
280                                 ) {
281                                         /* new messages start here */
282                                         do_template("start_of_new_msgs");
283                                         if (!go_to_the_very_end) {
284                                                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
285                                         }
286                                 }
287                                 if (BBS->msgs[i] > 0L) {
288                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime, NULL);
289                                 }
290                                 if (
291                                         (i == (BBS->num_msgs - 1))
292                                         && (BBS->msgs[i] <= BBS->lastseen)
293                                 ) {
294                                         /* no new messages */
295                                         do_template("no_new_msgs");
296                                         if (!go_to_the_very_end) {
297                                                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
298                                         }
299                                 }
300                         }
301                 }
302
303                 else if ( (seq == 0) || (seq == 2) ) {
304                         int first;
305                         int last;
306                         /* Display the selecto-bar with the page numbers */
307
308                         wc_printf("<div class=\"moreprompt\">");
309                         if (seq == 2) {
310                                 wc_printf("<a name=\"end_of_msgs\">");
311                         }
312                         wc_printf(_("Go to page: "));
313                         if (seq == 2) {
314                                 wc_printf("</a>");
315                         }
316
317                         first = 0;
318                         last = BBS->num_pages - 1;
319
320                         for (i=0; i<=last; ++i) {
321
322                                 if (
323                                         (i == first)
324                                         || (i == last)
325                                         || (i == BBS->requested_page)
326                                         || (
327                                                 ((BBS->requested_page - i) < RANGE)
328                                                 && ((BBS->requested_page - i) > (0 - RANGE))
329                                         )
330                                 ) {
331
332                                         if (
333                                                 (i == last) 
334                                                 && (last - BBS->requested_page > RANGE)
335                                         ) {
336                                                 wc_printf("...&nbsp;");
337                                         }
338                                         if (i == BBS->requested_page) {
339                                                 wc_printf("[");
340                                         }
341                                         else {
342                                                 wc_printf("<a href=\"readfwd?go=");
343                                                 urlescputs(ChrPtr(WC->CurRoom.name));
344                                                 wc_printf("?start_reading_at=%ld\">",
345                                                         BBS->msgs[i*Stat->maxmsgs]
346                                                 );
347                                                 /* wc_printf("?page=%d\">", i); */
348                                                 wc_printf("<span class=\"moreprompt_link\">");
349                                         }
350                                         if (
351                                                 (i == first)
352                                                 && (BBS->requested_page > (RANGE + 1))
353                                         ) {
354                                                 wc_printf(_("First"));
355                                         }
356                                         else if (
357                                                 (i == last)
358                                                 && (last - BBS->requested_page > RANGE)
359                                         ) {
360                                                 wc_printf(_("Last"));
361                                         }
362                                         else {
363                                                 wc_printf("%d", i + 1); /* change to one-based for display */
364                                         }
365                                         if (i == BBS->requested_page) {
366                                                 wc_printf("]");
367                                         }
368                                         else {
369                                                 wc_printf("</span>");
370                                                 wc_printf("</a>");
371                                         }
372                                         if (
373                                                 (i == first)
374                                                 && (BBS->requested_page > (RANGE + 1))
375                                         ) {
376                                                 wc_printf("&nbsp;...");
377                                         }
378                                         if (i != last) {
379                                                 wc_printf("&nbsp;");
380                                         }
381                                 }
382                         }
383                         wc_printf("</div>\n");
384                 }
385         }
386
387         if (go_to_the_very_end) {
388                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#end_of_msgs\";\n");
389         }
390         return(0);
391 }
392
393
394 int bbsview_Cleanup(void **ViewSpecific)
395 {
396         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
397
398         if (BBS->alloc_msgs != 0) {
399                 free(BBS->msgs);
400         }
401         free(BBS);
402
403         wDumpContent(1);
404         return 0;
405 }
406
407
408 void 
409 InitModule_BBSVIEWRENDERERS
410 (void)
411 {
412         RegisterReadLoopHandlerset(
413                 VIEW_BBS,
414                 bbsview_GetParamsGetServerCall,
415                 NULL,
416                 NULL,
417                 NULL, 
418                 bbsview_LoadMsgFromServer,
419                 bbsview_RenderView_or_Tail,
420                 bbsview_Cleanup,
421                 NULL
422         );
423 }