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