Error while joining MUC room in XMPP(smack) - java

I am trying to create a multi user chat. I am getting error while joining the room.
Method for creating chat room :
public void createMultiUserChatRoom(String roomName, String nickName) {
// Get the MultiUserChatManager
MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
// Get a MultiUserChat using MultiUserChatManager
MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat(roomName+"#conference.localhost");
try {
multiUserChat.create(nickName);
Form form = multiUserChat.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> formFieldList = submitForm.getFields();
for (FormField formField : formFieldList) {
if(!FormField.Type.hidden.equals(formField.getType()) && formField.getVariable() != null) {
submitForm.setDefaultAnswer(formField.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
submitForm.setAnswer("muc#roomconfig_publicroom", true);
multiUserChat.sendConfigurationForm(submitForm);
} catch (Exception e) {
e.printStackTrace();
}
}
Method for joining MUC room :
public void joinMultiUserChatRoom(String userName, String roomName) {
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat multiUserChat = manager.getMultiUserChat(roomName + "#conference.localhost");
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(-1);
try {
multiUserChat.join(userName, "", history, connection.getPacketReplyTimeout());
} catch (Exception e) {
e.printStackTrace();
}
}
Getting list of joined room by user :
public List<String> getJoinedGroupByUserName(String userName) {
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
List<String> joinedRooms = null;
try {
// Get the rooms where user3#host.org has joined
joinedRooms = manager.getJoinedRooms(userName+"#conference.localhost");
} catch (Exception e) {
e.printStackTrace();
}
return joinedRooms;
}
While user join the room i get this message : "This room is locked from entry until configuration is confirmed."

Room it's not really available (confirmed) after sending a configuration, the creator has to join after
multiUserChat.sendConfigurationForm(submitForm);
so basically creator must also
multiUserChat.join(username)
(if you don't need to stay inside, perform a muc.leave() after a join)

Related

How do I save the id from chatid with the telegram api

Hello. I'm trying to solve this problem for a while now. For some
reason I keep getting null pointer exception when I try to save a
object in my repository. Below you can see what happens and my
functions.
java.lang.NullPointerException at com.br.einstein.api.service.ApiTelegram.sendMsg(ApiTelegram.java:104)
at
com.br.einstein.api.service.ApiTelegram.onUpdateReceived(ApiTelegram.java:81)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at
org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
at
org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:317)
public void onUpdateReceived(Update update) {
ApiEinstein api = new ApiEinstein();
try {
JsonObject objSession = api.getSessionDetails();
String message = update.getMessage().getText();
api.sendChatRequest(objSession);
List < String > list = new ApiEinstein().ReadChatDetails(objSession);
sendMsg(update.getMessage().getChatId().toString(), list.toString());
new ApiEinstein().SendChatMessage(objSession, message);
api.syncChatSession(objSession);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void sendMsg(String chatId, String s) {
SendMessage sendMessage = new SendMessage();
// sendMessage.enableMarkdown(true);
sendMessage.setChatId(chatId);
sendMessage.setText(s);
long id = Long.valueOf(chatId);
Telegram telegram = new Telegram();
telegram.setChatId(id);
repository.save(telegram);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
} ```

How to (if possible) get the OpenFire GroupChat History without joined (SmackAPI)

What I'm doing now is:
1- Retrieving all GroupChat
List<HostedRoom> rooms = multiUserChatManager.getHostedRooms(groupChatService.asDomainBareJid());
for (HostedRoom room :
rooms) {
if (room.getJid().getLocalpart().equals(groupChatRoomId)){
multiUserChat = multiUserChatManager.getMultiUserChat(room.getJid());
}
}
2- Join in the GroupChat
if (multiUserChat != null) {
if (!multiUserChat.isJoined()) {
multiUserChat.join(connection.getUser().getResourcepart());
}
}
3- Getting all messages
public List<Message> getOldMessages() {
List<Message> oldMessages = new ArrayList<>();
try {
Message message = multiUserChat.nextMessage();
while (message != null) {
oldMessages.add(message);
message = multiUserChat.nextMessage();
}
} catch (MultiUserChatException.MucNotJoinedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
multiUserChat.addMessageListener(new MessageListener() {
#Override
public void processMessage(final Message message) {
listener.notifyMessage(message.getSubject(), message.getBody());
}
});
return oldMessages;
}
I need to do the same without joined and show my presence for all users into the GroupChat.
In other words my app show you the GroupChat that you previous joined and I want to implement the missed messages notification for all GroupChat in a list.

Smack Api Group not working

I am trying to create Group using Smack Api
I use the following code
It crashes on muc#roomconfig_roomowners tag
If i remove that line, it works fine, Group creates
But no group is assigned to the current user, no one is owner
public void createGroup() {
MultiUserChat chatRoom = MultiUserChatManager.getInstanceFor(mConnection).getMultiUserChat("room719#conference." + MYSITE);
try {
chatRoom.create("room719");
Form form = chatRoom.getConfigurationForm().createAnswerForm();
form.setAnswer("muc#roomconfig_publicroom", true);
form.setAnswer("muc#roomconfig_roomname", "room719");
// List owners = new ArrayList();
// owners.add("currUser#"+MYSITE);
form.setAnswer("muc#roomconfig_roomowners", Arrays.asList("currUser#"+MYSITE));
form.setAnswer("muc#roomconfig_persistentroom", true);
chatRoom.sendConfigurationForm(form);
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
This code is working for me, can you try this?
if( !chatRoom.isJoined() ){
chatRoom.createOrJoin("your nickname");
List<String> owners = new ArrayList<String>();
owners.add("currUser#"+MYSITE);
Form form = chatRoom.getConfigurationForm().createAnswerForm();
form.setAnswer("muc#roomconfig_roomname", "your roomname");
form.setAnswer("muc#roomconfig_roomowners", owners);
form.setAnswer("muc#roomconfig_persistentroom", true);
form.setAnswer("muc#roomconfig_publicroom", true);
chatRoom.sendConfigurationForm(form);
}

XMPP Group chat java - logic to join users in room inside invitation listener is not working

I am trying to create sample java application to implement the MultiUserChat of XMPP. Some how I can able to create user and make it online in openfire. Can any one suggest how to join all the users to the created chatRoom?
Here is my sample code inside the class SampleMultiUserChat Where I invite all the users to join the group but it is not getting joined. What I am missing?
SampleMultiUserChat(){
oConnectionConfiguration = new ConnectionConfiguration("10.10.1.105",5223);
createChatRoom();
}
/**
* #param args
*/
public static void main(String[] args) {
SampleMultiUserChat oSampleMultiUserChat = new SampleMultiUserChat();
for(int i = 2; i < 4; i++){
oSampleMultiUserChat.openXMPPConnection("user"+i);
oSampleMultiUserChat.createAcceptInvitationListener("user"+i);
oSampleMultiUserChat.inviteToJoinRoom("user"+i);
}
Thread mainThread = Thread.currentThread();
while(true){
try {
mainThread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void openXMPPConnection(String user){
XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
try {
oXmppConnection.connect();
oXmppConnection.login(user, "60474c9c10d7142b7508ce7a50acf414");
userConnection.put(user, oXmppConnection);
} catch (XMPPException e) {
System.out.println("Exception occured in login in user : "+user);
}
}
private void createChatRoom(){
XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
try {
oXmppConnection.connect();
oXmppConnection.login("user1", "60474c9c10d7142b7508ce7a50acf414");
myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom#conference.10.10.1.105");
myChattingRoom.create("roomNickName");
myChattingRoom.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
} catch (XMPPException e) {
e.printStackTrace();
}
}
private void inviteToJoinRoom(String user){
myChattingRoom.invite(user+"#10.10.1.105", "Please join my chatting room");
System.out.println("sent invitation by "+user);
}
private void sendMessage(String msg){
try {
myChattingRoom.sendMessage(msg);
} catch (XMPPException e) {
System.out.println("Exception occured while sending msg to chat room"+e);
}
}
private void createAcceptInvitationListener(String user){
MultiUserChat.addInvitationListener(userConnection.get(user), new InvitationListener() {
public void invitationReceived(Connection connection, String room, String inviter,
String reason, String password, Message msg) {
try {
myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("#")));
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
}
Thanks in advance.
I solved my above problem by creating new instance of MultiUserChat.
Here is my edited method 'createAcceptInvitationListener'
private void createAcceptInvitationListener(String user){
System.out.println("inside create accept invitation listener");
final XMPPConnection oXmppConnection = userConnection.get(user);
MultiUserChat.addInvitationListener(oXmppConnection, new InvitationListener() {
public void invitationReceived(Connection connection, String room, String inviter,
String reason, String password, Message msg) {
System.out.println("inside invitation received method");
try {
System.out.println(connection.getUser().substring(0, connection.getUser().indexOf("#")));
MultiUserChat myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom#conference.10.10.1.105");
myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("#")));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception occured while joining the chat room : "+e);
}
}
});
}
private void reservedRoomsCreation(MultiUserChat myChattingRoom) throws XMPPException{
Form form = myChattingRoom.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for(Iterator fields = form.getFields(); fields.hasNext();){
FormField formFields = (FormField) fields.next();
if (!FormField.TYPE_HIDDEN.equals(formFields.getType()) && formFields.getVariable() != null) {
submitForm.setDefaultAnswer(formFields.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
myChattingRoom.sendConfigurationForm(submitForm);
}

Join existing PeerGroup in JXTA/JXSE

I have a problem using JXSE.
Let's say i have a rendezVous peer and an Edge peer, not on the same local network.
The rendezVous peer create a peerGroup "test", and publish an advertisement in this group with the name "test advertisement"
Let's say i'm sure than my EdgePeer is connected to the rendezVous peer. I can find the existing group "test" with netpeerGroup.getRemoteAdvertisements().
But i don't know how to join this existing group. I tried netpeergroup.newGroup(testAdv), with testAdv = the founded peerGroupAdvertisement.
I can't find the Advertisement "test advertisement" in the "test" PeerGroup.
But if i do all thats things locally, it works. Maybe i don't understand the difference on how jxta works locally and over internet.
here the code for creating or joining a group :
public void addGroup(final String name) {
ModuleImplAdvertisement mAdv = null;
PeerGroup group = null;
temp = null;
defaultGroup.getDiscoveryService().getRemoteAdvertisements(null, DiscoveryService.GROUP,
"Name", name, 1, new DiscoveryListener() {
#Override
public void discoveryEvent(DiscoveryEvent event) {
Enumeration<Advertisement> advs = event.getResponse().getAdvertisements();
while(advs.hasMoreElements()) {
System.out.println("groupe found");
PeerGroupAdvertisement adv = (PeerGroupAdvertisement) advs.nextElement();
System.out.println("group name : " + adv.getName());
try {
temp = defaultGroup.newGroup(adv);
System.out.println("group joined");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
try {
Thread.sleep(10000);
System.out.println("waiting for group ...");
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(temp == null) {
try {
System.out.println("creating new group ..");
mAdv = defaultGroup.getAllPurposePeerGroupImplAdvertisement(); /* Getting the advertisement of implemented modules */
temp = defaultGroup.newGroup(generatePeerGroupID(name), mAdv, name, name); /* creating & publishing the group */
getDefaultGroup().getDiscoveryService().remotePublish(temp.getPeerGroupAdvertisement());
} catch (Exception e) {
e.printStackTrace();
}
}
finally found the problem. You had to start the RendezVous service on each groups, and not only the netPeerGroup.
That's why my software works locally but not on internet.

Categories

Resources