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