* Now attempting to fix the final few rendering problems in Internet Explorer. Testi...
[citadel.git] / webcit / bbsview_renderer.c
1 /* 
2  * $Id$
3  *
4  * BBS View renderer module for WebCit
5  *
6  * Note: we briefly had a dynamic UI for this.  I thought it was cool, but
7  * it was not received well by the user community.  If you want to play
8  * with it, go get r8256 of bbsview_renderer.c and have fun.
9  *
10  * Copyright (c) 1996-2010 by the citadel.org team
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #define RANGE 5
28
29 #include "webcit.h"
30 #include "webserver.h"
31 #include "groupdav.h"
32
33 /*
34  * Data which gets passed around between the various functions in this module
35  *
36  */
37 struct bbsview {
38         long *msgs;             /* Array of msgnums for messages we are displaying */
39         int num_msgs;           /* Number of msgnums stored in 'msgs' */
40         long lastseen;          /* The number of the last seen message in this room */
41         int alloc_msgs;         /* Currently allocated size of array */
42         int requested_page;     /* Which page number did the user request? */
43         int num_pages;          /* Total number of pages in this room */
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
59                 char *comma_pos = strchr(buf, ',');     /* kill first comma and everything to its right */
60                 if (comma_pos) {
61                         *comma_pos = 0;
62                 }
63
64                 char *colon_pos = strchr(buf, ':');     /* kill first colon and everything to its left */
65                 if (colon_pos) {
66                         strcpy(buf, ++colon_pos);
67                 }
68         }
69
70         return(atol(buf));
71 }
72
73
74
75 /*
76  * Entry point for message read operations.
77  */
78 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
79                                    void **ViewSpecific, 
80                                    long oper, 
81                                    char *cmd, 
82                                    long len)
83 {
84         struct bbsview *BBS = malloc(sizeof(struct bbsview));
85         memset(BBS, 0, sizeof(struct bbsview));
86         *ViewSpecific = BBS;
87
88         Stat->startmsg = -1;                                    /* not used here */
89         Stat->sortit = 1;                                       /* not used here */
90         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
91         BBS->requested_page = 0;
92         BBS->lastseen = bbsview_get_last_seen();
93
94         /* If a specific page was requested, make sure we go there */
95         if (havebstr("page")) {
96                 BBS->requested_page = ibstr("page");
97         }
98
99         /* Otherwise, if this is a "read new" operation, make sure we start on the page
100          * containing the first new message
101          */
102         else if (oper == 3) {
103                 BBS->requested_page = (-3);
104         }
105
106         if (havebstr("maxmsgs")) {
107                 Stat->maxmsgs = ibstr("maxmsgs");
108         }
109         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
110         
111         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
112         rlid[2].cmd(cmd, len);
113         
114         return 200;
115 }
116
117
118 /*
119  * This function is called for every message in the list.
120  */
121 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
122                               void **ViewSpecific, 
123                               message_summary* Msg, 
124                               int is_new, 
125                               int i)
126 {
127         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
128
129         if (BBS->alloc_msgs == 0) {
130                 BBS->alloc_msgs = 1000;
131                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
132                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
133         }
134
135         /* Check our buffer size */
136         if (BBS->num_msgs >= BBS->alloc_msgs) {
137                 BBS->alloc_msgs *= 2;
138                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
139                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
140         }
141
142         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
143
144         return 200;
145 }
146
147
148 int bbsview_sortfunc(const void *s1, const void *s2) {
149         long l1;
150         long l2;
151
152         l1 = *(long *)(s1);
153         l2 = *(long *)(s2);
154
155         if (l1 > l2) return(+1);
156         if (l1 < l2) return(-1);
157         return(0);
158 }
159
160
161 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
162                                void **ViewSpecific, 
163                                long oper)
164 {
165         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
166         int i;
167         int seq;
168         const StrBuf *Mime;
169         int start_index = 0;
170         int end_index = 0;
171
172         wc_printf("<div class=\"fix_scrollbar_bug\">");
173
174         if (Stat->nummsgs > 0) {
175                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
176                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
177         }
178
179         if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
180                 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
181         }
182         else {
183                 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
184         }
185
186         /* If the requested page number is "whichever page on which new messages start"
187          * then change that to an actual page number now.
188          */
189         if (BBS->requested_page == (-3)) {
190                 if (BBS->num_msgs == 0) {
191                         BBS->requested_page = 0;
192                 }
193                 else {
194                         for (i=0; i<BBS->num_msgs; ++i) {
195                                 if (
196                                         (BBS->msgs[i] > BBS->lastseen)
197                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
198                                 ) {
199                                         BBS->requested_page = (i / Stat->maxmsgs) ;
200                                 }
201                         }
202                 }
203         }
204
205         /* Still set to -3 ?  If so, that probably means that there are no new messages,
206          * so we'll go to the *end* of the final page.
207          */
208         if (BBS->requested_page == (-3)) {
209                 if (BBS->num_msgs == 0) {
210                         BBS->requested_page = 0;
211                 }
212                 else {
213                         BBS->requested_page = BBS->num_pages - 1;
214                 }
215         }
216
217         /* keep the requested page within bounds */
218         if (BBS->requested_page < 0) BBS->requested_page = 0;
219         if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
220
221         start_index = BBS->requested_page * Stat->maxmsgs;
222         if (start_index < 0) start_index = 0;
223         end_index = start_index + Stat->maxmsgs - 1;
224
225         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
226
227                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
228                         /* display the selected range of messages */
229
230                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
231                                 if (
232                                         (BBS->msgs[i] > BBS->lastseen)
233                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
234                                 ) {
235                                         /* new messages start here */
236                                         do_template("start_of_new_msgs", NULL);
237                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
238                                 }
239                                 if (BBS->msgs[i] > 0L) {
240                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
241                                 }
242                                 if (
243                                         (i == (BBS->num_msgs - 1))
244                                         && (BBS->msgs[i] <= BBS->lastseen)
245                                 ) {
246                                         /* no new messages */
247                                         do_template("no_new_msgs", NULL);
248                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
249                                 }
250                         }
251                 }
252
253                 else if ( (seq == 0) || (seq == 2) ) {
254                         /* Display the selecto-bar with the page numbers */
255
256                         wc_printf("<div class=\"moreprompt\">");
257                         wc_printf(_("Go to page: "));
258
259                         int first = 0;
260                         int last = BBS->num_pages - 1;
261
262                         for (i=0; i<=last; ++i) {
263
264                                 if (
265                                         (i == first)
266                                         || (i == last)
267                                         || (i == BBS->requested_page)
268                                         || (
269                                                 ((BBS->requested_page - i) < RANGE)
270                                                 && ((BBS->requested_page - i) > (0 - RANGE))
271                                         )
272                                 ) {
273
274                                         if (
275                                                 (i == last) 
276                                                 && (last - BBS->requested_page > RANGE)
277                                         ) {
278                                                 wc_printf("...&nbsp;");
279                                         }
280                                         if (i == BBS->requested_page) {
281                                                 wc_printf("[");
282                                         }
283                                         else {
284                                                 wc_printf("<a href=\"readfwd?page=%d\">", i);
285                                                 wc_printf("<span class=\"moreprompt_link\">");
286                                         }
287                                         if (
288                                                 (i == first)
289                                                 && (BBS->requested_page > (RANGE + 1))
290                                         ) {
291                                                 wc_printf(_("First"));
292                                         }
293                                         else if (
294                                                 (i == last)
295                                                 && (last - BBS->requested_page > RANGE)
296                                         ) {
297                                                 wc_printf(_("Last"));
298                                         }
299                                         else {
300                                                 wc_printf("%d", i + 1); // change to one-based for display
301                                         }
302                                         if (i == BBS->requested_page) {
303                                                 wc_printf("]");
304                                         }
305                                         else {
306                                                 wc_printf("</span>");
307                                                 wc_printf("</a>");
308                                         }
309                                         if (
310                                                 (i == first)
311                                                 && (BBS->requested_page > (RANGE + 1))
312                                         ) {
313                                                 wc_printf("&nbsp;...");
314                                         }
315                                         if (i != last) {
316                                                 wc_printf("&nbsp;");
317                                         }
318                                 }
319                         }
320                         wc_printf("</div>\n");
321                 }
322         }
323
324         wc_printf("</div>\n");
325         return(0);
326 }
327
328
329 int bbsview_Cleanup(void **ViewSpecific)
330 {
331         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
332
333         if (BBS->alloc_msgs != 0) {
334                 free(BBS->msgs);
335         }
336         free(BBS);
337
338         wDumpContent(1);
339         return 0;
340 }
341
342
343 void 
344 InitModule_BBSVIEWRENDERERS
345 (void)
346 {
347         RegisterReadLoopHandlerset(
348                 VIEW_BBS,
349                 bbsview_GetParamsGetServerCall,
350                 NULL,
351                 NULL, 
352                 bbsview_LoadMsgFromServer,
353                 bbsview_RenderView_or_Tail,
354                 bbsview_Cleanup
355         );
356 }