bbsview_renderer readfwd links now include a go= parameter to include the room name
[citadel.git] / webcit / bbsview_renderer.c
1 /* 
2  * BBS View renderer module for WebCit
3  *
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.
8  *
9  * Copyright (c) 1996-2011 by the citadel.org team
10  *
11  * This program is open source software.  You can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 3 of the
14  * License, or (at your option) any later version.
15  *
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.
20  *
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
24  */
25
26 #define RANGE 5
27
28 #include "webcit.h"
29 #include "webserver.h"
30 #include "groupdav.h"
31
32 /*
33  * Data which gets passed around between the various functions in this module
34  *
35  */
36 struct bbsview {
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 */
44 };
45
46
47 /*
48  * Attempt to determine the closest thing to the "last seen message number" using the
49  * results of the GTSN command
50  */
51 long bbsview_get_last_seen(void)
52 {
53         char buf[SIZ] = "0";
54
55         serv_puts("GTSN");
56         serv_getln(buf, sizeof buf);
57         if (buf[0] == '2') {
58                 char *colon_pos;
59                 char *comma_pos;
60
61                 comma_pos = strchr(buf, ',');   /* kill first comma and everything to its right */
62                 if (comma_pos) {
63                         *comma_pos = 0;
64                 }
65
66                 colon_pos = strchr(buf, ':');   /* kill first colon and everything to its left */
67                 if (colon_pos) {
68                         strcpy(buf, ++colon_pos);
69                 }
70         }
71
72         return(atol(buf));
73 }
74
75
76
77 /*
78  * Entry point for message read operations.
79  */
80 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
81                                    void **ViewSpecific, 
82                                    long oper, 
83                                    char *cmd, 
84                                    long len)
85 {
86         struct bbsview *BBS = malloc(sizeof(struct bbsview));
87         memset(BBS, 0, sizeof(struct bbsview));
88         *ViewSpecific = BBS;
89
90         Stat->startmsg = (-1);                                  /* not used here */
91         Stat->sortit = 1;                                       /* not used here */
92         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
93         BBS->requested_page = 0;
94         BBS->lastseen = bbsview_get_last_seen();
95         BBS->start_reading_at = 0;
96
97         /* By default, the requested page is the first one. */
98         if (havebstr("start_reading_at")) {
99                 BBS->start_reading_at = lbstr("start_reading_at");
100                 BBS->requested_page = (-4);
101         }
102
103         /* However, if we are asked to start with a specific message number, make sure
104          * we start on the page containing that message
105          */
106
107         /* Or, if a specific page was requested, make sure we go there */
108         else if (havebstr("page")) {
109                 BBS->requested_page = ibstr("page");
110         }
111
112         /* Otherwise, if this is a "read new" operation, make sure we start on the page
113          * containing the first new message
114          */
115         else if (oper == 3) {
116                 BBS->requested_page = (-3);
117         }
118
119         if (havebstr("maxmsgs")) {
120                 Stat->maxmsgs = ibstr("maxmsgs");
121         }
122         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
123         
124         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
125         rlid[2].cmd(cmd, len);
126         
127         return 200;
128 }
129
130
131 /*
132  * This function is called for every message in the list.
133  */
134 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
135                               void **ViewSpecific, 
136                               message_summary* Msg, 
137                               int is_new, 
138                               int i)
139 {
140         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
141
142         if (BBS->alloc_msgs == 0) {
143                 BBS->alloc_msgs = 1000;
144                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
145                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
146         }
147
148         /* Check our buffer size */
149         if (BBS->num_msgs >= BBS->alloc_msgs) {
150                 BBS->alloc_msgs *= 2;
151                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
152                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
153         }
154
155         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
156
157         return 200;
158 }
159
160
161 int bbsview_sortfunc(const void *s1, const void *s2) {
162         long l1;
163         long l2;
164
165         l1 = *(long *)(s1);
166         l2 = *(long *)(s2);
167
168         if (l1 > l2) return(+1);
169         if (l1 < l2) return(-1);
170         return(0);
171 }
172
173
174 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
175                                void **ViewSpecific, 
176                                long oper)
177 {
178         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
179         int i;
180         int seq;
181         const StrBuf *Mime;
182         int start_index = 0;
183         int end_index = 0;
184         int go_to_the_very_end = 0;
185
186         if (Stat->nummsgs > 0) {
187                 syslog(9, "sorting %d messages\n", BBS->num_msgs);
188                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
189         }
190
191         if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
192                 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
193         }
194         else {
195                 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
196         }
197
198         /* If the requested page number is -4,
199          * it means "whichever page on which msg#xxxxx starts"
200          * Change to the page number which contains that message.
201          */
202         if (BBS->requested_page == (-4)) {
203                 if (BBS->num_msgs == 0) {
204                         BBS->requested_page = 0;
205                 }
206                 else {
207                         for (i=0; i<BBS->num_msgs; ++i) {
208                                 if (
209                                         (BBS->msgs[i] >= BBS->start_reading_at)
210                                         && (BBS->requested_page == (-4))
211                                 ) {
212                                         BBS->requested_page = (i / Stat->maxmsgs) ;
213                                 }
214                         }
215                 }
216         }
217
218         /* If the requested page number is -3,
219          * it means "whichever page on which new messages start"
220          * Change that to an actual page number now.
221          */
222         if (BBS->requested_page == (-3)) {
223                 if (BBS->num_msgs == 0) {
224                         /*
225                          * The room is empty; just start at the top and leave it there.
226                          */
227                         BBS->requested_page = 0;
228                 }
229                 else if (
230                         (BBS->num_msgs > 0) 
231                         && (BBS->lastseen <= BBS->msgs[0])
232                 ) {
233                         /*
234                          * All messages are new; this is probably the user's first visit to the room,
235                          * so start at the last page instead of showing ancient history.
236                          */
237                         BBS->requested_page = BBS->num_pages - 1;
238                         go_to_the_very_end = 1;
239                 }
240                 else {
241                         /*
242                          * Some messages are old and some are new.  Go to the start of new messages.
243                          */
244                         for (i=0; i<BBS->num_msgs; ++i) {
245                                 if (
246                                         (BBS->msgs[i] > BBS->lastseen)
247                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
248                                 ) {
249                                         BBS->requested_page = (i / Stat->maxmsgs) ;
250                                 }
251                         }
252                 }
253         }
254
255         /* Still set to -3 ?  If so, that probably means that there are no new messages,
256          * so we'll go to the *end* of the final page.
257          */
258         if (BBS->requested_page == (-3)) {
259                 if (BBS->num_msgs == 0) {
260                         BBS->requested_page = 0;
261                 }
262                 else {
263                         BBS->requested_page = BBS->num_pages - 1;
264                 }
265         }
266
267         /* keep the requested page within bounds */
268         if (BBS->requested_page < 0) BBS->requested_page = 0;
269         if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
270
271         start_index = BBS->requested_page * Stat->maxmsgs;
272         if (start_index < 0) start_index = 0;
273         end_index = start_index + Stat->maxmsgs - 1;
274
275         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
276
277                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
278                         /* display the selected range of messages */
279
280                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
281                                 if (
282                                         (BBS->msgs[i] > BBS->lastseen)
283                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
284                                 ) {
285                                         /* new messages start here */
286                                         do_template("start_of_new_msgs");
287                                         if (!go_to_the_very_end) {
288                                                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
289                                         }
290                                 }
291                                 if (BBS->msgs[i] > 0L) {
292                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
293                                 }
294                                 if (
295                                         (i == (BBS->num_msgs - 1))
296                                         && (BBS->msgs[i] <= BBS->lastseen)
297                                 ) {
298                                         /* no new messages */
299                                         do_template("no_new_msgs");
300                                         if (!go_to_the_very_end) {
301                                                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
302                                         }
303                                 }
304                         }
305                 }
306
307                 else if ( (seq == 0) || (seq == 2) ) {
308                         int first;
309                         int last;
310                         /* Display the selecto-bar with the page numbers */
311
312                         wc_printf("<div class=\"moreprompt\">");
313                         if (seq == 2) {
314                                 wc_printf("<a name=\"end_of_msgs\">");
315                         }
316                         wc_printf(_("Go to page: "));
317                         if (seq == 2) {
318                                 wc_printf("</a>");
319                         }
320
321                         first = 0;
322                         last = BBS->num_pages - 1;
323
324                         for (i=0; i<=last; ++i) {
325
326                                 if (
327                                         (i == first)
328                                         || (i == last)
329                                         || (i == BBS->requested_page)
330                                         || (
331                                                 ((BBS->requested_page - i) < RANGE)
332                                                 && ((BBS->requested_page - i) > (0 - RANGE))
333                                         )
334                                 ) {
335
336                                         if (
337                                                 (i == last) 
338                                                 && (last - BBS->requested_page > RANGE)
339                                         ) {
340                                                 wc_printf("...&nbsp;");
341                                         }
342                                         if (i == BBS->requested_page) {
343                                                 wc_printf("[");
344                                         }
345                                         else {
346                                                 wc_printf("<a href=\"readfwd?go=");
347                                                 urlescputs(ChrPtr(WC->CurRoom.name));
348                                                 wc_printf("?page=%d\">", i);
349                                                 wc_printf("<span class=\"moreprompt_link\">");
350                                         }
351                                         if (
352                                                 (i == first)
353                                                 && (BBS->requested_page > (RANGE + 1))
354                                         ) {
355                                                 wc_printf(_("First"));
356                                         }
357                                         else if (
358                                                 (i == last)
359                                                 && (last - BBS->requested_page > RANGE)
360                                         ) {
361                                                 wc_printf(_("Last"));
362                                         }
363                                         else {
364                                                 wc_printf("%d", i + 1); /* change to one-based for display */
365                                         }
366                                         if (i == BBS->requested_page) {
367                                                 wc_printf("]");
368                                         }
369                                         else {
370                                                 wc_printf("</span>");
371                                                 wc_printf("</a>");
372                                         }
373                                         if (
374                                                 (i == first)
375                                                 && (BBS->requested_page > (RANGE + 1))
376                                         ) {
377                                                 wc_printf("&nbsp;...");
378                                         }
379                                         if (i != last) {
380                                                 wc_printf("&nbsp;");
381                                         }
382                                 }
383                         }
384                         wc_printf("</div>\n");
385                 }
386         }
387
388         if (go_to_the_very_end) {
389                 StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#end_of_msgs\";\n");
390         }
391         return(0);
392 }
393
394
395 int bbsview_Cleanup(void **ViewSpecific)
396 {
397         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
398
399         if (BBS->alloc_msgs != 0) {
400                 free(BBS->msgs);
401         }
402         free(BBS);
403
404         wDumpContent(1);
405         return 0;
406 }
407
408
409 void 
410 InitModule_BBSVIEWRENDERERS
411 (void)
412 {
413         RegisterReadLoopHandlerset(
414                 VIEW_BBS,
415                 bbsview_GetParamsGetServerCall,
416                 NULL,
417                 NULL, 
418                 bbsview_LoadMsgFromServer,
419                 bbsview_RenderView_or_Tail,
420                 bbsview_Cleanup
421         );
422 }