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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "msgCore.h" // precompiled header...
#include "nsIURL.h"
#include "nsNntpUrl.h"
#include "nsStringGlue.h"
#include "nsNewsUtils.h"
#include "nsMsgUtils.h"
#include "nntpCore.h"
#include "nsCOMPtr.h"
#include "nsIMsgDatabase.h"
#include "nsMsgDBCID.h"
#include "nsMsgNewsCID.h"
#include "nsIMsgFolder.h"
#include "nsIMsgNewsFolder.h"
#include "nsINntpService.h"
#include "nsIMsgMessageService.h"
#include "nsIMsgAccountManager.h"
#include "nsServiceManagerUtils.h"
nsNntpUrl::nsNntpUrl()
{
m_newsgroupPost = nullptr;
m_newsAction = nsINntpUrl::ActionUnknown;
m_addDummyEnvelope = false;
m_canonicalLineEnding = false;
m_filePath = nullptr;
m_getOldMessages = false;
m_key = nsMsgKey_None;
}
nsNntpUrl::~nsNntpUrl()
{
}
NS_IMPL_ADDREF_INHERITED(nsNntpUrl, nsMsgMailNewsUrl)
NS_IMPL_RELEASE_INHERITED(nsNntpUrl, nsMsgMailNewsUrl)
NS_INTERFACE_MAP_BEGIN(nsNntpUrl)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsINntpUrl)
NS_INTERFACE_MAP_ENTRY(nsINntpUrl)
NS_INTERFACE_MAP_ENTRY(nsIMsgMessageUrl)
NS_INTERFACE_MAP_ENTRY(nsIMsgI18NUrl)
NS_INTERFACE_MAP_END_INHERITING(nsMsgMailNewsUrl)
////////////////////////////////////////////////////////////////////////////////
// Begin nsINntpUrl specific support
////////////////////////////////////////////////////////////////////////////////
/* News URI parsing explanation:
* We support 3 different news URI schemes, essentially boiling down to 8
* different formats:
* news://host/group
* news://host/message
* news://host/
* news:group
* news:message
* nntp://host/group
* nntp://host/group/key
* news-message://host/group#key
*
* In addition, we use queries on the news URIs with authorities for internal
* NNTP processing. The most important one is ?group=group&key=key, for cache
* canonicalization.
*/
NS_IMETHODIMP nsNntpUrl::SetSpec(const nsACString &aSpec)
{
// For [s]news: URIs, we need to munge the spec if it is no authority, because
// the URI parser guesses the wrong thing otherwise
nsCString parseSpec(aSpec);
int32_t colon = parseSpec.Find(":");
// Our smallest scheme is 4 characters long, so colon must be at least 4
if (colon < 4 || colon + 1 == (int32_t) parseSpec.Length())
return NS_ERROR_MALFORMED_URI;
if (Substring(parseSpec, colon - 4, 4).EqualsLiteral("news") &&
parseSpec[colon + 1] != '/')
{
// To make this parse properly, we add in three slashes, which convinces the
// parser that the authority component is empty.
parseSpec = Substring(aSpec, 0, colon + 1);
parseSpec.AppendLiteral("///");
parseSpec += Substring(aSpec, colon + 1);
}
nsresult rv = nsMsgMailNewsUrl::SetSpec(parseSpec);
NS_ENSURE_SUCCESS(rv,rv);
nsAutoCString scheme;
rv = GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, rv);
if (scheme.EqualsLiteral("news") || scheme.EqualsLiteral("snews"))
rv = ParseNewsURL();
else if (scheme.EqualsLiteral("nntp") || scheme.EqualsLiteral("nntps"))
rv = ParseNntpURL();
else if (scheme.EqualsLiteral("news-message"))
{
nsAutoCString spec;
rv = GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
rv = nsParseNewsMessageURI(spec.get(), m_group, &m_key);
NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);
}
else
return NS_ERROR_MALFORMED_URI;
NS_ENSURE_SUCCESS(rv, rv);
rv = DetermineNewsAction();
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
nsresult nsNntpUrl::ParseNewsURL()
{
// The path here is the group/msgid portion
nsAutoCString path;
nsresult rv = GetFilePath(path);
NS_ENSURE_SUCCESS(rv, rv);
// Drop the potential beginning from the path
if (path.Length() && path[0] == '/')
path = Substring(path, 1);
// The presence of an `@' is a sign we have a msgid
if (path.Find("@") != -1 || path.Find("%40") != -1)
{
MsgUnescapeString(path, 0, m_messageID);
// Set group, key for ?group=foo&key=123 uris
nsAutoCString spec;
rv = GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
int32_t groupPos = spec.Find(kNewsURIGroupQuery); // find ?group=
int32_t keyPos = spec.Find(kNewsURIKeyQuery); // find &key=
if (groupPos != kNotFound && keyPos != kNotFound)
{
// get group name and message key
m_group = Substring(spec, groupPos + kNewsURIGroupQueryLen,
keyPos - groupPos - kNewsURIGroupQueryLen);
nsCString keyStr(Substring(spec, keyPos + kNewsURIKeyQueryLen));
m_key = keyStr.ToInteger(&rv, 10);
NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);
}
}
else
MsgUnescapeString(path, 0, m_group);
return NS_OK;
}
nsresult nsNntpUrl::ParseNntpURL()
{
nsAutoCString path;
nsresult rv = GetFilePath(path);
NS_ENSURE_SUCCESS(rv, rv);
if (path.Length() > 0 && path[0] == '/')
path = Substring(path, 1);
if (path.IsEmpty())
return NS_ERROR_MALFORMED_URI;
int32_t slash = path.FindChar('/');
if (slash == -1)
{
m_group = path;
m_key = nsMsgKey_None;
}
else
{
m_group = Substring(path, 0, slash);
nsAutoCString keyStr;
keyStr = Substring(path, slash + 1);
m_key = keyStr.ToInteger(&rv, 10);
NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);
// Keys must be at least one
if (m_key == 0)
return NS_ERROR_MALFORMED_URI;
}
return NS_OK;
}
nsresult nsNntpUrl::DetermineNewsAction()
{
nsAutoCString path;
nsresult rv = nsMsgMailNewsUrl::GetPath(path);
NS_ENSURE_SUCCESS(rv,rv);
nsAutoCString query;
rv = GetQuery(query);
NS_ENSURE_SUCCESS(rv, rv);
if (query.EqualsLiteral("cancel"))
{
m_newsAction = nsINntpUrl::ActionCancelArticle;
return NS_OK;
}
if (query.EqualsLiteral("list-ids"))
{
m_newsAction = nsINntpUrl::ActionListIds;
return NS_OK;
}
if (query.EqualsLiteral("newgroups"))
{
m_newsAction = nsINntpUrl::ActionListNewGroups;
return NS_OK;
}
if (StringBeginsWith(query, NS_LITERAL_CSTRING("search")))
{
m_newsAction = nsINntpUrl::ActionSearch;
return NS_OK;
}
if (StringBeginsWith(query, NS_LITERAL_CSTRING("part=")) ||
query.Find("&part=") > 0)
{
// news://news.mozilla.org:119/3B98D201.3020100%40cs.com?part=1
// news://news.mozilla.org:119/b58dme%24aia2%40ripley.netscape.com?header=print&part=1.2&type=image/jpeg&filename=Pole.jpg
m_newsAction = nsINntpUrl::ActionFetchPart;
return NS_OK;
}
if (!m_messageID.IsEmpty() || m_key != nsMsgKey_None)
{
m_newsAction = nsINntpUrl::ActionFetchArticle;
return NS_OK;
}
if (m_group.Find("*") >= 0)
{
// If the group is a wildmat, list groups instead of grabbing a group.
m_newsAction = nsINntpUrl::ActionListGroups;
return NS_OK;
}
if (!m_group.IsEmpty())
{
m_newsAction = nsINntpUrl::ActionGetNewNews;
return NS_OK;
}
// At this point, we have a URI that contains neither a query, a group, nor a
// message ID. Ergo, we don't know what it is.
m_newsAction = nsINntpUrl::ActionUnknown;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::SetGetOldMessages(bool aGetOldMessages)
{
m_getOldMessages = aGetOldMessages;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetGetOldMessages(bool * aGetOldMessages)
{
NS_ENSURE_ARG(aGetOldMessages);
*aGetOldMessages = m_getOldMessages;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetNewsAction(nsNewsAction *aNewsAction)
{
if (aNewsAction)
*aNewsAction = m_newsAction;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::SetNewsAction(nsNewsAction aNewsAction)
{
m_newsAction = aNewsAction;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetGroup(nsACString &group)
{
group = m_group;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetMessageID(nsACString &messageID)
{
messageID = m_messageID;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetKey(nsMsgKey *key)
{
NS_ENSURE_ARG_POINTER(key);
*key = m_key;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetPrincipalSpec(nsACString& aPrincipalSpec)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsNntpUrl::SetUri(const char * aURI)
{
mURI = aURI;
return NS_OK;
}
// from nsIMsgMessageUrl
NS_IMETHODIMP nsNntpUrl::GetUri(char ** aURI)
{
nsresult rv = NS_OK;
// if we have been given a uri to associate with this url, then use it
// otherwise try to reconstruct a URI on the fly....
if (mURI.IsEmpty()) {
nsAutoCString spec;
rv = GetSpec(spec);
NS_ENSURE_SUCCESS(rv,rv);
mURI = spec;
}
*aURI = ToNewCString(mURI);
if (!*aURI) return NS_ERROR_OUT_OF_MEMORY;
return rv;
}
NS_IMPL_GETSET(nsNntpUrl, AddDummyEnvelope, bool, m_addDummyEnvelope)
NS_IMPL_GETSET(nsNntpUrl, CanonicalLineEnding, bool, m_canonicalLineEnding)
NS_IMETHODIMP nsNntpUrl::SetMessageFile(nsIFile * aFile)
{
m_messageFile = aFile;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetMessageFile(nsIFile ** aFile)
{
if (aFile)
NS_IF_ADDREF(*aFile = m_messageFile);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// End nsINntpUrl specific support
////////////////////////////////////////////////////////////////////////////////
nsresult nsNntpUrl::SetMessageToPost(nsINNTPNewsgroupPost *post)
{
m_newsgroupPost = post;
if (post)
SetNewsAction(nsINntpUrl::ActionPostArticle);
return NS_OK;
}
nsresult nsNntpUrl::GetMessageToPost(nsINNTPNewsgroupPost **aPost)
{
NS_ENSURE_ARG_POINTER(aPost);
NS_IF_ADDREF(*aPost = m_newsgroupPost);
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::SetMessageHeader(nsIMsgDBHdr *aMsgHdr)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsNntpUrl::GetMessageHeader(nsIMsgDBHdr ** aMsgHdr)
{
nsresult rv;
nsCOMPtr <nsINntpService> nntpService = do_GetService(NS_NNTPSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr <nsIMsgMessageService> msgService = do_QueryInterface(nntpService, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsAutoCString spec(mOriginalSpec);
if (spec.IsEmpty()) {
// Handle the case where necko directly runs an internal news:// URL,
// one that looks like news://host/message-id?group=mozilla.announce&key=15
// Other sorts of URLs -- e.g. news://host/message-id -- will not succeed.
rv = GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
}
return msgService->MessageURIToMsgHdr(spec.get(), aMsgHdr);
}
NS_IMETHODIMP nsNntpUrl::IsUrlType(uint32_t type, bool *isType)
{
NS_ENSURE_ARG(isType);
switch(type)
{
case nsIMsgMailNewsUrl::eDisplay:
*isType = (m_newsAction == nsINntpUrl::ActionFetchArticle);
break;
default:
*isType = false;
};
return NS_OK;
}
NS_IMETHODIMP
nsNntpUrl::GetOriginalSpec(char **aSpec)
{
NS_ENSURE_ARG_POINTER(aSpec);
*aSpec = ToNewCString(mOriginalSpec);
if (!*aSpec) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
nsNntpUrl::SetOriginalSpec(const char *aSpec)
{
mOriginalSpec = aSpec;
return NS_OK;
}
NS_IMETHODIMP
nsNntpUrl::GetServer(nsIMsgIncomingServer **aServer)
{
NS_ENSURE_ARG_POINTER(aServer);
nsresult rv;
nsAutoCString scheme, user, host;
GetScheme(scheme);
GetUsername(user);
GetHost(host);
// No authority -> no server
if (host.IsEmpty())
{
*aServer = nullptr;
return NS_OK;
}
// Looking up the server...
// news-message is used purely internally, so it can never refer to the real
// attribute. nntp is never used internally, so it probably refers to the real
// one. news is used both internally and externally, so it could refer to
// either one. We'll assume it's an internal one first, though.
bool isNews = scheme.EqualsLiteral("news") || scheme.EqualsLiteral("snews");
bool isNntp = scheme.EqualsLiteral("nntp") || scheme.EqualsLiteral("nntps");
bool tryReal = isNntp;
nsCOMPtr<nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Ignoring return results: it is perfectly acceptable for the server to not
// exist, but FindServer (and not FindRealServer) throws NS_ERROR_UNEXPECTED
// in this case.
*aServer = nullptr;
if (tryReal)
accountManager->FindRealServer(user, host, NS_LITERAL_CSTRING("nntp"), 0,
aServer);
else
accountManager->FindServer(user, host, NS_LITERAL_CSTRING("nntp"), aServer);
if (!*aServer && (isNews || isNntp))
{
// Didn't find it, try the other option
if (tryReal)
accountManager->FindServer(user, host, NS_LITERAL_CSTRING("nntp"),
aServer);
else
accountManager->FindRealServer(user, host, NS_LITERAL_CSTRING("nntp"), 0,
aServer);
}
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::GetFolder(nsIMsgFolder **msgFolder)
{
NS_ENSURE_ARG_POINTER(msgFolder);
nsresult rv;
nsCOMPtr<nsIMsgIncomingServer> server;
rv = GetServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, rv);
// Need a server and a group to get the folder
if (!server || m_group.IsEmpty())
{
*msgFolder = nullptr;
return NS_OK;
}
// Find the group on the server
nsCOMPtr<nsINntpIncomingServer> nntpServer = do_QueryInterface(server, &rv);
NS_ENSURE_SUCCESS(rv, rv);
bool hasGroup = false;
rv = nntpServer->ContainsNewsgroup(m_group, &hasGroup);
NS_ENSURE_SUCCESS(rv, rv);
if (!hasGroup)
{
*msgFolder = nullptr;
return NS_OK;
}
nsCOMPtr<nsIMsgNewsFolder> newsFolder;
rv = nntpServer->FindGroup(m_group, getter_AddRefs(newsFolder));
NS_ENSURE_SUCCESS(rv, rv);
return newsFolder->QueryInterface(NS_GET_IID(nsIMsgFolder), (void**)msgFolder);
}
NS_IMETHODIMP
nsNntpUrl::GetFolderCharset(char **aCharacterSet)
{
nsCOMPtr<nsIMsgFolder> folder;
nsresult rv = GetFolder(getter_AddRefs(folder));
// don't assert here. this can happen if there is no message folder
// like when we display a news://host/message-id url
if (NS_FAILED(rv) || !folder)
return rv;
nsCString tmpStr;
rv = folder->GetCharset(tmpStr);
*aCharacterSet = ToNewCString(tmpStr);
return rv;
}
NS_IMETHODIMP nsNntpUrl::GetFolderCharsetOverride(bool * aCharacterSetOverride)
{
nsCOMPtr<nsIMsgFolder> folder;
nsresult rv = GetFolder(getter_AddRefs(folder));
NS_ENSURE_SUCCESS(rv,rv);
NS_ENSURE_TRUE(folder, NS_ERROR_FAILURE);
rv = folder->GetCharsetOverride(aCharacterSetOverride);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
NS_IMETHODIMP nsNntpUrl::GetCharsetOverRide(char ** aCharacterSet)
{
if (!mCharsetOverride.IsEmpty())
*aCharacterSet = ToNewCString(mCharsetOverride);
else
*aCharacterSet = nullptr;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::SetCharsetOverRide(const char * aCharacterSet)
{
mCharsetOverride = aCharacterSet;
return NS_OK;
}
NS_IMETHODIMP nsNntpUrl::CloneInternal(uint32_t aRefHandlingMode,
const nsACString& newRef,
nsIURI **_retval)
{
nsresult rv;
rv = nsMsgMailNewsUrl::CloneInternal(aRefHandlingMode, newRef, _retval);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgMessageUrl> newsurl = do_QueryInterface(*_retval, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return newsurl->SetUri(mURI.get());
}
|