* svn id tag
[citadel.git] / webcit / bbsview_renderer.c
1 /* 
2  * $Id$
3  *
4  * BBS View renderer module for WebCit
5  *
6  * Copyright (c) 1996-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24 #include "webserver.h"
25 #include "groupdav.h"
26
27 /*
28  * Data which gets passed around between the various functions in this module
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         int alloc_msgs;         /* Currently allocated size of array */
34 };
35
36
37 /*
38  * Entry point for message read operations.
39  */
40 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
41                                    void **ViewSpecific, 
42                                    long oper, 
43                                    char *cmd, 
44                                    long len)
45 {
46         struct bbsview *BBS = malloc(sizeof(struct bbsview));
47         memset(BBS, 0, sizeof(struct bbsview));
48         *ViewSpecific = BBS;
49
50         Stat->startmsg = -1;
51         Stat->sortit = 1;
52         
53         rlid[oper].cmd(cmd, len);               /* this performs the server call to fetch the msg list */
54         
55         if (havebstr("maxmsgs")) {
56                 Stat->maxmsgs = ibstr("maxmsgs");
57         }
58         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
59         Stat->num_displayed = DEFAULT_MAXMSGS;
60         
61         if (havebstr("startmsg")) {
62                 Stat->startmsg = lbstr("startmsg");
63         }
64
65         return 200;
66 }
67
68
69 /*
70  * begin_ajax_response() was moved from bbsview_LoadMsgFromServer() to here ...
71  */
72 int bbsview_PrintViewHeader(SharedMessageStatus *Stat, void **ViewSpecific)
73 {
74         if (WC->is_ajax) {
75                 begin_ajax_response();          /* for non-ajax, headers are output in messages.c */
76         }
77
78         return 200;
79 }
80
81
82 /*
83  * This function is called for every message in the list.
84  */
85 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
86                               void **ViewSpecific, 
87                               message_summary* Msg, 
88                               int is_new, 
89                               int i)
90 {
91         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
92
93         if (BBS->alloc_msgs == 0) {
94                 BBS->alloc_msgs = Stat->maxmsgs;
95                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
96         }
97
98         /* Theoretically this never happens because the initial allocation == maxmsgs */
99         if (BBS->num_msgs >= BBS->alloc_msgs) {
100                 BBS->alloc_msgs *= 2;
101                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
102         }
103
104         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
105
106         return 200;
107 }
108
109 int bbsview_sortfunc(const void *s1, const void *s2) {
110         long l1;
111         long l2;
112
113         l1 = *(long *)(s1);
114         l2 = *(long *)(s2);
115
116         if (l1 > l2) return(+1);
117         if (l1 < l2) return(-1);
118         return(0);
119 }
120
121
122 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
123                                void **ViewSpecific, 
124                                long oper)
125 {
126         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
127         int i;
128         const StrBuf *Mime;
129         char olderdiv[64];
130         char newerdiv[64];
131         int doing_older_messages = 0;
132         int doing_newer_messages = 0;
133
134         int increments[] = { 20, 50, 100 } ;
135 #define NUM_INCREMENTS  (sizeof(increments) / sizeof(int))
136
137         snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
138         snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
139
140         /* Determine whether we are in the middle of a 'click for older messages' or 'click for
141          * newer messages' operation.  If neither, then we are in the initial page load.
142          */
143         if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
144                 doing_older_messages = 1;
145                 doing_newer_messages = 0;
146         }
147         else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
148                 doing_older_messages = 0;
149                 doing_newer_messages = 1;
150         }
151         else {
152                 doing_older_messages = 0;
153                 doing_newer_messages = 0;
154         }
155
156
157         /* Cut the message list down to the requested size */
158         if (Stat->nummsgs > 0) {
159                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
160                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
161
162                 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
163
164                 if (BBS->num_msgs > Stat->maxmsgs) {
165
166                         if (doing_older_messages) {
167                                 /* LT ... cut it down to the LAST 20 messages received */
168                                 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
169                                         (Stat->maxmsgs * sizeof(long))
170                                 );
171                                 BBS->num_msgs = Stat->maxmsgs;
172                         }
173                         else {
174                                 /* GT ... cut it down to the FIRST 20 messages received */
175                                 BBS->num_msgs = Stat->maxmsgs;
176                         }
177                 }
178         }
179
180
181         /* Supply the link to prepend the previous 20 messages */
182
183         if ((!WC->is_ajax) && (Stat->nummsgs == 0)) {
184                 wc_printf("<div id=\"%s\">", olderdiv);
185                 wc_printf("<div class=\"moreprompt\">");
186                 for (i=0; i<NUM_INCREMENTS; ++i) {
187                         wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %d);\">",
188                                 olderdiv,
189                                 LONG_MAX,
190                                 increments[i]
191                         );
192                         wc_printf("<span class=\"moreprompt_link\">&uarr; ");
193                         wc_printf(_("Previous %d"), increments[i]);
194                         wc_printf(" &uarr;</span>");
195                         wc_printf("</a>");
196                 }
197                 wc_printf("</div>");
198                 wc_printf("<div class=\"nomsgs\"><br><em>");
199                 wc_printf(_("No messages here."));
200                 wc_printf("</em><br></div>\n");
201                 wc_printf("</div>");
202         }
203         else if (doing_newer_messages == 0) {
204                 wc_printf("<div id=\"%s\">", olderdiv);
205                 wc_printf("<div class=\"moreprompt\">");
206                 if (Stat->nummsgs > 0) {
207                         for (i=0; i<NUM_INCREMENTS; ++i) {
208                                 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %d);\">",
209                                         olderdiv,
210                                         BBS->msgs[0],
211                                         increments[i]
212                                 );
213                                 wc_printf("<span class=\"moreprompt_link\">&uarr; ");
214                                 wc_printf(_("Previous %d"), increments[i]);
215                                 wc_printf(" &uarr;</span>");
216                                 wc_printf("</a>");
217                         }
218                         wc_printf("</div>");
219                 }
220                 wc_printf("</div>");
221         }
222
223         /* Non-empty message set... */
224         if (Stat->nummsgs > 0) {
225                 /* Render the messages */
226         
227                 for (i=0; i<BBS->num_msgs; ++i) {
228                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
229                 }
230
231         }
232
233
234         /* Supply the link to append the next 20 messages */
235
236         if (doing_older_messages == 0) {
237                 wc_printf("<div id=\"%s\">", newerdiv);
238                 if (Stat->nummsgs >= Stat->maxmsgs) {
239                         wc_printf("<div class=\"moreprompt\">");
240                         for (i=0; i<NUM_INCREMENTS; ++i) {
241                                 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %d);\">",
242                                         newerdiv,
243                                         BBS->msgs[BBS->num_msgs-1],
244                                         increments[i]
245                                 );
246                                 wc_printf("<span class=\"moreprompt_link\">&darr; ");
247                                 wc_printf(_("Next %d"), increments[i]);
248                                 wc_printf(" &darr;</span>");
249                                 wc_printf("</a>");
250                         }
251                         wc_printf("</div>");
252                 }
253                 else {
254                         long gt = 0;    /* if new messages appear later, where will they begin? */
255                         if (Stat->nummsgs > 0) {
256                                 gt = BBS->msgs[BBS->num_msgs-1];
257                         }
258                         else {
259                                 gt = atol(bstr("gt"));
260                         }
261                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
262                                 newerdiv,
263                                 gt,
264                                 Stat->maxmsgs
265                         );
266                         wc_printf("<div class=\"moreprompt\">");
267                         wc_printf("<span class=\"moreprompt_link\">&darr; ");
268                         wc_printf("%s", _("no more messages"));
269                         wc_printf(" &darr;</span>");
270                         wc_printf("</div>");
271                         wc_printf("</a>");
272                 }
273                 wc_printf("</div>");
274         }
275
276         /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
277          * adding it every time we extend the visible message set.
278          */
279         if (!WC->is_ajax) {
280                 wc_printf("<br><br><br><br>");
281         }
282
283         return(0);
284 }
285
286
287 int bbsview_Cleanup(void **ViewSpecific)
288 {
289         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
290
291         if (BBS->alloc_msgs != 0) {
292                 free(BBS->msgs);
293         }
294         free(BBS);
295
296         if (WC->is_ajax) {
297                 end_ajax_response();
298                 WC->is_ajax = 0;
299         }
300         else {
301                 wDumpContent(1);
302         }
303         return 0;
304 }
305
306 void 
307 InitModule_BBSVIEWRENDERERS
308 (void)
309 {
310         RegisterReadLoopHandlerset(
311                 VIEW_BBS,
312                 bbsview_GetParamsGetServerCall,
313                 bbsview_PrintViewHeader,
314                 bbsview_LoadMsgFromServer,
315                 bbsview_RenderView_or_Tail,
316                 bbsview_Cleanup);
317 }