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