Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
session-initialize-bincimapd.cc
Go to the documentation of this file.
1
8#include <unistd.h>
9#include <syslog.h>
10#include <errno.h>
11
12#include "broker.h"
13#include "maildir.h"
14#include "depot.h"
15#include "globals.h"
16#include "iodevice.h"
17#include "iofactory.h"
18#include "multilogdevice.h"
19#include "session.h"
20#include "stdiodevice.h"
21#include "syslogdevice.h"
22#include "tools.h"
23#include "convert.h"
24#include "imapserver.h"
25
26#include <string>
27#include <map>
28#include <signal.h>
29
30using namespace ::std;
31using namespace Binc;
32
33extern char **environ;
34
35//----------------------------------------------------------------------
36bool Session::initialize(int argc, char *argv[])
37{
38 IOFactory &ioFactory = IOFactory::getInstance();
39
44 stdioDevice->setMaxOutputBufferSize(TRANSFER_BUFFER_SIZE);
45 ioFactory.addDevice(stdioDevice);
46
47 Session &session = Session::getInstance();
48
49 // Read command line arguments
50 if (!session.parseCommandLine(argc, argv))
51 return false;
52
53 // Show help if asked for it
54 if (session.command.help) {
55 printf("%s\n", session.args.usageString().c_str());
56 return false;
57 }
58
59 // Show version if asked for it
60 if (session.command.version) {
61 printf("Binc IMAP v" BINC_VERSION"\n");
62 return false;
63 }
64
65 // Assign command line arguments to global config.
66 session.assignCommandLineArgs();
67
68 // log settings
69 string ip = getenv("TCP6REMOTEIP") ? getenv("TCP6REMOTEIP") :
70 getenv("TCPREMOTEIP") ? getenv("TCPREMOTEIP") : "?";
71 session.setIP(ip);
72
73 string logtype = session.getEnv("LOG_TYPE");
74 lowercase(logtype);
75 trim(logtype);
76 if (logtype == "multilog") {
78 } else if (logtype == "" || logtype == "syslog") {
79 const string f = session.getEnv("SYSLOG_FACILITY");
80
81 int facility;
82
83 if (isdigit(f[0])) facility = atoi(f);
84 else {
85 if (f == "LOG_USER") facility = LOG_USER;
86 else if (f == "LOG_LOCAL0") facility = LOG_LOCAL0;
87 else if (f == "LOG_LOCAL1") facility = LOG_LOCAL1;
88 else if (f == "LOG_LOCAL2") facility = LOG_LOCAL2;
89 else if (f == "LOG_LOCAL3") facility = LOG_LOCAL3;
90 else if (f == "LOG_LOCAL4") facility = LOG_LOCAL4;
91 else if (f == "LOG_LOCAL5") facility = LOG_LOCAL5;
92 else if (f == "LOG_LOCAL6") facility = LOG_LOCAL6;
93 else if (f == "LOG_LOCAL7") facility = LOG_LOCAL7;
94 else facility = LOG_DAEMON;
95 }
96
97 session.setEnv("SYSLOG_FACILITY", toString(facility));
98
100 "bincimapd",
101 LOG_NDELAY | LOG_PID,
102 facility));
103 }
104
105 // Now that we know the log type, we can flush.
109
110 string pid = to_string(session.getPid());
111
112 char *logindetails = getenv("BINCIMAP_LOGIN");
113 if (logindetails == 0) {
114 bincLog
115 << "bincimapd: pid " << pid
116 << " BINCIMAP_LOGIN missing from environment (are you sure you invoked "
117 << argv[0] << " properly?)\n";
118 bincLog.flush();
119 return false;
120 }
121
122 DepotFactory &depotfactory = DepotFactory::getInstance();
123 depotfactory.assign(new IMAPdirDepot());
124 depotfactory.assign(new MaildirPPDepot());
125
126 string depottype = session.getEnv("DEPOT");
127 if (depottype == "") depottype = "Maildir++";
128
129 if ((depot = depotfactory.get(depottype)) == 0) {
130 bincLog << "bincimapd: pid " << pid
131 << " Found no Depot for: " << depottype
132 << ". Please check your configurations file under the Mailbox section\n";
133 bincLog.flush();
134 return false;
135 }
136
137 depot->assign(new Maildir());
138 depot->setDefaultType("Maildir");
139
140 // Configurable delimiter to ease crossover from other IMAPservers
141 string delimiter = session.getEnv("DELIMITER");
142 if (delimiter != "") depot->setDelimiter(delimiter[0]);
143
144 BrokerFactory &brokerfactory = BrokerFactory::getInstance();
145
146 brokerfactory.assign("APPEND", new AppendOperator());
147 brokerfactory.assign("CAPABILITY", new CapabilityOperator());
148 brokerfactory.assign("CHECK", new CheckOperator());
149 brokerfactory.assign("CLOSE", new CloseOperator());
150 brokerfactory.assign("COPY", new CopyOperator());
151 brokerfactory.assign("CREATE", new CreateOperator());
152 brokerfactory.assign("DELETE", new DeleteOperator());
153 brokerfactory.assign("EXAMINE", new ExamineOperator());
154 brokerfactory.assign("EXPUNGE", new ExpungeOperator());
155 brokerfactory.assign("FETCH", new FetchOperator());
156 brokerfactory.assign("IDLE", new IdleOperator());
157 brokerfactory.assign("ID", new IdOperator());
158 brokerfactory.assign("LIST", new ListOperator());
159 brokerfactory.assign("LOGOUT", new LogoutOperator());
160 brokerfactory.assign("LSUB", new LsubOperator());
161 brokerfactory.assign("NAMESPACE", new NamespaceOperator());
162 brokerfactory.assign("NOOP", new NoopPendingOperator());
163 brokerfactory.assign("RENAME", new RenameOperator());
164 brokerfactory.assign("SEARCH", new SearchOperator());
165 brokerfactory.assign("SELECT", new SelectOperator());
166 brokerfactory.assign("STATUS", new StatusOperator());
167 brokerfactory.assign("STORE", new StoreOperator());
168 brokerfactory.assign("SUBSCRIBE", new SubscribeOperator());
169 brokerfactory.assign("UNSUBSCRIBE", new UnsubscribeOperator());
170
171 // automatically create depot directory if it's not there already
172 string path;
173 if (session.args.getUnqualifiedArgs().size() > 0)
174 path = session.args.getUnqualifiedArgs()[0];
175 if (path == "") path = ".";
176 else if (chdir(path.c_str()) != 0) {
177 mkdir(path.c_str(), 0777);
178 if (chdir(path.c_str()) != 0) {
179 bincLog << "bincimapd: pid" << pid
180 << " Error entering depot " + toImapString(path) + ": "
181 << strerror(errno) << endl;
182 bincLog.flush();
183 return false;
184 }
185 }
186
187 // automatically create INBOX if it's not there already
188 if (depot->get("INBOX") == 0 && !depot->createMailbox("INBOX")) {
189 bincLog << "bincimapd: pid " << pid
190 << " " << depot->getLastError() << endl;
191 bincLog.flush();
192 return false;
193 }
194
195 // load subscription list
196 depot->loadSubscribes();
197
199
200 const string details = logindetails;
201 string::size_type det = details.find('+');
202 if (det == string::npos) {
203 bincLog << "bincimapd: pid " << pid
204 << " Invalid content of BINCIMAP_LOGIN - did you invoke "
205 << argv[0] << " correctly?" << endl;
206 bincLog.flush();
207 return false;
208 }
209
210 const string tag = details.substr(det + 1);
211 const string command = details.substr(0, det);
212 bincClient << tag << " OK " << command << " completed" << endl;
213 bincClient.flush();
214 bincClient.setTimeout(IDLE_TIMEOUT);
215
216 return true;
217}
Declaration of the Broker class.
void assign(const std::string &fname, Operator *o)
Definition: broker.cc:57
static BrokerFactory & getInstance(void)
Definition: broker.cc:36
std::string usageString(void) const
Definition: argparser.cc:260
const std::vector< std::string > & getUnqualifiedArgs() const
Definition: argparser.cc:319
static DepotFactory & getInstance(void)
Definition: depot.cc:53
void assign(Depot *)
Definition: depot.cc:47
Depot * get(const std::string &name) const
Definition: depot.cc:36
virtual Mailbox * get(const std::string &path) const
Definition: depot.cc:107
virtual void loadSubscribes(void)
Definition: depot.cc:354
const std::string & getLastError(void) const
Definition: depot.cc:85
virtual void assign(Mailbox *)
Definition: depot.cc:97
virtual bool createMailbox(const std::string &m) const
Definition: depot.cc:201
bool setDefaultType(const std::string &n)
Definition: depot.cc:169
void setDelimiter(char)
Definition: depot.cc:157
The IODevice class provides a framework for reading and writing to device.
Definition: iodevice.h:31
void setMaxOutputBufferSize(unsigned int max)
Definition: iodevice.cc:112
bool flush(void)
Definition: iodevice.cc:74
void setFlags(unsigned int f)
Definition: iodevice.cc:94
void setOutputLevelLimit(LogLevel level)
Definition: iodevice.cc:147
static void addDevice(IODevice *dev)
Definition: iofactory.cc:31
static IODevice & getLogger(void)
Definition: iofactory.cc:57
static IOFactory & getInstance(void)
Definition: iofactory.cc:24
bool initialize(int argc, char *argv[])
void setEnv(const std::string &key, const std::string &value)
Definition: session.cc:237
void assignCommandLineArgs(void)
Definition: session.cc:160
CommandLineArgs args
Definition: session.h:42
void setState(int n)
Definition: session.cc:46
void setIP(const std::string &ip)
Definition: session.cc:70
bool help
Definition: session.h:28
@ AUTHENTICATED
Definition: session.h:37
struct Binc::Session::@3 command
bool version
Definition: session.h:29
pid_t getPid(void)
Definition: session.cc:209
bool parseCommandLine(int argc, char *argv[])
Definition: session.cc:136
static Session & getInstance(void)
Definition: session.cc:33
std::string getEnv(const std::string &key)
Definition: session.cc:230
Declaration of miscellaneous convertion functions.
Global constants.
#define BINC_VERSION
Definition: globals.h:10
Declaration of the IMAPServer class.
Declaration of the IODevice class.
Declaration of the IOFactory class.
#define bincLog
Definition: iofactory.h:50
#define bincClient
Definition: iofactory.h:31
Declaration of the Maildir class.
Declaration of the MultilogDevice class.
Definition: bincimapd.cc:9
std::string toImapString(const std::string &s_in)
Definition: convert.h:103
std::string toString(int i_in)
Definition: convert.h:25
int atoi(const std::string &s_in)
Definition: convert.h:55
void lowercase(std::string &input)
Definition: convert.h:122
void trim(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:137
char ** environ
Declaration of the StdIODevice class.
Declaration of the SyslogDevice class.
Declaration of miscellaneous tools.