summaryrefslogtreecommitdiff
path: root/src/operator-capability.cc
blob: 73f5a26078fa6eb7610092e5a68653019aa062ba (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
/** --------------------------------------------------------------------
 *  @file  bincimapd-capability.cc
 *  @brief Implementation of the CAPABILITY command
 *  @author Andreas Aardal Hanssen, Erwin Hoffmann
 *  @date 2002-2005, 2023
 *  -----------------------------------------------------------------  **/
#include "depot.h"
#include "globals.h"
#include "iodevice.h"
#include "iofactory.h"
#include "operators.h"
#include "recursivedescent.h"
#include "session.h"

#include <string>

using namespace ::std;
using namespace Binc;

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

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

//----------------------------------------------------------------------
const string CapabilityOperator::getName(void) const
{
  return "CAPABILITY";
}

//----------------------------------------------------------------------
int CapabilityOperator::getState(void) const
{
  return Session::NONAUTHENTICATED | Session::AUTHENTICATED | Session::SELECTED;
}

//----------------------------------------------------------------------
void CapabilityOperator::addCapability(const string &cap)
{
  capabilities.push_back(cap);
}

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

  bincClient << "* CAPABILITY " << IMAP_VERSION;

  if (session.getState() == Session::NONAUTHENTICATED) {
    if (getenv("UCSPITLS"))
      if (!session.command.ssl) bincClient << " STARTTLS";

    const string authmethods = session.getEnv("BINCIMAP_LOGIN");
    auto cram = authmethods.find("+CRAM-MD5");

    if (session.command.ssl || session.hasEnv("ALLOW_NONSSL_PLAINTEXT_LOGINS")) {
      if (cram != string::npos)
        bincClient << " AUTH=LOGIN AUTH=PLAIN AUTH=CRAM-MD5";
      else
        bincClient << " AUTH=LOGIN AUTH=PLAIN";
    } else
      bincClient << " LOGINDISABLED";
  }

  bincClient << " IDLE LITERAL+ NAMESPACE CHILDREN";

  vector<string>::const_iterator i = capabilities.begin();
  while (i != capabilities.end()) {
    bincClient << " " << *i;
    ++i;
  }
  bincClient << endl;

  return OK;
}

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

  if (c_in.getUidMode()) return REJECT;

  Operator::ParseResult res;
  if ((res = expectCRLF()) != ACCEPT) {
    session.setLastError("Expected CRLF after CAPABILITY");
    return res;
  }

  c_in.setName("CAPABILITY");

  return ACCEPT;
}