viewdefs is now boolean
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "groupdav.h"
24
25
26 /* 
27  * Array type for a blog post.  The first message is the post; the rest are comments
28  */
29 struct blogpost {
30         int top_level_id;
31         long *msgs;             /* Array of msgnums for messages we are displaying */
32         int num_msgs;           /* Number of msgnums stored in 'msgs' */
33         int alloc_msgs;         /* Currently allocated size of array */
34 };
35
36
37 /*
38  * Destructor for 'struct blogpost' which does the rendering first.
39  * By rendering from here, we eliminate the need for a separate iterator, although
40  * we might run into trouble when we get around to displaying newest-to-oldest...
41  */
42 void blogpost_render_and_destroy(struct blogpost *bp) {
43         const StrBuf *Mime;
44         int p = 0;
45         int i;
46
47         p = atoi(BSTR("p"));    /* are we looking for a specific post? */
48
49         if ( ((p == 0) || (p == bp->top_level_id)) && (bp->num_msgs > 0) ) {
50                 /* Show the top level post */
51                 read_message(WC->WBuf, HKEY("view_message"), bp->msgs[0], NULL, &Mime);
52
53                 if (p == 0) {
54                         /* Show the number of comments */
55                         wc_printf("<a href=\"readfwd?p=%d?gotofirst=", bp->top_level_id);
56                         urlescputs(ChrPtr(WC->CurRoom.name));
57                         wc_printf("#comments\">%d comments</a>", bp->num_msgs - 1);
58                 }
59                 else if (bp->num_msgs < 2) {
60                         wc_printf("dere r no comments here!<br>\n");
61                 }
62                 else {
63                         wc_printf("<a name=\"comments\"></a>\n");
64                         wc_printf("%d comments<br>\n", bp->num_msgs - 1);
65                         wc_printf("<blockquote>");
66                         for (i=1; i<bp->num_msgs; ++i) {
67                                 read_message(WC->WBuf, HKEY("view_message"), bp->msgs[i], NULL, &Mime);
68                         }
69                         wc_printf("</blockquote>");
70                 }
71         }
72
73
74         if (bp->alloc_msgs > 0) {
75                 free(bp->msgs);
76         }
77         free(bp);
78 }
79
80
81 /*
82  * Data which gets returned from a call to blogview_learn_thread_references()
83  */
84 struct bltr {
85         int id;
86         int refs;
87 };
88
89
90 /*
91  * Entry point for message read operations.
92  */
93 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
94                                    void **ViewSpecific, 
95                                    long oper, 
96                                    char *cmd, 
97                                    long len)
98 {
99         HashList *BLOG = NewHash(1, NULL);
100         *ViewSpecific = BLOG;
101
102         Stat->startmsg = (-1);                                  /* not used here */
103         Stat->sortit = 1;                                       /* not used here */
104         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
105         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
106         
107         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
108         rlid[2].cmd(cmd, len);
109         
110         return 200;
111 }
112
113
114 /*
115  * Given a 'struct blogpost' containing a msgnum, populate the id
116  * and refs fields by fetching them from the Citadel server
117  */
118 struct bltr blogview_learn_thread_references(long msgnum)
119 {
120         StrBuf *Buf;
121         StrBuf *r;
122         struct bltr bltr = { 0, 0 } ;
123         Buf = NewStrBuf();
124         r = NewStrBuf();
125         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
126         StrBuf_ServGetln(Buf);
127         if (GetServerStatus(Buf, NULL) == 1) {
128                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
129                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
130                                 StrBufCutLeft(Buf, 5);
131                                 bltr.id = HashLittle(ChrPtr(Buf), StrLength(Buf));
132                         }
133                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
134                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
135                                 StrBufExtract_token(r, Buf, 0, '|');
136                                 bltr.refs = HashLittle(ChrPtr(r), StrLength(r));
137                         }
138                 }
139         }
140         FreeStrBuf(&Buf);
141         FreeStrBuf(&r);
142         return(bltr);
143 }
144
145
146 /*
147  * This function is called for every message in the list.
148  */
149 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
150                               void **ViewSpecific, 
151                               message_summary* Msg, 
152                               int is_new, 
153                               int i)
154 {
155         HashList *BLOG = (HashList *) *ViewSpecific;
156         struct bltr b;
157         struct blogpost *bp = NULL;
158
159         b = blogview_learn_thread_references(Msg->msgnum);
160
161         /* FIXME an optimization here -- one we ought to perform -- is to exit this
162          * function immediately if the viewer is only interested in a single post and
163          * that message ID is neither the id nor the refs.  Actually, that might *be*
164          * the way to display only a single message (with or without comments).
165          */
166
167         if (b.refs == 0) {
168                 bp = malloc(sizeof(struct blogpost));
169                 if (!bp) return(200);
170                 memset(bp, 0, sizeof (struct blogpost));
171                 bp->top_level_id = b.id;
172                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
173                                         (DeleteHashDataFunc)blogpost_render_and_destroy);
174         }
175         else {
176                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
177         }
178
179         /*
180          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
181          * blog post itself or a comment attached to it; either way, the code is the same from
182          * this point onward.
183          */
184         if (bp != NULL) {
185                 if (bp->alloc_msgs == 0) {
186                         bp->alloc_msgs = 1000;
187                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
188                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
189                 }
190                 if (bp->num_msgs >= bp->alloc_msgs) {
191                         bp->alloc_msgs *= 2;
192                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
193                         memset(&bp->msgs[bp->num_msgs], 0,
194                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
195                 }
196                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
197         }
198
199         return 200;
200 }
201
202
203 /*
204  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
205  */
206 int blogview_sortfunc(const void *s1, const void *s2) {
207         long *l1 = (long *)(s1);
208         long *l2 = (long *)(s2);
209
210         if (*l1 > *l2) return(-1);
211         if (*l1 < *l2) return(+1);
212         return(0);
213 }
214
215
216 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
217 {
218         /*HashList *BLOG = (HashList *) *ViewSpecific;*/
219
220         /*
221          * No code needed here -- we render during disposition.
222          * Maybe this is the location where we want to handle pretty permalinks.
223          */
224
225         return(0);
226 }
227
228
229 int blogview_Cleanup(void **ViewSpecific)
230 {
231         HashList *BLOG = (HashList *) *ViewSpecific;
232
233         DeleteHash(&BLOG);
234
235         wDumpContent(1);
236         return 0;
237 }
238
239
240 void 
241 InitModule_BLOGVIEWRENDERERS
242 (void)
243 {
244         RegisterReadLoopHandlerset(
245                 VIEW_BLOG,
246                 blogview_GetParamsGetServerCall,
247                 NULL,
248                 NULL, 
249                 blogview_LoadMsgFromServer,
250                 blogview_render,
251                 blogview_Cleanup
252         );
253 }