4 * BBS View renderer module for WebCit
6 * Copyright (c) 1996-2009 by the citadel.org team
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.
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.
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
24 #include "webserver.h"
28 * Data which gets passed around between the various functions in this module
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 */
38 * Entry point for message read operations.
40 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat,
46 struct bbsview *BBS = malloc(sizeof(struct bbsview));
47 memset(BBS, 0, sizeof(struct bbsview));
50 Stat->defaultsortorder = 1;
54 rlid[oper].cmd(cmd, len); /* this performs the server call to fetch the msg list */
56 if (havebstr("maxmsgs")) {
57 Stat->maxmsgs = ibstr("maxmsgs");
59 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
61 if (havebstr("startmsg")) {
62 Stat->startmsg = lbstr("startmsg");
64 if (lbstr("SortOrder") == 2) {
66 Stat->num_displayed = -DEFAULT_MAXMSGS;
70 Stat->num_displayed = DEFAULT_MAXMSGS;
78 * begin_ajax_response() was moved from bbsview_LoadMsgFromServer() to here ...
80 int bbsview_PrintViewHeader(SharedMessageStatus *Stat, void **ViewSpecific)
83 begin_ajax_response(); /* for non-ajax, headers are output in messages.c */
91 * This function is called for every message in the list.
93 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat,
99 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
101 if (BBS->alloc_msgs == 0) {
102 BBS->alloc_msgs = Stat->maxmsgs;
103 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
106 /* Theoretically this never happens because the initial allocation == maxmsgs */
107 if (BBS->num_msgs >= BBS->alloc_msgs) {
108 BBS->alloc_msgs *= 2;
109 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
112 BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
117 int bbsview_sortfunc_reverse(const void *s1, const void *s2) {
124 if (l1 > l2) return(-1);
125 if (l1 < l2) return(+1);
130 int bbsview_sortfunc_forward(const void *s1, const void *s2) {
137 if (l1 > l2) return(+1);
138 if (l1 < l2) return(-1);
143 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
147 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
152 int doing_older_messages = 0;
153 int doing_newer_messages = 0;
155 snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
156 snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
158 /* If this is the initial page load (and not an update), supply the required JavaScript code */
160 StrBufAppendPrintf(WC->trailing_javascript,
161 " function moremsgs(target_div, gt_or_lt, gt_or_lt_value, maxmsgs, sortorder) { \n"
162 " $(target_div).innerHTML = '<div class=\"moreprompt\">%s ... <img src=\"static/throbber.gif\"></div>'; \n"
163 " p = gt_or_lt + '=' + gt_or_lt_value + '&maxmsgs=' + maxmsgs \n"
164 " + '&is_summary=0&SortOrder=' + sortorder + '&is_ajax=1' \n"
165 " + '>_or_lt=' + gt_or_lt \n"
166 " + '&r=' + CtdlRandomString(); \n"
167 " new Ajax.Updater(target_div, 'read' + gt_or_lt, \n"
168 " { method: 'get', parameters: p, evalScripts: true } ); \n"
176 /* Determine whether we are in the middle of a 'click for older messages' or 'click for
177 * newer messages' operation. If neither, then we are in the initial page load.
179 if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
180 doing_older_messages = 1;
181 doing_newer_messages = 0;
183 else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
184 doing_older_messages = 0;
185 doing_newer_messages = 1;
188 doing_older_messages = 0;
189 doing_newer_messages = 0;
193 /* Cut the message list down to the requested size */
194 if (Stat->nummsgs > 0) {
195 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
196 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), (Stat->reverse ? bbsview_sortfunc_reverse : bbsview_sortfunc_forward));
198 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
200 if (BBS->num_msgs > Stat->maxmsgs) {
202 if (doing_older_messages) {
203 /* LT ... cut it down to the LAST 20 messages received */
204 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
205 (Stat->maxmsgs * sizeof(long))
207 BBS->num_msgs = Stat->maxmsgs;
210 /* GT ... cut it down to the FIRST 20 messages received */
211 BBS->num_msgs = Stat->maxmsgs;
217 /* Supply the link to prepend the previous 20 messages */
219 if (doing_newer_messages == 0) {
220 wc_printf("<div id=\"%s\">", olderdiv);
221 /* if (Stat->nummsgs > 0) { */
222 if (Stat->nummsgs > 0) {
223 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld, %d );\">",
227 (Stat->reverse ? 2 : 1)
230 wc_printf("<div class=\"moreprompt\">"
231 "↑ ↑ ↑ %s ↑ ↑ ↑"
232 "</div>", _("older messages")
241 /* Handle the empty message set gracefully... */
242 if (Stat->nummsgs == 0) {
244 wc_printf("<div class=\"nomsgs\"><br><em>");
245 wc_printf(_("No messages here."));
246 wc_printf("</em><br></div>\n");
250 /* Non-empty message set... */
252 /* Render the messages */
254 for (i=0; i<BBS->num_msgs; ++i) {
255 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
261 /* Supply the link to append the next 20 messages */
263 if (doing_older_messages == 0) {
264 wc_printf("<div id=\"%s\">", newerdiv);
265 /* if (Stat->nummsgs > 0) { */
266 if (Stat->nummsgs >= Stat->maxmsgs) {
267 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
269 BBS->msgs[BBS->num_msgs-1],
271 (Stat->reverse ? 2 : 1)
274 wc_printf("<div class=\"moreprompt\">"
275 "↓ ↓ ↓ %s ↓ ↓ ↓"
276 "</div>", _("newer messages")
281 long gt = 0; /* if new messages appear later, where will they begin? */
282 if (Stat->nummsgs > 0) {
283 gt = BBS->msgs[BBS->num_msgs-1];
286 gt = atol(bstr("gt"));
288 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
292 (Stat->reverse ? 2 : 1)
294 wc_printf("<div class=\"moreprompt\">");
295 wc_printf("%s", _("no more messages"));
302 /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
303 * adding it every time we extend the visible message set.
306 wc_printf("<br><br><br><br>");
313 int bbsview_Cleanup(void **ViewSpecific)
315 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
329 InitModule_BBSVIEWRENDERERS
332 RegisterReadLoopHandlerset(
334 bbsview_GetParamsGetServerCall,
335 bbsview_PrintViewHeader,
336 bbsview_LoadMsgFromServer,
337 bbsview_RenderView_or_Tail,