-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.js
100 lines (91 loc) · 2.84 KB
/
chatbot.js
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
var WebSocket = require('ws');
var ws = new WebSocket('wss://wechat.iku.im/ws');
var DSA = require('otr').DSA;
var OTR = require('otr').OTR;
console.log("creating DSA");
var myKey = new DSA();
console.log("created DSA: " + myKey.fingerprint());
function initAKE(options) {
var buddy = new OTR(options);
buddy.REQUIRE_ENCRYPTION = true
buddy.on('ui', function (msg, encrypted, meta) {
console.log(msg, encrypted)
});
buddy.on('io', function (msg, meta) {
ws.send(
JSON.stringify(
{
"from":"alice",
"msg":msg
}
));
});
buddy.on('error', function (err, severity) {
if (severity === 'error') // either 'error' or 'warn'
console.error("error occurred: " + err);
});
buddy.on('status', function (state) {
switch (state) {
case OTR.CONST.STATUS_AKE_SUCCESS:
// sucessfully ake'd with buddy
// check if buddy.msgstate === OTR.CONST.MSGSTATE_ENCRYPTED
var newmsg = "Message to userA."
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
buddy.sendMsg(newmsg, '')
break;
case OTR.CONST.STATUS_END_OTR:
// if buddy.msgstate === OTR.CONST.MSGSTATE_FINISHED
// inform the user that his correspondent has closed his end
// of the private connection and the user should do the same
break;
}
})
ws.on('message', function incoming(res) {
var events = res.split('\n');
events.forEach(
function(e){
console.log(e);
try {
var data = JSON.parse(e);
if (data.from != 'alice') {
buddy.receiveMsg(data.msg);
}
} catch (exception) {
console.error(data);
}
}
);
});
}
ws.on('open', function open() {
var options = {
fragment_size: 140,
send_interval: 200,
priv: myKey
};
initAKE(options);
ws.send(
JSON.stringify(
{
"from":"alice",
"msg":"?OTRv23?"
}
));
});