summaryrefslogtreecommitdiff
path: root/src/operator-capability.cc
blob: 73240256dc29ae9104ded7ce474e2d4b2d42a79e (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
/**
 *  @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 Binc;
using std::string;

CapabilityOperator::CapabilityOperator() {}

CapabilityOperator::~CapabilityOperator() {}

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

Session::State CapabilityOperator::getState() const
{
  return Session::State(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";

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

  return Operator::ProcessResult::OK;
}

Parser::ParseResult CapabilityOperator::parse(Request &c_in, Parser &p)
{
  Session &session = Session::getInstance();

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

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

  c_in.setName("CAPABILITY");

  return Parser::ParseResult::ACCEPT;
}