Readloop remove special cases
[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 "dav.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 a 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;  /* This is used in templates; do not remove it */
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 (with_comments) {
62                 /* Show any existing comments, then offer the comment box */
63                 wc_printf("<a name=\"comments\"></a>\n");
64                 wc_printf(_("%d comments"), bp->num_msgs - 1);
65                 wc_printf(" | <a href=\"");
66                 tmplput_blog_permalink(NULL, NULL);
67                 wc_printf("\">%s</a>", _("permalink"));
68                 wc_printf("<br>\n");
69                 for (i=1; i<bp->num_msgs; ++i) {
70                         read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime);
71                 }
72                 do_template("view_blog_comment_box");
73         }
74
75         else {
76                 /* Show only the number of comments */
77                 wc_printf("<a href=\"readfwd?p=%d?go=", bp->top_level_id);
78                 urlescputs(ChrPtr(WC->CurRoom.name));
79                 wc_printf("#comments\">");
80                 wc_printf(_("%d comments"), bp->num_msgs - 1);
81                 wc_printf("</a> | <a href=\"");
82                 tmplput_blog_permalink(NULL, NULL);
83                 wc_printf("\">%s</a>", _("permalink"));
84                 wc_printf("<br><br><br>\n");
85         }
86 }
87
88
89 /*
90  * Destructor for "struct blogpost"
91  */
92 void blogpost_destroy(struct blogpost *bp) {
93         if (bp->alloc_msgs > 0) {
94                 free(bp->msgs);
95         }
96         free(bp);
97 }
98
99
100 /*
101  * Entry point for message read operations.
102  */
103 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
104                                    void **ViewSpecific, 
105                                    long oper, 
106                                    char *cmd, 
107                                     long len,
108                                     char *filter,
109                                     long flen)
110 {
111         HashList *BLOG = NewHash(1, NULL);
112         *ViewSpecific = BLOG;
113
114         Stat->startmsg = (-1);                                  /* not used here */
115         Stat->sortit = 1;                                       /* not used here */
116         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
117         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
118         
119         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
120         rlid[2].cmd(cmd, len);
121         
122         return 200;
123 }
124
125
126 /*
127  * Given a msgnum, populate the id and refs fields of
128  * a "struct bltr" by fetching them from the Citadel server
129  */
130 struct bltr blogview_learn_thread_references(long msgnum)
131 {
132         StrBuf *Buf;
133         StrBuf *r;
134         int len;
135         struct bltr bltr = { 0, 0 } ;
136         Buf = NewStrBuf();
137         r = NewStrBuf();
138         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
139         StrBuf_ServGetln(Buf);
140         if (GetServerStatus(Buf, NULL) == 1) {
141                 while (len = StrBuf_ServGetln(Buf), 
142                        ((len >= 0) && 
143                         ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
144                 {
145                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
146                                 StrBufCutLeft(Buf, 5);
147                                 bltr.id = ThreadIdHash(Buf);
148                         }
149                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
150                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
151                                 StrBufExtract_token(r, Buf, 0, '|');
152                                 bltr.refs = ThreadIdHash(r);
153                         }
154                 }
155         }
156         FreeStrBuf(&Buf);
157         FreeStrBuf(&r);
158         return(bltr);
159 }
160
161
162 /*
163  * This function is called for every message in the list.
164  */
165 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
166                               void **ViewSpecific, 
167                               message_summary* Msg, 
168                               int is_new, 
169                               int i)
170 {
171         HashList *BLOG = (HashList *) *ViewSpecific;
172         struct bltr b;
173         struct blogpost *bp = NULL;
174         int p = 0;
175
176         b = blogview_learn_thread_references(Msg->msgnum);
177
178         /* Stop processing if the viewer is only interested in a single post and
179          * that message ID is neither the id nor the refs.
180          */
181         p = atoi(BSTR("p"));    /* are we looking for a specific post? */
182         if ((p != 0) && (p != b.id) && (p != b.refs)) {
183                 return 200;
184         }
185
186         /*
187          * Add our little bundle of blogworthy wonderfulness to the hash table
188          */
189         if (b.refs == 0) {
190                 bp = malloc(sizeof(struct blogpost));
191                 if (!bp) return(200);
192                 memset(bp, 0, sizeof (struct blogpost));
193                 bp->top_level_id = b.id;
194                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp, (DeleteHashDataFunc)blogpost_destroy);
195         }
196         else {
197                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
198         }
199
200         /*
201          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
202          * blog post itself or a comment attached to it; either way, the code is the same from
203          * this point onward.
204          */
205         if (bp != NULL) {
206                 if (bp->alloc_msgs == 0) {
207                         bp->alloc_msgs = 1000;
208                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
209                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
210                 }
211                 if (bp->num_msgs >= bp->alloc_msgs) {
212                         bp->alloc_msgs *= 2;
213                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
214                         memset(&bp->msgs[bp->num_msgs], 0,
215                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
216                 }
217                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
218         }
219         else {
220                 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
221         }
222
223         return 200;
224 }
225
226
227 /*
228  * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
229  * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
230  */
231 static int blogview_sortfunc(const void *a, const void *b) { 
232         struct blogpost * const *one = a;
233         struct blogpost * const *two = b;
234
235         if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
236         if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
237         return(0);
238 }
239
240
241 /*
242  * All blogpost entries are now in the hash list.
243  * Sort them, select the desired range, and render what we want to see.
244  */
245 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
246 {
247         HashList *BLOG = (HashList *) *ViewSpecific;
248         HashPos *it;
249         const char *Key;
250         void *Data;
251         long len;
252         int i;
253         struct blogpost **blogposts = NULL;
254         int num_blogposts = 0;
255         int num_blogposts_alloc = 0;
256         int with_comments = 0;
257         int firstp = 0;
258         int maxp = 0;
259
260         /* Comments are shown if we are only viewing a single blog post */
261         if (atoi(BSTR("p"))) with_comments = 1;
262
263         firstp = atoi(BSTR("firstp"));  /* start reading at... */
264         maxp = atoi(BSTR("maxp"));      /* max posts to show... */
265         if (maxp < 1) maxp = 5;         /* default; move somewhere else? */
266
267         /* Iterate through the hash list and copy the data pointers into an array */
268         it = GetNewHashPos(BLOG, 0);
269         while (GetNextHashPos(BLOG, it, &len, &Key, &Data)) {
270                 if (num_blogposts >= num_blogposts_alloc) {
271                         if (num_blogposts_alloc == 0) {
272                                 num_blogposts_alloc = 100;
273                         }
274                         else {
275                                 num_blogposts_alloc *= 2;
276                         }
277                         blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
278                 }
279                 blogposts[num_blogposts++] = (struct blogpost *) Data;
280         }
281         DeleteHashPos(&it);
282
283         /* Now we have our array.  It is ONLY an array of pointers.  The objects to
284          * which they point are still owned by the hash list.
285          */
286         if (num_blogposts > 0) {
287                 int start_here = 0;
288                 /* Sort newest-to-oldest */
289                 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
290
291                 /* allow the user to select a starting point in the list */
292                 for (i=0; i<num_blogposts; ++i) {
293                         if (blogposts[i]->top_level_id == firstp) {
294                                 start_here = i;
295                         }
296                 }
297
298                 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
299
300                 /* Now go through the list and render what we've got */
301                 for (i=start_here; i<num_blogposts; ++i) {
302                         if ((i > 0) && (i == start_here)) {
303                                 int j = i - maxp;
304                                 if (j < 0) j = 0;
305                                 wc_printf("<div class=\"newer_blog_posts\"><a href=\"readfwd?go=");
306                                 urlescputs(ChrPtr(WC->CurRoom.name));
307                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[j]->top_level_id, maxp);
308                                 wc_printf("%s →</a></div>\n", _("Newer posts"));
309                         }
310                         if (i < (start_here + maxp)) {
311                                 blogpost_render(blogposts[i], with_comments);
312                         }
313                         else if (i == (start_here + maxp)) {
314                                 wc_printf("<div class=\"older_blog_posts\"><a href=\"readfwd?go=");
315                                 urlescputs(ChrPtr(WC->CurRoom.name));
316                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[i]->top_level_id, maxp);
317                                 wc_printf("← %s</a></div>\n", _("Older posts"));
318                         }
319                 }
320
321                 /* Done.  We are only freeing the array of pointers; the data itself
322                  * will be freed along with the hash list.
323                  */
324                 free(blogposts);
325         }
326
327         return(0);
328 }
329
330
331 int blogview_Cleanup(void **ViewSpecific)
332 {
333         HashList *BLOG = (HashList *) *ViewSpecific;
334
335         DeleteHash(&BLOG);
336
337         wDumpContent(1);
338         return 0;
339 }
340
341
342 void 
343 InitModule_BLOGVIEWRENDERERS
344 (void)
345 {
346         RegisterReadLoopHandlerset(
347                 VIEW_BLOG,
348                 blogview_GetParamsGetServerCall,
349                 NULL,
350                 NULL,
351                 NULL, 
352                 blogview_LoadMsgFromServer,
353                 blogview_render,
354                 blogview_Cleanup
355         );
356         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
357 }