* When a 'read new' operation returns zero messages, make the 'older messages' link...
[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 ((!WC->is_ajax) && (Stat->nummsgs == 0)) {
181                 wc_printf("<div id=\"%s\">", olderdiv);
182                 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld);\">",
183                         olderdiv,
184                         LONG_MAX,
185                         Stat->maxmsgs
186                 );
187         
188                 wc_printf("<div class=\"moreprompt\">"
189                         "&uarr; &uarr; &uarr; %s &uarr; &uarr; &uarr;"
190                         "</div>", _("older messages")
191                 );
192                 wc_printf("</a>");
193                 wc_printf("<div class=\"nomsgs\"><br><em>");
194                 wc_printf(_("No messages here."));
195                 wc_printf("</em><br></div>\n");
196                 wc_printf("</div>");
197         }
198         else if (doing_newer_messages == 0) {
199                 wc_printf("<div id=\"%s\">", olderdiv);
200                 if (Stat->nummsgs > 0) {
201                         wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld);\">",
202                                 olderdiv,
203                                 BBS->msgs[0],
204                                 Stat->maxmsgs
205                         );
206                 
207                         wc_printf("<div class=\"moreprompt\">"
208                                 "&uarr; &uarr; &uarr; %s &uarr; &uarr; &uarr;"
209                                 "</div>", _("older messages")
210                         );
211                         wc_printf("</a>");
212                 }
213                 wc_printf("</div>");
214         }
215
216         /* Non-empty message set... */
217         if (Stat->nummsgs > 0) {
218                 /* Render the messages */
219         
220                 for (i=0; i<BBS->num_msgs; ++i) {
221                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
222                 }
223
224         }
225
226
227         /* Supply the link to append the next 20 messages */
228
229         if (doing_older_messages == 0) {
230                 wc_printf("<div id=\"%s\">", newerdiv);
231                 /* if (Stat->nummsgs > 0) { */
232                 if (Stat->nummsgs >= Stat->maxmsgs) {
233                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
234                                 newerdiv,
235                                 BBS->msgs[BBS->num_msgs-1],
236                                 Stat->maxmsgs
237                         );
238                 
239                         wc_printf("<div class=\"moreprompt\">"
240                                 "&darr; &darr; &darr; %s &darr; &darr; &darr;"
241                                 "</div>", _("newer messages")
242                         );
243                         wc_printf("</a>");
244                 }
245                 else {
246                         long gt = 0;    /* if new messages appear later, where will they begin? */
247                         if (Stat->nummsgs > 0) {
248                                 gt = BBS->msgs[BBS->num_msgs-1];
249                         }
250                         else {
251                                 gt = atol(bstr("gt"));
252                         }
253                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
254                                 newerdiv,
255                                 gt,
256                                 Stat->maxmsgs
257                         );
258                         wc_printf("<div class=\"moreprompt\">");
259                         wc_printf("%s", _("no more messages"));
260                         wc_printf("</div>");
261                         wc_printf("</a>");
262                 }
263                 wc_printf("</div>");
264         }
265
266         /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
267          * adding it every time we extend the visible message set.
268          */
269         if (!WC->is_ajax) {
270                 wc_printf("<br><br><br><br>");
271         }
272
273         return(0);
274 }
275
276
277 int bbsview_Cleanup(void **ViewSpecific)
278 {
279         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
280
281         if (BBS->alloc_msgs != 0) {
282                 free(BBS->msgs);
283         }
284         free(BBS);
285
286         if (WC->is_ajax) {
287                 end_ajax_response();
288                 WC->is_ajax = 0;
289         }
290         else {
291                 wDumpContent(1);
292         }
293         return 0;
294 }
295
296 void 
297 InitModule_BBSVIEWRENDERERS
298 (void)
299 {
300         RegisterReadLoopHandlerset(
301                 VIEW_BBS,
302                 bbsview_GetParamsGetServerCall,
303                 bbsview_PrintViewHeader,
304                 bbsview_LoadMsgFromServer,
305                 bbsview_RenderView_or_Tail,
306                 bbsview_Cleanup);
307 }