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