* New new BBS view
[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  * We do this weird "pivot point" thing instead of starting the page numbers at 0 or 1 so that
37  * the border between old and new messages always falls on a page boundary.  We'll renumber them
38  * to page numbers starting at 1 when presenting them to the user.
39  */
40 struct bbsview {
41         long *msgs;             /* Array of msgnums for messages we are displaying */
42         int num_msgs;           /* Number of msgnums stored in 'msgs' */
43         int alloc_msgs;         /* Currently allocated size of array */
44         long pivot_msgnum;      /* Page numbers are relative to this message number */
45         int requested_page;     /* Which page number did the user request? */
46 };
47
48
49 /*
50  * Attempt to determine the closest thing to the "last seen message number" using the
51  * results of the GTSN command
52  */
53 long bbsview_get_last_seen(void)
54 {
55         char buf[SIZ] = "0";
56
57         serv_puts("GTSN");
58         serv_getln(buf, sizeof buf);
59         if (buf[0] == '2') {
60
61                 char *comma_pos = strchr(buf, ',');     /* kill first comma and everything to its right */
62                 if (comma_pos) {
63                         *comma_pos = 0;
64                 }
65
66                 char *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
95         if (havebstr("page")) {
96                 BBS->requested_page = ibstr("page");
97         }
98         
99         if (havebstr("pivot")) {
100                 BBS->pivot_msgnum = ibstr("pivot");
101         }
102         else if (oper == 2) {   /* 2 == "read all" (otherwise we pivot at the beginning of new msgs) */
103                 BBS->pivot_msgnum = 0;                          /* start from the top */
104         }
105         else {
106                 BBS->pivot_msgnum = bbsview_get_last_seen();
107         }
108
109         if (havebstr("maxmsgs")) {
110                 Stat->maxmsgs = ibstr("maxmsgs");
111         }
112         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
113         
114         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
115         rlid[2].cmd(cmd, len);
116         
117         return 200;
118 }
119
120
121 /*
122  * This function is called for every message in the list.
123  */
124 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
125                               void **ViewSpecific, 
126                               message_summary* Msg, 
127                               int is_new, 
128                               int i)
129 {
130         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
131
132         if (BBS->alloc_msgs == 0) {
133                 BBS->alloc_msgs = 1000;
134                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
135         }
136
137         /* Check our buffer size */
138         if (BBS->num_msgs >= BBS->alloc_msgs) {
139                 BBS->alloc_msgs *= 2;
140                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
141         }
142
143         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
144
145         return 200;
146 }
147
148
149 int bbsview_sortfunc(const void *s1, const void *s2) {
150         long l1;
151         long l2;
152
153         l1 = *(long *)(s1);
154         l2 = *(long *)(s2);
155
156         if (l1 > l2) return(+1);
157         if (l1 < l2) return(-1);
158         return(0);
159 }
160
161
162 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
163                                void **ViewSpecific, 
164                                long oper)
165 {
166         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
167         int i;
168         int seq;
169         const StrBuf *Mime;
170         int pivot_index = 0;
171         int page_offset = 0;
172         int start_index = 0;
173         int end_index = 0;
174
175         /* Cut the message list down to the requested size */
176         if (Stat->nummsgs > 0) {
177                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
178                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
179
180                 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
181
182                 if (BBS->num_msgs > Stat->maxmsgs) {
183
184                         /* Locate the pivot point in our message index */
185                         for (i=0; i<(BBS->num_msgs); ++i) {
186                                 if (BBS->msgs[i] <= BBS->pivot_msgnum) {
187                                         pivot_index = i;
188                                 }
189                         }
190
191                         page_offset = (pivot_index / Stat->maxmsgs) + 2;
192
193                 }
194         }
195
196         start_index = pivot_index + (BBS->requested_page * Stat->maxmsgs) ;
197         if (start_index < 0) start_index = 0;
198         end_index = start_index + Stat->maxmsgs - 1;
199
200         for (seq = 0; seq < 3; ++seq) {         /* cheap and sleazy way of rendering the funbar twice */
201
202                 if (seq == 1) {
203                         /* display the selected range of messages */
204
205                         if (Stat->nummsgs > 0) {
206                                 wc_printf("<font size=\"-1\">\n");
207                                 for (i=start_index; (i<=end_index && i<=BBS->num_msgs); ++i) {
208                                         if (BBS->msgs[i] > 0L) {
209                                                 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
210                                         }
211                                 }
212                                 wc_printf("</font><br>\n");
213                         }
214                 }
215                 else {
216                         /* Display the range selecto-bar */
217
218                         wc_printf("<div class=\"moreprompt\">");
219                         wc_printf(_("Go to page: "));
220
221                         int first = 1;
222                         int last = (BBS->num_msgs / Stat->maxmsgs) + 2 ;
223
224                         for (i=1; i<=last; ++i) {
225
226                                 if (
227                                         (i == first)
228                                         || (i == last)
229                                         || ((i - page_offset) == BBS->requested_page)
230                                         || (
231                                                 ((BBS->requested_page - (i - page_offset)) < RANGE)
232                                                 && ((BBS->requested_page - (i - page_offset)) > (0 - RANGE))
233                                         )
234                                 ) {
235
236                                         if (
237                                                 (i == last) 
238                                                 && (last - (BBS->requested_page + page_offset) > RANGE)
239                                         ) {
240                                                 wc_printf("...&nbsp;");
241                                         }
242                                         if ((i - page_offset) == BBS->requested_page) {
243                                                 wc_printf("[");
244                                         }
245                                         else {
246                                                 wc_printf("<a href=\"readfwd?pivot=%ld?page=%d\">",
247                                                         BBS->pivot_msgnum,
248                                                         i - page_offset
249                                                 );
250                                                 wc_printf("<span class=\"moreprompt_link\">");
251                                         }
252                                         if (
253                                                 (i == first)
254                                                 && ((BBS->requested_page + page_offset) > (RANGE + 1))
255                                         ) {
256                                                 wc_printf(_("First"));
257                                         }
258                                         else if (
259                                                 (i == last)
260                                                 && (last - (BBS->requested_page + page_offset) > RANGE)
261                                         ) {
262                                                 wc_printf(_("Last"));
263                                         }
264                                         else {
265                                                 wc_printf("%d", i);
266                                         }
267                                         if ((i - page_offset) == BBS->requested_page) {
268                                                 wc_printf("]");
269                                         }
270                                         else {
271                                                 wc_printf("</span>");
272                                                 wc_printf("</a>");
273                                         }
274                                         if (
275                                                 (i == first)
276                                                 && ((BBS->requested_page + page_offset) > (RANGE + 1))
277                                         ) {
278                                                 wc_printf("&nbsp;...");
279                                         }
280                                         if (i != last) {
281                                                 wc_printf("&nbsp;");
282                                         }
283                                 }
284                         }
285                         wc_printf("</div>\n");
286                 }
287         }
288
289         return(0);
290 }
291
292
293 int bbsview_Cleanup(void **ViewSpecific)
294 {
295         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
296
297         if (BBS->alloc_msgs != 0) {
298                 free(BBS->msgs);
299         }
300         free(BBS);
301
302         wDumpContent(1);
303         return 0;
304 }
305
306
307 void 
308 InitModule_BBSVIEWRENDERERS
309 (void)
310 {
311         RegisterReadLoopHandlerset(
312                 VIEW_BBS,
313                 bbsview_GetParamsGetServerCall,
314                 NULL,
315                 NULL, 
316                 bbsview_LoadMsgFromServer,
317                 bbsview_RenderView_or_Tail,
318                 bbsview_Cleanup
319         );
320 }