-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
200 lines (160 loc) · 4.9 KB
/
client.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
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
199
200
var util = require("util"),
xmpp = require("node-xmpp"),
Room = require("./room.js"),
EventEmitter = require('events').EventEmitter;
// Make client instances store a reference to its websocket client
function Client(websocket) {
this.connection = null;
this.websocket = websocket;
this.defaultShow = 'chat';
this.defaultStatus = 'Cháchara';
this.cachedShow = null;
this.cachedStatus = null;
this.buffer = [];
this.bufferSize = 20;
// Testing mode, few seconds!
this.awayTimeout = {
milliseconds: 5000,
timeout: null
};
this.disconnectTimeout = {
milliseconds: 30000,
timeout: null
};
}
util.inherits(Client, EventEmitter);
Client.prototype.connect = function(jid, password, callback) {
var self = this;
self.jid = jid;
self.nick = jid.split("@")[0];
self.password = password;
self.connection = new xmpp.Client({
nick: self.nick,
jid : self.jid + "/webapp",
password : self.password
});
self.rooms = {};
self.chats = [];
self.connection.on('online', onOnline);
self.connection.on('error', function(e) {
if (e == 'XMPP authentication failure') callback(e);
else console.log(e);
});
self.setStatus = function (show, status) {
var elem = new xmpp.Element('presence');
elem.c('show').t(show);
elem.c('status').t(status);
self.connection.send(elem);
};
function onOnline() {
self.connection.on('stanza', onStanza);
self.setStatus(self.defaultShow, self.defaultStatus);
if (callback != undefined) {
callback();
}
}
// TODO refactor
function onStanza(stanza) {
if (stanza.name == "message") {
if (stanza.attrs.type == "groupchat") {
var fromParts = stanza.attrs.from.split('/');
var room = fromParts[0];
var nick = fromParts[1];
// Dispatch message to appropriate room.
if (self.rooms[room]) {
self.rooms[room]['onMessage'] && self.rooms[room].onMessage(stanza);
}
} else if (stanza.attrs.type == "chat") {
self.onMessage(stanza);
}
} else if (stanza.name == "presence") {
//if (stanza.getChild('x') !== undefined && stanza.getChild('x').getChild('photo') !== undefined) {
// self.onAvatarAvailable(stanza);
//} else {
var fromParts = stanza.attrs.from.split('/');
var room = fromParts[0];
if (self.rooms[room]) {
self.rooms[room]['onPresence'] && self.rooms[room].onPresence(stanza);
}
//}
} else if (stanza.name == "iq") {
if (stanza.attrs.id == "v3") {
self.onAvatar(stanza);
} else {
}
}
}
// Connect to XMPP.
Client.prototype.join = function(name, callback) {
// No strict checking at the moment
this.rooms[name] = new Room(this, name);
this.rooms[name].join();
callback(this.rooms[name], this.websocket);
}
// Leave a room
Client.prototype.leave = function(name, callback) {
this.rooms[name].leave();
callback(this.rooms[name], this.websocket);
}
Client.prototype.say = function(what, to, callback) {
// Send a message.
var elem = new xmpp.Element('message', {
from : this.jid,
to : to,
type : 'chat'
});
elem.c('body').t(what);
this.connection.send(elem);
if (this.chats.indexOf(to) == -1) {
this.chats.push(to);
}
callback();
}
Client.prototype.disconnect = function() {
if (this.connection) {
this.connection.send(new xmpp.Element('presence', {type: 'unavailable'})
.c('status').t('Logged out')
.tree());
this.connection.end();
}
}
self.connection.on("error", function(err) {
console.log(util.inspect(err));
});
Client.prototype.onAvatarAvailable = function(stanza) {
var elem = new xmpp.Element("iq", {
type : "get",
from : self.jid,
to : stanza.attrs.from.split("/")[0],
id : "v3"
});
elem.c("vCard", { xmlns : "vcard-temp" });
self.connection.send(elem);
}
Client.prototype.onAvatar = function(stanza) {
var vCardNode = stanza.getChild("vCard");
if (vCardNode === undefined) return false;
var photoNode = vCardNode.getChild("PHOTO");
if (photoNode === undefined) return false;
self.emit("avatar", self.websocket, {
type : 'avatar',
from : stanza.attrs.from,
fileType : photoNode.getChild("TYPE").getText(),
binval : photoNode.getChild("BINVAL").getText().split("\n").join("")
});
}
Client.prototype.onMessage = function(stanza) {
if (stanza.getChild("body") == undefined) {
console.log("Somebody is typing!");
} else {
this.emit("message", this.websocket, {
type : "chat",
to : stanza.attrs.to,
from : stanza.attrs.from,
body : stanza.getChild("body").getText(),
timestamp: (new Date).toLocaleTimeString()
});
}
}
};
module.exports = Client;