2 * Blog view renderer module for WebCit
4 * Copyright (c) 1996-2010 by the citadel.org team
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.
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.
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
22 #include "webserver.h"
27 * Array type for a blog post. The first message is the post; the rest are comments
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 */
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...
42 void blogpost_render_and_destroy(struct blogpost *bp) {
47 p = atoi(BSTR("p")); /* are we looking for a specific post? */
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_message"), bp->msgs[0], NULL, &Mime);
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("\">%d comments</a>", bp->num_msgs - 1);
59 else if (bp->num_msgs < 2) {
60 wc_printf("dere r no comments here!<br>\n");
63 wc_printf("%d comments<br>\n", bp->num_msgs - 1);
64 wc_printf("<blockquote>");
65 for (i=1; i<bp->num_msgs; ++i) {
66 read_message(WC->WBuf, HKEY("view_message"), bp->msgs[i], NULL, &Mime);
68 wc_printf("</blockquote>");
73 if (bp->alloc_msgs > 0) {
81 * Data which gets returned from a call to blogview_learn_thread_references()
90 * Entry point for message read operations.
92 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
98 HashList *BLOG = NewHash(1, NULL);
101 Stat->startmsg = (-1); /* not used here */
102 Stat->sortit = 1; /* not used here */
103 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
104 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
106 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
107 rlid[2].cmd(cmd, len);
114 * Given a 'struct blogpost' containing a msgnum, populate the id
115 * and refs fields by fetching them from the Citadel server
117 struct bltr blogview_learn_thread_references(long msgnum)
121 struct bltr bltr = { 0, 0 } ;
124 serv_printf("MSG0 %ld|1", msgnum); /* top level citadel headers only */
125 StrBuf_ServGetln(Buf);
126 if (GetServerStatus(Buf, NULL) == 1) {
127 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
128 if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
129 StrBufCutLeft(Buf, 5);
130 bltr.id = HashLittle(ChrPtr(Buf), StrLength(Buf));
132 else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
133 StrBufCutLeft(Buf, 5); /* trim the field name */
134 StrBufExtract_token(r, Buf, 0, '|');
135 bltr.refs = HashLittle(ChrPtr(r), StrLength(r));
146 * This function is called for every message in the list.
148 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
150 message_summary* Msg,
154 HashList *BLOG = (HashList *) *ViewSpecific;
156 struct blogpost *bp = NULL;
158 b = blogview_learn_thread_references(Msg->msgnum);
160 /* FIXME an optimization here -- one we ought to perform -- is to exit this
161 * function immediately if the viewer is only interested in a single post and
162 * that message ID is neither the id nor the refs. Actually, that might *be*
163 * the way to display only a single message (with or without comments).
167 bp = malloc(sizeof(struct blogpost));
168 if (!bp) return(200);
169 memset(bp, 0, sizeof (struct blogpost));
170 bp->top_level_id = b.id;
171 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
172 (DeleteHashDataFunc)blogpost_render_and_destroy);
175 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
179 * Now we have a 'struct blogpost' to which we can add a message. It's either the
180 * blog post itself or a comment attached to it; either way, the code is the same from
184 if (bp->alloc_msgs == 0) {
185 bp->alloc_msgs = 1000;
186 bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
187 memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
189 if (bp->num_msgs >= bp->alloc_msgs) {
191 bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
192 memset(&bp->msgs[bp->num_msgs], 0,
193 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
195 bp->msgs[bp->num_msgs++] = Msg->msgnum;
203 * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
205 int blogview_sortfunc(const void *s1, const void *s2) {
206 long *l1 = (long *)(s1);
207 long *l2 = (long *)(s2);
209 if (*l1 > *l2) return(-1);
210 if (*l1 < *l2) return(+1);
215 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
217 /*HashList *BLOG = (HashList *) *ViewSpecific;*/
220 * No code needed here -- we render during disposition.
221 * Maybe this is the location where we want to handle pretty permalinks.
228 int blogview_Cleanup(void **ViewSpecific)
230 HashList *BLOG = (HashList *) *ViewSpecific;
240 InitModule_BLOGVIEWRENDERERS
243 RegisterReadLoopHandlerset(
245 blogview_GetParamsGetServerCall,
248 blogview_LoadMsgFromServer,