Skip to content

Commit 6a864e8

Browse files
committed
created a muc plugin
1 parent 32b4365 commit 6a864e8

File tree

5 files changed

+425
-1
lines changed

5 files changed

+425
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ doc
77
plugins/*.min.js
88
*.tar.gz
99
*.zip
10+
*~

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BASE_FILES = $(SRC_DIR)/base64.js \
1212
STROPHE = strophe.js
1313
STROPHE_MIN = strophe.min.js
1414

15-
PLUGIN_FILES = $(PLUGIN_DIR)/strophe.pubsub.js $(PLUGIN_DIR)/strophe.flxhr.js
15+
PLUGIN_FILES = $(PLUGIN_DIR)/strophe.pubsub.js $(PLUGIN_DIR)/strophe.flxhr.js $(PLUGIN_DIR)/strophe.muc.js
1616
PLUGIN_FILES_MIN = $(PLUGIN_FILES:.js=.min.js)
1717

1818
DIST_FILES = LICENSE.txt README.txt contrib examples plugins tests doc \

plugins/strophe.muc.js

+291
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/*
2+
Plugin to implement the MUC extension. http://xmpp.org/extensions/xep-0045.html
3+
*/
4+
5+
Strophe.addConnectionPlugin('muc', {
6+
_connection: null,
7+
// The plugin must have the init function
8+
/***Function
9+
Initialize the MUC plugin. Sets the correct connection object and
10+
extends the namesace.
11+
*/
12+
init: function(conn) {
13+
this._connection = conn;
14+
/* extend name space
15+
* NS.MUC - XMPP Multi-user chat namespace
16+
* from XEP 45.
17+
*
18+
*/
19+
Strophe.addNamespace('MUC_OWNER', Strophe.NS.MUC+"#owner");
20+
Strophe.addNamespace('MUC_ADMIN', Strophe.NS.MUC+"#admin");
21+
},
22+
/***Function
23+
Join a multi-user chat room
24+
Parameters:
25+
(String) room - The multi-user chat room to join.
26+
(String) nick - The nickname to use in the chat room. Optional
27+
(Function) msg_handler_cb - The function call to handle messages from the
28+
specified chat room.
29+
(Function) pres_handler_cb - The function call back to handle presence
30+
in the chat room.
31+
(String) password - The optional password to use. (password protected
32+
rooms only)
33+
*/
34+
join: function(room, nick, msg_handler_cb, pres_handler_cb, password) {
35+
var room_nick = this.test_append_nick(room, nick);
36+
var msg = $pres({from: this._connection.jid,
37+
to: room_nick})
38+
.c("x",{xmlns: Strophe.NS.MUC});
39+
if (password)
40+
{
41+
var password_elem = Strophe.xmlElement("password",
42+
[],
43+
password);
44+
msg.cnode(password_elem);
45+
}
46+
if (msg_handler_cb)
47+
{
48+
this._connection.addHandler(function(stanza) {
49+
var from = stanza.getAttribute('from');
50+
var roomname = from.split("/");
51+
// filter on room name
52+
if (roomname.length > 1 && roomname[0] == room)
53+
{
54+
return msg_handler_cb(stanza);
55+
}
56+
else
57+
{
58+
return true;
59+
}
60+
},
61+
null,
62+
"message",
63+
null,
64+
null,
65+
null);
66+
}
67+
if (pres_handler_cb)
68+
{
69+
this._connection.addHandler(function(stanza) {
70+
var xquery = stanza.getElementsByTagName("x");
71+
if (xquery.length > 0)
72+
{
73+
//Handle only MUC user protocol
74+
var xmlns = xquery[0].getAttribute("xmlns");
75+
76+
if (xmlns && xmlns.match(Strophe.NS.MUC))
77+
{
78+
return pres_handler_cb(stanza);
79+
}
80+
}
81+
return true;
82+
},
83+
null,
84+
"presence",
85+
null,
86+
null,
87+
null);
88+
}
89+
this._connection.send(msg);
90+
},
91+
/***Function
92+
Leave a multi-user chat room
93+
Parameters:
94+
(String) room - The multi-user chat room to leave.
95+
(String) nick - The nick name used in the room.
96+
(Function) handler_cb - Optional function to handle the successful leave.
97+
Returns:
98+
iqid - The unique id for the room leave.
99+
*/
100+
leave: function(room, nick, handler_cb) {
101+
var room_nick = this.test_append_nick(room, nick);
102+
var presenceid = this._connection.getUniqueId();
103+
var presence = $pres({type: "unavailable",
104+
id: presenceid,
105+
from: this._connection.jid,
106+
to: room_nick})
107+
.c("x",{xmlns: Strophe.NS.MUC});
108+
this._connection.addHandler(handler_cb,
109+
null,
110+
"presence",
111+
null,
112+
presenceid,
113+
null);
114+
this._connection.send(presence);
115+
return presenceid;
116+
},
117+
/***Function
118+
Parameters:
119+
(String) room - The multi-user chat room name.
120+
(String) nick - The nick name used in the chat room.
121+
(String) message - The message to send to the room.
122+
Returns:
123+
msgiq - the unique id used to send the message
124+
*/
125+
message: function(room, nick, message) {
126+
var room_nick = this.test_append_nick(room, nick);
127+
var msgid = this._connection.getUniqueId();
128+
var msg = $msg({to: room_nick,
129+
from: this._connection.jid,
130+
type: "groupchat",
131+
id: msgid}).c("body",
132+
{xmlns: Strophe.NS.CLIENT}).t(message);
133+
msg.up().c("x", {xmlns: "jabber:x:event"}).c("composing");
134+
this._connection.send(msg);
135+
return msgid;
136+
},
137+
/***Function
138+
Start a room configuration.
139+
Parameters:
140+
(String) room - The multi-user chat room name.
141+
Returns:
142+
id - the unique id used to send the configuration request
143+
*/
144+
configure: function(room) {
145+
//send iq to start room configuration
146+
var config = $iq({to:room,
147+
type: "get"}).c("query",
148+
{xmlns: Strophe.NS.MUC_OWNER});
149+
var stanza = config.tree();
150+
return this._connection.sendIQ(stanza,
151+
function(){},
152+
function(){});
153+
},
154+
/***Function
155+
Cancel the room configuration
156+
Parameters:
157+
(String) room - The multi-user chat room name.
158+
Returns:
159+
id - the unique id used to cancel the configuration.
160+
*/
161+
cancelConfigure: function(room) {
162+
//send iq to start room configuration
163+
var config = $iq({to: room,
164+
type: "set"})
165+
.c("query", {xmlns: Strophe.NS.MUC_OWNER})
166+
.c("x", {xmlns: "jabber:x:data", type: "cancel"});
167+
var stanza = config.tree();
168+
return this._connection.sendIQ(stanza,
169+
function(){},
170+
function(){});
171+
},
172+
/***Function
173+
Save a room configuration.
174+
Parameters:
175+
(String) room - The multi-user chat room name.
176+
(Array) configarray - an array of form elements used to configure the room.
177+
Returns:
178+
id - the unique id used to save the configuration.
179+
*/
180+
saveConfiguration: function(room, configarray) {
181+
var config = $iq({to: room,
182+
type: "set"})
183+
.c("query", {xmlns: Strophe.NS.MUC_OWNER})
184+
.c("x", {xmlns: "jabber:x:data", type: "submit"});
185+
for (var i = 0; i >= configarray.length; i++) {
186+
config.cnode(configarray[i]);
187+
}
188+
var stanza = config.tree();
189+
return this._connection.sendIQ(stanza,
190+
function(){},
191+
function(){});
192+
},
193+
/***Function
194+
Parameters:
195+
(String) room - The multi-user chat room name.
196+
Returns:
197+
id - the unique id used to create the chat room.
198+
*/
199+
createInstantRoom: function(room) {
200+
var room = $iq({to: room,
201+
type: "set"})
202+
.c("query", {xmlns: Strophe.NS.MUC_OWNER})
203+
.c("x", {xmlns: "jabber:x:data",
204+
type: "submit"});
205+
return this._connection.sendIQ(room.tree(),
206+
function() {},
207+
function() {});
208+
},
209+
/***
210+
Set the topic of the chat room.
211+
Parameters:
212+
(String) room - The multi-user chat room name.
213+
(String) topic - Topic message.
214+
*/
215+
setTopic: function(room, topic) {
216+
var msg = $msg({to: room,
217+
from: this._connection.jid,
218+
type: "groupchat"})
219+
.c("subject", {xmlns: "jabber:client"}).t(topic);
220+
this._connection.send(msg.tree());
221+
},
222+
/***Function
223+
Changes the role and affiliation of a member of a MUC room.
224+
The modification can only be done by a room moderator. An error will be
225+
returned if the user doesn't have permission.
226+
Parameters:
227+
(String) room - The multi-user chat room name.
228+
(String) nick - The nick name of the user to modify.
229+
(String) role - The new role of the user.
230+
(String) affiliation - The new affiliation of the user.
231+
(String) reason - The reason for the change.
232+
Returns:
233+
iq - the id of the mode change request.
234+
*/
235+
modifyUser: function(room, nick, role, affiliation, reason) {
236+
var item_attrs = {nick: nick};
237+
if (role !== null)
238+
{
239+
item_attrs["role"] = role;
240+
}
241+
if (affiliation !== null)
242+
{
243+
item_attrs["affiliation"] = affiliation;
244+
}
245+
var item = $build("item", item_attrs);
246+
if (reason !== null)
247+
{
248+
item.cnode(Strophe.xmlElement("reason", reason));
249+
}
250+
var room = $iq({to: room,
251+
type: "set"})
252+
.c("query", {xmlns: Strophe.NS.MUC_OWNER}).cnode(item.tree());
253+
return this._connection.sendIQ(room.tree(),
254+
function() {},
255+
function() {});
256+
},
257+
/***Function
258+
Change the current users nick name.
259+
Parameters:
260+
(String) room - The multi-user chat room name.
261+
(String) user - The new nick name.
262+
*/
263+
changeNick: function(room, user) {
264+
var room_nick = this.test_append_nick(room, user);
265+
var presence = $pres({from: this._connection.jid,
266+
to: room_nick})
267+
.c("x",{xmlns: Strophe.NS.MUC});
268+
this._connection.send(presence.tree());
269+
},
270+
/***Function
271+
List all chat room available on a server.
272+
Parameters:
273+
(String) server - name of chat server.
274+
(String) handle_cb - Function to call for room list return.
275+
*/
276+
listRooms: function(server, handle_cb) {
277+
var iq = $iq({to: server,
278+
from: this._connection.jid,
279+
type: "get"})
280+
.c("query",{xmlns: Strophe.NS.DISCO_ITEMS});
281+
this._connection.sendIQ(iq, handle_cb, function(){});
282+
},
283+
test_append_nick: function(room, nick) {
284+
var room_nick = room;
285+
if (nick)
286+
{
287+
room_nick += "/" + nick;
288+
}
289+
return room_nick;
290+
}
291+
});

tests/muc.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2+
"http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
<head>
5+
<script src="http://code.jquery.com/jquery-latest.js"></script>
6+
<script type="text/javascript" src="testrunner.js"></script>
7+
<script type="text/javascript" src="../strophe.js"></script>
8+
<script type="text/javascript" src="../plugins/strophe.muc.js"></script>
9+
<script type="text/javascript" src="muc.js"></script>
10+
<link rel="stylesheet" href="testsuite.css" type="text/css" media="screen" />
11+
12+
<script>
13+
Strophe.Test.run();
14+
</script>
15+
16+
</head>
17+
<body>
18+
<h1>QUnit tests for Strophe</h1>
19+
<h2 id="userAgent"></h2>
20+
<div><a style='display:none;' id="run_tests" href='#'>Run tests.</a>
21+
<a sytle='display:none;' id="disconnect" href='#'>Disconnect.</a>
22+
</div>
23+
<div id="muc_item">
24+
</div>
25+
<ol id="tests"></ol>
26+
<div id='main'></div>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)