summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/src/nsImapHostSessionList.cpp
blob: fbdbeac231f0356d55aeeeab2207de15990b1cda (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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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"
#include "nsImapHostSessionList.h"
#include "nsImapNamespace.h"
#include "nsIImapIncomingServer.h"
#include "nsCOMPtr.h"
#include "nsIMsgIncomingServer.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsMsgUtils.h"
#include "mozilla/Services.h"

nsIMAPHostInfo::nsIMAPHostInfo(const char* serverKey,
                               nsIImapIncomingServer* server) {
  fServerKey = serverKey;
  NS_ASSERTION(server, "*** Fatal null imap incoming server...");
  server->GetServerDirectory(fOnlineDir);
  fNextHost = NULL;
  fCapabilityFlags = kCapabilityUndefined;
  fHierarchyDelimiters = NULL;
#ifdef DEBUG_bienvenu1
  fHaveWeEverDiscoveredFolders =
      true;  // try this, see what bad happens - we'll need to
             // figure out a way to make new accounts have it be false
#else
  fHaveWeEverDiscoveredFolders = false;  // try this, see what bad happens
#endif
  fDiscoveryForHostInProgress = false;
  fCanonicalOnlineSubDir = NULL;
  fNamespaceList = nsImapNamespaceList::CreatensImapNamespaceList();
  fUsingSubscription = true;
  server->GetUsingSubscription(&fUsingSubscription);
  fOnlineTrashFolderExists = false;
  fShouldAlwaysListInbox = true;
  fPasswordVerifiedOnline = false;
  fDeleteIsMoveToTrash = true;
  fShowDeletedMessages = false;
  fGotNamespaces = false;
  fHaveAdminURL = false;
  fNamespacesOverridable = true;
  server->GetOverrideNamespaces(&fNamespacesOverridable);
  fTempNamespaceList = nsImapNamespaceList::CreatensImapNamespaceList();
}

nsIMAPHostInfo::~nsIMAPHostInfo() {
  PR_Free(fHierarchyDelimiters);
  delete fNamespaceList;
  delete fTempNamespaceList;
}

NS_IMPL_ISUPPORTS(nsImapHostSessionList, nsIImapHostSessionList, nsIObserver,
                  nsISupportsWeakReference)

nsImapHostSessionList::nsImapHostSessionList() {
  gCachedHostInfoMonitor = PR_NewMonitor(/* "accessing-hostlist-monitor"*/);
  fHostInfoList = nullptr;
}

nsImapHostSessionList::~nsImapHostSessionList() {
  ResetAll();
  PR_DestroyMonitor(gCachedHostInfoMonitor);
}

nsresult nsImapHostSessionList::Init() {
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED);
  observerService->AddObserver(this, "profile-before-change", true);
  observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
  return NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::Observe(nsISupports* aSubject,
                                             const char* aTopic,
                                             const char16_t* someData) {
  if (!strcmp(aTopic, "profile-before-change"))
    ResetAll();
  else if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
    nsCOMPtr<nsIObserverService> observerService =
        mozilla::services::GetObserverService();
    NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED);
    observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
    observerService->RemoveObserver(this, "profile-before-change");
  }
  return NS_OK;
}

nsIMAPHostInfo* nsImapHostSessionList::FindHost(const char* serverKey) {
  nsIMAPHostInfo* host;

  // ### should also check userName here, if NON NULL
  for (host = fHostInfoList; host; host = host->fNextHost) {
    if (host->fServerKey.Equals(serverKey, nsCaseInsensitiveCStringComparator))
      return host;
  }
  return host;
}

// reset any cached connection info - delete the lot of 'em
NS_IMETHODIMP nsImapHostSessionList::ResetAll() {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* nextHost = NULL;
  for (nsIMAPHostInfo* host = fHostInfoList; host; host = nextHost) {
    nextHost = host->fNextHost;
    delete host;
  }
  fHostInfoList = NULL;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return NS_OK;
}

NS_IMETHODIMP
nsImapHostSessionList::AddHostToList(const char* serverKey,
                                     nsIImapIncomingServer* server) {
  nsIMAPHostInfo* newHost = NULL;
  PR_EnterMonitor(gCachedHostInfoMonitor);
  if (!FindHost(serverKey)) {
    // stick it on the front
    newHost = new nsIMAPHostInfo(serverKey, server);
    if (newHost) {
      newHost->fNextHost = fHostInfoList;
      fHostInfoList = newHost;
    }
  }
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (newHost == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetPasswordForHost(const char* serverKey,
                                                        nsString& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fCachedPassword;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetPasswordForHost(
    const char* serverKey, const nsAString& password) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fCachedPassword = password;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetPasswordVerifiedOnline(
    const char* serverKey) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fPasswordVerifiedOnline = true;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetPasswordVerifiedOnline(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fPasswordVerifiedOnline;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetOnlineDirForHost(const char* serverKey,
                                                         nsString& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) CopyASCIItoUTF16(host->fOnlineDir, result);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetOnlineDirForHost(
    const char* serverKey, const char* onlineDir) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) {
    if (onlineDir) host->fOnlineDir = onlineDir;
  }
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetDeleteIsMoveToTrashForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fDeleteIsMoveToTrash;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetShowDeletedMessagesForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fShowDeletedMessages;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetDeleteIsMoveToTrashForHost(
    const char* serverKey, bool isMoveToTrash) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fDeleteIsMoveToTrash = isMoveToTrash;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetShowDeletedMessagesForHost(
    const char* serverKey, bool showDeletedMessages) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fShowDeletedMessages = showDeletedMessages;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetGotNamespacesForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fGotNamespaces;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetGotNamespacesForHost(
    const char* serverKey, bool gotNamespaces) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fGotNamespaces = gotNamespaces;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetHostIsUsingSubscription(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fUsingSubscription;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetHostIsUsingSubscription(
    const char* serverKey, bool usingSubscription) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fUsingSubscription = usingSubscription;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetHostHasAdminURL(const char* serverKey,
                                                        bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fHaveAdminURL;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetHostHasAdminURL(const char* serverKey,
                                                        bool haveAdminURL) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fHaveAdminURL = haveAdminURL;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetHaveWeEverDiscoveredFoldersForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fHaveWeEverDiscoveredFolders;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetHaveWeEverDiscoveredFoldersForHost(
    const char* serverKey, bool discovered) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fHaveWeEverDiscoveredFolders = discovered;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetDiscoveryForHostInProgress(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host)
    result = host->fDiscoveryForHostInProgress;
  else
    result = false;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetDiscoveryForHostInProgress(
    const char* serverKey, bool inProgress) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fDiscoveryForHostInProgress = inProgress;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetOnlineTrashFolderExistsForHost(
    const char* serverKey, bool exists) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fOnlineTrashFolderExists = exists;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetOnlineTrashFolderExistsForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fOnlineTrashFolderExists;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::AddNewNamespaceForHost(
    const char* serverKey, nsImapNamespace* ns) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fNamespaceList->AddNewNamespace(ns);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetNamespaceFromPrefForHost(
    const char* serverKey, const char* namespacePref,
    EIMAPNamespaceType nstype) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) {
    if (namespacePref) {
      int numNamespaces = host->fNamespaceList->UnserializeNamespaces(
          namespacePref, nullptr, 0);
      char** prefixes = (char**)PR_CALLOC(numNamespaces * sizeof(char*));
      if (prefixes) {
        int len = host->fNamespaceList->UnserializeNamespaces(
            namespacePref, prefixes, numNamespaces);
        for (int i = 0; i < len; i++) {
          char* thisns = prefixes[i];
          char delimiter = '/';  // a guess
          if (PL_strlen(thisns) >= 1) delimiter = thisns[PL_strlen(thisns) - 1];
          nsImapNamespace* ns =
              new nsImapNamespace(nstype, thisns, delimiter, true);
          if (ns) host->fNamespaceList->AddNewNamespace(ns);
          PR_FREEIF(thisns);
        }
        PR_Free(prefixes);
      }
    }
  }
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetNamespaceForMailboxForHost(
    const char* serverKey, const char* mailbox_name, nsImapNamespace*& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fNamespaceList->GetNamespaceForMailbox(mailbox_name);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::ClearPrefsNamespacesForHost(
    const char* serverKey) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fNamespaceList->ClearNamespaces(true, false, true);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::ClearServerAdvertisedNamespacesForHost(
    const char* serverKey) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fNamespaceList->ClearNamespaces(false, true, true);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetDefaultNamespaceOfTypeForHost(
    const char* serverKey, EIMAPNamespaceType type, nsImapNamespace*& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fNamespaceList->GetDefaultNamespaceOfType(type);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetNamespacesOverridableForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fNamespacesOverridable;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetNamespacesOverridableForHost(
    const char* serverKey, bool overridable) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fNamespacesOverridable = overridable;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetNumberOfNamespacesForHost(
    const char* serverKey, uint32_t& result) {
  int32_t intResult = 0;

  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) intResult = host->fNamespaceList->GetNumberOfNamespaces();
  PR_ExitMonitor(gCachedHostInfoMonitor);
  NS_ASSERTION(intResult >= 0, "negative number of namespaces");
  result = (uint32_t)intResult;
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetNamespaceNumberForHost(
    const char* serverKey, int32_t n, nsImapNamespace*& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) result = host->fNamespaceList->GetNamespaceNumber(n);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

nsresult nsImapHostSessionList::SetNamespacesPrefForHost(
    nsIImapIncomingServer* aHost, EIMAPNamespaceType type, const char* pref) {
  if (type == kPersonalNamespace)
    aHost->SetPersonalNamespace(nsDependentCString(pref));
  else if (type == kPublicNamespace)
    aHost->SetPublicNamespace(nsDependentCString(pref));
  else if (type == kOtherUsersNamespace)
    aHost->SetOtherUsersNamespace(nsDependentCString(pref));
  else
    NS_ASSERTION(false, "bogus namespace type");
  return NS_OK;
}
// do we need this? What should we do about the master thing?
// Make sure this is running in the Mozilla thread when called
NS_IMETHODIMP nsImapHostSessionList::CommitNamespacesForHost(
    nsIImapIncomingServer* aHost) {
  NS_ENSURE_ARG_POINTER(aHost);
  nsCString serverKey;
  nsCOMPtr<nsIMsgIncomingServer> incomingServer = do_QueryInterface(aHost);
  if (!incomingServer) return NS_ERROR_NULL_POINTER;

  nsresult rv = incomingServer->GetKey(serverKey);
  NS_ENSURE_SUCCESS(rv, rv);

  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey.get());
  if (host) {
    host->fGotNamespaces =
        true;  // so we only issue NAMESPACE once per host per session.
    EIMAPNamespaceType type = kPersonalNamespace;
    for (int i = 1; i <= 3; i++) {
      switch (i) {
        case 1:
          type = kPersonalNamespace;
          break;
        case 2:
          type = kPublicNamespace;
          break;
        case 3:
          type = kOtherUsersNamespace;
          break;
        default:
          type = kPersonalNamespace;
          break;
      }

      int32_t numInNS = host->fNamespaceList->GetNumberOfNamespaces(type);
      if (numInNS == 0)
        SetNamespacesPrefForHost(aHost, type, "");
      else if (numInNS >= 1) {
        char* pref = PR_smprintf("");
        for (int count = 1; count <= numInNS; count++) {
          nsImapNamespace* ns =
              host->fNamespaceList->GetNamespaceNumber(count, type);
          if (ns) {
            if (count > 1) {
              // append the comma
              char* tempPref = PR_smprintf("%s,", pref);
              PR_FREEIF(pref);
              pref = tempPref;
            }
            char* tempPref = PR_smprintf("%s\"%s\"", pref, ns->GetPrefix());
            PR_FREEIF(pref);
            pref = tempPref;
          }
        }
        if (pref) {
          SetNamespacesPrefForHost(aHost, type, pref);
          PR_Free(pref);
        }
      }
    }
    // clear, but don't delete the entries in, the temp namespace list
    host->fTempNamespaceList->ClearNamespaces(true, true, false);

    // Now reset all of libmsg's namespace references.
    // Did I mention this needs to be running in the mozilla thread?
    aHost->ResetNamespaceReferences();
  }
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::FlushUncommittedNamespacesForHost(
    const char* serverKey, bool& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fTempNamespaceList->ClearNamespaces(true, true, true);
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

// Returns NULL if there is no personal namespace on the given host
NS_IMETHODIMP nsImapHostSessionList::GetOnlineInboxPathForHost(
    const char* serverKey, nsString& result) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) {
    nsImapNamespace* ns = NULL;
    ns = host->fNamespaceList->GetDefaultNamespaceOfType(kPersonalNamespace);
    if (ns) {
      CopyASCIItoUTF16(nsDependentCString(ns->GetPrefix()), result);
      result.AppendLiteral("INBOX");
    }
  } else
    result.Truncate();
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::GetShouldAlwaysListInboxForHost(
    const char* /*serverKey*/, bool& result) {
  result = true;

  /*
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo *host = FindHost(serverKey);
  if (host)
    ret = host->fShouldAlwaysListInbox;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  */
  return NS_OK;
}

NS_IMETHODIMP nsImapHostSessionList::SetShouldAlwaysListInboxForHost(
    const char* serverKey, bool shouldList) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) host->fShouldAlwaysListInbox = shouldList;
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
}

NS_IMETHODIMP
nsImapHostSessionList::SetNamespaceHierarchyDelimiterFromMailboxForHost(
    const char* serverKey, const char* boxName, char delimiter) {
  PR_EnterMonitor(gCachedHostInfoMonitor);
  nsIMAPHostInfo* host = FindHost(serverKey);
  if (host) {
    nsImapNamespace* ns = host->fNamespaceList->GetNamespaceForMailbox(boxName);
    if (ns && !ns->GetIsDelimiterFilledIn()) ns->SetDelimiter(delimiter, true);
  }
  PR_ExitMonitor(gCachedHostInfoMonitor);
  return (host) ? NS_OK : NS_ERROR_ILLEGAL_VALUE;
}