* Greatly simplified the bbsview code by eliminating the concept of using a 'pivot...
[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
92         if (havebstr("page")) {
93                 BBS->requested_page = ibstr("page");
94         }
95         
96         // FIXME do something with bbsview_get_last_seen();
97
98         if (havebstr("maxmsgs")) {
99                 Stat->maxmsgs = ibstr("maxmsgs");
100         }
101         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
102         
103         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
104         rlid[2].cmd(cmd, len);
105         
106         return 200;
107 }
108
109
110 /*
111  * This function is called for every message in the list.
112  */
113 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
114                               void **ViewSpecific, 
115                               message_summary* Msg, 
116                               int is_new, 
117                               int i)
118 {
119         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
120
121         if (BBS->alloc_msgs == 0) {
122                 BBS->alloc_msgs = 1000;
123                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
124                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
125         }
126
127         /* Check our buffer size */
128         if (BBS->num_msgs >= BBS->alloc_msgs) {
129                 BBS->alloc_msgs *= 2;
130                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
131                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
132         }
133
134         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
135
136         return 200;
137 }
138
139
140 int bbsview_sortfunc(const void *s1, const void *s2) {
141         long l1;
142         long l2;
143
144         l1 = *(long *)(s1);
145         l2 = *(long *)(s2);
146
147         if (l1 > l2) return(+1);
148         if (l1 < l2) return(-1);
149         return(0);
150 }
151
152
153 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
154                                void **ViewSpecific, 
155                                long oper)
156 {
157         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
158         int i;
159         int seq;
160         const StrBuf *Mime;
161         int start_index = 0;
162         int end_index = 0;
163
164         if (Stat->nummsgs > 0) {
165                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
166                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
167         }
168
169         start_index = BBS->requested_page * Stat->maxmsgs;
170         if (start_index < 0) start_index = 0;
171         end_index = start_index + Stat->maxmsgs - 1;
172
173         for (seq = 0; seq < 3; ++seq) {         /* cheap and sleazy way of rendering the funbar twice */
174
175                 if (seq == 1) {
176                         /* display the selected range of messages */
177
178                         if (Stat->nummsgs > 0) {
179                                 for (i=start_index; (i<=end_index && i<=BBS->num_msgs); ++i) {
180                                         if (BBS->msgs[i] > 0L) {
181                                                 read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
182                                         }
183                                 }
184                         }
185                 }
186                 else {
187                         /* Display the range selecto-bar */
188
189                         wc_printf("<div class=\"moreprompt\">");
190                         wc_printf(_("Go to page: "));
191
192                         int first = 0;
193                         int last = ( (Stat->maxmsgs > 0) ? (BBS->num_msgs / Stat->maxmsgs) : 0 );
194
195                         for (i=0; i<=last; ++i) {
196
197                                 if (
198                                         (i == first)
199                                         || (i == last)
200                                         || (i == BBS->requested_page)
201                                         || (
202                                                 ((BBS->requested_page - i) < RANGE)
203                                                 && ((BBS->requested_page - i) > (0 - RANGE))
204                                         )
205                                 ) {
206
207                                         if (
208                                                 (i == last) 
209                                                 && (last - BBS->requested_page > RANGE)
210                                         ) {
211                                                 wc_printf("...&nbsp;");
212                                         }
213                                         if (i == BBS->requested_page) {
214                                                 wc_printf("[");
215                                         }
216                                         else {
217                                                 wc_printf("<a href=\"readfwd?page=%d\">", i);
218                                                 wc_printf("<span class=\"moreprompt_link\">");
219                                         }
220                                         if (
221                                                 (i == first)
222                                                 && (BBS->requested_page > (RANGE + 1))
223                                         ) {
224                                                 wc_printf(_("First"));
225                                         }
226                                         else if (
227                                                 (i == last)
228                                                 && (last - BBS->requested_page > RANGE)
229                                         ) {
230                                                 wc_printf(_("Last"));
231                                         }
232                                         else {
233                                                 wc_printf("%d", i + 1); // change to one-based for display
234                                         }
235                                         if (i == BBS->requested_page) {
236                                                 wc_printf("]");
237                                         }
238                                         else {
239                                                 wc_printf("</span>");
240                                                 wc_printf("</a>");
241                                         }
242                                         if (
243                                                 (i == first)
244                                                 && (BBS->requested_page > (RANGE + 1))
245                                         ) {
246                                                 wc_printf("&nbsp;...");
247                                         }
248                                         if (i != last) {
249                                                 wc_printf("&nbsp;");
250                                         }
251                                 }
252                         }
253                         wc_printf("</div>\n");
254                 }
255         }
256
257         return(0);
258 }
259
260
261 int bbsview_Cleanup(void **ViewSpecific)
262 {
263         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
264
265         if (BBS->alloc_msgs != 0) {
266                 free(BBS->msgs);
267         }
268         free(BBS);
269
270         wDumpContent(1);
271         return 0;
272 }
273
274
275 void 
276 InitModule_BBSVIEWRENDERERS
277 (void)
278 {
279         RegisterReadLoopHandlerset(
280                 VIEW_BBS,
281                 bbsview_GetParamsGetServerCall,
282                 NULL,
283                 NULL, 
284                 bbsview_LoadMsgFromServer,
285                 bbsview_RenderView_or_Tail,
286                 bbsview_Cleanup
287         );
288 }