summaryrefslogtreecommitdiff
path: root/src/session.cc
blob: aa09abed75086f9ee8fd94e3d240f1fc3063c614 (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
/**
 *  @file session.cc
 *  @brief Implementation of the Session class.
 */

#include "session.h"

#include "argparser.h"
#include "convert.h"
#include "globals.h"
#include "tools.h"

#include <map>
#include <string>

#include <syslog.h>
#include <unistd.h>

using namespace Binc;
using std::string;

extern char **environ;

Session::Session()
{
  readbytes = 0;
  writebytes = 0;
  statements = 0;
  bodies = 0;
  mailboxchanges = true;
  logfacility = LOG_DAEMON;
}

Session &Session::getInstance()
{
  static Session session;
  return session;
}

int Session::getState() const
{
  return state;
}

void Session::setState(int n)
{
  state = n;
}

const string &Session::getUserID() const
{
  return userid;
}

void Session::setUserID(const string &s)
{
  userid = s;
}

const string &Session::getIP() const
{
  return ip;
}

void Session::setIP(const string &s)
{
  ip = s;
}

void Session::setLogFacility(int facility)
{
  logfacility = facility;
}

int Session::getLogFacility() const
{
  return logfacility;
}

void Session::addBody()
{
  ++bodies;
}

void Session::addStatement()
{
  ++statements;
}

void Session::addReadBytes(int i)
{
  readbytes += i;
}

void Session::addWriteBytes(int i)
{
  writebytes += i;
}

int Session::getBodies() const
{
  return bodies;
}

int Session::getStatements() const
{
  return statements;
}

int Session::getWriteBytes() const
{
  return writebytes;
}

int Session::getReadBytes() const
{
  return readbytes;
}

bool Session::parseCommandLine(int argc, char *argv[])
{
  args.addOptional("h|help", "Display this help screen", true);
  args.addOptional("version", "Display the version of Binc IMAP", true);
  args.addOptional("a|allow-plain", "Allow authentication when not TLS protected", true);
  args.addOptional("v|show-version", "Enable verbose IMAP greeting", false);
  args.addOptional("l|log-type", "Sets the method used for logging", false);
  args.addOptional("d|depot", "Sets the depot type", false);
  args.addOptional("D|delimiter", "Sets the mailbox delimiter", false);

  if (!args.parse(argc, argv)) {
    setLastError("Command line error, " + args.errorString());
    return false;
  }

  command.help = args["help"] == "yes" ? true : false;
  command.version = args["version"] == "yes" ? true : false;

  unparsedArgs = argv + args.argc();

  return true;
}

void Session::assignCommandLineArgs()
{
  if (args.hasArg("allow-plain")) setEnv("ALLOW_NONSSL_PLAINTEXT_LOGINS", "yes");

  if (args.hasArg("show-version")) setEnv("SHOW_VERSION_IN_GREETING", "yes");

  if (args.hasArg("log-type")) setEnv("LOG_TYPE", args["log-type"]);

  if (args.hasArg("depot")) setEnv("DEPOT", args["depot"]);

  if (args.hasArg("delimiter")) setEnv("DELIMITER", args["delimiter"]);
}

const string &Session::getLastError() const
{
  return lastError;
}

void Session::setLastError(const string &error) const
{
  lastError = error;
}

const string &Session::getResponseCode() const
{
  return responseCode;
}

void Session::setResponseCode(const string &code) const
{
  responseCode = "[" + code + "] ";
}

void Session::clearResponseCode() const
{
  responseCode = "";
}

pid_t Session::getPid()
{
  if (pid == 0) pid = getpid();

  return pid;
}

int Session::timeout() const
{
  return state == NONAUTHENTICATED ? AUTH_TIMEOUT : IDLE_TIMEOUT;
}

bool Session::hasEnv(const string &key) const
{
  return getenv(key.c_str()) != nullptr;
}

string Session::getEnv(const string &key)
{
  char *c = getenv(key.c_str());
  return c ? c : "";
}

void Session::setEnv(const string &key, const string &value)
{
  string env = key + "=" + value;
  putenv(strdup(env.c_str()));
}