summaryrefslogtreecommitdiff
path: root/src/operator-lsub.cc
blob: c4d1908800619688f3e9f11203b369b6c1c127c3 (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
/**
 *  @file  operator-lsub.cc
 *  @brief Implementation of the LSUB command.
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 */

#include "convert.h"
#include "depot.h"
#include "iodevice.h"
#include "iofactory.h"
#include "mailbox.h"
#include "operators.h"
#include "recursivedescent.h"
#include "regmatch.h"
#include "session.h"

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

namespace {
  const int DIR_SELECT = 0x01;
  const int DIR_MARKED = 0x02;
  const int DIR_NOINFERIORS = 0x04;
  const int DIR_LEAF = 0x08;
}

using namespace Binc;
using std::multimap;
using std::string;

LsubOperator::LsubOperator() {}

LsubOperator::~LsubOperator() {}

const string LsubOperator::getName() const
{
  return "LSUB";
}

Session::State LsubOperator::getState() const
{
  return Session::State(Session::AUTHENTICATED | Session::SELECTED);
}

Operator::ProcessResult LsubOperator::process(Depot &depot, Request &command)
{
  const char delim = depot.getDelimiter();

  // remove leading or trailing delimiter in wildcard
  string wildcard = command.getListMailbox();
  trim(wildcard, string(&delim, 1));

  // convert wildcard to regular expression
  string regex = toRegex(wildcard, depot.getDelimiter());
  string wildcardLower = regex;
  lowercase(wildcardLower);
  if (wildcardLower.substr(0, 6) == "^inbox")
    regex = "^[iI][nN][bB][oO][xX]" + regex.substr(6);

  // remove leading or trailing delimiter in reference
  string ref = command.getMailbox();
  trim(ref, string(&delim, 1));
  wildcardLower = ref;
  lowercase(wildcardLower);
  if (wildcardLower.substr(0, 5) == "inbox"
      && (wildcardLower.length() == 5 || wildcardLower[5] == delim))
  {
    ref = "INBOX" + ref.substr(5);
  }

  // a multimap from mailbox name to flags
  multimap<string, int> mailboxes;

  // read through all entries in depository.
  for (auto i = depot.begin("."); i != depot.end(); ++i) {
    const string path = *i;
    const string mpath = depot.filenameToMailbox(path);
    Mailbox *m = nullptr;

    // skip entries that are not identified as mailboxes
    if ((m = depot.get(mpath)) == nullptr) continue;

    // convert file name to mailbox name. skip it if there is no
    // corresponding mailbox name.
    string tmp = toCanonMailbox(depot.filenameToMailbox(path));
    trim(tmp, string(&delim, 1));
    if (tmp == "") {
      continue;
    } else {
      int flags = DIR_SELECT;
      multimap<string, int>::iterator mi = mailboxes.find(tmp);
      if (mi != mailboxes.end()) {
        flags |= mi->second;
        mailboxes.erase(mi);
      }
      mailboxes.insert(make_pair(tmp, flags));
    }

    // now add all superior mailboxes with no flags set if not
    // added already.
    string::size_type pos = tmp.rfind(delim);
    while (pos != string::npos) {
      tmp = tmp.substr(0, pos);
      trim(tmp, string(&delim, 1));

      multimap<string, int>::iterator mi = mailboxes.find(tmp);
      if (mi == mailboxes.end()) mailboxes.insert(make_pair(tmp, 0));

      pos = tmp.rfind(delim);
    }
  }

  // find leaf nodes O(N^2)
  multimap<string, int>::iterator i;
  for (i = mailboxes.begin(); i != mailboxes.end(); ++i) {
    string mailbox = i->first;
    mailbox += delim;

    bool leaf = true;
    multimap<string, int>::const_iterator j;
    for (j = mailboxes.begin(); j != mailboxes.end(); ++j) {
      string::size_type pos = j->first.rfind(delim);
      if (pos == string::npos) continue;
      string base = j->first.substr(0, pos + 1);
      if (mailbox == base) {
        leaf = false;
        break;
      }
    }
    if (leaf) {
      unsigned int flags = i->second;
      flags |= DIR_LEAF;
      i->second = flags;
    }
  }

  depot.loadSubscribes();

  std::vector<string> subscribed = depot.getSubscriptions();
  sort(subscribed.begin(), subscribed.end());

  // finally, print all mailbox entries with flags.
  for (const auto &j : subscribed) {
    if (ref == "" || (ref.length() <= j.length() && ref == j.substr(0, ref.length()))) {
      if (regexMatch(j.substr(ref.length()), regex) == 0) {
        int flags = 0;
        for (i = mailboxes.begin(); i != mailboxes.end(); ++i) {
          if (i->first == j) {
            flags = i->second;
            break;
          }
        }
        bincClient << "* LSUB (";
        string sep = "";
        bool noselect = false;
        if (!(flags & DIR_SELECT)) {
          bincClient << sep << "\\Noselect";
          sep = " ";
          noselect = true;
        }
        if (!noselect) {
          if (flags & DIR_MARKED)
            bincClient << sep << "\\Marked";
          else
            bincClient << sep << "\\Unmarked";
          sep = " ";
        }
        if (flags & DIR_LEAF)
          bincClient << sep << "\\HasNoChildren";
        else
          bincClient << sep << "\\HasChildren";
        sep = " ";
        if (flags & DIR_NOINFERIORS) bincClient << sep << "\\Noinferiors";
        bincClient << ") \"" << depot.getDelimiter() << "\" " << toImapString(j) << std::endl;
      }
    }
  }

  return Operator::ProcessResult::OK;
}

Parser::ParseResult LsubOperator::parse(Request &c_in)
{
  constexpr auto ACCEPT = Parser::ParseResult::ACCEPT;
  constexpr auto ERROR = Parser::ParseResult::ERROR;
  constexpr auto REJECT = Parser::ParseResult::REJECT;

  Session &session = Session::getInstance();

  if (c_in.getUidMode()) return REJECT;

  Parser::ParseResult res;
  if ((res = expectSPACE()) != ACCEPT) {
    session.setLastError("Expected SPACE after LSUB");
    return ERROR;
  }

  string mailbox;
  if ((res = expectMailbox(mailbox)) != ACCEPT) {
    session.setLastError("Expected mailbox after LSUB SPACE");
    return ERROR;
  }

  c_in.setMailbox(mailbox);

  if ((res = expectSPACE()) != ACCEPT) {
    session.setLastError("Expected SPACE after LSUB SPACE mailbox");
    return ERROR;
  }

  string listmailbox;
  if ((res = expectListMailbox(listmailbox)) != ACCEPT) {
    session.setLastError("Expected list_mailbox after LSUB SPACE"
                         " mailbox SPACE");
    return ERROR;
  }

  if ((res = expectCRLF()) != ACCEPT) {
    session.setLastError("Expected CRLF after LSUB SPACE"
                         " mailbox SPACE list_mailbox");
    return ERROR;
  }

  c_in.setListMailbox(listmailbox);
  c_in.setName("LSUB");

  return ACCEPT;
}