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