blogpost_render() is now called with a new parameter 'with_comments' to determine...
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2011 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation -- either version 3 of the
9  * License, or (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  * Generate a permalink for a post
28  * (Call with NULL arguments to make this function wcprintf() the permalink
29  * instead of writing it to the template)
30  */
31 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
32         char perma[SIZ];
33         
34         strcpy(perma, "/readfwd?go=");
35         urlesc(&perma[strlen(perma)], sizeof(perma)-strlen(perma), (char *)ChrPtr(WC->CurRoom.name));
36         snprintf(&perma[strlen(perma)], sizeof(perma)-strlen(perma), "?p=%d", WC->bptlid);
37         if (!Target) {
38                 wc_printf("%s", perma);
39         }
40         else {
41                 StrBufAppendPrintf(Target, "%s", perma);
42         }
43 }
44
45
46 /*
47  * Render single blog post and (optionally) its comments
48  */
49 void blogpost_render(struct blogpost *bp, int with_comments)
50 {
51         const StrBuf *Mime;
52         int i;
53
54         WC->bptlid = bp->top_level_id;
55
56         /* Always show the top level post, unless we somehow ended up with an empty list */
57         if (bp->num_msgs > 0) {
58                 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime);
59         }
60
61         /* If we were asked to suppress comments, show only the comment count */
62         if (!with_comments) {
63                 /* Show the number of comments */
64                 wc_printf("<a href=\"readfwd?p=%d?go=", bp->top_level_id);
65                 urlescputs(ChrPtr(WC->CurRoom.name));
66                 wc_printf("#comments\">");
67                 wc_printf(_("%d comments"), bp->num_msgs - 1);
68                 wc_printf("</a> | <a href=\"");
69                 tmplput_blog_permalink(NULL, NULL);
70                 wc_printf("\">%s</a>", _("permalink"));
71                 wc_printf("<br><br><br>\n");
72         }
73
74         else if (bp->num_msgs < 2) {
75                 wc_printf(_("%d comments"), 0);
76         }
77
78         else {
79                 wc_printf("<a name=\"comments\"></a>\n");
80                 wc_printf(_("%d comments"), bp->num_msgs - 1);
81                 wc_printf(" | <a href=\"");
82                 tmplput_blog_permalink(NULL, NULL);
83                 wc_printf("\">%s</a>", _("permalink"));
84                 wc_printf("<br>\n");
85                 for (i=1; i<bp->num_msgs; ++i) {
86                         read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime);
87                 }
88         }
89
90         /* offer the comment box */
91         if (with_comments) {
92                 do_template("blog_comment_box");
93         }
94
95 }
96
97
98 /*
99  * Destructor for "struct blogpost"
100  */
101 void blogpost_destroy(struct blogpost *bp) {
102         if (bp->alloc_msgs > 0) {
103                 free(bp->msgs);
104         }
105         free(bp);
106 }
107
108
109 /*
110  * Entry point for message read operations.
111  */
112 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
113                                    void **ViewSpecific, 
114                                    long oper, 
115                                    char *cmd, 
116                                    long len)
117 {
118         HashList *BLOG = NewHash(1, NULL);
119         *ViewSpecific = BLOG;
120
121         Stat->startmsg = (-1);                                  /* not used here */
122         Stat->sortit = 1;                                       /* not used here */
123         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
124         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
125         
126         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
127         rlid[2].cmd(cmd, len);
128         
129         return 200;
130 }
131
132
133 /*
134  * Given a msgnum, populate the id and refs fields of
135  * a "struct bltr" by fetching them from the Citadel server
136  */
137 struct bltr blogview_learn_thread_references(long msgnum)
138 {
139         StrBuf *Buf;
140         StrBuf *r;
141         int len;
142         struct bltr bltr = { 0, 0 } ;
143         Buf = NewStrBuf();
144         r = NewStrBuf();
145         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
146         StrBuf_ServGetln(Buf);
147         if (GetServerStatus(Buf, NULL) == 1) {
148                 while (len = StrBuf_ServGetln(Buf), 
149                        ((len >= 0) && 
150                         ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
151                 {
152                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
153                                 StrBufCutLeft(Buf, 5);
154                                 bltr.id = ThreadIdHash(Buf);
155                         }
156                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
157                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
158                                 StrBufExtract_token(r, Buf, 0, '|');
159                                 bltr.refs = ThreadIdHash(r);
160                         }
161                 }
162         }
163         FreeStrBuf(&Buf);
164         FreeStrBuf(&r);
165         return(bltr);
166 }
167
168
169 /*
170  * This function is called for every message in the list.
171  */
172 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
173                               void **ViewSpecific, 
174                               message_summary* Msg, 
175                               int is_new, 
176                               int i)
177 {
178         HashList *BLOG = (HashList *) *ViewSpecific;
179         struct bltr b;
180         struct blogpost *bp = NULL;
181         int p = 0;
182
183         b = blogview_learn_thread_references(Msg->msgnum);
184
185         /* Stop processing if the viewer is only interested in a single post and
186          * that message ID is neither the id nor the refs.
187          */
188         p = atoi(BSTR("p"));    /* are we looking for a specific post? */
189         if ((p != 0) && (p != b.id) && (p != b.refs)) {
190                 return 200;
191         }
192
193         /*
194          * Add our little bundle of blogworthy wonderfulness to the hash table
195          */
196         if (b.refs == 0) {
197                 bp = malloc(sizeof(struct blogpost));
198                 if (!bp) return(200);
199                 memset(bp, 0, sizeof (struct blogpost));
200                 bp->top_level_id = b.id;
201                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp, (DeleteHashDataFunc)blogpost_destroy);
202         }
203         else {
204                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
205         }
206
207         /*
208          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
209          * blog post itself or a comment attached to it; either way, the code is the same from
210          * this point onward.
211          */
212         if (bp != NULL) {
213                 if (bp->alloc_msgs == 0) {
214                         bp->alloc_msgs = 1000;
215                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
216                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
217                 }
218                 if (bp->num_msgs >= bp->alloc_msgs) {
219                         bp->alloc_msgs *= 2;
220                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
221                         memset(&bp->msgs[bp->num_msgs], 0,
222                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
223                 }
224                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
225         }
226
227         return 200;
228 }
229
230
231 /*
232  * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
233  * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
234  */
235 static int blogview_sortfunc(const void *a, const void *b) { 
236         struct blogpost * const *one = a;
237         struct blogpost * const *two = b;
238
239         if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
240         if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
241         return(0);
242 }
243
244
245 /*
246  * All blogpost entries are now in the hash list.
247  * Sort them, select the desired range, and render what we want to see.
248  */
249 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
250 {
251         HashList *BLOG = (HashList *) *ViewSpecific;
252         HashPos *it;
253         const char *Key;
254         void *Data;
255         long len;
256         int i;
257         struct blogpost **blogposts = NULL;
258         int num_blogposts = 0;
259         int num_blogposts_alloc = 0;
260         int with_comments = 0;
261
262         /* Comments are shown if we are only viewing a single blog post */
263         if (atoi(BSTR("p"))) with_comments = 1;
264
265         /* Iterate through the hash list and copy the data pointers into an array */
266         it = GetNewHashPos(BLOG, 0);
267         while (GetNextHashPos(BLOG, it, &len, &Key, &Data)) {
268                 if (num_blogposts >= num_blogposts_alloc) {
269                         if (num_blogposts_alloc == 0) {
270                                 num_blogposts_alloc = 100;
271                         }
272                         else {
273                                 num_blogposts_alloc *= 2;
274                         }
275                         blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
276                 }
277                 blogposts[num_blogposts++] = (struct blogpost *) Data;
278         }
279         DeleteHashPos(&it);
280
281         /* Now we have our array.  It is ONLY an array of pointers.  The objects to
282          * which they point are still owned by the hash list.
283          */
284         if (num_blogposts > 0) {
285
286                 /* Sort newest-to-oldest */
287                 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
288
289                 /* FIXME -- allow the user to select a starting point in the list */
290
291                 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
292
293                 /* Now go through the list and render what we've got */
294                 for (i=0; i<num_blogposts; ++i) {
295                         blogpost_render(blogposts[i], with_comments);
296                 }
297
298                 /* Done.  We are only freeing the array of pointers; the data itself
299                  * will be freed along with the hash list.
300                  */
301                 free(blogposts);
302         }
303
304         return(0);
305 }
306
307
308 int blogview_Cleanup(void **ViewSpecific)
309 {
310         HashList *BLOG = (HashList *) *ViewSpecific;
311
312         DeleteHash(&BLOG);
313
314         wDumpContent(1);
315         return 0;
316 }
317
318
319 void 
320 InitModule_BLOGVIEWRENDERERS
321 (void)
322 {
323         RegisterReadLoopHandlerset(
324                 VIEW_BLOG,
325                 blogview_GetParamsGetServerCall,
326                 NULL,
327                 NULL, 
328                 blogview_LoadMsgFromServer,
329                 blogview_render,
330                 blogview_Cleanup
331         );
332         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
333 }