7ed776e01472a69ddaf2f0e3a34703d372465878
[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-2010 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
59                 char *comma_pos = strchr(buf, ',');     /* kill first comma and everything to its right */
60                 if (comma_pos) {
61                         *comma_pos = 0;
62                 }
63
64                 char *colon_pos = strchr(buf, ':');     /* kill first colon and everything to its left */
65                 if (colon_pos) {
66                         strcpy(buf, ++colon_pos);
67                 }
68         }
69
70         return(atol(buf));
71 }
72
73
74
75 /*
76  * Entry point for message read operations.
77  */
78 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
79                                    void **ViewSpecific, 
80                                    long oper, 
81                                    char *cmd, 
82                                    long len)
83 {
84         struct bbsview *BBS = malloc(sizeof(struct bbsview));
85         memset(BBS, 0, sizeof(struct bbsview));
86         *ViewSpecific = BBS;
87
88         Stat->startmsg = (-1);                                  /* not used here */
89         Stat->sortit = 1;                                       /* not used here */
90         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
91         BBS->requested_page = 0;
92         BBS->lastseen = bbsview_get_last_seen();
93         BBS->start_reading_at = 0;
94
95         /* By default, the requested page is the first one. */
96         if (havebstr("start_reading_at")) {
97                 BBS->start_reading_at = lbstr("start_reading_at");
98                 BBS->requested_page = (-4);
99         }
100
101         /* However, if we are asked to start with a specific message number, make sure
102          * we start on the page containing that message
103          */
104
105         /* Or, if a specific page was requested, make sure we go there */
106         else if (havebstr("page")) {
107                 BBS->requested_page = ibstr("page");
108         }
109
110         /* Otherwise, if this is a "read new" operation, make sure we start on the page
111          * containing the first new message
112          */
113         else if (oper == 3) {
114                 BBS->requested_page = (-3);
115         }
116
117         if (havebstr("maxmsgs")) {
118                 Stat->maxmsgs = ibstr("maxmsgs");
119         }
120         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
121         
122         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
123         rlid[2].cmd(cmd, len);
124         
125         return 200;
126 }
127
128
129 /*
130  * This function is called for every message in the list.
131  */
132 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
133                               void **ViewSpecific, 
134                               message_summary* Msg, 
135                               int is_new, 
136                               int i)
137 {
138         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
139
140         if (BBS->alloc_msgs == 0) {
141                 BBS->alloc_msgs = 1000;
142                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
143                 memset(BBS->msgs, 0, (BBS->alloc_msgs * sizeof(long)) );
144         }
145
146         /* Check our buffer size */
147         if (BBS->num_msgs >= BBS->alloc_msgs) {
148                 BBS->alloc_msgs *= 2;
149                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
150                 memset(&BBS->msgs[BBS->num_msgs], 0, ((BBS->alloc_msgs - BBS->num_msgs) * sizeof(long)) );
151         }
152
153         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
154
155         return 200;
156 }
157
158
159 int bbsview_sortfunc(const void *s1, const void *s2) {
160         long l1;
161         long l2;
162
163         l1 = *(long *)(s1);
164         l2 = *(long *)(s2);
165
166         if (l1 > l2) return(+1);
167         if (l1 < l2) return(-1);
168         return(0);
169 }
170
171
172 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
173                                void **ViewSpecific, 
174                                long oper)
175 {
176         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
177         int i;
178         int seq;
179         const StrBuf *Mime;
180         int start_index = 0;
181         int end_index = 0;
182
183         if (Stat->nummsgs > 0) {
184                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
185                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), bbsview_sortfunc);
186         }
187
188         if ((BBS->num_msgs % Stat->maxmsgs) == 0) {
189                 BBS->num_pages = BBS->num_msgs / Stat->maxmsgs;
190         }
191         else {
192                 BBS->num_pages = (BBS->num_msgs / Stat->maxmsgs) + 1;
193         }
194
195         /* If the requested page number is -4,
196          * it means "whichever page on which msg#xxxxx starts"
197          * Change to the page number which contains that message.
198          */
199         if (BBS->requested_page == (-4)) {
200                 if (BBS->num_msgs == 0) {
201                         BBS->requested_page = 0;
202                 }
203                 else {
204                         for (i=0; i<BBS->num_msgs; ++i) {
205                                 if (
206                                         (BBS->msgs[i] >= BBS->start_reading_at)
207                                         && (BBS->requested_page == (-4))
208                                 ) {
209                                         BBS->requested_page = (i / Stat->maxmsgs) ;
210                                 }
211                         }
212                 }
213         }
214
215         /* If the requested page number is -3,
216          * it means "whichever page on which new messages start"
217          * Change that to an actual page number now.
218          */
219         if (BBS->requested_page == (-3)) {
220                 if (BBS->num_msgs == 0) {
221                         BBS->requested_page = 0;
222                 }
223                 else {
224                         for (i=0; i<BBS->num_msgs; ++i) {
225                                 if (
226                                         (BBS->msgs[i] > BBS->lastseen)
227                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
228                                 ) {
229                                         BBS->requested_page = (i / Stat->maxmsgs) ;
230                                 }
231                         }
232                 }
233         }
234
235         /* Still set to -3 ?  If so, that probably means that there are no new messages,
236          * so we'll go to the *end* of the final page.
237          */
238         if (BBS->requested_page == (-3)) {
239                 if (BBS->num_msgs == 0) {
240                         BBS->requested_page = 0;
241                 }
242                 else {
243                         BBS->requested_page = BBS->num_pages - 1;
244                 }
245         }
246
247         /* keep the requested page within bounds */
248         if (BBS->requested_page < 0) BBS->requested_page = 0;
249         if (BBS->requested_page >= BBS->num_pages) BBS->requested_page = BBS->num_pages - 1;
250
251         start_index = BBS->requested_page * Stat->maxmsgs;
252         if (start_index < 0) start_index = 0;
253         end_index = start_index + Stat->maxmsgs - 1;
254
255         for (seq = 0; seq < 3; ++seq) {         /* cheap & sleazy way of rendering the page numbers twice */
256
257                 if ( (seq == 1) && (Stat->nummsgs > 0)) {
258                         /* display the selected range of messages */
259
260                         for (i=start_index; (i<=end_index && i<BBS->num_msgs); ++i) {
261                                 if (
262                                         (BBS->msgs[i] > BBS->lastseen)
263                                         && ( (i == 0) || (BBS->msgs[i-1] <= BBS->lastseen) )
264                                 ) {
265                                         /* new messages start here */
266                                         do_template("start_of_new_msgs", NULL);
267                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#newmsgs\";\n");
268                                 }
269                                 if (BBS->msgs[i] > 0L) {
270                                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
271                                 }
272                                 if (
273                                         (i == (BBS->num_msgs - 1))
274                                         && (BBS->msgs[i] <= BBS->lastseen)
275                                 ) {
276                                         /* no new messages */
277                                         do_template("no_new_msgs", NULL);
278                                         StrBufAppendPrintf(WC->trailing_javascript, "location.href=\"#nonewmsgs\";\n");
279                                 }
280                         }
281                 }
282
283                 else if ( (seq == 0) || (seq == 2) ) {
284                         /* Display the selecto-bar with the page numbers */
285
286                         wc_printf("<div class=\"moreprompt\">");
287                         wc_printf(_("Go to page: "));
288
289                         int first = 0;
290                         int last = BBS->num_pages - 1;
291
292                         for (i=0; i<=last; ++i) {
293
294                                 if (
295                                         (i == first)
296                                         || (i == last)
297                                         || (i == BBS->requested_page)
298                                         || (
299                                                 ((BBS->requested_page - i) < RANGE)
300                                                 && ((BBS->requested_page - i) > (0 - RANGE))
301                                         )
302                                 ) {
303
304                                         if (
305                                                 (i == last) 
306                                                 && (last - BBS->requested_page > RANGE)
307                                         ) {
308                                                 wc_printf("...&nbsp;");
309                                         }
310                                         if (i == BBS->requested_page) {
311                                                 wc_printf("[");
312                                         }
313                                         else {
314                                                 wc_printf("<a href=\"readfwd?page=%d\">", i);
315                                                 wc_printf("<span class=\"moreprompt_link\">");
316                                         }
317                                         if (
318                                                 (i == first)
319                                                 && (BBS->requested_page > (RANGE + 1))
320                                         ) {
321                                                 wc_printf(_("First"));
322                                         }
323                                         else if (
324                                                 (i == last)
325                                                 && (last - BBS->requested_page > RANGE)
326                                         ) {
327                                                 wc_printf(_("Last"));
328                                         }
329                                         else {
330                                                 wc_printf("%d", i + 1); // change to one-based for display
331                                         }
332                                         if (i == BBS->requested_page) {
333                                                 wc_printf("]");
334                                         }
335                                         else {
336                                                 wc_printf("</span>");
337                                                 wc_printf("</a>");
338                                         }
339                                         if (
340                                                 (i == first)
341                                                 && (BBS->requested_page > (RANGE + 1))
342                                         ) {
343                                                 wc_printf("&nbsp;...");
344                                         }
345                                         if (i != last) {
346                                                 wc_printf("&nbsp;");
347                                         }
348                                 }
349                         }
350                         wc_printf("</div>\n");
351                 }
352         }
353
354         return(0);
355 }
356
357
358 int bbsview_Cleanup(void **ViewSpecific)
359 {
360         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
361
362         if (BBS->alloc_msgs != 0) {
363                 free(BBS->msgs);
364         }
365         free(BBS);
366
367         wDumpContent(1);
368         return 0;
369 }
370
371
372 void 
373 InitModule_BBSVIEWRENDERERS
374 (void)
375 {
376         RegisterReadLoopHandlerset(
377                 VIEW_BBS,
378                 bbsview_GetParamsGetServerCall,
379                 NULL,
380                 NULL, 
381                 bbsview_LoadMsgFromServer,
382                 bbsview_RenderView_or_Tail,
383                 bbsview_Cleanup
384         );
385 }