Remove $Id$ tags from most of webcit
[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 };
43
44
45 /*
46  * Attempt to determine the closest thing to the "last seen message number" using the
47  * results of the GTSN command
48  */
49 long bbsview_get_last_seen(void)
50 {
51         char buf[SIZ] = "0";
52
53         serv_puts("GTSN");
54         serv_getln(buf, sizeof buf);
55         if (buf[0] == '2') {
56
57                 char *comma_pos = strchr(buf, ',');     /* kill first comma and everything to its right */
58                 if (comma_pos) {
59                         *comma_pos = 0;
60                 }
61
62                 char *colon_pos = strchr(buf, ':');     /* kill first colon and everything to its left */
63                 if (colon_pos) {
64                         strcpy(buf, ++colon_pos);
65                 }
66         }
67
68         return(atol(buf));
69 }
70
71
72
73 /*
74  * Entry point for message read operations.
75  */
76 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
77                                    void **ViewSpecific, 
78                                    long oper, 
79                                    char *cmd, 
80                                    long len)
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
92         /* If a specific page was requested, make sure we go there */
93         if (havebstr("page")) {
94                 BBS->requested_page = ibstr("page");
95         }
96
97         /* Otherwise, if this is a "read new" operation, make sure we start on the page
98          * containing the first new message
99          */
100         else if (oper == 3) {
101                 BBS->requested_page = (-3);
102         }
103
104         if (havebstr("maxmsgs")) {
105                 Stat->maxmsgs = ibstr("maxmsgs");
106         }
107         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
108         
109         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
110         rlid[2].cmd(cmd, len);
111         
112         return 200;
113 }
114
115
116 /*
117  * This function is called for every message in the list.
118  */
119 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
120                               void **ViewSpecific, 
121                               message_summary* Msg, 
122                               int is_new, 
123                               int i)
124 {
125         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
126
127         if (BBS->alloc_msgs == 0) {
128                 BBS->alloc_msgs = 1000;
129                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
130                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
131         }
132
133         /* Check our buffer size */
134         if (BBS->num_msgs >= BBS->alloc_msgs) {
135                 BBS->alloc_msgs *= 2;
136                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
137                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
138         }
139
140         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
141
142         return 200;
143 }
144
145
146 int bbsview_sortfunc(const void *s1, const void *s2) {
147         long l1;
148         long l2;
149
150         l1 = *(long *)(s1);
151         l2 = *(long *)(s2);
152
153         if (l1 > l2) return(+1);
154         if (l1 < l2) return(-1);
155         return(0);
156 }
157
158
159 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
160                                void **ViewSpecific, 
161                                long oper)
162 {
163         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
164         int i;
165         int seq;
166         const StrBuf *Mime;
167         int start_index = 0;
168         int end_index = 0;
169
170         wc_printf("<div class=\"fix_scrollbar_bug\">");
171
172         if (Stat->nummsgs > 0) {
173                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
174                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
175         }
176
177         if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
178                 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
179         }
180         else {
181                 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
182         }
183
184         /* If the requested page number is "whichever page on which new messages start"
185          * then change that to an actual page number now.
186          */
187         if (BBS->requested_page == (-3)) {
188                 if (BBS->num_msgs == 0) {
189                         BBS->requested_page = 0;
190                 }
191                 else {
192                         for (i=0; i<BBS->num_msgs; ++i) {
193                                 if (
194                                         (BBS->msgs[i] > BBS->lastseen)
195                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
196                                 ) {
197                                         BBS->requested_page = (i / Stat->maxmsgs) ;
198                                 }
199                         }
200                 }
201         }
202
203         /* Still set to -3 ?  If so, that probably means that there are no new messages,
204          * so we'll go to the *end* of the final page.
205          */
206         if (BBS->requested_page == (-3)) {
207                 if (BBS->num_msgs == 0) {
208                         BBS->requested_page = 0;
209                 }
210                 else {
211                         BBS->requested_page = BBS->num_pages - 1;
212                 }
213         }
214
215         /* keep the requested page within bounds */
216         if (BBS->requested_page < 0) BBS->requested_page = 0;
217         if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
218
219         start_index = BBS->requested_page * Stat->maxmsgs;
220         if (start_index < 0) start_index = 0;
221         end_index = start_index + Stat->maxmsgs - 1;
222
223         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
224
225                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
226                         /* display the selected range of messages */
227
228                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
229                                 if (
230                                         (BBS->msgs[i] > BBS->lastseen)
231                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
232                                 ) {
233                                         /* new messages start here */
234                                         do_template("start_of_new_msgs", NULL);
235                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
236                                 }
237                                 if (BBS->msgs[i] > 0L) {
238                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
239                                 }
240                                 if (
241                                         (i == (BBS->num_msgs - 1))
242                                         && (BBS->msgs[i] <= BBS->lastseen)
243                                 ) {
244                                         /* no new messages */
245                                         do_template("no_new_msgs", NULL);
246                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
247                                 }
248                         }
249                 }
250
251                 else if ( (seq == 0) || (seq == 2) ) {
252                         /* Display the selecto-bar with the page numbers */
253
254                         wc_printf("<div class=\"moreprompt\">");
255                         wc_printf(_("Go to page: "));
256
257                         int first = 0;
258                         int last = BBS->num_pages - 1;
259
260                         for (i=0; i<=last; ++i) {
261
262                                 if (
263                                         (i == first)
264                                         || (i == last)
265                                         || (i == BBS->requested_page)
266                                         || (
267                                                 ((BBS->requested_page - i) < RANGE)
268                                                 && ((BBS->requested_page - i) > (0 - RANGE))
269                                         )
270                                 ) {
271
272                                         if (
273                                                 (i == last) 
274                                                 && (last - BBS->requested_page > RANGE)
275                                         ) {
276                                                 wc_printf("...&nbsp;");
277                                         }
278                                         if (i == BBS->requested_page) {
279                                                 wc_printf("[");
280                                         }
281                                         else {
282                                                 wc_printf("<a href=\"readfwd?page=%d\">", i);
283                                                 wc_printf("<span class=\"moreprompt_link\">");
284                                         }
285                                         if (
286                                                 (i == first)
287                                                 && (BBS->requested_page > (RANGE + 1))
288                                         ) {
289                                                 wc_printf(_("First"));
290                                         }
291                                         else if (
292                                                 (i == last)
293                                                 && (last - BBS->requested_page > RANGE)
294                                         ) {
295                                                 wc_printf(_("Last"));
296                                         }
297                                         else {
298                                                 wc_printf("%d", i + 1); // change to one-based for display
299                                         }
300                                         if (i == BBS->requested_page) {
301                                                 wc_printf("]");
302                                         }
303                                         else {
304                                                 wc_printf("</span>");
305                                                 wc_printf("</a>");
306                                         }
307                                         if (
308                                                 (i == first)
309                                                 && (BBS->requested_page > (RANGE + 1))
310                                         ) {
311                                                 wc_printf("&nbsp;...");
312                                         }
313                                         if (i != last) {
314                                                 wc_printf("&nbsp;");
315                                         }
316                                 }
317                         }
318                         wc_printf("</div>\n");
319                 }
320         }
321
322         wc_printf("</div>\n");
323         return(0);
324 }
325
326
327 int bbsview_Cleanup(void **ViewSpecific)
328 {
329         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
330
331         if (BBS->alloc_msgs != 0) {
332                 free(BBS->msgs);
333         }
334         free(BBS);
335
336         wDumpContent(1);
337         return 0;
338 }
339
340
341 void 
342 InitModule_BBSVIEWRENDERERS
343 (void)
344 {
345         RegisterReadLoopHandlerset(
346                 VIEW_BBS,
347                 bbsview_GetParamsGetServerCall,
348                 NULL,
349                 NULL, 
350                 bbsview_LoadMsgFromServer,
351                 bbsview_RenderView_or_Tail,
352                 bbsview_Cleanup
353         );
354 }