Mailing list header changes (fuck you Google)
[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 CtxType CTX_BLOGPOST = CTX_NONE;
20
21 typedef struct __BLOG {
22         HashList *BLOGPOSTS;
23         long p;
24         int gotonext;
25         long firstp;
26         long maxp;
27         StrBuf *Charset;
28         StrBuf *Buf;
29         const StrBuf *FilterTag;
30 } BLOG;
31
32 /* 
33  * Array type for a blog post.  The first message is the post; the rest are comments
34  */
35 typedef struct _blogpost {
36         int top_level_id;
37         long *msgs;             /* Array of msgnums for messages we are displaying */
38         int num_msgs;           /* Number of msgnums stored in 'msgs' */
39         int alloc_msgs;         /* Currently allocated size of array */
40         int unread_oments;
41 }blogpost;
42
43
44 /*
45  * XML sitemap generator -- go through the message list for a Blog room
46  */
47 void sitemap_do_blog(void) {
48         wcsession *WCC = WC;
49         blogpost oneBP;
50         int num_msgs = 0;
51         int i;
52         SharedMessageStatus Stat;
53         message_summary *Msg = NULL;
54         StrBuf *Buf = NewStrBuf();
55         StrBuf *FoundCharset = NewStrBuf();
56         WCTemplputParams SubTP;
57
58         memset(&Stat, 0, sizeof Stat);
59         memset(&oneBP, 0, sizeof(blogpost));
60         memset(&SubTP, 0, sizeof(WCTemplputParams));    
61         StackContext(NULL, &SubTP, &oneBP, CTX_BLOGPOST, 0, NULL);
62
63         Stat.maxload = INT_MAX;
64         Stat.lowest_found = (-1);
65         Stat.highest_found = (-1);
66         num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
67         if (num_msgs < 1) return;
68
69         for (i=0; i<num_msgs; ++i) {
70                 Msg = GetMessagePtrAt(i, WCC->summ);
71                 if (Msg != NULL) {
72                         ReadOneMessageSummary(Msg, FoundCharset, Buf);
73                         /* Show only top level posts, not comments */
74                         if ((Msg->reply_inreplyto_hash != 0) && (Msg->reply_references_hash == 0)) {
75                                 oneBP.top_level_id = Msg->reply_inreplyto_hash;
76                                 DoTemplate(HKEY("view_blog_sitemap"), WCC->WBuf, &SubTP);
77                         }
78                 }
79         }
80         UnStackContext(&SubTP);
81         FreeStrBuf(&Buf);
82         FreeStrBuf(&FoundCharset);
83 }
84
85
86
87 /*
88  * Generate a permalink for a post
89  * (Call with NULL arguments to make this function wcprintf() the permalink
90  * instead of writing it to the template)
91  */
92 void tmplput_blog_toplevel_id(StrBuf *Target, WCTemplputParams *TP) {
93         blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
94         char buf[SIZ];
95         snprintf(buf, SIZ, "%d", bp->top_level_id);
96         StrBufAppendTemplateStr(Target, TP, buf, 0);
97 }
98
99 void tmplput_blog_comment_count(StrBuf *Target, WCTemplputParams *TP) {
100         blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
101         char buf[SIZ];
102         snprintf(buf, SIZ, "%d", bp->num_msgs -1);
103         StrBufAppendTemplateStr(Target, TP, buf, 0);
104 }
105 void tmplput_blog_comment_unread_count(StrBuf *Target, WCTemplputParams *TP) {
106         blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
107         char buf[SIZ];
108         snprintf(buf, SIZ, "%d", bp->unread_oments);
109         StrBufAppendTemplateStr(Target, TP, buf, 0);
110 }
111
112
113
114 /*
115  * Render a single blog post and (optionally) its comments
116  */
117 void blogpost_render(blogpost *bp, int with_comments, WCTemplputParams *TP)
118 {
119         wcsession *WCC = WC;
120         WCTemplputParams SubTP;
121         const StrBuf *Mime;
122         int i;
123
124         memset(&SubTP, 0, sizeof(WCTemplputParams));
125         StackContext(TP, &SubTP, bp, CTX_BLOGPOST, 0, NULL);
126
127         /* Always show the top level post, unless we somehow ended up with an empty list */
128         if (bp->num_msgs > 0) {
129                 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime, TP);
130         }
131
132         if (with_comments) {
133                 /* Show any existing comments, then offer the comment box */
134                 DoTemplate(HKEY("view_blog_show_commentlink"), WCC->WBuf, &SubTP);
135
136                 for (i=1; i<bp->num_msgs; ++i) {
137                         read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime, &SubTP);
138                 }
139                 DoTemplate(HKEY("view_blog_comment_box"), WCC->WBuf, &SubTP);
140         }
141
142         else {
143                 /* Show only the number of comments */
144                 DoTemplate(HKEY("view_blog_show_no_comments"), WCC->WBuf, &SubTP);
145         }
146         UnStackContext(&SubTP);
147 }
148
149
150 /*
151  * Destructor for "blogpost"
152  */
153 void blogpost_destroy(blogpost *bp) {
154         if (bp->alloc_msgs > 0) {
155                 free(bp->msgs);
156         }
157         free(bp);
158 }
159
160
161 /*
162  * Entry point for message read operations.
163  */
164 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
165                                     void **ViewSpecific, 
166                                     long oper, 
167                                     char *cmd, 
168                                     long len,
169                                     char *filter,
170                                     long flen)
171 {
172         BLOG *BL = (BLOG*) malloc(sizeof(BLOG)); 
173         BL->BLOGPOSTS = NewHash(1, lFlathash);
174         
175         /* are we looking for a specific post? */
176         BL->p = lbstr("p");
177         BL->gotonext = havebstr("gotonext");
178         BL->Charset = NewStrBuf();
179         BL->Buf = NewStrBuf();
180         BL->FilterTag = sbstr("FilterTag");
181         BL->firstp = lbstr("firstp");   /* start reading at... */
182         BL->maxp   = lbstr("maxp");         /* max posts to show... */
183         if (BL->maxp < 1) BL->maxp = 5;     /* default; move somewhere else? */
184         putlbstr("maxp", BL->maxp);
185         *ViewSpecific = BL;
186
187         Stat->startmsg = (-1);                                  /* not used here */
188         Stat->sortit = 1;                                       /* not used here */
189         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
190         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
191         
192         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
193         snprintf(cmd, len, "MSGS ALL||2|8\n");
194
195         if (BL->gotonext)
196                 Stat->load_seen = 1;
197         return 200;
198 }
199
200
201 int blogview_IdentifyBlogposts (StrBuf *Line, 
202                                 const char **pos, 
203                                 message_summary *Msg, 
204                                 StrBuf *ConversionBuffer,
205                                 void **ViewSpecific)
206 {
207         BLOG *BL = (BLOG*) *ViewSpecific;
208         blogpost *bp = NULL;
209
210         /* Stop processing if the viewer is only interested in a single post and
211          * that message ID is neither the id nor the refs.
212          */
213         if ((BL->p != 0) &&
214             (BL->p != Msg->reply_inreplyto_hash) &&
215             (BL->p != Msg->reply_references_hash)) {
216                 return 0;
217         }
218
219         if ((Msg->reply_references_hash == 0) && 
220             (BL->FilterTag != NULL) && 
221             (strstr(ChrPtr(Msg->EnvTo) , ChrPtr(BL->FilterTag)) == NULL)) {
222                 /* filtering for tags, blogpost doesn't fit. */
223                 return 0;
224         }
225             
226         /*
227          * build up a hashtable of the blogposts.
228          */
229         if (Msg->reply_references_hash == 0) {
230                 bp = malloc(sizeof(blogpost));
231
232                 if (bp == NULL) return 0;
233
234                 memset(bp, 0, sizeof (blogpost));
235
236                 bp->top_level_id = Msg->reply_inreplyto_hash;
237                 bp->alloc_msgs = 1000;
238                 bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
239                 memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
240
241                 /* the first one is the blogpost itself, all subequent are comments. */
242                 bp->msgs[0] = Msg->msgnum;
243                 bp->num_msgs = 1;
244
245                 Put(BL->BLOGPOSTS,
246                     LKEY(Msg->reply_inreplyto_hash),
247                     bp,
248                     (DeleteHashDataFunc)blogpost_destroy);
249         }
250         /*
251          * Comments will be handled on the next iteration.
252          */
253
254         return 1;
255 }
256
257
258 /*
259  * This function is called for every message in the list.
260  */
261 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
262                               void **ViewSpecific, 
263                               message_summary* Msg, 
264                               int is_new, 
265                               int i)
266 {
267         blogpost *bp = NULL;
268         BLOG *BL = (BLOG*) *ViewSpecific;
269
270         if (Msg->reply_references_hash != 0) {
271                 /* 
272                  * this is a comment. try to assign it to a blogpost.
273                  */
274                 GetHash(BL->BLOGPOSTS,
275                         LKEY(Msg->reply_references_hash),
276                         (void *)&bp);
277
278                 /*
279                  * Now we have a 'blogpost' to which we can add the comment.  It's either the
280                  * blog post itself or a comment attached to it; either way, the code is the same from
281                  * this point onward.
282                  */
283                 if (bp != NULL) {
284                         if (bp->num_msgs >= bp->alloc_msgs) {
285                                 bp->alloc_msgs *= 2;
286                                 bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
287                                 memset(&bp->msgs[bp->num_msgs], 0,
288                                        ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
289                         }
290                         bp->msgs[bp->num_msgs++] = Msg->msgnum;
291                         if ((Msg->Flags & MSGFLAG_READ) != 0) {
292                                 bp->unread_oments++;
293                         }
294                 }
295                 else {
296                         /*
297                          * Ok, this comment probably belongs to one of the blogposts
298                          * we ruled out by filters. don't load it.
299                          */
300                         return 200;
301                 }
302         }
303         return 200;
304 }
305
306
307 /*
308  * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
309  * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
310  */
311 static int blogview_sortfunc(const void *a, const void *b) { 
312         blogpost const *one = GetSearchPayload(a);
313         blogpost const *two = GetSearchPayload(b);
314
315         if ( one->msgs[0] > two->msgs[0] ) return(-1);
316         if ( one->msgs[0] < two->msgs[0] ) return(+1);
317         return(0);
318 }
319
320
321 /*
322  * All blogpost entries are now in the hash list.
323  * Sort them, select the desired range, and render what we want to see.
324  */
325 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
326 {
327         wcsession *WCC = WC;
328         BLOG *BL = (BLOG*) *ViewSpecific;
329         HashPos *it;
330         const char *Key;
331         blogpost *thisBlogpost;
332         void *Data;
333         long len;
334         int num_blogposts = 0;
335         int with_comments = 0;
336         WCTemplputParams SubTP;
337         WCTemplputParams StopSubTP;
338         blogpost oneBP;
339         long firstPOffset = 0;
340         int count = 0;
341         int totalCount = 0;
342         StrBuf *PrevNext = NULL;
343
344         num_blogposts = GetCount(BL->BLOGPOSTS);
345         if (num_blogposts == 0) {
346                 /* Nothing to do... */
347                 return 0;
348         }
349         memset(&SubTP, 0, sizeof(WCTemplputParams));    
350         memset(&StopSubTP, 0, sizeof(WCTemplputParams));    
351         memset(&oneBP, 0, sizeof(blogpost));
352
353         /* Comments are shown if we are only viewing a single blog post */
354         with_comments = (BL->p != 0);
355
356         it = GetNewHashPos(BL->BLOGPOSTS, 0);
357
358         if ((BL->gotonext) && (BL->p == 0)) {
359                 /* did we come here via gotonext? lets find out whether
360                  * this blog has just one blogpost with new comments just display 
361                  * this one.
362                  */
363                 blogpost *unread_bp = NULL;
364                 int unread_count = 0;
365                 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
366                         blogpost *one_bp = (blogpost *) Data;
367                         if (one_bp->unread_oments > 0) {
368                                 unread_bp = one_bp;
369                                 unread_count++;
370                         }
371                 }
372                 if (unread_count == 1) {
373                         blogpost_render(unread_bp, 1, NULL);/// TODO other than null?
374
375                         DeleteHashPos(&it);
376                         return 0;
377                 }
378         }
379
380         /* Now we have our array.  It is ONLY an array of pointers.  The objects to
381          * which they point are still owned by the hash list.
382          */
383
384         /* get it into the right sort of order - blogposts may have been edited */
385         SortByPayload(BL->BLOGPOSTS, blogview_sortfunc);
386
387         /* Find the start post to display: */
388         if (BL->firstp != 0) {
389                 /* Translate the firstpost id into an offset */
390                 RewindHashPos(BL->BLOGPOSTS, it, 0);
391                 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
392                         thisBlogpost = (blogpost *) Data;
393                         if (thisBlogpost->top_level_id == BL->firstp) {
394                                 firstPOffset = count;
395                                 break;
396                         }
397                         count ++;
398                 }
399         }
400
401         if ((num_blogposts > BL->maxp) || (firstPOffset != 0)){
402                 PrevNext = NewStrBuf();
403                 if (firstPOffset  > 0) {
404                         const char *k;
405                         long len;
406                         long posPrev = 0;
407                         /* we now need to go up to maxp items back */
408                         if (firstPOffset > BL->maxp) {
409                                 posPrev = firstPOffset - BL->maxp;
410                         }
411                         GetHashAt(BL->BLOGPOSTS, posPrev, &len, &k, &Data);
412                         thisBlogpost = (blogpost *) Data;
413                         StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
414                         DoTemplate(HKEY("view_blog_newer_posts"), PrevNext, &SubTP);
415                 }
416                 if (firstPOffset + BL->maxp <= num_blogposts) {
417                         const char *k;
418                         long len;
419                         long posNext = firstPOffset + BL->maxp;
420                         GetHashAt(BL->BLOGPOSTS, posNext, &len, &k, &Data);
421                         thisBlogpost = (blogpost *) Data;
422                         StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
423                         DoTemplate(HKEY("view_blog_older_posts"), PrevNext, &SubTP);
424                 }
425         }
426
427         StrBufAppendBuf(WCC->WBuf, PrevNext, 0);
428         count = totalCount = 0;
429         RewindHashPos(BL->BLOGPOSTS, it, 0);
430
431         /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
432         while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
433                 thisBlogpost = (blogpost *) Data;
434
435                 /* allow the user to select a starting point in the list */
436                 if (totalCount < firstPOffset) {
437                         /* skip all till we found the first valid: */
438                         totalCount ++;
439                         continue;
440                 }
441                 if (count >= BL->maxp) {
442                         /* enough is enough. */
443                         break;
444                 }
445                 StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
446                 blogpost_render(thisBlogpost, with_comments, &SubTP);
447                 UnStackContext(&SubTP);
448                 count ++;
449                 totalCount ++;
450         }
451         StrBufAppendBuf(WCC->WBuf, PrevNext, 0);
452         FreeStrBuf(&PrevNext);
453         DeleteHashPos(&it);
454
455         return(0);
456 }
457
458
459 int blogview_Cleanup(void **ViewSpecific)
460 {
461         BLOG *BL = (BLOG*) *ViewSpecific;
462
463         FreeStrBuf(&BL->Buf);
464         FreeStrBuf(&BL->Charset);
465         DeleteHash(&BL->BLOGPOSTS);
466         free(BL);
467         wDumpContent(1);
468         return 0;
469 }
470
471
472 void 
473 InitModule_BLOGVIEWRENDERERS
474 (void)
475 {
476         const char* browseListFields[] = {
477                 "msgn",
478                 "nvto",
479                 "wefw",
480                 NULL
481         };
482         RegisterCTX(CTX_BLOGPOST);
483
484         RegisterReadLoopHandlerset(
485                 VIEW_BLOG,
486                 blogview_GetParamsGetServerCall,
487                 NULL,
488                 NULL,
489                 blogview_IdentifyBlogposts, 
490                 blogview_LoadMsgFromServer,
491                 blogview_render,
492                 blogview_Cleanup,
493                 browseListFields
494         );
495
496         RegisterNamespace("BLOG:TOPLEVEL:MSGID", 0, 0, tmplput_blog_toplevel_id, NULL, CTX_BLOGPOST);
497         RegisterNamespace("BLOG:COMMENTS:COUNT", 0, 0, tmplput_blog_comment_count, NULL, CTX_BLOGPOST);
498         RegisterNamespace("BLOG:COMMENTS:UNREAD:COUNT", 0, 0, tmplput_blog_comment_unread_count, NULL, CTX_BLOGPOST);
499 }