-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchmailConfig.cpp
More file actions
199 lines (165 loc) · 4.62 KB
/
Copy pathFetchmailConfig.cpp
File metadata and controls
199 lines (165 loc) · 4.62 KB
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
#include <sstream>
#include "FetchmailConfig.h"
#include <libutils/FileUtils.h>
#include <libutils/String.h>
using namespace Utils;
namespace OPI
{
FetchmailConfig::FetchmailConfig(const string &cfgpath):
configfile(cfgpath),
host(R"(^poll\s+(\S+)\s+with proto)"),
user(R"(\s+user\s+(\S+)\s+there with password\s+(.+)\s+is\s+(\S+)\s+here\s+(ssl|))"),
cfgfile(cfgpath+".lnk")
{
this->ReadConfig();
}
void FetchmailConfig::AddAccount(const string &email, const string &host, const string &identity, const string &password, const string &user, bool use_ssl)
{
if( this->_hasuser( host, identity ) )
{
throw runtime_error("User already exists");
}
this->cfgfile[host+"\t"+identity] = email;
this->config[host][identity] = {password, user, use_ssl};
}
void FetchmailConfig::UpdateAccount(const string &email, const string &host, const string &identity, const string &password, const string &user, bool use_ssl)
{
if( ! this->_hasuser( host, identity ) )
{
throw runtime_error("User doesnt exists");
}
userinfo ui = this->config[host][identity];
// Only update user and password if provided
if( password != "" )
{
ui.password = password;
}
if( user != "" )
{
ui.user = user;
}
ui.use_ssl = use_ssl;
this->cfgfile[host+"\t"+identity] = email;
this->config[host][identity] = ui;
}
list<string> FetchmailConfig::GetHosts()
{
list<string> hosts;
for( const auto& host: this->config )
{
hosts.push_back( host.first );
}
return hosts;
}
map<string, string> FetchmailConfig::GetAccount(const string &host, const string &identity)
{
if( ! this->_hasuser( host, identity ) )
{
throw runtime_error("User doesnt exists");
}
return {
{"email", this->cfgfile[host+"\t"+identity]},
{"host", host},
{"identity", identity},
{"password", this->config[host][identity].password},
{"username", this->config[host][identity].user},
{"ssl", this->config[host][identity].use_ssl?"true":"false"},
};
}
list<map<string, string> > FetchmailConfig::GetAccounts(const string &user)
{
list<map<string, string> > ret;
bool fetchall = user == "";
for( const auto& domain: this->config )
{
for( const auto& identity: domain.second )
{
map<string,string> userline = this->GetAccount(domain.first, identity.first);
if( fetchall || user == userline["username"] )
{
ret.push_back( userline );
}
}
}
return ret;
}
void FetchmailConfig::DeleteAccount(const string &host, const string &identity)
{
if( ! this->_hasuser( host, identity ) )
{
throw runtime_error("User doesnt exists");
}
this->config[host].erase(identity);
this->cfgfile.erase(host+"\t"+identity);
// Last identity at host?
if( this->config[host].size() == 0 )
{
this->config.erase( host );
}
}
static inline string getmatchstring(const string& line, Regex::Match m)
{
return line.substr(m.rm_so, m.rm_eo - m.rm_so);
}
void FetchmailConfig::ReadConfig()
{
this->config.empty();
list<string> lines = File::GetContent( this->configfile );
string lasthost="";
for( const string& line: lines)
{
vector<Regex::Match> m;
if( (m = host.DoMatch(line)).size() > 1)
{
lasthost = line.substr( m[1].rm_so, m[1].rm_eo-m[1].rm_so );
}
else if( (m = user.DoMatch(line)).size() >= 4 )
{
if( lasthost == "")
{
continue;
}
string identity = String::Trimmed( getmatchstring( line, m[1]), "'\"" );
string password = String::Trimmed( getmatchstring( line, m[2]), "'\"" );
string user = String::Trimmed( getmatchstring( line, m[3]), "'\"" );
bool usessl = ( m.size() == 5) && ( getmatchstring( line, m[4]) == "ssl" );
this->config[lasthost][identity] = { password, user, usessl };
}
}
}
void FetchmailConfig::WriteConfig()
{
stringstream out;
out << "set postmaster \"postmaster\"\n"
<< "set bouncemail\n"
<< "set no spambounce\n"
<< "set properties \"\"\n"
<< "set daemon 300\n"
<< "set syslog";
for( const auto& cfgline: config)
{
string host = cfgline.first;
out << "\npoll "<<host<<" with proto POP3\n";
map< string, struct userinfo > users = cfgline.second;
for( const auto& user: users )
{
out << "\tuser '"<<user.first <<"' there with password '"
<<user.second.password << "' is '"<<user.second.user
<< "' here";
if( user.second.use_ssl )
{
out << " ssl";
}
out << " smtpaddress localdomain\n";
}
}
this->cfgfile.Sync(true, File::UserRW);
File::Write( this->configfile, out.str(), File::UserRW);
}
FetchmailConfig::~FetchmailConfig() = default;
bool FetchmailConfig::_hasuser(const string &host, const string &identity)
{
return ( this->config.find(host) != this->config.end() ) &&
( this->config[host].find(identity) != this->config[host].end() );
}
} // End namespace