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