2 * BBS View renderer module for WebCit
4 * Note: we briefly had a dynamic UI for this. I thought it was cool, but
5 * it was not received well by the user community. If you want to play
6 * with it, go get commit dcf99fe61379b78436c387ea3f89ebfd4ffaf635 of
7 * bbsview_renderer.c and have fun.
9 * Copyright (c) 1996-2010 by the citadel.org team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "webserver.h"
33 * Data which gets passed around between the various functions in this module
37 long *msgs; /* Array of msgnums for messages we are displaying */
38 int num_msgs; /* Number of msgnums stored in 'msgs' */
39 long lastseen; /* The number of the last seen message in this room */
40 int alloc_msgs; /* Currently allocated size of array */
41 int requested_page; /* Which page number did the user request? */
42 int num_pages; /* Total number of pages in this room */
43 long start_reading_at; /* Start reading at the page containing this message */
48 * Attempt to determine the closest thing to the "last seen message number" using the
49 * results of the GTSN command
51 long bbsview_get_last_seen(void)
56 serv_getln(buf, sizeof buf);
59 char *comma_pos = strchr(buf, ','); /* kill first comma and everything to its right */
64 char *colon_pos = strchr(buf, ':'); /* kill first colon and everything to its left */
66 strcpy(buf, ++colon_pos);
76 * Entry point for message read operations.
78 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat,
84 struct bbsview *BBS = malloc(sizeof(struct bbsview));
85 memset(BBS, 0, sizeof(struct bbsview));
88 Stat->startmsg = (-1); /* not used here */
89 Stat->sortit = 1; /* not used here */
90 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
91 BBS->requested_page = 0;
92 BBS->lastseen = bbsview_get_last_seen();
93 BBS->start_reading_at = 0;
95 /* By default, the requested page is the first one. */
96 if (havebstr("start_reading_at")) {
97 BBS->start_reading_at = lbstr("start_reading_at");
98 BBS->requested_page = (-4);
101 /* However, if we are asked to start with a specific message number, make sure
102 * we start on the page containing that message
105 /* Or, if a specific page was requested, make sure we go there */
106 else if (havebstr("page")) {
107 BBS->requested_page = ibstr("page");
110 /* Otherwise, if this is a "read new" operation, make sure we start on the page
111 * containing the first new message
113 else if (oper == 3) {
114 BBS->requested_page = (-3);
117 if (havebstr("maxmsgs")) {
118 Stat->maxmsgs = ibstr("maxmsgs");
120 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
122 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
123 rlid[2].cmd(cmd, len);
130 * This function is called for every message in the list.
132 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat,
134 message_summary* Msg,
138 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
140 if (BBS->alloc_msgs == 0) {
141 BBS->alloc_msgs = 1000;
142 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
143 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
146 /* Check our buffer size */
147 if (BBS->num_msgs >= BBS->alloc_msgs) {
148 BBS->alloc_msgs *= 2;
149 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
150 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
153 BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
159 int bbsview_sortfunc(const void *s1, const void *s2) {
166 if (l1 > l2) return(+1);
167 if (l1 < l2) return(-1);
172 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
176 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
183 wc_printf("<div class=\"fix_scrollbar_bug\">");
185 if (Stat->nummsgs > 0) {
186 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
187 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
190 if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
191 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
194 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
197 /* If the requested page number is -4,
198 * it means "whichever page on which msg#xxxxx starts"
199 * Change to the page number which contains that message.
201 if (BBS->requested_page == (-4)) {
202 if (BBS->num_msgs == 0) {
203 BBS->requested_page = 0;
206 for (i=0; i<BBS->num_msgs; ++i) {
208 (BBS->msgs[i] >= BBS->start_reading_at)
209 && (BBS->requested_page == (-4))
211 BBS->requested_page = (i / Stat->maxmsgs) ;
217 /* If the requested page number is -3,
218 * it means "whichever page on which new messages start"
219 * Change that to an actual page number now.
221 if (BBS->requested_page == (-3)) {
222 if (BBS->num_msgs == 0) {
223 BBS->requested_page = 0;
226 for (i=0; i<BBS->num_msgs; ++i) {
228 (BBS->msgs[i] > BBS->lastseen)
229 && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
231 BBS->requested_page = (i / Stat->maxmsgs) ;
237 /* Still set to -3 ? If so, that probably means that there are no new messages,
238 * so we'll go to the *end* of the final page.
240 if (BBS->requested_page == (-3)) {
241 if (BBS->num_msgs == 0) {
242 BBS->requested_page = 0;
245 BBS->requested_page = BBS->num_pages - 1;
249 /* keep the requested page within bounds */
250 if (BBS->requested_page < 0) BBS->requested_page = 0;
251 if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
253 start_index = BBS->requested_page * Stat->maxmsgs;
254 if (start_index < 0) start_index = 0;
255 end_index = start_index + Stat->maxmsgs - 1;
257 for (seq = 0; seq < 3; ++seq) { /* cheap & sleazy way of rendering the page numbers twice */
259 if ( (seq == 1) && (Stat->nummsgs > 0)) {
260 /* display the selected range of messages */
262 for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
264 (BBS->msgs[i] > BBS->lastseen)
265 && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
267 /* new messages start here */
268 do_template("start_of_new_msgs", NULL);
269 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
271 if (BBS->msgs[i] > 0L) {
272 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
275 (i == (BBS->num_msgs - 1))
276 && (BBS->msgs[i] <= BBS->lastseen)
278 /* no new messages */
279 do_template("no_new_msgs", NULL);
280 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
285 else if ( (seq == 0) || (seq == 2) ) {
286 /* Display the selecto-bar with the page numbers */
288 wc_printf("<div class=\"moreprompt\">");
289 wc_printf(_("Go to page: "));
292 int last = BBS->num_pages - 1;
294 for (i=0; i<=last; ++i) {
299 || (i == BBS->requested_page)
301 ((BBS->requested_page - i) < RANGE)
302 && ((BBS->requested_page - i) > (0 - RANGE))
308 && (last - BBS->requested_page > RANGE)
310 wc_printf("... ");
312 if (i == BBS->requested_page) {
316 wc_printf("<a href=\"readfwd?page=%d\">", i);
317 wc_printf("<span class=\"moreprompt_link\">");
321 && (BBS->requested_page > (RANGE + 1))
323 wc_printf(_("First"));
327 && (last - BBS->requested_page > RANGE)
329 wc_printf(_("Last"));
332 wc_printf("%d", i + 1); // change to one-based for display
334 if (i == BBS->requested_page) {
338 wc_printf("</span>");
343 && (BBS->requested_page > (RANGE + 1))
345 wc_printf(" ...");
352 wc_printf("</div>\n");
356 wc_printf("</div>\n");
361 int bbsview_Cleanup(void **ViewSpecific)
363 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
365 if (BBS->alloc_msgs != 0) {
376 InitModule_BBSVIEWRENDERERS
379 RegisterReadLoopHandlerset(
381 bbsview_GetParamsGetServerCall,
384 bbsview_LoadMsgFromServer,
385 bbsview_RenderView_or_Tail,