summaryrefslogtreecommitdiff
path: root/src/operator-status.cc
blob: 82c3bcf5c3d279f56fc24902db2e45dd0a695d03 (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
/**  --------------------------------------------------------------------
 *  @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 ::std;
using namespace Binc;

//----------------------------------------------------------------------
StatusOperator::StatusOperator(void) {}

//----------------------------------------------------------------------
StatusOperator::~StatusOperator(void) {}

//----------------------------------------------------------------------
const string StatusOperator::getName(void) const
{
  return "STATUS";
}

//----------------------------------------------------------------------
int StatusOperator::getState(void) const
{
  return 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 NO;
  }

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

  string prefix;
  for (vector<string>::const_iterator i = command.statuses.begin(); i != command.statuses.end(); ++i) {
    string tmp = *i;
    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 << ")" << endl;

  return OK;
}

//----------------------------------------------------------------------
Operator::ParseResult StatusOperator::parse(Request &c_in) const
{
  Session &session = Session::getInstance();

  if (c_in.getUidMode()) return REJECT;

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

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

  c_in.setMailbox(mailbox);

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

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

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

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

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

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

  return ACCEPT;
}