use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.
the class BroadcastPlugin method userToUserBroadcast.
/**
* Handles Broadcasts made from a user to another user
*
* @param message
* the message
* @param type
* the message type
* @param from
* the sender
*/
private void userToUserBroadcast(Message message, Type type, String from) {
String jid = XmppStringUtils.parseBareJid(from);
String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
ChatManager chatManager = SparkManager.getChatManager();
ChatContainer container = chatManager.getChatContainer();
ChatRoomImpl chatRoom;
try {
chatRoom = (ChatRoomImpl) container.getChatRoom(jid);
} catch (ChatRoomNotFoundException e) {
chatRoom = new ChatRoomImpl(jid, nickname, nickname);
SparkManager.getChatManager().getChatContainer().addChatRoom(chatRoom);
}
Message m = new Message();
m.setBody(message.getBody());
m.setTo(message.getTo());
String broadcasttype = type == Message.Type.normal ? Res.getString("broadcast") : Res.getString("message.alert.notify");
// m.setFrom(name +" "+broadcasttype);
m.setFrom(nickname + " - " + broadcasttype);
chatRoom.getTranscriptWindow().insertMessage(m.getFrom(), message, ChatManager.FROM_COLOR);
chatRoom.addToTranscript(m, true);
chatRoom.increaseUnreadMessageCount();
broadcastRooms.add(chatRoom);
LocalPreferences pref = SettingsManager.getLocalPreferences();
if (pref.getShowToasterPopup()) {
SparkToaster toaster = new SparkToaster();
toaster.setDisplayTime(30000);
toaster.setBorder(BorderFactory.createBevelBorder(0));
toaster.setTitle(nickname + " - " + broadcasttype);
toaster.showToaster(message.getBody());
}
SparkManager.getChatManager().fireGlobalMessageReceievedListeners(chatRoom, message);
DelayInformation inf = message.getExtension("delay", "urn:xmpp:delay");
if (inf == null) {
SoundPreference soundPreference = (SoundPreference) SparkManager.getPreferenceManager().getPreference(new SoundPreference().getNamespace());
SoundPreferences preferences = soundPreference.getPreferences();
if (preferences.isPlayIncomingSound()) {
File incomingFile = new File(preferences.getIncomingSound());
SparkManager.getSoundManager().playClip(incomingFile);
}
}
chatRoom.addMessageListener(new MessageListener() {
boolean waiting = true;
public void messageReceived(ChatRoom room, Message message) {
removeAsBroadcast(room);
}
public void messageSent(ChatRoom room, Message message) {
removeAsBroadcast(room);
}
private void removeAsBroadcast(ChatRoom room) {
if (waiting) {
broadcastRooms.remove(room);
// Notify decorators
SparkManager.getChatManager().notifySparkTabHandlers(room);
waiting = false;
}
}
});
}
use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.
the class GatewayPlugin method registerPresenceListener.
private void registerPresenceListener() {
StanzaFilter orFilter = new OrFilter(new StanzaTypeFilter(Presence.class), new StanzaTypeFilter(Message.class));
SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
if (stanza instanceof Presence) {
Presence presence = (Presence) stanza;
Transport transport = TransportUtils.getTransport(stanza.getFrom());
if (transport != null) {
boolean registered = true;
if (presence.getType() == Presence.Type.unavailable) {
registered = false;
}
GatewayItem button = uiMap.get(transport);
button.signedIn(registered);
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
transferTab.revalidate();
transferTab.repaint();
return 41;
}
};
worker.start();
}
} else if (stanza instanceof Message) {
Message message = (Message) stanza;
String from = message.getFrom();
boolean hasError = message.getType() == Message.Type.error;
String body = message.getBody();
if (from != null && hasError) {
Transport transport = TransportUtils.getTransport(from);
if (transport != null) {
String title = "Alert from " + transport.getName();
// Show error
MessageDialog.showAlert(body, title, "Information", SparkRes.getImageIcon(SparkRes.INFORMATION_IMAGE));
}
}
}
}, orFilter);
ChatManager chatManager = SparkManager.getChatManager();
chatManager.addContactItemHandler(this);
// Iterate through Contacts and check for
final ContactList contactList = SparkManager.getWorkspace().getContactList();
for (ContactGroup contactGroup : contactList.getContactGroups()) {
for (ContactItem contactItem : contactGroup.getContactItems()) {
Presence presence = contactItem.getPresence();
if (presence.isAvailable()) {
String domain = XmppStringUtils.parseDomain(presence.getFrom());
Transport transport = TransportUtils.getTransport(domain);
if (transport != null) {
handlePresence(contactItem, presence);
contactGroup.fireContactGroupUpdated();
}
}
}
}
SparkManager.getSessionManager().addPresenceListener(presence -> {
for (Transport transport : TransportUtils.getTransports()) {
GatewayItem button = uiMap.get(transport);
if (button.isLoggedIn()) {
if (!presence.isAvailable()) {
return;
}
// Create new presence
Presence p = new Presence(presence.getType(), presence.getStatus(), presence.getPriority(), presence.getMode());
p.setTo(transport.getServiceName());
try {
SparkManager.getConnection().sendStanza(p);
} catch (SmackException.NotConnectedException e) {
Log.warning("Unable to forward presence change to transport.", e);
}
}
}
});
}
use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.
the class UriManager method handleMessage.
/**
* handles the ?message URI
*
* @param uri
* the decoded uri
*/
public void handleMessage(URI uri) {
String query = uri.getQuery();
int bodyIndex = query.indexOf("body=");
String jid = retrieveJID(uri);
String body = null;
// Find body
if (bodyIndex != -1) {
body = query.substring(bodyIndex + 5);
}
body = org.jivesoftware.spark.util.StringUtils.unescapeFromXML(body);
UserManager userManager = SparkManager.getUserManager();
String nickname = userManager.getUserNicknameFromJID(jid);
if (nickname == null) {
nickname = jid;
}
ChatManager chatManager = SparkManager.getChatManager();
ChatRoom chatRoom = chatManager.createChatRoom(jid, nickname, nickname);
if (body != null) {
Message message = new Message();
message.setBody(body);
chatRoom.sendMessage(message);
}
chatManager.getChatContainer().activateChatRoom(chatRoom);
}
use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.
the class ConferenceUtils method createPrivateConference.
/**
* Creates a private conference.
*
* @param serviceName the service name to use for the private conference.
* @param message the message sent to each individual invitee.
* @param roomName the name of the room to create.
* @param jids a collection of the user JIDs to invite.
* @throws SmackException thrown if an error occurs during room creation.
*/
public static void createPrivateConference(String serviceName, String message, String roomName, Collection<String> jids) throws SmackException {
final String roomJID = XmppStringUtils.escapeLocalpart(roomName) + "@" + serviceName;
final MultiUserChat multiUserChat = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getMultiUserChat(roomJID);
final LocalPreferences pref = SettingsManager.getLocalPreferences();
final GroupChatRoom room = UIComponentRegistry.createGroupChatRoom(multiUserChat);
try {
// Attempt to create room.
multiUserChat.create(pref.getNickname());
} catch (XMPPException | SmackException e) {
throw new SmackException(e);
}
try {
// Since this is a private room, make the room not public and set user as owner of the room.
Form submitForm = multiUserChat.getConfigurationForm().createAnswerForm();
submitForm.setAnswer("muc#roomconfig_publicroom", false);
submitForm.setAnswer("muc#roomconfig_roomname", roomName);
final List<String> owners = new ArrayList<>();
owners.add(SparkManager.getSessionManager().getBareAddress());
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
multiUserChat.sendConfigurationForm(submitForm);
} catch (XMPPException | SmackException e1) {
Log.error("Unable to send conference room chat configuration form.", e1);
}
ChatManager chatManager = SparkManager.getChatManager();
// Check if room already is open
try {
chatManager.getChatContainer().getChatRoom(room.getRoomname());
} catch (ChatRoomNotFoundException e) {
chatManager.getChatContainer().addChatRoom(room);
chatManager.getChatContainer().activateChatRoom(room);
}
for (String jid : jids) {
multiUserChat.invite(jid, message);
room.getTranscriptWindow().insertNotificationMessage(Res.getString("message.waiting.for.user.to.join", jid), ChatManager.NOTIFICATION_COLOR);
}
}
use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.
the class ConversationInvitation method actionPerformed.
public void actionPerformed(ActionEvent actionEvent) {
final Object obj = actionEvent.getSource();
if (obj == joinButton) {
String name = XmppStringUtils.parseLocalpart(roomName);
ConferenceUtils.enterRoomOnSameThread(name, roomName, password);
} else {
try {
MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).decline(roomName, inviter, "No thank you");
} catch (SmackException.NotConnectedException e) {
Log.warning("unable to decline invatation from " + inviter + " to join room " + roomName, e);
}
}
// Close Container
ChatManager chatManager = SparkManager.getChatManager();
chatManager.getChatContainer().closeTab(this);
}
Aggregations