4 * BBS View renderer module for WebCit
6 * Copyright (c) 1996-2010 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 */
34 long lastseen; /* msgnum of the last seen message in this room */
39 * Attempt to determine the closest thing to the "last seen message number" using the
40 * results of the GTSN command
42 long bbsview_get_last_seen(void)
47 serv_getln(buf, sizeof buf);
50 char *comma_pos = strchr(buf, ','); /* kill first comma and everything to its right */
55 char *colon_pos = strchr(buf, ':'); /* kill first colon and everything to its left */
57 strcpy(buf, ++colon_pos);
67 * Entry point for message read operations.
69 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat,
75 struct bbsview *BBS = malloc(sizeof(struct bbsview));
76 memset(BBS, 0, sizeof(struct bbsview));
81 BBS->lastseen = bbsview_get_last_seen(); /* FIXME do something with this */
83 rlid[oper].cmd(cmd, len); /* this performs the server call to fetch the msg list */
85 if (havebstr("maxmsgs")) {
86 Stat->maxmsgs = ibstr("maxmsgs");
88 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
89 Stat->num_displayed = DEFAULT_MAXMSGS;
91 if (havebstr("startmsg")) {
92 Stat->startmsg = lbstr("startmsg");
100 * begin_ajax_response() was moved from bbsview_LoadMsgFromServer() to here ...
102 int bbsview_PrintViewHeader(SharedMessageStatus *Stat, void **ViewSpecific)
105 begin_ajax_response(); /* for non-ajax, headers are output in messages.c */
113 * This function is called for every message in the list.
115 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat,
117 message_summary* Msg,
121 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
123 if (BBS->alloc_msgs == 0) {
124 BBS->alloc_msgs = Stat->maxmsgs;
125 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
128 /* Theoretically this never happens because the initial allocation == maxmsgs */
129 if (BBS->num_msgs >= BBS->alloc_msgs) {
130 BBS->alloc_msgs *= 2;
131 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
134 BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
139 int bbsview_sortfunc(const void *s1, const void *s2) {
146 if (l1 > l2) return(+1);
147 if (l1 < l2) return(-1);
152 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
156 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
161 int doing_older_messages = 0;
162 int doing_newer_messages = 0;
164 int increments[] = { 20, 50, 100 } ;
165 #define NUM_INCREMENTS (sizeof(increments) / sizeof(int))
167 snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
168 snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
170 /* Determine whether we are in the middle of a 'click for older messages' or 'click for
171 * newer messages' operation. If neither, then we are in the initial page load.
173 if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
174 doing_older_messages = 1;
175 doing_newer_messages = 0;
177 else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
178 doing_older_messages = 0;
179 doing_newer_messages = 1;
182 doing_older_messages = 0;
183 doing_newer_messages = 0;
187 /* Cut the message list down to the requested size */
188 if (Stat->nummsgs > 0) {
189 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
190 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
192 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
194 if (BBS->num_msgs > Stat->maxmsgs) {
196 if (doing_older_messages) {
197 /* LT ... cut it down to the LAST 20 messages received */
198 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
199 (Stat->maxmsgs * sizeof(long))
201 BBS->num_msgs = Stat->maxmsgs;
204 /* GT ... cut it down to the FIRST 20 messages received */
205 BBS->num_msgs = Stat->maxmsgs;
211 /* Supply the link to prepend the previous 20 messages */
213 if ((!WC->is_ajax) && (Stat->nummsgs == 0)) {
214 wc_printf("<div id=\"%s\">", olderdiv);
215 wc_printf("<div class=\"moreprompt\">");
216 for (i=0; i<NUM_INCREMENTS; ++i) {
217 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %d);\">",
222 wc_printf("<span class=\"moreprompt_link\">↑ ");
223 wc_printf(_("Previous %d"), increments[i]);
224 wc_printf(" ↑</span>");
228 wc_printf("<div class=\"nomsgs\"><br><em>");
229 wc_printf(_("No messages here."));
230 wc_printf("</em><br></div>\n");
233 else if (doing_newer_messages == 0) {
234 wc_printf("<div id=\"%s\">", olderdiv);
235 wc_printf("<div class=\"moreprompt\">");
236 if (Stat->nummsgs > 0) {
237 for (i=0; i<NUM_INCREMENTS; ++i) {
238 wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %d);\">",
243 wc_printf("<span class=\"moreprompt_link\">↑ ");
244 wc_printf(_("Previous %d"), increments[i]);
245 wc_printf(" ↑</span>");
253 /* Non-empty message set... */
254 if (Stat->nummsgs > 0) {
255 /* Render the messages */
257 for (i=0; i<BBS->num_msgs; ++i) {
258 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
264 /* Supply the link to append the next 20 messages */
266 if (doing_older_messages == 0) {
267 wc_printf("<div id=\"%s\">", newerdiv);
268 if (Stat->nummsgs >= Stat->maxmsgs) {
269 wc_printf("<div class=\"moreprompt\">");
270 for (i=0; i<NUM_INCREMENTS; ++i) {
271 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %d);\">",
273 BBS->msgs[BBS->num_msgs-1],
276 wc_printf("<span class=\"moreprompt_link\">↓ ");
277 wc_printf(_("Next %d"), increments[i]);
278 wc_printf(" ↓</span>");
284 long gt = 0; /* if new messages appear later, where will they begin? */
285 if (Stat->nummsgs > 0) {
286 gt = BBS->msgs[BBS->num_msgs-1];
289 gt = atol(bstr("gt"));
291 wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld);\">",
296 wc_printf("<div class=\"moreprompt\">");
297 wc_printf("<span class=\"moreprompt_link\">↓ ");
298 wc_printf("%s", _("no more messages"));
299 wc_printf(" ↓</span>");
306 /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
307 * adding it every time we extend the visible message set.
310 wc_printf("<br><br><br><br>");
317 int bbsview_Cleanup(void **ViewSpecific)
319 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
321 if (BBS->alloc_msgs != 0) {
337 InitModule_BBSVIEWRENDERERS
340 RegisterReadLoopHandlerset(
342 bbsview_GetParamsGetServerCall,
343 bbsview_PrintViewHeader,
345 bbsview_LoadMsgFromServer,
346 bbsview_RenderView_or_Tail,