summaryrefslogtreecommitdiff
path: root/src/operator-status.cc
blob: 4c2588f1857a850af618f22421733d927990b089 (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
/**
 *  @file  operator-status.cc
 *  @brief  Implementation of the STATUS 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 "session.h"
#include "status.h"

#include <iostream>
#include <string>

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

using namespace Binc;

StatusOperator::StatusOperator() {}

StatusOperator::~StatusOperator() {}

const std::string StatusOperator::getName() const
{
  return "STATUS";
}

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

Operator::ProcessResult StatusOperator::process(Depot &depot, Request &command)
{
  Session &session = Session::getInstance();

  Status status;
  if (!depot.getStatus(command.getMailbox(), status)) {
    session.setLastError(depot.getLastError());
    return ProcessResult::NO;
  }

  bincClient << "* STATUS " << toImapString(command.getMailbox()) << " (";

  std::string prefix;
  for (auto tmp : command.statuses) {
    uppercase(tmp);
    if (tmp == "UIDNEXT") {
      bincClient << prefix << "UIDNEXT " << status.getUidNext();
      prefix = " ";
    } else if (tmp == "MESSAGES") {
      bincClient << prefix << "MESSAGES " << status.getMessages();
      prefix = " ";
    } else if (tmp == "RECENT") {
      bincClient << prefix << "RECENT " << status.getRecent();
      prefix = " ";
    } else if (tmp == "UIDVALIDITY") {
      bincClient << prefix << "UIDVALIDITY " << status.getUidValidity();
      prefix = " ";
    } else if (tmp == "UNSEEN") {
      bincClient << prefix << "UNSEEN " << status.getUnseen();
      prefix = " ";
    }
  }
  bincClient << ")" << std::endl;

  return ProcessResult::OK;
}

Parser::ParseResult StatusOperator::parse(Request &c_in, Parser &p)
{
  constexpr auto ACCEPT = Parser::ParseResult::ACCEPT;

  Session &session = Session::getInstance();

  if (c_in.getUidMode()) return Parser::ParseResult::REJECT;

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

  std::string mailbox;
  if ((res = p.expectMailbox(mailbox)) != ACCEPT) {
    session.setLastError("Expected mailbox");
    return res;
  }

  c_in.setMailbox(mailbox);

  if ((res = p.expectSPACE()) != ACCEPT) {
    session.setLastError("Expected SPACE");
    return res;
  }

  if ((res = p.expectThisString("(")) != ACCEPT) {
    session.setLastError("Expected (");
    return res;
  }

  while (1) {
    if ((res = p.expectThisString("MESSAGES")) == ACCEPT) {
      c_in.getStatuses().push_back("MESSAGES");
    } else if ((res = p.expectThisString("RECENT")) == ACCEPT) {
      c_in.getStatuses().push_back("RECENT");
    } else if ((res = p.expectThisString("UIDNEXT")) == ACCEPT) {
      c_in.getStatuses().push_back("UIDNEXT");
    } else if ((res = p.expectThisString("UIDVALIDITY")) == ACCEPT) {
      c_in.getStatuses().push_back("UIDVALIDITY");
    } else if ((res = p.expectThisString("UNSEEN")) == ACCEPT) {
      c_in.getStatuses().push_back("UNSEEN");
    } else {
      session.setLastError("Expected status_att");
      return res;
    }

    if (p.expectSPACE() != ACCEPT) break;
  }

  if ((res = p.expectThisString(")")) != ACCEPT) {
    session.setLastError("Expected )");
    return res;
  }

  if ((res = p.expectCRLF()) != ACCEPT) {
    session.setLastError("Expected CRLF");
    return Parser::ParseResult::ERROR;
  }

  return ACCEPT;
}