-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
104 lines (87 loc) · 2.86 KB
/
main.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
#include <iostream>
#include <strophe.h>
#include <term.h>
#include <unistd.h>
#include <cryptopp/base64.h>
#include "mongo.h"
#include "xmpp.h"
#include "commands.h"
#include "command.h"
using namespace std;
using namespace CryptoPP;
void end(int err)
{
Mongo::get_instance()->release();
Xmpp::release();
if( err != 0)
exit(err);
}
int main(int argc, char **argv)
{
bool init = false;
Mongo* mongo = Mongo::get_instance();
if( mongo)
{
if( !mongo->connect())
{
cerr << "Unable to connect to database" << endl;
end(1);
}
mongo->select_db("Chatty");
mongo->select_collection("jabber");
if( init)
{
mongo->delete_all();
}
// Test for the first run
if( mongo->count() == 0)
{
string user;
string domain;
string resource;
string pw;
string tmp;
cout << "Hello let me introduce myself. I am chatty and to be able to do what i am designed to i need a jabber account." << endl;
cout << "So please give me a user" << endl;
cin >> user;
cout << "And the domain" << endl;
cin >> domain;
cout << "And a Resource" << endl;
cin >> resource;
cout << "Then I need the password for the " << user << "@" << domain << "/" << resource << " account" << endl;
tmp = getpass("");
new StringSource( tmp, true, new Base64Encoder(new StringSink(pw)));
cout << "thx" << endl;
mongo->write(BSON(GENOID << "user" << user.c_str() << "domain" << domain.c_str() << "resource" << resource.c_str() << "pw" << pw.c_str()));
setupterm(nullptr, fileno(stdout), nullptr);
putp(tigetstr("clear"));
}
// get the data from the database
BSONObj jabberData = mongo->read_one();
if( jabberData.isEmpty())
{
cerr << "no useful account could be found" << endl;
end(1);
}
string password;
new StringSource(jabberData["pw"].String(), true, new Base64Decoder(new StringSink(password)));
string jabberid = jabberData["user"].str() + "@" + jabberData["domain"].str() + "/"+ jabberData["resource"].str();
Xmpp* xampp = Xmpp::get_instance();
Command* command = new Command();
command->register_command("update", update);
xampp->register_commands(command);
if( !xampp->connect(jabberid, password))
{
cerr << "Error while connecting to jabber!" << endl;
end(2);
}
xampp->run();
}
else
{
cerr << "No Database could be found!" << endl;
end(1);
}
end(0);
return 0;
}