* If we perform a 'read new' operation in a room with no new messages, display the...
[citadel.git] / webcit / bbsview_renderer.c
1 /* 
2  * $Id$
3  *
4  * BBS View renderer module for WebCit
5  *
6  * Note: we briefly had a dynamic UI for this.  I thought it was cool, but
7  * it was not received well by the user community.  If you want to play
8  * with it, go get r8256 of bbsview_renderer.c and have fun.
9  *
10  * Copyright (c) 1996-2010 by the citadel.org team
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #define RANGE 5
28
29 #include "webcit.h"
30 #include "webserver.h"
31 #include "groupdav.h"
32
33 /*
34  * Data which gets passed around between the various functions in this module
35  *
36  */
37 struct bbsview {
38         long *msgs;             /* Array of msgnums for messages we are displaying */
39         int num_msgs;           /* Number of msgnums stored in 'msgs' */
40         long lastseen;          /* The number of the last seen message in this room */
41         int alloc_msgs;         /* Currently allocated size of array */
42         int requested_page;     /* Which page number did the user request? */
43 };
44
45
46 /*
47  * Attempt to determine the closest thing to the "last seen message number" using the
48  * results of the GTSN command
49  */
50 long bbsview_get_last_seen(void)
51 {
52         char buf[SIZ] = "0";
53
54         serv_puts("GTSN");
55         serv_getln(buf, sizeof buf);
56         if (buf[0] == '2') {
57
58                 char *comma_pos = strchr(buf, ',');     /* kill first comma and everything to its right */
59                 if (comma_pos) {
60                         *comma_pos = 0;
61                 }
62
63                 char *colon_pos = strchr(buf, ':');     /* kill first colon and everything to its left */
64                 if (colon_pos) {
65                         strcpy(buf, ++colon_pos);
66                 }
67         }
68
69         return(atol(buf));
70 }
71
72
73
74 /*
75  * Entry point for message read operations.
76  */
77 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
78                                    void **ViewSpecific, 
79                                    long oper, 
80                                    char *cmd, 
81                                    long len)
82 {
83         struct bbsview *BBS = malloc(sizeof(struct bbsview));
84         memset(BBS, 0, sizeof(struct bbsview));
85         *ViewSpecific = BBS;
86
87         Stat->startmsg = -1;                                    /* not used here */
88         Stat->sortit = 1;                                       /* not used here */
89         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
90         BBS->requested_page = 0;
91         BBS->lastseen = bbsview_get_last_seen();
92
93         /* If a specific page was requested, make sure we go there */
94         if (havebstr("page")) {
95                 BBS->requested_page = ibstr("page");
96         }
97
98         /* Otherwise, if this is a "read new" operation, make sure we start on the page
99          * containing the first new message
100          */
101         else if (oper == 3) {
102                 BBS->requested_page = (-3);
103         }
104
105         if (havebstr("maxmsgs")) {
106                 Stat->maxmsgs = ibstr("maxmsgs");
107         }
108         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
109         
110         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
111         rlid[2].cmd(cmd, len);
112         
113         return 200;
114 }
115
116
117 /*
118  * This function is called for every message in the list.
119  */
120 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
121                               void **ViewSpecific, 
122                               message_summary* Msg, 
123                               int is_new, 
124                               int i)
125 {
126         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
127
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)) );
132         }
133
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)) );
139         }
140
141         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
142
143         return 200;
144 }
145
146
147 int bbsview_sortfunc(const void *s1, const void *s2) {
148         long l1;
149         long l2;
150
151         l1 = *(long *)(s1);
152         l2 = *(long *)(s2);
153
154         if (l1 > l2) return(+1);
155         if (l1 < l2) return(-1);
156         return(0);
157 }
158
159
160 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
161                                void **ViewSpecific, 
162                                long oper)
163 {
164         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
165         int i;
166         int seq;
167         const StrBuf *Mime;
168         int start_index = 0;
169         int end_index = 0;
170
171         if (Stat->nummsgs > 0) {
172                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
173                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
174         }
175
176         /* If the requested page number is "whichever page on which new messages start"
177          * then change that to an actual page number now.
178          */
179         if (BBS->requested_page == (-3)) {
180                 if (BBS->num_msgs == 0) {
181                         BBS->requested_page = 0;
182                 }
183                 else {
184                         for (i=0; i<BBS->num_msgs; ++i) {
185                                 if (
186                                         (BBS->msgs[i] > BBS->lastseen)
187                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
188                                 ) {
189                                         BBS->requested_page = (i / Stat->maxmsgs) ;
190                                 }
191                         }
192                 }
193         }
194
195         /* Still set to -3 ?  If so, that probably means that there are no new messages,
196          * so we'll go to the *end* of the final page.
197          */
198         if (BBS->requested_page == (-3)) {
199                 if (BBS->num_msgs == 0) {
200                         BBS->requested_page = 0;
201                 }
202                 else {
203                         BBS->requested_page = (BBS->num_msgs / Stat->maxmsgs);
204                 }
205         }
206
207         start_index = BBS->requested_page * Stat->maxmsgs;
208         if (start_index < 0) start_index = 0;
209         end_index = start_index + Stat->maxmsgs - 1;
210
211         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
212
213                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
214                         /* display the selected range of messages */
215
216                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
217                                 if (
218                                         (
219                                                 (BBS->msgs[i] > BBS->lastseen)
220                                                 && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
221                                         )
222                                         || (
223                                                 (i == (BBS->num_msgs - 1))
224                                                 && (BBS->msgs[i] <= BBS->lastseen)
225                                         )
226                                 ) {
227                                         /* new messages start here */
228                                         wc_printf("<a name=\"newmsgs\">");
229                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
230                                 }
231                                 if (BBS->msgs[i] > 0L) {
232                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
233                                 }
234                         }
235                 }
236
237                 else if ( (seq == 0) || (seq == 2) ) {
238                         /* Display the selecto-bar with the page numbers */
239
240                         wc_printf("<div class=\"moreprompt\">");
241                         wc_printf(_("Go to page: "));
242
243                         int first = 0;
244                         int last = ( (Stat->maxmsgs > 0) ? (BBS->num_msgs / Stat->maxmsgs) : 0 );
245
246                         for (i=0; i<=last; ++i) {
247
248                                 if (
249                                         (i == first)
250                                         || (i == last)
251                                         || (i == BBS->requested_page)
252                                         || (
253                                                 ((BBS->requested_page - i) < RANGE)
254                                                 && ((BBS->requested_page - i) > (0 - RANGE))
255                                         )
256                                 ) {
257
258                                         if (
259                                                 (i == last) 
260                                                 && (last - BBS->requested_page > RANGE)
261                                         ) {
262                                                 wc_printf("...&nbsp;");
263                                         }
264                                         if (i == BBS->requested_page) {
265                                                 wc_printf("[");
266                                         }
267                                         else {
268                                                 wc_printf("<a href=\"readfwd?page=%d\">", i);
269                                                 wc_printf("<span class=\"moreprompt_link\">");
270                                         }
271                                         if (
272                                                 (i == first)
273                                                 && (BBS->requested_page > (RANGE + 1))
274                                         ) {
275                                                 wc_printf(_("First"));
276                                         }
277                                         else if (
278                                                 (i == last)
279                                                 && (last - BBS->requested_page > RANGE)
280                                         ) {
281                                                 wc_printf(_("Last"));
282                                         }
283                                         else {
284                                                 wc_printf("%d", i + 1); // change to one-based for display
285                                         }
286                                         if (i == BBS->requested_page) {
287                                                 wc_printf("]");
288                                         }
289                                         else {
290                                                 wc_printf("</span>");
291                                                 wc_printf("</a>");
292                                         }
293                                         if (
294                                                 (i == first)
295                                                 && (BBS->requested_page > (RANGE + 1))
296                                         ) {
297                                                 wc_printf("&nbsp;...");
298                                         }
299                                         if (i != last) {
300                                                 wc_printf("&nbsp;");
301                                         }
302                                 }
303                         }
304                         wc_printf("</div>\n");
305                 }
306         }
307
308         return(0);
309 }
310
311
312 int bbsview_Cleanup(void **ViewSpecific)
313 {
314         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
315
316         if (BBS->alloc_msgs != 0) {
317                 free(BBS->msgs);
318         }
319         free(BBS);
320
321         wDumpContent(1);
322         return 0;
323 }
324
325
326 void 
327 InitModule_BBSVIEWRENDERERS
328 (void)
329 {
330         RegisterReadLoopHandlerset(
331                 VIEW_BBS,
332                 bbsview_GetParamsGetServerCall,
333                 NULL,
334                 NULL, 
335                 bbsview_LoadMsgFromServer,
336                 bbsview_RenderView_or_Tail,
337                 bbsview_Cleanup
338         );
339 }