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 /* 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.
140 if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
141 doing_older_messages = 1;
142 doing_newer_messages = 0;
144 else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
145 doing_older_messages = 0;
146 doing_newer_messages = 1;
149 doing_older_messages = 0;
150 doing_newer_messages = 0;
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);
159 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
161 if (BBS->num_msgs > Stat->maxmsgs) {
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))
168 BBS->num_msgs = Stat->maxmsgs;
171 /* GT ... cut it down to the FIRST 20 messages received */
172 BBS->num_msgs = Stat->maxmsgs;
178 /* Supply the link to prepend the previous 20 messages */
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);\">",
190 wc_printf("<div class=\"moreprompt\">"
191 "↑ ↑ ↑ %s ↑ ↑ ↑"
192 "</div>", _("older messages")
201 /* Handle the empty message set gracefully... */
202 if (Stat->nummsgs == 0) {
204 wc_printf("<div class=\"nomsgs\"><br><em>");
205 wc_printf(_("No messages here."));
206 wc_printf("</em><br></div>\n");
210 /* Non-empty message set... */
212 /* Render the messages */
214 for (i=0; i<BBS->num_msgs; ++i) {
215 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
221 /* Supply the link to append the next 20 messages */
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);\">",
229 BBS->msgs[BBS->num_msgs-1],
233 wc_printf("<div class=\"moreprompt\">"
234 "↓ ↓ ↓ %s ↓ ↓ ↓"
235 "</div>", _("newer messages")
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];
245 gt = atol(bstr("gt"));
247 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
252 wc_printf("<div class=\"moreprompt\">");
253 wc_printf("%s", _("no more messages"));
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.
264 wc_printf("<br><br><br><br>");
271 int bbsview_Cleanup(void **ViewSpecific)
273 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
275 if (BBS->alloc_msgs != 0) {
291 InitModule_BBSVIEWRENDERERS
294 RegisterReadLoopHandlerset(
296 bbsview_GetParamsGetServerCall,
297 bbsview_PrintViewHeader,
298 bbsview_LoadMsgFromServer,
299 bbsview_RenderView_or_Tail,