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));
53 rlid[oper].cmd(cmd, len); /* this performs the server call to fetch the msg list */
55 if (havebstr("maxmsgs")) {
56 Stat->maxmsgs = ibstr("maxmsgs");
58 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
59 Stat->num_displayed = DEFAULT_MAXMSGS;
61 if (havebstr("startmsg")) {
62 Stat->startmsg = lbstr("startmsg");
70 * begin_ajax_response() was moved from bbsview_LoadMsgFromServer() to here ...
72 int bbsview_PrintViewHeader(SharedMessageStatus *Stat, void **ViewSpecific)
75 begin_ajax_response(); /* for non-ajax, headers are output in messages.c */
83 * This function is called for every message in the list.
85 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat,
91 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
93 if (BBS->alloc_msgs == 0) {
94 BBS->alloc_msgs = Stat->maxmsgs;
95 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
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)));
104 BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
109 int bbsview_sortfunc(const void *s1, const void *s2) {
116 if (l1 > l2) return(+1);
117 if (l1 < l2) return(-1);
122 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
126 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
131 int doing_older_messages = 0;
132 int doing_newer_messages = 0;
134 snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
135 snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
137 /* If this is the initial page load (and not an update), supply the required JavaScript code */
139 StrBufAppendPrintf(WC->trailing_javascript,
140 " function moremsgs(target_div, gt_or_lt, gt_or_lt_value, maxmsgs) { \n"
141 " $(target_div).innerHTML = '<div class=\"moreprompt\">%s ... <img src=\"static/throbber.gif\"></div>'; \n"
142 " p = gt_or_lt + '=' + gt_or_lt_value + '&maxmsgs=' + maxmsgs \n"
143 " + '&is_summary=0&is_ajax=1' \n"
144 " + '>_or_lt=' + gt_or_lt \n"
145 " + '&r=' + CtdlRandomString(); \n"
146 " new Ajax.Updater(target_div, 'read' + gt_or_lt, \n"
147 " { method: 'get', parameters: p, evalScripts: true } ); \n"
155 /* Determine whether we are in the middle of a 'click for older messages' or 'click for
156 * newer messages' operation. If neither, then we are in the initial page load.
158 if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
159 doing_older_messages = 1;
160 doing_newer_messages = 0;
162 else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
163 doing_older_messages = 0;
164 doing_newer_messages = 1;
167 doing_older_messages = 0;
168 doing_newer_messages = 0;
172 /* Cut the message list down to the requested size */
173 if (Stat->nummsgs > 0) {
174 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
175 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
177 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
179 if (BBS->num_msgs > Stat->maxmsgs) {
181 if (doing_older_messages) {
182 /* LT ... cut it down to the LAST 20 messages received */
183 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
184 (Stat->maxmsgs * sizeof(long))
186 BBS->num_msgs = Stat->maxmsgs;
189 /* GT ... cut it down to the FIRST 20 messages received */
190 BBS->num_msgs = Stat->maxmsgs;
196 /* Supply the link to prepend the previous 20 messages */
198 if (doing_newer_messages == 0) {
199 wc_printf("<div id=\"%s\">", olderdiv);
200 /* if (Stat->nummsgs > 0) { */
201 if (Stat->nummsgs > 0) {
202 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld);\">",
208 wc_printf("<div class=\"moreprompt\">"
209 "↑ ↑ ↑ %s ↑ ↑ ↑"
210 "</div>", _("older messages")
219 /* Handle the empty message set gracefully... */
220 if (Stat->nummsgs == 0) {
222 wc_printf("<div class=\"nomsgs\"><br><em>");
223 wc_printf(_("No messages here."));
224 wc_printf("</em><br></div>\n");
228 /* Non-empty message set... */
230 /* Render the messages */
232 for (i=0; i<BBS->num_msgs; ++i) {
233 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
239 /* Supply the link to append the next 20 messages */
241 if (doing_older_messages == 0) {
242 wc_printf("<div id=\"%s\">", newerdiv);
243 /* if (Stat->nummsgs > 0) { */
244 if (Stat->nummsgs >= Stat->maxmsgs) {
245 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
247 BBS->msgs[BBS->num_msgs-1],
251 wc_printf("<div class=\"moreprompt\">"
252 "↓ ↓ ↓ %s ↓ ↓ ↓"
253 "</div>", _("newer messages")
258 long gt = 0; /* if new messages appear later, where will they begin? */
259 if (Stat->nummsgs > 0) {
260 gt = BBS->msgs[BBS->num_msgs-1];
263 gt = atol(bstr("gt"));
265 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
270 wc_printf("<div class=\"moreprompt\">");
271 wc_printf("%s", _("no more messages"));
278 /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
279 * adding it every time we extend the visible message set.
282 wc_printf("<br><br><br><br>");
289 int bbsview_Cleanup(void **ViewSpecific)
291 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
293 if (BBS->alloc_msgs != 0) {
309 InitModule_BBSVIEWRENDERERS
312 RegisterReadLoopHandlerset(
314 bbsview_GetParamsGetServerCall,
315 bbsview_PrintViewHeader,
316 bbsview_LoadMsgFromServer,
317 bbsview_RenderView_or_Tail,