Removed the unused second argument from the do_template() macro
[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  * Array type for a blog post.  The first message is the post; the rest are comments
28  */
29 struct blogpost {
30         int top_level_id;
31         long *msgs;             /* Array of msgnums for messages we are displaying */
32         int num_msgs;           /* Number of msgnums stored in 'msgs' */
33         int alloc_msgs;         /* Currently allocated size of array */
34 };
35
36
37 /*
38  * Generate a permalink for a post
39  * (Call with NULL arguments to make this function wcprintf() the permalink
40  * instead of writing it to the template)
41  */
42 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
43         char perma[SIZ];
44         
45         strcpy(perma, "/readfwd?go=");
46         urlesc(&perma[strlen(perma)], sizeof(perma)-strlen(perma), (char *)ChrPtr(WC->CurRoom.name));
47         snprintf(&perma[strlen(perma)], sizeof(perma)-strlen(perma), "?p=%d", WC->bptlid);
48         if (!Target) {
49                 wc_printf("%s", perma);
50         }
51         else {
52                 StrBufAppendPrintf(Target, "%s", perma);
53         }
54 }
55
56
57 /*
58  * Destructor for 'struct blogpost' which does the rendering first.
59  * By rendering from here, we eliminate the need for a separate iterator, although
60  * we might run into trouble when we get around to displaying newest-to-oldest...
61  */
62 void blogpost_render_and_destroy(struct blogpost *bp) {
63         const StrBuf *Mime;
64         int p = 0;
65         int i;
66
67         p = atoi(BSTR("p"));    /* are we looking for a specific post? */
68         WC->bptlid = bp->top_level_id;
69
70         if ( ((p == 0) || (p == bp->top_level_id)) && (bp->num_msgs > 0) ) {
71                 /* Show the top level post */
72                 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime);
73
74                 if (p == 0) {
75                         /* Show the number of comments */
76                         wc_printf("<a href=\"readfwd?p=%d?go=", bp->top_level_id);
77                         urlescputs(ChrPtr(WC->CurRoom.name));
78                         wc_printf("#comments\">");
79                         wc_printf(_("%d comments"), bp->num_msgs - 1);
80                         wc_printf("</a> | <a href=\"");
81                         tmplput_blog_permalink(NULL, NULL);
82                         wc_printf("\">%s</a>", _("permalink"));
83                         wc_printf("<br><br><br>\n");
84                 }
85                 else if (bp->num_msgs < 2) {
86                         wc_printf(_("%d comments"), 0);
87                 }
88                 else {
89                         wc_printf("<a name=\"comments\"></a>\n");
90                         wc_printf(_("%d comments"), bp->num_msgs - 1);
91                         wc_printf(" | <a href=\"");
92                         tmplput_blog_permalink(NULL, NULL);
93                         wc_printf("\">%s</a>", _("permalink"));
94                         wc_printf("<br>\n");
95                         for (i=1; i<bp->num_msgs; ++i) {
96                                 read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime);
97                         }
98                 }
99         }
100
101
102         if (bp->alloc_msgs > 0) {
103                 free(bp->msgs);
104         }
105
106         /* offer the comment box */
107         if (p == bp->top_level_id) {
108                 do_template("blog_comment_box");
109         }
110
111         free(bp);
112 }
113
114
115 /*
116  * Data which gets returned from a call to blogview_learn_thread_references()
117  */
118 struct bltr {
119         int id;
120         int refs;
121 };
122
123
124 /*
125  * Entry point for message read operations.
126  */
127 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
128                                    void **ViewSpecific, 
129                                    long oper, 
130                                    char *cmd, 
131                                    long len)
132 {
133         HashList *BLOG = NewHash(1, NULL);
134         *ViewSpecific = BLOG;
135
136         Stat->startmsg = (-1);                                  /* not used here */
137         Stat->sortit = 1;                                       /* not used here */
138         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
139         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
140         
141         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
142         rlid[2].cmd(cmd, len);
143         
144         return 200;
145 }
146
147
148 /*
149  * Given a 'struct blogpost' containing a msgnum, populate the id
150  * and refs fields by fetching them from the Citadel server
151  */
152 struct bltr blogview_learn_thread_references(long msgnum)
153 {
154         StrBuf *Buf;
155         StrBuf *r;
156         int len;
157         struct bltr bltr = { 0, 0 } ;
158         Buf = NewStrBuf();
159         r = NewStrBuf();
160         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
161         StrBuf_ServGetln(Buf);
162         if (GetServerStatus(Buf, NULL) == 1) {
163                 while (len = StrBuf_ServGetln(Buf), 
164                        ((len >= 0) && 
165                         ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
166                 {
167                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
168                                 StrBufCutLeft(Buf, 5);
169                                 bltr.id = abs(HashLittle(ChrPtr(Buf), StrLength(Buf)));
170                         }
171                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
172                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
173                                 StrBufExtract_token(r, Buf, 0, '|');
174                                 bltr.refs = abs(HashLittle(ChrPtr(r), StrLength(r)));
175                         }
176                 }
177         }
178         FreeStrBuf(&Buf);
179         FreeStrBuf(&r);
180         return(bltr);
181 }
182
183
184 /*
185  * This function is called for every message in the list.
186  */
187 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
188                               void **ViewSpecific, 
189                               message_summary* Msg, 
190                               int is_new, 
191                               int i)
192 {
193         HashList *BLOG = (HashList *) *ViewSpecific;
194         struct bltr b;
195         struct blogpost *bp = NULL;
196
197         b = blogview_learn_thread_references(Msg->msgnum);
198
199         /* FIXME an optimization here -- one we ought to perform -- is to exit this
200          * function immediately if the viewer is only interested in a single post and
201          * that message ID is neither the id nor the refs.  Actually, that might *be*
202          * the way to display only a single message (with or without comments).
203          */
204
205         if (b.refs == 0) {
206                 bp = malloc(sizeof(struct blogpost));
207                 if (!bp) return(200);
208                 memset(bp, 0, sizeof (struct blogpost));
209                 bp->top_level_id = b.id;
210                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
211                                         (DeleteHashDataFunc)blogpost_render_and_destroy);
212         }
213         else {
214                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
215         }
216
217         /*
218          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
219          * blog post itself or a comment attached to it; either way, the code is the same from
220          * this point onward.
221          */
222         if (bp != NULL) {
223                 if (bp->alloc_msgs == 0) {
224                         bp->alloc_msgs = 1000;
225                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
226                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
227                 }
228                 if (bp->num_msgs >= bp->alloc_msgs) {
229                         bp->alloc_msgs *= 2;
230                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
231                         memset(&bp->msgs[bp->num_msgs], 0,
232                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
233                 }
234                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
235         }
236
237         return 200;
238 }
239
240
241 /*
242  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
243  */
244 int blogview_sortfunc(const void *s1, const void *s2) {
245         long *l1 = (long *)(s1);
246         long *l2 = (long *)(s2);
247
248         if (*l1 > *l2) return(-1);
249         if (*l1 < *l2) return(+1);
250         return(0);
251 }
252
253
254 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
255 {
256         /* HashList *BLOG = (HashList *) *ViewSpecific; */
257
258         /*
259          * No code needed here -- we render during disposition.
260          * Maybe this is the location where we want to handle pretty permalinks.
261          */
262
263         return(0);
264 }
265
266
267 int blogview_Cleanup(void **ViewSpecific)
268 {
269         HashList *BLOG = (HashList *) *ViewSpecific;
270
271         DeleteHash(&BLOG);
272
273         wDumpContent(1);
274         return 0;
275 }
276
277 void 
278 InitModule_BLOGVIEWRENDERERS
279 (void)
280 {
281         RegisterReadLoopHandlerset(
282                 VIEW_BLOG,
283                 blogview_GetParamsGetServerCall,
284                 NULL,
285                 NULL, 
286                 blogview_LoadMsgFromServer,
287                 blogview_render,
288                 blogview_Cleanup
289         );
290         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
291 }