* the JavaScript code for the new BBS view has been moved out of bbs_renderer.c and...
[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         snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
135         snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
136
137         /* Determine whether we are in the middle of a 'click for older messages' or 'click for
138          * newer messages' operation.  If neither, then we are in the initial page load.
139          */
140         if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
141                 doing_older_messages = 1;
142                 doing_newer_messages = 0;
143         }
144         else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
145                 doing_older_messages = 0;
146                 doing_newer_messages = 1;
147         }
148         else {
149                 doing_older_messages = 0;
150                 doing_newer_messages = 0;
151         }
152
153
154         /* Cut the message list down to the requested size */
155         if (Stat->nummsgs > 0) {
156                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
157                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
158
159                 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
160
161                 if (BBS->num_msgs > Stat->maxmsgs) {
162
163                         if (doing_older_messages) {
164                                 /* LT ... cut it down to the LAST 20 messages received */
165                                 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
166                                         (Stat->maxmsgs * sizeof(long))
167                                 );
168                                 BBS->num_msgs = Stat->maxmsgs;
169                         }
170                         else {
171                                 /* GT ... cut it down to the FIRST 20 messages received */
172                                 BBS->num_msgs = Stat->maxmsgs;
173                         }
174                 }
175         }
176
177
178         /* Supply the link to prepend the previous 20 messages */
179
180         if (doing_newer_messages == 0) {
181                 wc_printf("<div id=\"%s\">", olderdiv);
182                 /* if (Stat->nummsgs > 0) { */
183                 if (Stat->nummsgs > 0) {
184                         wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld);\">",
185                                 olderdiv,
186                                 BBS->msgs[0],
187                                 Stat->maxmsgs
188                         );
189                 
190                         wc_printf("<div class=\"moreprompt\">"
191                                 "&uarr; &uarr; &uarr; %s &uarr; &uarr; &uarr;"
192                                 "</div>", _("older messages")
193                         );
194                         wc_printf("</a>");
195                 }
196                 wc_printf("</div>");
197         }
198
199
200
201         /* Handle the empty message set gracefully... */
202         if (Stat->nummsgs == 0) {
203                 if (!WC->is_ajax) {
204                         wc_printf("<div class=\"nomsgs\"><br><em>");
205                         wc_printf(_("No messages here."));
206                         wc_printf("</em><br></div>\n");
207                 }
208         }
209
210         /* Non-empty message set... */
211         else {
212                 /* Render the messages */
213         
214                 for (i=0; i<BBS->num_msgs; ++i) {
215                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
216                 }
217
218         }
219
220
221         /* Supply the link to append the next 20 messages */
222
223         if (doing_older_messages == 0) {
224                 wc_printf("<div id=\"%s\">", newerdiv);
225                 /* if (Stat->nummsgs > 0) { */
226                 if (Stat->nummsgs >= Stat->maxmsgs) {
227                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
228                                 newerdiv,
229                                 BBS->msgs[BBS->num_msgs-1],
230                                 Stat->maxmsgs
231                         );
232                 
233                         wc_printf("<div class=\"moreprompt\">"
234                                 "&darr; &darr; &darr; %s &darr; &darr; &darr;"
235                                 "</div>", _("newer messages")
236                         );
237                         wc_printf("</a>");
238                 }
239                 else {
240                         long gt = 0;    /* if new messages appear later, where will they begin? */
241                         if (Stat->nummsgs > 0) {
242                                 gt = BBS->msgs[BBS->num_msgs-1];
243                         }
244                         else {
245                                 gt = atol(bstr("gt"));
246                         }
247                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
248                                 newerdiv,
249                                 gt,
250                                 Stat->maxmsgs
251                         );
252                         wc_printf("<div class=\"moreprompt\">");
253                         wc_printf("%s", _("no more messages"));
254                         wc_printf("</div>");
255                         wc_printf("</a>");
256                 }
257                 wc_printf("</div>");
258         }
259
260         /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
261          * adding it every time we extend the visible message set.
262          */
263         if (!WC->is_ajax) {
264                 wc_printf("<br><br><br><br>");
265         }
266
267         return(0);
268 }
269
270
271 int bbsview_Cleanup(void **ViewSpecific)
272 {
273         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
274
275         if (BBS->alloc_msgs != 0) {
276                 free(BBS->msgs);
277         }
278         free(BBS);
279
280         if (WC->is_ajax) {
281                 end_ajax_response();
282                 WC->is_ajax = 0;
283         }
284         else {
285                 wDumpContent(1);
286         }
287         return 0;
288 }
289
290 void 
291 InitModule_BBSVIEWRENDERERS
292 (void)
293 {
294         RegisterReadLoopHandlerset(
295                 VIEW_BBS,
296                 bbsview_GetParamsGetServerCall,
297                 bbsview_PrintViewHeader,
298                 bbsview_LoadMsgFromServer,
299                 bbsview_RenderView_or_Tail,
300                 bbsview_Cleanup);
301 }