summaryrefslogtreecommitdiffstats
path: root/lurker/render/thread.cpp
blob: e41d5f856c4d8e9423c167b3a6193418f41e02bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*  $Id: thread.cpp 1649 2009-10-19 14:35:01Z terpstra $
 *  
 *  thread.cpp - Handle a thread/ command
 *  
 *  Copyright (C) 2002 - Wesley W. Terpstra
 *  
 *  License: GPL
 *  
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 *  
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; version 2.
 *    
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _FILE_OFFSET_BITS 64

#include <Keys.h>

#include "commands.h"
#include "Threading.h"
#include "Cache.h"

int handle_thread(const Config& cfg, ESort::Reader* db, const string& param)
{
	Request req = parse_request(param);
	cfg.options = req.options;
	
	if (!MessageId::is_full(req.options.c_str()) ||
	    req.options.length() != MessageId::full_len)
		error(_("Bad request"), param,
		      _("The given parameter was not of the correct format. "
		        "A thread request must be formatted like: "
		         "thread/YYYYMMDD.HHMMSS.hashcode.xml"));
	
	MessageId id(req.options.c_str());
	string ok;
	
	Summary source(id);
	// Identical error for a not allowed message as non-existing (security)
	if ((ok = source.load(db, cfg)) != "" || !source.allowed())
	{
		if (ok == "") ok = "not in a mailbox"; // fake
		error(_("Database thread source pull failure"), ok,
		      _("The specified message does not exist."));
	}
	
	if (source.deleted())
		error(_("Database thread source pull failure"), "not found",
		      _("The specified message has been deleted."));
	
	Threading::Key spot;
	Threading thread;
	if ((ok = thread.load(db, source, spot)) != "" ||
	    (ok = thread.draw_tree(db)) != "")
		error(_("Database thread tree load failure"), ok,
		      _("Something internal to the database failed. "
		        "Please contact the lurker user mailing list for "
		        "further assistence."));
	
	set<string> lists;
	for (Threading::Key j = 0; j < thread.size(); ++j)
	{
		Summary& sum = thread.getSummary(j);
		if (!sum.loaded() && (ok = sum.load(db, cfg)) != "")
			break;
		
		const set<string>& mboxs = sum.mboxs();
		set<string>::const_iterator i;
		for (i = mboxs.begin(); i != mboxs.end(); ++i)
			lists.insert(*i);
	}
	
	if (ok != "")
		error(_("Database thread sumary load failure"), ok,
		      _("Something internal to the database failed. "
		        "Please contact the lurker user mailing list for "
		        "further assistence."));
	
	Cache cache(cfg, "thread", param, req.ext);
	
	cache.o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
		<< "<?xml-stylesheet type=\"text/xsl\" href=\"../ui/thread.xsl\"?>\n"
		<< "<thread xml:lang=\"" << req.language << "\">\n"
		<< " <mode>" << req.ext << "</mode>\n"
		<< " " << cfg(req.language) << "\n"
		<< " <hash>" << subject_hash(source.subject().c_str()) << "</hash>\n";
	
	set<string>::const_iterator list;
	for (list = lists.begin(); list != lists.end(); ++list)
	{
		Config::Lists::const_iterator desc = cfg.lists.find(*list);
		if (desc == cfg.lists.end()) continue;
		cache.o << " " << desc->second(req.language) << "\n";
	}
	
	int head = -1;
	for (Threading::Key i = 0; i < thread.size(); ++i)
	{
		if (i == spot)
			cache.o << " <row selected=\"true\">\n";
		else	cache.o << " <row>\n";
		
		cache.o << "  <tree>";
		thread.draw_tree_row(cache.o, &head, i);
		cache.o << "</tree>\n  " << thread.getSummary(i) << "\n </row>\n";
	}
	
	cache.o << "</thread>\n";
	
	return 0;
}