-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo-import-enron.cpp
198 lines (182 loc) · 7.06 KB
/
topo-import-enron.cpp
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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <map>
#include <set>
#include "entity.h"
namespace po = boost::program_options;
namespace fs = boost::filesystem;
namespace gr = boost; //weird but they didnt subspace it
typedef std::set<unsigned int> members_t;
typedef unsigned int score_t;
struct group {
members_t members;
score_t weight;
};
struct relation {
};
typedef gr::adjacency_list<gr::setS, gr::setS, gr::undirectedS, group, relation> connectedness_graph;
typedef std::set<std::string> message_id_set;
typedef std::map<members_t, connectedness_graph::vertex_descriptor> initial_group_partition_map;
boost::regex re_terms("([^:]+): ([^\r\n]+[\r\n]+(?:\\s+[^\r\n]+[\r\n]+)*)");
boost::regex re_email("(?:(?:\"([^\"]+)\"\\s+<)|(?:(?:^|,)\\s*([^<,@\"]+)\\s+<))?(\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b)", boost::regex::icase);
int main(int argc, char* argv[])
{
std::string server;
std::string port;
std::string user;
std::string folder;
std::string password;
fs::path from;
fs::path to;
bool inbox;
//[Gmail]/Sent Mail
po::options_description general_options("General");
general_options.add_options()
("help", "list options");
po::options_description file_options("Load");
file_options.add_options()
("save-raw", po::value<fs::path>(&to), "path to save the data (after download phase)");
po::options_description source_options("Download");
source_options.add_options()
("load", po::value<fs::path>(&from), "mail folder");
po::options_description run_options("Run");
po::options_description all_options("Email Topology Options");
all_options
.add(general_options)
.add(file_options)
.add(source_options);
if(argc < 2) {
std::cout << all_options << std::endl;
return 1;
}
po::variables_map vm;
try {
int options_style = po::command_line_style::default_style;
po::store(po::parse_command_line(argc, argv, all_options, options_style), vm);
po::notify(vm);
} catch(std::exception& e) {
std::cout << all_options << std::endl;
std::cout << "Command line parsing failed: " << e.what() << std::endl;
return 1;
}
if(vm.count("help")) {
std::cout << all_options << std::endl;
return 1;
}
email_id_bimap email_id;
connectedness_graph cg;
entity_map em;
initial_group_partition_map igpm;
message_id_set message_id;
if(!vm.count("save-raw")) {
std::cout << "you must specify --save-raw with a file name" << std::endl;
return 1;
}
if(!vm.count("load") || !fs::exists(from) || !fs::is_directory(from)) {
std::cout << "missing source data folder (or not a folder)" << std::endl;
return 1;
}
std::vector<char> buffer(128 * 1024);
std::string headers;
std::cout << "loading " << from;
for(fs::recursive_directory_iterator fe, fi(from); fi != fe; ++fi) {
if(fs::is_directory(*fi))
continue;
fs::ifstream in(*fi);
headers.clear();
while(in.good()) {
in.getline(&buffer[0], buffer.size());
std::string line = &buffer[0];
boost::algorithm::trim(line);
if(line.empty())
break;
headers += "\n";
headers += line;
}
members_t g;
for(boost::sregex_iterator i(headers.begin(), headers.end(), re_terms), e; i != e; ++i) {
const boost::smatch& what = *i;
std::string field = what[1].str();
if(boost::algorithm::iequals(field, "Message-ID")) {
std::string id = what[2].str();
boost::algorithm::trim(id);
std::pair<message_id_set::iterator, bool> result = message_id.insert(id);
//skip this duplicate message
if(!result.second) {
g.clear();
break;
}
} else if(boost::algorithm::iequals(field, "From") || boost::algorithm::iequals(field, "To") || boost::algorithm::iequals(field, "Cc")) {
std::string data = what[2].str();
boost::replace_all(data, "\n", "");
boost::replace_all(data, "\r", "");
for(boost::sregex_iterator j(data.begin(), data.end(), re_email), e; j != e; ++j) {
std::string name = (*j)[1].str();
if(name.empty())
name = (*j)[2].str();
std::string email_address = (*j)[3].str();
boost::algorithm::to_lower(email_address);
boost::algorithm::trim(name);
std::pair<email_id_bimap::map_by<email>::iterator, bool> result = email_id.by<email>().insert(
email_id_bimap::map_by<email>::value_type(email_address, email_id.size()));
if(result.second)
std::cout << "@" << std::flush;
if(!name.empty() && boost::to_lower_copy(name) != email_address)
em[email_address].insert(name);
g.insert(result.first->second);
}
}
}
if(g.empty()) {
continue;
}
initial_group_partition_map::iterator r = igpm.find(g);
if(r == igpm.end()) {
connectedness_graph::vertex_descriptor node = gr::add_vertex(cg);
cg[node].members = g;
cg[node].weight = 1;
igpm.insert(r, std::make_pair(g, node));
} else {
connectedness_graph::vertex_descriptor node = r->second;
cg[node].weight++;
}
std::cout << ". " << std::flush;
}
std::cout << std::endl;
if(fs::exists(to))
fs::remove(to);
fs::ofstream out(to);
std::cout << "saving data to " << to.file_string();
for(connectedness_graph::vertex_iterator i = gr::vertices(cg).first; i != gr::vertices(cg).second; ++i) {
out << (unsigned long long)cg[*i].weight;
for(members_t::iterator j = cg[*i].members.begin(); j != cg[*i].members.end(); ++j) {
out << "\t" << email_id.by<bit>().equal_range(*j).first->second;
}
out << std::endl;
}
out << "-" << std::endl;
for(entity_map::iterator i = em.begin(); i != em.end(); ++i) {
out << i->first;
for(std::set<std::string>::iterator k = i->second.begin(); k != i->second.end(); ++k) {
out << "\t" << *k;
}
out << std::endl;
}
return 0;
}