summaryrefslogtreecommitdiffstats
path: root/mailnews/db/msgdb/src/nsImapMailDatabase.cpp
blob: 84865eb801f925ae10ca7f7d529d8b8ecaa26f91 (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
/* -*- Mode: C++; tab-width: 4; 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 <sys/stat.h>

#include "msgCore.h"
#include "nsImapMailDatabase.h"
#include "nsDBFolderInfo.h"

const char *kPendingHdrsScope = "ns:msg:db:row:scope:pending:all";	// scope for all offine ops table
const char *kPendingHdrsTableKind = "ns:msg:db:table:kind:pending";
struct mdbOid gAllPendingHdrsTableOID;

nsImapMailDatabase::nsImapMailDatabase()
{
  m_mdbAllPendingHdrsTable = nullptr;
}

nsImapMailDatabase::~nsImapMailDatabase()
{
}

NS_IMETHODIMP	nsImapMailDatabase::GetSummaryValid(bool *aResult)
{
  NS_ENSURE_ARG_POINTER(aResult);
  if (m_dbFolderInfo)
  {
    uint32_t version;
    m_dbFolderInfo->GetVersion(&version);
    *aResult = (GetCurVersion() == version);
  }
  else
      *aResult = false;

  return NS_OK;
}

NS_IMETHODIMP	nsImapMailDatabase::SetSummaryValid(bool valid)
{
  if (m_dbFolderInfo)
  {
    m_dbFolderInfo->SetVersion(valid ? GetCurVersion() : 0);
    Commit(nsMsgDBCommitType::kLargeCommit);
  }
  return NS_OK;
}

// IMAP does not set local file flags, override does nothing
void nsImapMailDatabase::UpdateFolderFlag(nsIMsgDBHdr * /* msgHdr */, bool /* bSet */,
                                          nsMsgMessageFlagType /* flag */, nsIOutputStream ** /* ppFileStream */)
{
}

// We override this to avoid our parent class (nsMailDatabase)'s 
// grabbing of the folder semaphore, and bailing on failure.
NS_IMETHODIMP nsImapMailDatabase::DeleteMessages(uint32_t aNumKeys, nsMsgKey* nsMsgKeys, nsIDBChangeListener *instigator)
{
  return nsMsgDatabase::DeleteMessages(aNumKeys, nsMsgKeys, instigator);
}

// override so nsMailDatabase methods that deal with m_folderStream are *not* called
NS_IMETHODIMP nsImapMailDatabase::StartBatch()
{
  return NS_OK;
}

NS_IMETHODIMP nsImapMailDatabase::EndBatch()
{
  return NS_OK;
}

nsresult nsImapMailDatabase::AdjustExpungedBytesOnDelete(nsIMsgDBHdr *msgHdr)
{
  uint32_t msgFlags;
  msgHdr->GetFlags(&msgFlags);
  if (msgFlags & nsMsgMessageFlags::Offline && m_dbFolderInfo)
  {
    uint32_t size = 0;
    (void)msgHdr->GetOfflineMessageSize(&size);
    return m_dbFolderInfo->ChangeExpungedBytes (size);
  }
  return NS_OK;
}

NS_IMETHODIMP nsImapMailDatabase::ForceClosed()
{
  m_mdbAllPendingHdrsTable = nullptr;
  return nsMailDatabase::ForceClosed();
}

nsresult nsImapMailDatabase::GetAllPendingHdrsTable()
{
  nsresult rv = NS_OK;
  if (!m_mdbAllPendingHdrsTable)
    rv = GetTableCreateIfMissing(kPendingHdrsScope, kPendingHdrsTableKind, getter_AddRefs(m_mdbAllPendingHdrsTable),
                                                m_pendingHdrsRowScopeToken, m_pendingHdrsTableKindToken) ;
  return rv;
}

NS_IMETHODIMP nsImapMailDatabase::AddNewHdrToDB(nsIMsgDBHdr *newHdr, bool notify)
{
  nsresult rv = nsMsgDatabase::AddNewHdrToDB(newHdr, notify);
  if (NS_SUCCEEDED(rv))
    rv = UpdatePendingAttributes(newHdr);
  return rv;
}

NS_IMETHODIMP nsImapMailDatabase::UpdatePendingAttributes(nsIMsgDBHdr* aNewHdr)
{
  nsresult rv = GetAllPendingHdrsTable();
  NS_ENSURE_SUCCESS(rv, rv);
  mdb_count numPendingHdrs = 0;
  m_mdbAllPendingHdrsTable->GetCount(GetEnv(), &numPendingHdrs);
  if (numPendingHdrs > 0)
  {
    mdbYarn messageIdYarn;
    nsCOMPtr <nsIMdbRow> pendingRow;
    mdbOid  outRowId;

    nsCString messageId;
    aNewHdr->GetMessageId(getter_Copies(messageId));
    messageIdYarn.mYarn_Buf = (void*)messageId.get();
    messageIdYarn.mYarn_Fill = messageId.Length();
    messageIdYarn.mYarn_Form = 0;
    messageIdYarn.mYarn_Size = messageIdYarn.mYarn_Fill;

    m_mdbStore->FindRow(GetEnv(), m_pendingHdrsRowScopeToken,
              m_messageIdColumnToken, &messageIdYarn, &outRowId, getter_AddRefs(pendingRow));
    if (pendingRow)
    {
      mdb_count numCells;
      mdbYarn cellYarn;
      mdb_column cellColumn;
      uint32_t existingFlags;

      pendingRow->GetCount(GetEnv(), &numCells);
      aNewHdr->GetFlags(&existingFlags);
      // iterate over the cells in the pending hdr setting properties on the aNewHdr.
      // we skip cell 0, which is the messageId;
      nsMsgHdr* msgHdr = static_cast<nsMsgHdr*>(aNewHdr);      // closed system, cast ok
      nsIMdbRow *row = msgHdr->GetMDBRow();
      for (mdb_count cellIndex = 1; cellIndex < numCells; cellIndex++)
      {
        nsresult err = pendingRow->SeekCellYarn(GetEnv(), cellIndex, &cellColumn, nullptr);
        if (NS_SUCCEEDED(err))
        {
          err = pendingRow->AliasCellYarn(GetEnv(), cellColumn, &cellYarn);
          if (NS_SUCCEEDED(err))
          {
            if (row)
              row->AddColumn(GetEnv(), cellColumn, &cellYarn);
          }
        }
      }
      // We might have changed some cached values, so force a refresh.
      msgHdr->ClearCachedValues();
      uint32_t resultFlags;
      msgHdr->OrFlags(existingFlags, &resultFlags);
      m_mdbAllPendingHdrsTable->CutRow(GetEnv(), pendingRow);
      pendingRow->CutAllColumns(GetEnv());
    }
  }
  return rv;
}

nsresult nsImapMailDatabase::GetRowForPendingHdr(nsIMsgDBHdr *pendingHdr,
                                                 nsIMdbRow **row)
{
  nsresult rv = GetAllPendingHdrsTable();
  NS_ENSURE_SUCCESS(rv, rv);

  mdbYarn messageIdYarn;
  nsCOMPtr<nsIMdbRow> pendingRow;
  mdbOid  outRowId;
  nsCString messageId;
  pendingHdr->GetMessageId(getter_Copies(messageId));
  messageIdYarn.mYarn_Buf = (void*)messageId.get();
  messageIdYarn.mYarn_Fill = messageId.Length();
  messageIdYarn.mYarn_Form = 0;
  messageIdYarn.mYarn_Size = messageIdYarn.mYarn_Fill;

  rv = m_mdbStore->FindRow(GetEnv(), m_pendingHdrsRowScopeToken,
            m_messageIdColumnToken, &messageIdYarn, &outRowId, getter_AddRefs(pendingRow));

  if (!pendingRow)
    rv  = m_mdbStore->NewRow(GetEnv(), m_pendingHdrsRowScopeToken, getter_AddRefs(pendingRow));

  NS_ENSURE_SUCCESS(rv, rv);
  if (pendingRow)
  {
    // now we need to add cells to the row to remember the messageid, property and property value, and flags.
    // Then, when hdrs are added to the db, we'll check if they have a matching message-id, and if so,
    // set the property and flags
    // XXX we already fetched messageId from the pending hdr, could it have changed by the time we get here? 
    nsCString messageId;
    pendingHdr->GetMessageId(getter_Copies(messageId));
    // we're just going to ignore messages without a message-id. They should be rare. If SPAM messages often
    // didn't have message-id's, they'd be filtered on the server, most likely, and spammers would then
    // start putting in message-id's.
    if (!messageId.IsEmpty())
    {
       extern const char *kMessageIdColumnName;
       m_mdbAllPendingHdrsTable->AddRow(GetEnv(), pendingRow);
       // make sure this is the first cell so that when we ignore the first
       // cell in nsImapMailDatabase::AddNewHdrToDB, we're ignoring the right one
      (void) SetProperty(pendingRow, kMessageIdColumnName, messageId.get());
      pendingRow.forget(row);
    }
    else
      return NS_ERROR_FAILURE;
  }
  return rv;
}

NS_IMETHODIMP nsImapMailDatabase::SetAttributeOnPendingHdr(nsIMsgDBHdr *pendingHdr, const char *property,
                                  const char *propertyVal)
{
  NS_ENSURE_ARG_POINTER(pendingHdr);
  nsCOMPtr<nsIMdbRow> pendingRow;
  nsresult rv = GetRowForPendingHdr(pendingHdr, getter_AddRefs(pendingRow));
  NS_ENSURE_SUCCESS(rv, rv);
  return SetProperty(pendingRow, property, propertyVal);
}

NS_IMETHODIMP
nsImapMailDatabase::SetUint32AttributeOnPendingHdr(nsIMsgDBHdr *pendingHdr,
                                                   const char *property,
                                                   uint32_t propertyVal)
{
  NS_ENSURE_ARG_POINTER(pendingHdr);
  nsCOMPtr<nsIMdbRow> pendingRow;
  nsresult rv = GetRowForPendingHdr(pendingHdr, getter_AddRefs(pendingRow));
  NS_ENSURE_SUCCESS(rv, rv);
  return SetUint32Property(pendingRow, property, propertyVal);
}

NS_IMETHODIMP
nsImapMailDatabase::SetUint64AttributeOnPendingHdr(nsIMsgDBHdr *aPendingHdr,
                                                   const char *aProperty,
                                                   uint64_t aPropertyVal)
{
  NS_ENSURE_ARG_POINTER(aPendingHdr);
  nsCOMPtr<nsIMdbRow> pendingRow;
  nsresult rv = GetRowForPendingHdr(aPendingHdr, getter_AddRefs(pendingRow));
  NS_ENSURE_SUCCESS(rv, rv);
  return SetUint64Property(pendingRow, aProperty, aPropertyVal);
}