summaryrefslogtreecommitdiffstats
blob: 26be309c7c18b902f32a369d8054cbf8f15781c7 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * Copyright (c) 2010-2015, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.muc;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.isode.stroke.client.StanzaChannel;
import com.isode.stroke.elements.CapsInfo;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.elements.Form;
import com.isode.stroke.elements.IQ;
import com.isode.stroke.elements.MUCAdminPayload;
import com.isode.stroke.elements.MUCDestroyPayload;
import com.isode.stroke.elements.MUCInvitationPayload;
import com.isode.stroke.elements.MUCItem;
import com.isode.stroke.elements.MUCOccupant;
import com.isode.stroke.elements.MUCOwnerPayload;
import com.isode.stroke.elements.MUCPayload;
import com.isode.stroke.elements.MUCUserPayload;
import com.isode.stroke.elements.Message;
import com.isode.stroke.elements.Presence;
import com.isode.stroke.jid.JID;
import com.isode.stroke.jid.JID.CompareType;
import com.isode.stroke.presence.DirectedPresenceSender;
import com.isode.stroke.queries.GenericRequest;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot1;
import com.isode.stroke.signals.Slot2;

/**
 * Class representing multi user chat room
 *
 */
public class MUCImpl extends MUC {

	private boolean createAsReservedIfNew;
	private IQRouter iqRouter_;
	private boolean joinComplete_;
	private Date joinSince_;
	private boolean joinSucceeded_;
	private MUCRegistry mucRegistry;
	private Map<String, MUCOccupant> occupants = new HashMap<String, MUCOccupant>();
	private JID ownMUCJID = new JID();
	private String password;
	private DirectedPresenceSender presenceSender;
	private StanzaChannel stanzaChannel;
	private boolean unlocking;
	private boolean isUnlocked_;
	private SignalConnection scopedConnection_;
	private Presence joinRequestPresence_ = new Presence();

	/**
	 * Create a MUC Session
	 * @param stanzaChannel stanza channel, not null
	 * @param iqRouter IQ stanza router, not null
	 * @param presenceSender Presence Sender, not null
	 * @param muc JID of the chat room, not null
	 * @param mucRegistry MUC registry, not null
	 * 
	 * @see #disconnect()
	 */
	public MUCImpl(StanzaChannel stanzaChannel, IQRouter iqRouter, 
			DirectedPresenceSender presenceSender, final JID muc, 
			MUCRegistry mucRegistry) {
		ownMUCJID = muc;
		this.stanzaChannel = stanzaChannel;
		this.iqRouter_ = iqRouter;
		this.presenceSender = presenceSender;
		this.mucRegistry = mucRegistry;
		this.createAsReservedIfNew = false;
		this.unlocking = false;
		this.isUnlocked_ = false;
		scopedConnection_ = this.stanzaChannel.onPresenceReceived.connect(
				new Slot1<Presence>() {
			@Override
			public void call(Presence p1) { 
				handleIncomingPresence(p1);
			}            
		});
	}

	/**
	 * Cancel the command for configuring room 
	 */
	@Override
	public void cancelConfigureRoom() {
		MUCOwnerPayload mucPayload = new MUCOwnerPayload();
		mucPayload.setPayload(new Form(Form.Type.CANCEL_TYPE));
		GenericRequest<MUCOwnerPayload> request = new GenericRequest<MUCOwnerPayload>(
				IQ.Type.Set, getJID(), mucPayload, iqRouter_);
		request.send();
	}

	/**
	 * Change the affiliation of the given Jabber ID.
	 * It must be called with the real JID, not the room JID.
	 * @param jid real jabber ID, not null
	 * @param affiliation new affiliation, not null 
	 */
	@Override
	public void changeAffiliation(final JID jid, final MUCOccupant.Affiliation affiliation) {
		final MUCAdminPayload mucPayload = new MUCAdminPayload();
		MUCItem item = new MUCItem();
		item.affiliation = affiliation;
		item.realJID = jid.toBare();
		mucPayload.addItem(item);
		GenericRequest<MUCAdminPayload> request = new GenericRequest<MUCAdminPayload>(
				IQ.Type.Set, getJID(), mucPayload, iqRouter_);
		request.onResponse.connect(new Slot2<MUCAdminPayload, ErrorPayload>() {
			@Override
			public void call(MUCAdminPayload p1, ErrorPayload p2) {
				handleAffiliationChangeResponse(p1,p2,jid,affiliation);
			}
		});
		request.send();
	}

	/**
	 * Change the role of the specified occupant. It must be
	 * called with the room JID, not the real JID. 
	 * @param jid Jabber ID of the occupant in the chat room, not null
	 * @param role new role, not null
	 */
	@Override
	public void changeOccupantRole(final JID jid, final MUCOccupant.Role role) {
		final MUCAdminPayload mucPayload = new MUCAdminPayload();
		MUCItem item = new MUCItem();
		item.role = role;
		item.nick = jid.getResource();
		mucPayload.addItem(item);
		GenericRequest<MUCAdminPayload> request = new GenericRequest<MUCAdminPayload>(
				IQ.Type.Set, getJID(), mucPayload, iqRouter_);
		request.onResponse.connect(new Slot2<MUCAdminPayload, ErrorPayload>() {
			@Override
			public void call(MUCAdminPayload p1, ErrorPayload p2) {
				handleOccupantRoleChangeResponse(p1,p2,jid,role); 
			} 
		});
		request.send();
	}

	/**
	 * Change the subject of the chat room
	 * @param subject new subject, not null
	 */
	@Override
	public void changeSubject(String subject) {
		Message message = new Message();
		message.setSubject(subject);
		message.setType(Message.Type.Groupchat);
		message.setTo(ownMUCJID.toBare());
		stanzaChannel.sendMessage(message);
	}

	/**
	 * Configure a chat room room
	 * @param form form to be used for configuration, not null
	 */
	@Override
	public void configureRoom(Form form) {
		MUCOwnerPayload mucPayload = new MUCOwnerPayload();
		mucPayload.setPayload(form);
		GenericRequest<MUCOwnerPayload> request = new GenericRequest<MUCOwnerPayload>(
				IQ.Type.Set, getJID(), mucPayload, iqRouter_);
		if (unlocking) {
			request.onResponse.connect(new Slot2<MUCOwnerPayload, ErrorPayload>() {
				@Override
				public void call(MUCOwnerPayload p1, ErrorPayload p2) {
					handleCreationConfigResponse(p1, p2);
				}
			});
		}else {
			request.onResponse.connect(new Slot2<MUCOwnerPayload, ErrorPayload>() {
				@Override
				public void call(MUCOwnerPayload p1, ErrorPayload p2) {
					handleConfigurationResultReceived(p1, p2);
				}
			});
		}
		request.send();
	}

	/**
	 * Destroy the chat room 
	 */
	@Override
	public void destroyRoom() {
		MUCOwnerPayload mucPayload = new MUCOwnerPayload();
		MUCDestroyPayload mucDestroyPayload = new MUCDestroyPayload();
		mucPayload.setPayload(mucDestroyPayload);
		GenericRequest<MUCOwnerPayload> request = new GenericRequest<MUCOwnerPayload>(
				IQ.Type.Set, getJID(), mucPayload, iqRouter_);
		request.onResponse.connect(new Slot2<MUCOwnerPayload, ErrorPayload>() {
			@Override
			public void call(MUCOwnerPayload p1, ErrorPayload p2) {
				handleConfigurationResultReceived(p1,p2);
			}
		});
		request.send();
	}

	/**
	 * Returns the (bare) JID of the MUC.
	 * @return bare JID(i.e. without resource)
	 */
	@Override
	public JID getJID() {
		return ownMUCJID.toBare();
	}

	/**
	* Returns if the room is unlocked and other people can join the room.
	* @return True if joinable by others; false otherwise.
	*/
	@Override
	public boolean isUnlocked() {
		return isUnlocked_;
	}

	/**
	 * Get the MUC occupant with the given nick name
	 * @param nick nick name, not null
	 * @return MUC occupant if it exists or null if not
	 */
	@Override
	public MUCOccupant getOccupant(String nick) {
		return occupants.get(nick);
	}

	/**
	 * Determine if the room contains occupant with given nick
	 * @param nick given nick
	 * @return true if the occupant exists, false otherwise
	 */
	@Override
	public boolean hasOccupant(String nick) {
		return occupants.containsKey(nick);
	}

	@Override
	public Map<String, MUCOccupant> getOccupants() {
		return occupants;
	}

	/**
	 * Invite the person with give JID to the chat room
	 * @param person jabber ID o the person to invite,not nul
	 */
	@Override
	public void invitePerson(JID person) {
		invitePerson(person, "", false, false);
	}
	
	/**
	 * Send an invite for the person to join the MUC 
	 * @param person jabber ID of the person to invite, not null
	 * @param reason join reason, not null
	 * @param isImpromptu 
	 */
	@Override
	public void invitePerson(JID person, String reason, boolean isImpromptu) {
		invitePerson(person, reason, isImpromptu, false);
	}


	/**
	 * Send an invite for the person to join the MUC 
	 * @param person jabber ID of the person to invite, not null
	 * @param reason join reason, not null
	 * @param isImpromptu 
	 * @param isReuseChat 
	 */
	@Override
	public void invitePerson(JID person, String reason, boolean isImpromptu, boolean isReuseChat) {
		Message message = new Message();
		message.setTo(person);
		message.setType(Message.Type.Normal);
		MUCInvitationPayload invite = new MUCInvitationPayload();
		invite.setReason(reason);
		invite.setIsImpromptu(isImpromptu);
		invite.setIsContinuation(isReuseChat);
		invite.setJID(ownMUCJID.toBare());
		message.addPayload(invite);
		stanzaChannel.sendMessage(message);
	}

	/**
	 * Join the MUC with default context.
	 * @param nick nick name of the user, not null
	 */
	@Override
	public void joinAs(String nick) {
		joinSince_ = null;
		internalJoin(nick);
	}

	/**
	 * Join the MUC with context since date.
	 * @param nick nick name, not null
	 * @param since  date since the nick joined, not null
	 */
	@Override
	public void joinWithContextSince(String nick, Date since) { 
		joinSince_ = since;
		internalJoin(nick);
	}

	/**
	 * Kick the given occupant out of the chat room
	 * @param jid jabber ID of the user to kick, not null
	 */
	@Override
	public void kickOccupant(JID jid) {
		changeOccupantRole(jid, MUCOccupant.Role.NoRole);
	}    

	@Override
	public void changeNickname(final String newNickname) {
		Presence changeNicknamePresence = new Presence();
		changeNicknamePresence.setTo(new JID(ownMUCJID.toBare().toString() + "/" + newNickname));
		presenceSender.sendPresence(changeNicknamePresence);
	}

	/**
	 * Leave the chat room 
	 */
	@Override
	public void part() {
		presenceSender.removeDirectedPresenceReceiver(ownMUCJID, 
				DirectedPresenceSender.SendPresence.AndSendPresence);
		mucRegistry.removeMUC(getJID());
	}

	/**
	 * Send a request to get a list of users for the given affiliation
	 * @param affiliation affiliation, not null
	 */
	@Override
	public void requestAffiliationList(final MUCOccupant.Affiliation affiliation) {
		MUCAdminPayload mucPayload = new MUCAdminPayload();
		MUCItem item = new  MUCItem();
		item.affiliation = affiliation;
		mucPayload.addItem(item);
		GenericRequest<MUCAdminPayload> request = new GenericRequest<MUCAdminPayload>(
				IQ.Type.Get, getJID(), mucPayload, iqRouter_);
		request.onResponse.connect(new Slot2<MUCAdminPayload, ErrorPayload>() {
			@Override
			public void call(MUCAdminPayload p1, ErrorPayload p2) {
				handleAffiliationListResponse(p1,p2,affiliation);
			} 
		});
		request.send();
	}

	/**
	 * Send a request for getting form for configuring a room 
	 */
	@Override
	public void requestConfigurationForm() {
		MUCOwnerPayload mucPayload = new MUCOwnerPayload();
		GenericRequest<MUCOwnerPayload> request = new GenericRequest<MUCOwnerPayload>(
				IQ.Type.Get, getJID(), mucPayload, iqRouter_);
		request.onResponse.connect(new Slot2<MUCOwnerPayload, ErrorPayload>() {
			@Override
			public void call(MUCOwnerPayload p1, ErrorPayload p2) {
				handleConfigurationFormReceived(p1,p2);
			} 
		});
		request.send();
	}

	/**
	 * Set the reserved status of room to true.
	 * By default a new room with the default configuration is created. 
	 * The effect of calling this function is to leave the room in reserved state
	 * but not configured so that it can be configured later.
	 */
	@Override
	public void setCreateAsReservedIfNew() {
		createAsReservedIfNew = true;
	}

	/**
	 * Set the password used for entering the room.
	 * @param newPassword password, can be null
	 */
	@Override
	public void setPassword(String newPassword) {
		password = newPassword;
	}

	/**
	 * Get the nick name of the MUC room
	 * @return nick name, can be null
	 */
	private String getOwnNick()  {
		return ownMUCJID.getResource();
	}

	private void handleAffiliationChangeResponse(MUCAdminPayload ref, 
			ErrorPayload error, JID jid, MUCOccupant.Affiliation affiliation) {
		if (error != null) {
			onAffiliationChangeFailed.emit(error, jid, affiliation);
		}
	}

	private void handleAffiliationListResponse(MUCAdminPayload payload, 
			ErrorPayload error, MUCOccupant.Affiliation affiliation) {
		if (error != null) {
			onAffiliationListFailed.emit(error);
		} else {                    
			Vector<JID> jids = new Vector<JID>();
			for (MUCItem item : payload.getItems()) {
				if (item.realJID != null) {
					jids.add(item.realJID);
				}
			}
			onAffiliationListReceived.emit(affiliation, jids);
		}
	}

	private void handleConfigurationFormReceived(MUCOwnerPayload payload, 
			ErrorPayload error) {
		Form form = null;
		if (payload != null) {
			form = payload.getForm();
		}
		if (error != null || form == null) {
			onConfigurationFailed.emit(error);
		} else {
			onConfigurationFormReceived.emit(form);
		}
	}

	private void handleConfigurationResultReceived(
			MUCOwnerPayload payload, ErrorPayload error) {
		if (error != null) {
			onConfigurationFailed.emit(error);
		}
	}

	private void handleCreationConfigResponse(MUCOwnerPayload ref , ErrorPayload error) {
		unlocking = false;
		if (error != null) {
			presenceSender.removeDirectedPresenceReceiver(ownMUCJID, 
					DirectedPresenceSender.SendPresence.AndSendPresence);
			onJoinFailed.emit(error);
		} else {
			onJoinComplete.emit(getOwnNick()); /* Previously, this wasn't needed here, 
			as the presence duplication bug caused an emit elsewhere. */
			isUnlocked_ = true;
			onUnlocked.emit();
		}
	}

	private void handleIncomingPresence(Presence presence) {
		if (!isFromMUC(presence.getFrom())) {
			return;
		}

		MUCUserPayload mucPayload = null;

		MUCUserPayload dummyUserPayload = new MUCUserPayload();
		for (MUCUserPayload payload : presence.getPayloads(dummyUserPayload)) {
			if (!payload.getItems().isEmpty() || !payload.getStatusCodes().isEmpty()) {
				mucPayload = payload;
			}
		}

		// On the first incoming presence, check if our join has succeeded
		// (i.e. we start getting non-error presence from the MUC) or not
		if (!joinSucceeded_) {
			if(presence.getType().equals(Presence.Type.Error)) {
				onJoinFailed.emit(presence.getPayload(new ErrorPayload()));
				return;
			}
			else {
				joinSucceeded_ = true;
				presenceSender.addDirectedPresenceReceiver(ownMUCJID, 
						DirectedPresenceSender.SendPresence.DontSendPresence);
				if ((presenceSender.getLastSentUndirectedPresence() != null) && !(isEqualExceptID(presenceSender.getLastSentUndirectedPresence(), joinRequestPresence_))) {
					// our presence changed between join request and join complete, send current presence to MUC
					Presence latestPresence = new Presence(presenceSender.getLastSentUndirectedPresence());
					latestPresence.setTo(ownMUCJID);
					presenceSender.sendPresence(latestPresence);
				}
			}
		}

		String nick = presence.getFrom().getResource();
		if (nick == null || nick.isEmpty()) {
			return;
		}
		MUCOccupant.Role role = MUCOccupant.Role.NoRole;
		MUCOccupant.Affiliation affiliation= MUCOccupant.Affiliation.NoAffiliation;
		JID realJID = null;
		if (mucPayload != null && mucPayload.getItems().size() > 0) {
			role = mucPayload.getItems().get(0).role != null 
			? mucPayload.getItems().get(0).role : MUCOccupant.Role.NoRole;
			affiliation = mucPayload.getItems().get(0).affiliation != null 
			? mucPayload.getItems().get(0).affiliation : MUCOccupant.Affiliation.NoAffiliation;
			realJID = mucPayload.getItems().get(0).realJID;
		}

		//100 is non-anonymous
		//TODO: 100 may also be specified in a <message/>
		//170 is room logging to http
		//TODO: Nick changes

		if (presence.getType().equals(Presence.Type.Unavailable)) {
			LeavingType type = LeavingType.LeavePart;
			String newNickname = null;            
			if (mucPayload != null) {
				if (mucPayload.getPayload() instanceof MUCDestroyPayload) {
					type = LeavingType.LeaveDestroy;
				} else for (MUCUserPayload.StatusCode status : mucPayload.getStatusCodes()) {
					if (status.code == 307) {
						type = LeavingType.LeaveKick;
					} else if (status.code == 301) {
						type = LeavingType.LeaveBan;
					} else if (status.code == 321) {
						type = LeavingType.LeaveNotMember;
					} else if (status.code == 303) {
						if (mucPayload.getItems().size() == 1) {
							newNickname = mucPayload.getItems().get(0).nick;
						}
					}
				}
			}

			if (newNickname != null) {
				if (occupants.containsKey(nick)) {
					MUCOccupant occupant = occupants.get(nick);
					occupants.remove(nick);
					occupant.setNick(newNickname);
					occupants.put(newNickname, occupant);
					onOccupantNicknameChanged.emit(nick, newNickname);
				}
			}
			else {
				if (presence.getFrom().equals(ownMUCJID)) {
					handleUserLeft(type);
					return;
				} else {
					if (occupants.containsKey(nick)) {
						//TODO: part type
						MUCOccupant occupant = occupants.get(nick);
						occupants.remove(nick);
						onOccupantLeft.emit(occupant, type, "");
					}
				}
			}
		} 
		else if (presence.getType().equals(Presence.Type.Available)) {
			MUCOccupant occupant = new MUCOccupant(nick, role, affiliation);
			boolean isJoin = true;
			if (realJID != null) {
				occupant.setRealJID(realJID);
			}
			if (occupants.containsKey(nick)) {
				isJoin = false;
				MUCOccupant oldOccupant = occupants.get(nick);
				if (!oldOccupant.getRole().equals(role)) {
					onOccupantRoleChanged.emit(nick, occupant, oldOccupant.getRole());
				}
				if (!oldOccupant.getAffiliation().equals(affiliation)) {
					onOccupantAffiliationChanged.emit(nick, affiliation, oldOccupant.getAffiliation());
				}
				occupants.remove(nick);
			}
			occupants.put(nick, occupant);

			if (isJoin) {
				onOccupantJoined.emit(occupant);
			}
			onOccupantPresenceChange.emit(presence);
		}

		if (mucPayload != null && !joinComplete_) {
			boolean isLocked = false;            
			for (MUCUserPayload.StatusCode status : mucPayload.getStatusCodes()) {
				if(status.code == 110) {
					/* Simply knowing this is your presence is enough, 210 doesn't seem to be necessary. */
					joinComplete_ = true;
					if (!ownMUCJID.equals(presence.getFrom())) {
						presenceSender.removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender.SendPresence.DontSendPresence);
						ownMUCJID = presence.getFrom();
						presenceSender.addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender.SendPresence.AndSendPresence);
					}
				}
				if (status.code == 201) {
					isLocked = true;                    
					/* Room is created and locked */
					/* Currently deal with this by making an instant room */
					if (!ownMUCJID.equals(presence.getFrom())) {
						presenceSender.removeDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender.SendPresence.DontSendPresence);
						ownMUCJID = presence.getFrom();
						presenceSender.addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender.SendPresence.AndSendPresence);
					}
					if (createAsReservedIfNew) {
						unlocking = true;
						requestConfigurationForm();
					} else {
						// Accept default room configuration and create an instant room http://xmpp.org/extensions/xep-0045.html#createroom-instant
						MUCOwnerPayload mucOwnerPayload = new MUCOwnerPayload();
						presenceSender.addDirectedPresenceReceiver(ownMUCJID, DirectedPresenceSender.SendPresence.DontSendPresence);
						mucOwnerPayload.setPayload(new Form(Form.Type.SUBMIT_TYPE));
						GenericRequest<MUCOwnerPayload> request = new GenericRequest<MUCOwnerPayload>(IQ.Type.Set, 
								getJID(), mucOwnerPayload, iqRouter_);
						request.onResponse.connect(new Slot2<MUCOwnerPayload, ErrorPayload>() {
							@Override
							public void call(MUCOwnerPayload p1,ErrorPayload p2) {
								handleCreationConfigResponse(p1,p2);

							}
						});
						request.send();
					}
				}
			}
			if (joinComplete_ && !isLocked) {
				onJoinComplete.emit(getOwnNick());
			}
			if (!isLocked && !isUnlocked_ && (presence.getFrom().equals(ownMUCJID))) {
				isUnlocked_ = true;
				onUnlocked.emit();
			}
		}
	}

	private void handleOccupantRoleChangeResponse(MUCAdminPayload ref , ErrorPayload error, JID jid, MUCOccupant.Role role) {
		if (error != null) {
			onRoleChangeFailed.emit(error, jid, role);
		}
	}

	/**
	 * This function compares two Presence elements for equality based on to, from, status, show and entity capability information.
	 * @return True if equal; else otherwise.
	 */
	private static boolean isEqualExceptID(final Presence lhs, final Presence rhs) {
		boolean isEqual = false;
		if(lhs == null || rhs == null) {
			isEqual = (lhs == null && rhs == null);
		}
		else if (lhs.getFrom().equals(rhs.getFrom()) && lhs.getTo().equals(rhs.getTo()) && lhs.getStatus().equals(rhs.getStatus()) && lhs.getShow().equals(rhs.getShow())) {
			CapsInfo lhsCaps = lhs.getPayload(new CapsInfo());
			CapsInfo rhsCaps = rhs.getPayload(new CapsInfo());

			if (lhsCaps != null && rhsCaps != null) {
				isEqual = (lhsCaps.equals(rhsCaps));
			}
			else {
				isEqual = (lhsCaps == null && rhsCaps == null);
			}
		}
		return isEqual;
	}

	private void internalJoin(String nick) {
		//TODO: history request
		joinComplete_ = false;
		joinSucceeded_ = false;

		mucRegistry.addMUC(getJID());

		ownMUCJID = new JID(ownMUCJID.getNode(), ownMUCJID.getDomain(), nick);
		Presence joinPresence = (presenceSender.getLastSentUndirectedPresence() != null ? new Presence(presenceSender.getLastSentUndirectedPresence()) : new Presence());
		if(joinPresence.getType() != Presence.Type.Available) {
			throw new RuntimeException("From[" + joinPresence.getFrom() + "] and" +
					" To[" + joinPresence.getTo() + "] is not available");   
		}
		joinPresence.setTo(ownMUCJID);
		MUCPayload mucPayload = new MUCPayload();
		if (joinSince_ != null) {
			mucPayload.setSince(joinSince_);
		}
		if (password != null) {
			mucPayload.setPassword(password);
		}
		joinPresence.addPayload(mucPayload);
		joinRequestPresence_ = joinPresence;
		presenceSender.sendPresence(joinPresence);
	}


	private boolean isFromMUC(final JID j) {
		return ownMUCJID.compare(j, CompareType.WithoutResource) == 0;
	}

	@Override
	public void handleUserLeft(LeavingType type) {
		String resource = ownMUCJID.getResource();
		if (occupants.containsKey(resource)) {
			MUCOccupant me = occupants.get(resource);
			occupants.remove(resource);
			onOccupantLeft.emit(me, type, "");
		}
		occupants.clear();
		joinComplete_ = false;
		joinSucceeded_ = false;
		isUnlocked_ = false;        
		presenceSender.removeDirectedPresenceReceiver(ownMUCJID, 
				DirectedPresenceSender.SendPresence.DontSendPresence);
	}
	
	/**
	 * Disconnect signals for this MUC.
	 * This method should be called when the MUC object is no longer in use
	 * so as to enable the garbage collector to remove this object from used space. 
	 */
	@Override
	public void disconnect() {
		if (scopedConnection_ != null) {
			scopedConnection_.disconnect();
			scopedConnection_ = null;
		}
	}

	protected void finalize() throws Throwable {
		try {
		  	disconnect();
	   	}
	   	finally {
			super.finalize();
	   	}
	}

	//TODO: Invites(direct/mediated)

	//TODO: requesting membership

	//TODO: get member list

	//TODO: request voice

	//TODO: moderator use cases

	//TODO: Admin use cases

	//TODO: Owner use cases

}