File: nntpnewsrc.C

package info (click to toggle)
cone 0.89-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 25,628 kB
  • ctags: 14,171
  • sloc: ansic: 85,400; cpp: 82,903; sh: 11,713; makefile: 1,732; perl: 832; yacc: 291; sed: 16
file content (215 lines) | stat: -rw-r--r-- 3,161 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
** Copyright 2003, Double Precision Inc.
**
** See COPYING for distribution information.
*/

#include "nntpnewsrc.H"
#include <sstream>

using namespace std;

mail::nntp::newsrc::newsrc() : subscribed(true)
{
}

mail::nntp::newsrc::~newsrc()
{
}

mail::nntp::newsrc::newsrc(string s) : subscribed(true)
{
	if (s.size() == 0 || s[0] == '#')
		return;

	string::iterator b=s.begin(), e=s.end();

	while (b != e && !unicode_isspace((unsigned char)*b))
	{
		if (*b == ':')
		{
			newsgroupname=string(s.begin(), b);
			break;
		}

		if (*b == '!')
		{
			subscribed=false;
			newsgroupname=string(s.begin(), b);
			break;
		}
		b++;
	}

	while (b != e)
	{
		if (unicode_isspace((unsigned char)*b) || *b == ',')
		{
			b++;
			continue;
		}

		string::iterator p=b;

		while (b != e)
		{
			if (unicode_isspace((unsigned char)*b) || *b == ',')
				break;
			b++;
		}

		string range=string(p, b);

		msgnum_t firstMsg=0, lastMsg=0;
		char dummy;

		istringstream i(range);

		i >> firstMsg >> dummy >> lastMsg;

		if (firstMsg > 0)
		{
			if (dummy == '-' && lastMsg >= firstMsg)
				;
			else
				lastMsg=firstMsg;

			if (msglist.size() > 0)
			{
				msgnum_t ending=msglist.end()[-1].second;

				if (ending+1 == firstMsg)
				{
					msglist.end()[-1].second=lastMsg;
					continue;
				}

				if (ending >= firstMsg)
					continue; // Error -- ignore
			}

			msglist.push_back( make_pair(firstMsg, lastMsg));
		}
	}
}

mail::nntp::newsrc::operator string() const
{
	ostringstream o;

	o << newsgroupname << (subscribed ? ":":"!");

	string sep=" ";

	vector< pair<msgnum_t, msgnum_t> >::const_iterator b=msglist.begin(),
		e=msglist.end();

	while (b != e)
	{
		o << sep;
		sep=",";
		o << b->first;

		if (b->second > b->first)
			o << "-" << b->second;
		b++;
	}

	return o.str();
}

// Mark msg as read

void mail::nntp::newsrc::read(msgnum_t m)
{
	vector< pair<msgnum_t, msgnum_t> >::iterator b=msglist.begin(),
		e=msglist.end();

	// We typically do stuff at the end of the list

	while (b != e)
	{
		--e;

		if (e->first > m)
			continue;

		if (m <= e->second)
			return; // Already marked read

		bool growThis= e->second + 1 == m; // Grow this entry

		bool growNext= e != msglist.end() && m + 1 == e[1].first;

		if (growThis)
		{
			if (growNext) // Merge two ranges
			{
				e->second=e[1].second;
				msglist.erase(e+1);
				return;
			}

			++e->second;
			return;
		}
		if (growNext)
		{
			e[1].first--;
			return;
		}

		msglist.insert(e+1, make_pair(m, m));
		return;
	}

	if (msglist.size() > 0 && m + 1 && msglist[0].first)
	{
		msglist[0].first--;
		return;
	}

	msglist.insert(msglist.begin(), make_pair(m, m));
}

void mail::nntp::newsrc::unread(msgnum_t m)
{
	vector< pair<msgnum_t, msgnum_t> >::iterator b=msglist.begin(),
		e=msglist.end();

	while (b != e)
	{
		--e;

		if (e->second < m)
			break;

		if (e->first > m)
			continue;


		if (e->first == e->second) // Delete this range
		{
			msglist.erase(e);
			break;
		}

		if (e->first == m)
		{
			++e->first;
			break;
		}

		if (e->second == m)
		{
			--e->second;
			break;
		}

		// Split

		msglist.insert(e+1, make_pair(m+1, e->second));
		e->second=m-1;
		break;
	}
}