use viewspecific to transport params through the blogview_renderer
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2012 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, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19
20 typedef struct __BLOG {
21         HashList *BLOG;
22         long p;
23         int gotonext;
24 } BLOG;
25
26
27 /*
28  * Generate a permalink for a post
29  * (Call with NULL arguments to make this function wcprintf() the permalink
30  * instead of writing it to the template)
31  */
32 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
33         char perma[SIZ];
34         
35         strcpy(perma, "/readfwd?go=");
36         urlesc(&perma[strlen(perma)], sizeof(perma)-strlen(perma), (char *)ChrPtr(WC->CurRoom.name));
37         snprintf(&perma[strlen(perma)], sizeof(perma)-strlen(perma), "?p=%d", WC->bptlid);
38         if (!Target) {
39                 wc_printf("%s", perma);
40         }
41         else {
42                 StrBufAppendPrintf(Target, "%s", perma);
43         }
44 }
45
46
47 /*
48  * Render a single blog post and (optionally) its comments
49  */
50 void blogpost_render(struct blogpost *bp, int with_comments)
51 {
52         const StrBuf *Mime;
53         int i;
54
55         WC->bptlid = bp->top_level_id;  /* This is used in templates; do not remove it */
56
57         /* Always show the top level post, unless we somehow ended up with an empty list */
58         if (bp->num_msgs > 0) {
59                 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime);
60         }
61
62         if (with_comments) {
63                 /* Show any existing comments, then offer the comment box */
64                 wc_printf("<a class=\"blog_show_comments_link\" name=\"comments\"></a>\n");
65                 wc_printf(_("%d comments"), bp->num_msgs - 1);
66                 wc_printf(" | <a class=\"blog_permalink_link\" href=\"");
67                 tmplput_blog_permalink(NULL, NULL);
68                 wc_printf("\">%s</a>", _("permalink"));
69                 wc_printf("</div>\n");
70                 for (i=1; i<bp->num_msgs; ++i) {
71                         read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime);
72                 }
73                 do_template("view_blog_comment_box");
74         }
75
76         else {
77                 /* Show only the number of comments */
78                 wc_printf("<a class=\"blog_show_comments_link\" href=\"readfwd?p=%d?go=", bp->top_level_id);
79                 urlescputs(ChrPtr(WC->CurRoom.name));
80                 wc_printf("#comments\">");
81                 wc_printf(_("%d comments"), bp->num_msgs - 1);
82                 wc_printf("</a> | <a class=\"blog_permalink_link\" href=\"");
83                 tmplput_blog_permalink(NULL, NULL);
84                 wc_printf("\">%s</a>", _("permalink"));
85                 wc_printf("<hr>\n</div>\n");
86         }
87 }
88
89
90 /*
91  * Destructor for "struct blogpost"
92  */
93 void blogpost_destroy(struct blogpost *bp) {
94         if (bp->alloc_msgs > 0) {
95                 free(bp->msgs);
96         }
97         free(bp);
98 }
99
100
101 /*
102  * Entry point for message read operations.
103  */
104 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
105                                    void **ViewSpecific, 
106                                    long oper, 
107                                    char *cmd, 
108                                     long len,
109                                     char *filter,
110                                     long flen)
111 {
112         BLOG *BL = (BLOG*) malloc(sizeof(BLOG)); 
113         BL->BLOG = NewHash(1, NULL);
114         
115         /* are we looking for a specific post? */
116         BL->p = lbstr("p");
117         BL->gotonext = havebstr("gotonext");
118         *ViewSpecific = BL;
119
120         Stat->startmsg = (-1);                                  /* not used here */
121         Stat->sortit = 1;                                       /* not used here */
122         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
123         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
124         
125         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
126         rlid[2].cmd(cmd, len);
127         if (BL->gotonext)
128                 Stat->load_seen = 1;
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         BLOG *BL = (BLOG*) *ViewSpecific;
179         struct bltr b;
180         struct blogpost *bp = NULL;
181
182         b = blogview_learn_thread_references(Msg->msgnum);
183
184         /* Stop processing if the viewer is only interested in a single post and
185          * that message ID is neither the id nor the refs.
186          */
187         if ((BL->p != 0) && (BL->p != b.id) && (BL->p != b.refs)) {
188                 return 200;
189         }
190
191         /*
192          * Add our little bundle of blogworthy wonderfulness to the hash table
193          */
194         if (b.refs == 0) {
195                 bp = malloc(sizeof(struct blogpost));
196                 if (!bp) return(200);
197                 memset(bp, 0, sizeof (struct blogpost));
198                 bp->top_level_id = b.id;
199                 Put(BL->BLOG, (const char *)&b.id, sizeof(b.id), bp, (DeleteHashDataFunc)blogpost_destroy);
200         }
201         else {
202                 GetHash(BL->BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
203         }
204
205         /*
206          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
207          * blog post itself or a comment attached to it; either way, the code is the same from
208          * this point onward.
209          */
210         if (bp != NULL) {
211                 if (bp->alloc_msgs == 0) {
212                         bp->alloc_msgs = 1000;
213                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
214                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
215                 }
216                 if (bp->num_msgs >= bp->alloc_msgs) {
217                         bp->alloc_msgs *= 2;
218                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
219                         memset(&bp->msgs[bp->num_msgs], 0,
220                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
221                 }
222                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
223         }
224         else {
225                 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
226         }
227
228         return 200;
229 }
230
231
232 /*
233  * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
234  * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
235  */
236 static int blogview_sortfunc(const void *a, const void *b) { 
237         struct blogpost * const *one = a;
238         struct blogpost * const *two = b;
239
240         if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
241         if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
242         return(0);
243 }
244
245
246 /*
247  * All blogpost entries are now in the hash list.
248  * Sort them, select the desired range, and render what we want to see.
249  */
250 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
251 {
252         BLOG *BL = (BLOG*) *ViewSpecific;
253         HashPos *it;
254         const char *Key;
255         void *Data;
256         long len;
257         int i;
258         struct blogpost **blogposts = NULL;
259         int num_blogposts = 0;
260         int num_blogposts_alloc = 0;
261         int with_comments = 0;
262         int firstp = 0;
263         int maxp = 0;
264
265         /* Comments are shown if we are only viewing a single blog post */
266         if (atoi(BSTR("p"))) with_comments = 1;
267
268         firstp = atoi(BSTR("firstp"));  /* start reading at... */
269         maxp = atoi(BSTR("maxp"));      /* max posts to show... */
270         if (maxp < 1) maxp = 5;         /* default; move somewhere else? */
271
272         /* Iterate through the hash list and copy the data pointers into an array */
273         it = GetNewHashPos(BL->BLOG, 0);
274         while (GetNextHashPos(BL->BLOG, it, &len, &Key, &Data)) {
275                 if (num_blogposts >= num_blogposts_alloc) {
276                         if (num_blogposts_alloc == 0) {
277                                 num_blogposts_alloc = 100;
278                         }
279                         else {
280                                 num_blogposts_alloc *= 2;
281                         }
282                         blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
283                 }
284                 blogposts[num_blogposts++] = (struct blogpost *) Data;
285         }
286         DeleteHashPos(&it);
287
288         /* Now we have our array.  It is ONLY an array of pointers.  The objects to
289          * which they point are still owned by the hash list.
290          */
291         if (num_blogposts > 0) {
292                 int start_here = 0;
293                 /* Sort newest-to-oldest */
294                 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
295
296                 /* allow the user to select a starting point in the list */
297                 for (i=0; i<num_blogposts; ++i) {
298                         if (blogposts[i]->top_level_id == firstp) {
299                                 start_here = i;
300                         }
301                 }
302
303                 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
304
305                 /* Now go through the list and render what we've got */
306                 for (i=start_here; i<num_blogposts; ++i) {
307                         if ((i > 0) && (i == start_here)) {
308                                 int j = i - maxp;
309                                 if (j < 0) j = 0;
310                                 wc_printf("<div class=\"newer_blog_posts\"><a href=\"readfwd?go=");
311                                 urlescputs(ChrPtr(WC->CurRoom.name));
312                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[j]->top_level_id, maxp);
313                                 wc_printf("%s →</a></div>\n", _("Newer posts"));
314                         }
315                         if (i < (start_here + maxp)) {
316                                 blogpost_render(blogposts[i], with_comments);
317                         }
318                         else if (i == (start_here + maxp)) {
319                                 wc_printf("<div class=\"older_blog_posts\"><a href=\"readfwd?go=");
320                                 urlescputs(ChrPtr(WC->CurRoom.name));
321                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[i]->top_level_id, maxp);
322                                 wc_printf("← %s</a></div>\n", _("Older posts"));
323                         }
324                 }
325
326                 /* Done.  We are only freeing the array of pointers; the data itself
327                  * will be freed along with the hash list.
328                  */
329                 free(blogposts);
330         }
331
332         return(0);
333 }
334
335
336 int blogview_Cleanup(void **ViewSpecific)
337 {
338         BLOG *BL = (BLOG*) *ViewSpecific;
339
340         DeleteHash(&BL->BLOG);
341         free(BL);
342         wDumpContent(1);
343         return 0;
344 }
345
346
347 void 
348 InitModule_BLOGVIEWRENDERERS
349 (void)
350 {
351         RegisterReadLoopHandlerset(
352                 VIEW_BLOG,
353                 blogview_GetParamsGetServerCall,
354                 NULL,
355                 NULL,
356                 NULL, 
357                 blogview_LoadMsgFromServer,
358                 blogview_render,
359                 blogview_Cleanup
360         );
361         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
362 }