Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[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         long *msgs;             /* Array of msgnums for messages we are displaying */
31         int num_msgs;           /* Number of msgnums stored in 'msgs' */
32         int alloc_msgs;         /* Currently allocated size of array */
33 };
34
35
36 /*
37  * Destructor for 'struct blogpost' which does the rendering first.
38  * By rendering from here, we eliminate the need for a separate iterator, although
39  * we might run into trouble when we get around to displaying newest-to-oldest...
40  * FIXME do the needful with regard to gettext
41  */
42 void blogpost_render_and_destroy(struct blogpost *bp) {
43         const StrBuf *Mime;
44
45         if (bp->num_msgs > 0) {
46                 read_message(WC->WBuf, HKEY("view_message"), bp->msgs[0], NULL, &Mime);
47                 wc_printf("<div align=\"right\"><i>%d comments</i></div>\n", bp->num_msgs - 1);
48                 wc_printf("<br>\n");
49         }
50
51
52         if (bp->alloc_msgs > 0) {
53                 free(bp->msgs);
54         }
55         free(bp);
56 }
57
58
59 /*
60  * Data which gets returned from a call to blogview_learn_thread_references()
61  */
62 struct bltr {
63         int id;
64         int refs;
65 };
66
67
68 /*
69  * Entry point for message read operations.
70  */
71 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
72                                    void **ViewSpecific, 
73                                    long oper, 
74                                    char *cmd, 
75                                    long len)
76 {
77         HashList *BLOG = NewHash(1, NULL);
78         *ViewSpecific = BLOG;
79
80         Stat->startmsg = (-1);                                  /* not used here */
81         Stat->sortit = 1;                                       /* not used here */
82         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
83         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
84         
85         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
86         rlid[2].cmd(cmd, len);
87         
88         return 200;
89 }
90
91
92 /*
93  * Given a 'struct blogpost' containing a msgnum, populate the id
94  * and refs fields by fetching them from the Citadel server
95  */
96 struct bltr blogview_learn_thread_references(long msgnum)
97 {
98         StrBuf *Buf;
99         StrBuf *r;
100         struct bltr bltr = { 0, 0 } ;
101         Buf = NewStrBuf();
102         r = NewStrBuf();
103         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
104         StrBuf_ServGetln(Buf);
105         if (GetServerStatus(Buf, NULL) == 1) {
106                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
107                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
108                                 StrBufCutLeft(Buf, 5);
109                                 bltr.id = HashLittle(ChrPtr(Buf), StrLength(Buf));
110                         }
111                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
112                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
113                                 StrBufExtract_token(r, Buf, 0, '|');
114                                 bltr.refs = HashLittle(ChrPtr(r), StrLength(r));
115                         }
116                 }
117         }
118         FreeStrBuf(&Buf);
119         FreeStrBuf(&r);
120         return(bltr);
121 }
122
123
124 /*
125  * This function is called for every message in the list.
126  */
127 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
128                               void **ViewSpecific, 
129                               message_summary* Msg, 
130                               int is_new, 
131                               int i)
132 {
133         HashList *BLOG = (HashList *) *ViewSpecific;
134         struct bltr b;
135         struct blogpost *bp = NULL;
136
137         b = blogview_learn_thread_references(Msg->msgnum);
138
139         /* FIXME an optimization here -- one we ought to perform -- is to exit this
140          * function immediately if the viewer is only interested in a single post and
141          * that message ID is neither the id nor the refs.  Actually, that might *be*
142          * the way to display only a single message (with or without comments).
143          */
144
145         if (b.refs == 0) {
146                 bp = malloc(sizeof(struct blogpost));
147                 if (!bp) return(200);
148                 memset(bp, 0, sizeof (struct blogpost));
149                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
150                                         (DeleteHashDataFunc)blogpost_render_and_destroy);
151         }
152         else {
153                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
154         }
155
156         /*
157          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
158          * blog post itself or a comment attached to it; either way, the code is the same from
159          * this point onward.
160          */
161         if (bp != NULL) {
162                 if (bp->alloc_msgs == 0) {
163                         bp->alloc_msgs = 1000;
164                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
165                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
166                 }
167                 if (bp->num_msgs >= bp->alloc_msgs) {
168                         bp->alloc_msgs *= 2;
169                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
170                         memset(&bp->msgs[bp->num_msgs], 0,
171                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
172                 }
173                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
174         }
175
176         return 200;
177 }
178
179
180 /*
181  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
182  */
183 int blogview_sortfunc(const void *s1, const void *s2) {
184         long *l1 = (long *)(s1);
185         long *l2 = (long *)(s2);
186
187         if (*l1 > *l2) return(-1);
188         if (*l1 < *l2) return(+1);
189         return(0);
190 }
191
192
193 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
194 {
195         /*HashList *BLOG = (HashList *) *ViewSpecific;*/
196
197         /*
198          * This will require several different modes:
199          * * Top level
200          * * Single story permalink
201          * * Comments
202          * * etc
203          */
204
205         return(0);
206 }
207
208
209 int blogview_Cleanup(void **ViewSpecific)
210 {
211         HashList *BLOG = (HashList *) *ViewSpecific;
212
213         DeleteHash(&BLOG);
214
215         wDumpContent(1);
216         return 0;
217 }
218
219
220 void 
221 InitModule_BLOGVIEWRENDERERS
222 (void)
223 {
224         RegisterReadLoopHandlerset(
225                 VIEW_BLOG,
226                 blogview_GetParamsGetServerCall,
227                 NULL,
228                 NULL, 
229                 blogview_LoadMsgFromServer,
230                 blogview_render,
231                 blogview_Cleanup
232         );
233 }