2 * Blog view renderer module for WebCit
4 * Copyright (c) 1996-2012 by the citadel.org team
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.
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.
16 #include "webserver.h"
20 typedef struct __BLOG {
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)
32 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
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);
39 wc_printf("%s", perma);
42 StrBufAppendPrintf(Target, "%s", perma);
48 * Render a single blog post and (optionally) its comments
50 void blogpost_render(struct blogpost *bp, int with_comments)
55 WC->bptlid = bp->top_level_id; /* This is used in templates; do not remove it */
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);
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);
73 do_template("view_blog_comment_box");
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");
91 * Destructor for "struct blogpost"
93 void blogpost_destroy(struct blogpost *bp) {
94 if (bp->alloc_msgs > 0) {
102 * Entry point for message read operations.
104 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
112 BLOG *BL = (BLOG*) malloc(sizeof(BLOG));
113 BL->BLOG = NewHash(1, NULL);
115 /* are we looking for a specific post? */
117 BL->gotonext = havebstr("gotonext");
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;
125 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
126 rlid[2].cmd(cmd, len);
134 * Given a msgnum, populate the id and refs fields of
135 * a "struct bltr" by fetching them from the Citadel server
137 struct bltr blogview_learn_thread_references(long msgnum)
142 struct bltr bltr = { 0, 0 } ;
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),
150 ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
152 if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
153 StrBufCutLeft(Buf, 5);
154 bltr.id = ThreadIdHash(Buf);
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);
170 * This function is called for every message in the list.
172 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
174 message_summary* Msg,
178 BLOG *BL = (BLOG*) *ViewSpecific;
180 struct blogpost *bp = NULL;
182 b = blogview_learn_thread_references(Msg->msgnum);
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.
187 if ((BL->p != 0) && (BL->p != b.id) && (BL->p != b.refs)) {
192 * Add our little bundle of blogworthy wonderfulness to the hash table
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);
202 GetHash(BL->BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
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
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)) );
216 if (bp->num_msgs >= bp->alloc_msgs) {
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)) );
222 bp->msgs[bp->num_msgs++] = Msg->msgnum;
225 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
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
236 static int blogview_sortfunc(const void *a, const void *b) {
237 struct blogpost * const *one = a;
238 struct blogpost * const *two = b;
240 if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
241 if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
247 * All blogpost entries are now in the hash list.
248 * Sort them, select the desired range, and render what we want to see.
250 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
252 BLOG *BL = (BLOG*) *ViewSpecific;
258 struct blogpost **blogposts = NULL;
259 int num_blogposts = 0;
260 int num_blogposts_alloc = 0;
261 int with_comments = 0;
265 /* Comments are shown if we are only viewing a single blog post */
266 if (atoi(BSTR("p"))) with_comments = 1;
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? */
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;
280 num_blogposts_alloc *= 2;
282 blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
284 blogposts[num_blogposts++] = (struct blogpost *) Data;
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.
291 if (num_blogposts > 0) {
293 /* Sort newest-to-oldest */
294 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
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) {
303 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
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)) {
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"));
315 if (i < (start_here + maxp)) {
316 blogpost_render(blogposts[i], with_comments);
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"));
326 /* Done. We are only freeing the array of pointers; the data itself
327 * will be freed along with the hash list.
336 int blogview_Cleanup(void **ViewSpecific)
338 BLOG *BL = (BLOG*) *ViewSpecific;
340 DeleteHash(&BL->BLOG);
348 InitModule_BLOGVIEWRENDERERS
351 RegisterReadLoopHandlerset(
353 blogview_GetParamsGetServerCall,
357 blogview_LoadMsgFromServer,
361 RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);