-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skype.cc
139 lines (94 loc) · 3.87 KB
/
Skype.cc
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
#include "Skype.hh"
#include "Account.hh"
#include "Conversation.hh"
const v8::Persistent<v8::String> o_nix::Skype::LOGIN_EVENT_NAME = NODE_PSYMBOL("login");
const v8::Persistent<v8::String> o_nix::Skype::EMIT_NAME = NODE_PSYMBOL("emit");
o_nix::Skype::Skype() {
}
o_nix::Skype::~Skype() {
}
void o_nix::Skype::Init(v8::Handle<v8::Object> target) {
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(New);
v8::Local<v8::ObjectTemplate> instance = tpl->InstanceTemplate();
tpl->SetClassName(v8::String::NewSymbol("Skype"));
instance->SetInternalFieldCount(1);
v8::Persistent<v8::Function> constructor = v8::Persistent<v8::Function>::New(tpl->GetFunction());
target->Set(v8::String::NewSymbol("Skype"), constructor);
node::SetMethod(instance, "setKey", SetKey);
node::SetMethod(instance, "setLogin", SetLogin);
node::SetMethod(instance, "setPassword", SetPassword);
node::SetMethod(instance, "login", Login);
}
::Account *o_nix::Skype::newAccount(int oid) {
cout << "Creating a new Account..." << endl;
o_nix::Account* acc = new o_nix::Account(oid, this);
acc->SetSkype(this);
return acc;
}
::Conversation *o_nix::Skype::newConversation(int oid) {
cout << "New Conversation object created." << endl;
return new o_nix::Conversation(oid, this);
}
v8::Handle<v8::Value> o_nix::Skype::New(const v8::Arguments& args) {
o_nix::Skype* skype = new o_nix::Skype();
skype->Wrap(args.This());
return args.This();
}
v8::Handle<v8::Value> o_nix::Skype::SetKey(const v8::Arguments& args) {
Skype* skype = node::ObjectWrap::Unwrap<o_nix::Skype>(args.This());
skype->_key = Base::toUtf8(args[0]);
return v8::Undefined();
}
v8::Handle<v8::Value> o_nix::Skype::SetLogin(const v8::Arguments& args) {
Skype* skype = node::ObjectWrap::Unwrap<o_nix::Skype>(args.This());
skype->_login = Base::toUtf8(args[0]);
return v8::Undefined();
}
v8::Handle<v8::Value> o_nix::Skype::SetPassword(const v8::Arguments& args) {
Skype* skype = node::ObjectWrap::Unwrap<o_nix::Skype>(args.This());
skype->_password = Base::toUtf8(args[0]);
return v8::Undefined();
}
v8::Handle<v8::Value> o_nix::Skype::FillInbox(const v8::Arguments &args) {
Skype* skype = node::ObjectWrap::Unwrap<o_nix::Skype>(args.This());
return v8::Undefined();
}
v8::Handle<v8::Value> o_nix::Skype::Login(const v8::Arguments& args) {
Skype* skype = node::ObjectWrap::Unwrap<o_nix::Skype>(args.This());
cout << "Starting skype..." << endl;
cout << "Login is " << skype->_login.c_str() << endl;
cout << "Password is " << skype->_password.c_str() << endl;
skype->init(skype->_key.c_str(), "127.0.0.1", 8963, "streamlog.txt");
skype->start();
cout << "Skype is started." << endl;
if (skype->GetAccount(skype->_login.c_str(), skype->_account)) {
cout << "Skype account obtained. Logging in..." << endl;
skype->_account->LoginWithPassword(skype->_password.c_str());
}
return v8::Undefined();
}
void o_nix::Skype::OnLoggedIn_(uv_async_t *handle, int status) {
v8::HandleScope scope;
pair<o_nix::Skype*, o_nix::Account*>* data = reinterpret_cast< pair<o_nix::Skype*, o_nix::Account*>* > (handle->data);
o_nix::Skype* skype = data->first;
cout << "Getting skype conversations list..." << endl;
skype->GetConversationList(skype->_conversations, Conversation::INBOX_CONVERSATIONS);
fetch(skype->_conversations);
v8::Handle<v8::Value> argv[1] = {
LOGIN_EVENT_NAME
};
node::MakeCallback(skype->handle_, EMIT_NAME, 1, argv);
uv_close((uv_handle_t*) handle, NULL);
delete data, handle;
}
void o_nix::Skype::OnLoggedIn(o_nix::Account* account) {
cout << "Logged in!" << endl;
uv_async_t* async = new uv_async_t();
async->data = new pair<o_nix::Skype*, o_nix::Account*> (this, account);
uv_async_init(uv_default_loop(), async, OnLoggedIn_);
uv_async_send(async);
}
void o_nix::Skype::OnLoggedOut(const o_nix::Account *account, const char *reason) {
cout << "Logged out. Reason: " << reason;
}