Show parse_response.cc syntax highlighted
#include <sys/time.h>
#include <sstream>
#include <string>
#include <map>
#include <iostream>
#include <IMAPParser.h>
#include <netxx/netxx.h>
#include <netxx/tls/netxx.h>
using namespace std;
using namespace IMAP;
inline struct timeval operator-(const struct timeval &a, const struct timeval &b)
{
struct timeval rv;
rv.tv_sec = a.tv_sec - b.tv_sec;
if(a.tv_usec < b.tv_usec) {
rv.tv_sec--;
rv.tv_usec = b.tv_usec - a.tv_usec;
} else
rv.tv_usec = a.tv_usec - b.tv_usec;
return rv;
}
inline ostream &operator<<(ostream &out, const struct timeval &v)
{
return out << (v.tv_sec + (v.tv_usec/1000000.0)) << "s";
}
int main(int argc, char **argv)
{
struct timeval t0, t1, t2, t3, p0, p1, p2;
IMAP::MessageMap cache;
IMAP::MessageList msglist;
char line[1024];
if(argc < 3) {
cerr << "Usage: " << argv[0] << " user password" << endl;
return 1;
}
gettimeofday(&t0, NULL);
Netxx::TLS::Context ssl_context;
ssl_context.load_ca_file("/etc/certificate_authorities.pem");
ssl_context.remove_protocol(Netxx::TLS::Context::PROTOCOL_SSLv2);
IMAPParser *p = new IMAPParser();
//Net::Socket s("tick.uoregon.edu", "143", true);
Netxx::TLS::Stream client(ssl_context, Netxx::Address("imap.uoregon.edu:993"));
Netxx::Netbuf<1024> buffer(client);
std::iostream s(&buffer);
string fetch_data("");
string tmp;
s << "1 login " << argv[1] << " " << argv[2] << endl;
s << "2 select inbox" << endl;
do { s.getline(line, 1024); } while(!s.eof() && line[0] != '2');
gettimeofday(&t1, NULL);
try {
cerr << p->parseAttributeFetch(s, &cache) << endl;
} catch(ParseError &e) {
cerr << "Parse error: " << e.what() << endl;
}
gettimeofday(&t2, NULL);
try {
cerr << p->parseMailboxThreads(s, &cache, &msglist) << endl;
} catch(ParseError &e) {
cerr << "Parse error: " << e.what() << endl;
}
gettimeofday(&t3, NULL);
s << "5 logout\n";
s.flush();
p0 = t1 - t0;
p1 = t2 - t1;
p2 = t3 - t2;
IMAP::MessageMap::iterator i;
map<string, string>::iterator j;
for(i=cache.begin(); i != cache.end(); i++) {
cout << "UID: " << (*i).first << endl;
for(j=(*i).second.begin(); j != (*i).second.end(); j++) {
cout << "\t" << (*j).first << ": " << (*j).second << endl;
}
}
cout << "Threaded subjects: " << endl;
MessageList::iterator k;
int n;
string uid;
for(k=msglist.begin(); k!= msglist.end(); k++)
{
const MessageAttributes &ma = **k;
MessageAttributes::const_iterator uidp;
uidp = ma.find(IMAP::IMAPParser::uidSelector);
if(uidp == ma.end()) {
cerr << "Could not find UID in map!" << endl;
continue;
}
uid = uidp->second;
n = atoi(cache[uid][IMAP::IMAPParser::threadlevelSelector].c_str());
while(n > 1) {
cout << "\t";
n--;
}
cout << cache[uid][IMAP::IMAPParser::subjectSelector] << endl;
}
cout << "Setup took: " << p0 << endl;
cout << "Parsing took: " << p1 << endl;
cout << "Thread processing took: " << p2 << endl;
return 0;
}
See more files for this project here