3 * BBS View renderer module for WebCit
5 * Note: we briefly had a dynamic UI for this. I thought it was cool, but
6 * it was not received well by the user community. If you want to play
7 * with it, go get commit dcf99fe61379b78436c387ea3f89ebfd4ffaf635 of
8 * bbsview_renderer.c and have fun.
10 * Copyright (c) 1996-2012 by the citadel.org team
12 * This program is open source software. You can redistribute it and/or
13 * modify it under the terms of the GNU General Public License, version 3.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 #include "webserver.h"
28 * Data which gets passed around between the various functions in this module
32 long *msgs; /* Array of msgnums for messages we are displaying */
33 int num_msgs; /* Number of msgnums stored in 'msgs' */
34 long lastseen; /* The number of the last seen message in this room */
35 int alloc_msgs; /* Currently allocated size of array */
36 int requested_page; /* Which page number did the user request? */
37 int num_pages; /* Total number of pages in this room */
38 long start_reading_at; /* Start reading at the page containing this message */
43 * Attempt to determine the closest thing to the "last seen message number" using the
44 * results of the GTSN command
46 long bbsview_get_last_seen(void) {
50 serv_getln(buf, sizeof buf);
55 comma_pos = strchr(buf, ','); /* kill first comma and everything to its right */
60 colon_pos = strchr(buf, ':'); /* kill first colon and everything to its left */
62 strcpy(buf, ++colon_pos);
72 * Entry point for message read operations.
74 int bbsview_GetParamsGetServerCall(SharedMessageStatus * Stat,
75 void **ViewSpecific, long oper, char *cmd, long len, char *filter, long flen) {
76 struct bbsview *BBS = malloc(sizeof(struct bbsview));
77 memset(BBS, 0, sizeof(struct bbsview));
80 Stat->startmsg = (-1); /* not used here */
81 Stat->sortit = 1; /* not used here */
82 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
83 BBS->requested_page = 0;
84 BBS->lastseen = bbsview_get_last_seen();
85 BBS->start_reading_at = 0;
87 /* By default, the requested page is the first one. */
88 if (havebstr("start_reading_at")) {
89 BBS->start_reading_at = lbstr("start_reading_at");
90 BBS->requested_page = (-4);
93 /* However, if we are asked to start with a specific message number, make sure
94 * we start on the page containing that message
97 /* Or, if a specific page was requested, make sure we go there */
98 else if (havebstr("page")) {
99 BBS->requested_page = ibstr("page");
102 /* Otherwise, if this is a "read new" operation, make sure we start on the page
103 * containing the first new message
105 else if (oper == 3) {
106 BBS->requested_page = (-3);
109 if (havebstr("maxmsgs")) {
110 Stat->maxmsgs = ibstr("maxmsgs");
112 if (Stat->maxmsgs == 0)
113 Stat->maxmsgs = DEFAULT_MAXMSGS;
115 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
116 rlid[2].cmd(cmd, len);
123 * This function is called for every message in the list.
125 int bbsview_LoadMsgFromServer(SharedMessageStatus * Stat, void **ViewSpecific, message_summary * Msg, int is_new, int i) {
126 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
128 if (BBS->alloc_msgs == 0) {
129 BBS->alloc_msgs = 1000;
130 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
131 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)));
134 /* Check our buffer size */
135 if (BBS->num_msgs >= BBS->alloc_msgs) {
136 BBS->alloc_msgs *= 2;
137 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
138 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)));
141 BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
147 int bbsview_sortfunc(const void *s1, const void *s2) {
162 int bbsview_RenderView_or_Tail(SharedMessageStatus * Stat, void **ViewSpecific, long oper) {
163 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
169 int go_to_the_very_end = 0;
171 if (Stat->nummsgs > 0) {
172 syslog(LOG_DEBUG, "sorting %d messages\n", BBS->num_msgs);
173 qsort(BBS->msgs, (size_t) (BBS->num_msgs), sizeof(long), bbsview_sortfunc);
176 if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
177 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
180 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
183 /* If the requested page number is -4,
184 * it means "whichever page on which msg#xxxxx starts"
185 * Change to the page number which contains that message.
187 if (BBS->requested_page == (-4)) {
188 if (BBS->num_msgs == 0) {
189 BBS->requested_page = 0;
192 for (i = 0; i < BBS->num_msgs; ++i) {
193 if ((BBS->msgs[i] >= BBS->start_reading_at)
194 && (BBS->requested_page == (-4))
196 BBS->requested_page = (i / Stat->maxmsgs);
202 /* If the requested page number is -3,
203 * it means "whichever page on which new messages start"
204 * Change that to an actual page number now.
206 if (BBS->requested_page == (-3)) {
207 if (BBS->num_msgs == 0) {
209 * The room is empty; just start at the top and leave it there.
211 BBS->requested_page = 0;
213 else if ((BBS->num_msgs > 0)
214 && (BBS->lastseen <= BBS->msgs[0])
217 * All messages are new; this is probably the user's first visit to the room,
218 * so start at the last page instead of showing ancient history.
220 BBS->requested_page = BBS->num_pages - 1;
221 go_to_the_very_end = 1;
225 * Some messages are old and some are new. Go to the start of new messages.
227 for (i = 0; i < BBS->num_msgs; ++i) {
228 if ((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)
251 BBS->requested_page = 0;
252 if (BBS->requested_page >= BBS->num_pages)
253 BBS->requested_page = BBS->num_pages - 1;
255 start_index = BBS->requested_page * Stat->maxmsgs;
258 end_index = start_index + Stat->maxmsgs - 1;
260 for (seq = 0; seq < 3; ++seq) { /* cheap & sleazy way of rendering the page numbers twice */
262 if ((seq == 1) && (Stat->nummsgs > 0)) {
263 /* display the selected range of messages */
265 for (i = start_index; (i <= end_index && i < BBS->num_msgs); ++i) {
266 if ((BBS->msgs[i] > BBS->lastseen)
267 && ((i == 0) || (BBS->msgs[i - 1] <= BBS->lastseen))
269 /* new messages start here */
270 do_template("start_of_new_msgs");
271 if (!go_to_the_very_end) {
272 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
275 if (BBS->msgs[i] > 0L) {
276 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime, NULL);
278 if ((i == (BBS->num_msgs - 1))
279 && (BBS->msgs[i] <= BBS->lastseen)
281 /* no new messages */
282 do_template("no_new_msgs");
283 if (!go_to_the_very_end) {
284 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
290 else if ((seq == 0) || (seq == 2)) {
293 /* Display the selecto-bar with the page numbers */
295 wc_printf("<div class=\"moreprompt\">");
297 wc_printf("<a name=\"end_of_msgs\">");
299 wc_printf(_("Go to page: "));
305 last = BBS->num_pages - 1;
307 for (i = 0; i <= last; ++i) {
311 || (i == BBS->requested_page)
312 || (((BBS->requested_page - i) < RANGE)
313 && ((BBS->requested_page - i) > (0 - RANGE))
318 && (last - BBS->requested_page > RANGE)
320 wc_printf("... ");
322 if (i == BBS->requested_page) {
326 wc_printf("<a href=\"readfwd?go=");
327 urlescputs(ChrPtr(WC->CurRoom.name));
328 wc_printf("?start_reading_at=%ld\">", BBS->msgs[i * Stat->maxmsgs]
330 /* wc_printf("?page=%d\">", i); */
331 wc_printf("<span class=\"moreprompt_link\">");
334 && (BBS->requested_page > (RANGE + 1))
336 wc_printf(_("First"));
339 && (last - BBS->requested_page > RANGE)
341 wc_printf(_("Last"));
344 wc_printf("%d", i + 1); /* change to one-based for display */
346 if (i == BBS->requested_page) {
350 wc_printf("</span>");
354 && (BBS->requested_page > (RANGE + 1))
356 wc_printf(" ...");
363 wc_printf("</div>\n");
367 if (go_to_the_very_end) {
368 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#end_of_msgs\";\n");
374 int bbsview_Cleanup(void **ViewSpecific) {
375 struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
377 if (BBS->alloc_msgs != 0) {
387 void InitModule_BBSVIEWRENDERERS(void) {
388 RegisterReadLoopHandlerset(VIEW_BBS,
389 bbsview_GetParamsGetServerCall,
390 NULL, NULL, NULL, bbsview_LoadMsgFromServer, bbsview_RenderView_or_Tail, bbsview_Cleanup, NULL);