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