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"
26 * Data which gets passed around between the various functions in this module
38 struct blogpost *msgs; /* Array of msgnums for messages we are displaying */
39 int num_msgs; /* Number of msgnums stored in 'msgs' */
40 int alloc_msgs; /* Currently allocated size of array */
45 * Entry point for message read operations.
47 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
53 struct blogview *BLOG = malloc(sizeof(struct blogview));
54 memset(BLOG, 0, sizeof(struct blogview));
57 Stat->startmsg = (-1); /* not used here */
58 Stat->sortit = 1; /* not used here */
59 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
60 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
62 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
63 rlid[2].cmd(cmd, len);
70 * This function is called for every message in the list.
72 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
78 struct blogview *BLOG = (struct blogview *) *ViewSpecific;
80 if (BLOG->alloc_msgs == 0) {
81 BLOG->alloc_msgs = 1000;
82 BLOG->msgs = malloc(BLOG->alloc_msgs * sizeof(struct blogpost));
83 memset(BLOG->msgs, 0, (BLOG->alloc_msgs * sizeof(struct blogpost)) );
86 /* Check our buffer size */
87 if (BLOG->num_msgs >= BLOG->alloc_msgs) {
88 BLOG->alloc_msgs *= 2;
89 BLOG->msgs = realloc(BLOG->msgs, (BLOG->alloc_msgs * sizeof(long)));
90 memset(&BLOG->msgs[BLOG->num_msgs], 0, ((BLOG->alloc_msgs - BLOG->num_msgs) * sizeof(long)) );
93 BLOG->msgs[BLOG->num_msgs++].msgnum = Msg->msgnum;
94 BLOG->msgs[BLOG->num_msgs].id = 0;
95 BLOG->msgs[BLOG->num_msgs].refs = 0;
96 BLOG->msgs[BLOG->num_msgs].comment_count = 0;
104 * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
106 int blogview_sortfunc(const void *s1, const void *s2) {
107 struct blogpost *l1 = (struct blogpost *)(s1);
108 struct blogpost *l2 = (struct blogpost *)(s2);
110 if (l1->msgnum > l2->msgnum) return(-1);
111 if (l1->msgnum < l2->msgnum) return(+1);
118 * Given a 'struct blogpost' containing a msgnum, populate the id
119 * and refs fields by fetching them from the Citadel server
121 void blogview_learn_thread_references(struct blogpost *bp)
125 serv_printf("MSG0 %ld|1", bp->msgnum); /* top level citadel headers only */
126 StrBuf_ServGetln(Buf);
127 if (GetServerStatus(Buf, NULL) == 1) {
128 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
129 if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
130 StrBufCutLeft(Buf, 5);
131 bp->id = HashLittle(ChrPtr(Buf), StrLength(Buf));
133 else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
134 StrBufCutLeft(Buf, 5); /* trim the field name */
135 StrBufCutAt(Buf, 0, "|"); /* trim all but the first thread ref */
136 bp->refs = HashLittle(ChrPtr(Buf), StrLength(Buf));
146 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
148 struct blogview *BLOG = (struct blogview *) *ViewSpecific;
152 if (Stat->nummsgs > 0) {
153 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
154 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
157 /* Pass #2 - learn thread references */
158 lprintf(9, "learning thread references\n");
159 for (i=0; (i<BLOG->num_msgs); ++i) {
160 if (BLOG->msgs[i].msgnum > 0L) {
161 blogview_learn_thread_references(&BLOG->msgs[i]);
165 /* Pass #3 - turn it into a thread tree */
166 /* FIXME implement this */
169 * This will require several different modes:
171 * * Single story permalink
177 for (i=0; (i<BLOG->num_msgs); ++i) {
178 if (BLOG->msgs[i].msgnum > 0L) {
179 wc_printf("Message %d, #%ld, id %d, refs %d<br>\n",
181 BLOG->msgs[i].msgnum,
190 for (i=0; (i<BLOG->num_msgs); ++i) {
191 if (BLOG->msgs[i].msgnum > 0L) {
192 if (BLOG->msgs[i].refs == 0) {
193 wc_printf("<b>Message %d, #%ld, id %d, refs %d</b><br>\n",
195 BLOG->msgs[i].msgnum,
199 for (j=0; (j<BLOG->num_msgs); ++j) {
200 if (BLOG->msgs[j].refs == BLOG->msgs[i].id) {
201 wc_printf("* comment %d<br>\n", j);
212 int blogview_Cleanup(void **ViewSpecific)
214 struct blogview *BLOG = (struct blogview *) *ViewSpecific;
216 if (BLOG->alloc_msgs != 0) {
227 InitModule_BLOGVIEWRENDERERS
230 RegisterReadLoopHandlerset(
232 blogview_GetParamsGetServerCall,
235 blogview_LoadMsgFromServer,