public void sendDisplayedReceipt(String receiverJid, String stanzaId, String dialog_id) { Chat chat; if ((chat = privateChats.get(receiverJid)) == null) { chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this); privateChats.put(receiverJid, chat); } Message message = new Message(receiverJid); Displayed read = new Displayed(stanzaId); QuickbloxChatExtension extension = new QuickbloxChatExtension(); extension.setProperty("dialog_id", dialog_id); message.setStanzaId(StanzaIdUtil.newStanzaId()); message.setType(Message.Type.chat); message.addExtension(read); message.addExtension(extension); try { chat.sendMessage(message); } catch (SmackException.NotConnectedException ex) { Logger.logExceptionToFabric(ex); offlineMessages.add(message); } }
public void sendReceivedReceipt(String receiverJid, String stanzaId, String dialog_id) { Chat chat; if ((chat = privateChats.get(receiverJid)) == null) { chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this); privateChats.put(receiverJid, chat); } Message message = new Message(receiverJid); Received delivered = new Received(stanzaId); QuickbloxChatExtension extension = new QuickbloxChatExtension(); extension.setProperty("dialog_id", dialog_id); message.setStanzaId(StanzaIdUtil.newStanzaId()); message.setType(Message.Type.chat); message.addExtension(delivered); message.addExtension(extension); try { chat.sendMessage(message); } catch (SmackException.NotConnectedException ex) { offlineMessages.add(message); } }
public void sendPrivateMessage(String body, String receiverJid, long timestamp, String stanzaId) { Log.d(TAG, "Sending message to : " + receiverJid); Chat chat; if ((chat = privateChats.get(receiverJid)) == null) { chat = ChatManager.getInstanceFor(privateChatConnection).createChat(receiverJid, this); privateChats.put(receiverJid, chat); } QuickbloxChatExtension extension = new QuickbloxChatExtension(); extension.setProperty("date_sent", timestamp + ""); extension.setProperty("save_to_history", "1"); Message message = new Message(receiverJid); message.setStanzaId(stanzaId); message.setBody(body); message.setType(Message.Type.chat); message.addExtension(new Markable()); message.addExtension(extension); try { chat.sendMessage(message); } catch (SmackException.NotConnectedException ex) { offlineMessages.add(message); } }
public void processMessage(Chat chat, Message message) { ExtensionElement extension = message.getExtension(NAMESPACE); if (extension == null) { return; } ChatState state; try { state = ChatState.valueOf(extension.getElementName()); } catch (Exception ex) { return; } fireNewChatState(chat, state); }
@Override public void processMessage(Chat chat, Message message) { if (message.getBody() == null) { return; } if (message.getType() == Message.Type.groupchat) { messageService.get().processPublicMessage(message); return; } messageService.get().processPrivateMessage(message); }
@Override public void chatCreated(Chat chat, boolean createdLocally) { if (!privateChats.containsKey(chat.getParticipant())) { chat.addMessageListener(this); privateChats.put(chat.getParticipant().split("/")[0], chat); } }
void onChatCreated(Chat chatCreated) { if (chat != null) { if (chat.getParticipant().equals(chatCreated.getParticipant())) { chat.removeMessageListener(messageListener); chat = chatCreated; chat.addMessageListener(messageListener); } } else { chat = chatCreated; chat.addMessageListener(messageListener); } }
/** * send the raw string as a message without wrapping it with xml attributes * @param message the message to be sent * @param buddyJID the buddy to send the message to */ public void sendRaw(String message, String buddyJID){ ChatManager chatManager = ChatManager.getInstanceFor(connection); if (connection != null && connection.isConnected() && chatManager != null){ try{ Chat chat = chatManager.createChat(buddyJID); chat.sendMessage(message); }catch (Exception e){ e.printStackTrace(); } } }
/** * sends a text message * * @param message the message text to send * @param buddyJID the Buddy to receive the message * @return true if sending was successful */ public boolean sendTextMessage(String message, String buddyJID, long id){ ChatManager chatManager = ChatManager.getInstanceFor(connection); if (connection != null && connection.isConnected() && chatManager != null){ if (buddyJID.indexOf('@') == -1) buddyJID += "@" + service; Chat chat = chatManager.createChat(buddyJID); try{ // wrap the message with all necessary xml attributes Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element msg = doc.createElement("message"); doc.appendChild(msg); msg.setAttribute("type", MessageHistory.TYPE_TEXT); msg.setAttribute("id", String.valueOf(id)); Element file = doc.createElement("content"); msg.appendChild(file); file.setTextContent(message); // transform everything to a string Transformer t = TransformerFactory.newInstance().newTransformer(); StringWriter writer = new StringWriter(); StreamResult r = new StreamResult(writer); t.transform(new DOMSource(doc), r); message = writer.toString(); // send the message chat.sendMessage(message); Log.d("DEBUG", "Success: Sent message"); return true; }catch (Exception e){ Log.e("ERROR", "Couldn't send message."); Log.e("ERROR", e.toString()); return false; } } Log.e("ERROR", "Sending failed: No connection."); return false; }
/** * send an acknowledgement * @param buddyId the buddyId to receive the acknowledgement * @param othersId the id the buddy has sent the message with * @param type the type of acknowledgement to send * @return true if sending was successful */ public boolean sendAcknowledgement(String buddyId, long othersId, String type){ ChatManager chatManager = ChatManager.getInstanceFor(connection); if (connection != null && connection.isConnected() && chatManager != null){ if (buddyId.indexOf('@') == -1) buddyId += "@" + service; Chat chat = chatManager.createChat(buddyId); try{ // create the message structure Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element ack = doc.createElement("acknowledgement"); doc.appendChild(ack); ack.setAttribute("id", String.valueOf(othersId)); ack.setAttribute("type", type); // create the string representation of the message Transformer t = TransformerFactory.newInstance().newTransformer(); StringWriter writer = new StringWriter(); StreamResult r = new StreamResult(writer); t.transform(new DOMSource(doc), r); String message = writer.toString(); // send the message chat.sendMessage(message); Log.d("DEBUG", "Success: Sent message"); return true; }catch (Exception e){ Log.e("ERROR", "Couldn't send message."); Log.e("ERROR", e.toString()); return false; } } Log.e("ERROR", "Sending failed: No connection."); return false; }
@Override public void processMessage(Chat chat, Message message) { if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) { if (message.getBody() != null) { saveMessage(message.getFrom(), message.getBody(), true); } } }
/** * Sets the current state of the provided chat. This method will send an empty bodied Message * stanza(/packet) with the state attached as a {@link org.jivesoftware.smack.packet.ExtensionElement}, if * and only if the new chat state is different than the last state. * * @param newState the new state of the chat * @param chat the chat. * @throws NotConnectedException */ public void setCurrentState(ChatState newState, Chat chat) throws NotConnectedException { if(chat == null || newState == null) { throw new IllegalArgumentException("Arguments cannot be null."); } if(!updateChatState(chat, newState)) { return; } Message message = new Message(); ChatStateExtension extension = new ChatStateExtension(newState); message.addExtension(extension); chat.sendMessage(message); }
private synchronized boolean updateChatState(Chat chat, ChatState newState) { ChatState lastChatState = chatStates.get(chat); if (lastChatState != newState) { chatStates.put(chat, newState); return true; } return false; }
private void fireNewChatState(Chat chat, ChatState state) { for (ChatMessageListener listener : chat.getListeners()) { if (listener instanceof ChatStateListener) { ((ChatStateListener) listener).stateChanged(chat, state); } } }
@Override public void processMessage(Message message) { Chat chat = chatManager.getThreadChat(message.getThread()); if (chat == null) { return; } if (updateChatState(chat, ChatState.active)) { message.addExtension(new ChatStateExtension(ChatState.active)); } }
private void sendChatState (String to, ChatState currentChatState) { try { if (mConnection != null && mConnection.isConnected()) { // findOrCreateSession(to, false); Chat thisChat = mChatManager.createChat(JidCreate.from(to).asEntityJidIfPossible()); ChatStateManager.getInstance(mConnection).setCurrentState(currentChatState, thisChat); } } catch (Exception e) { Log.w(ImApp.LOG_TAG,"error sending chat state: " + e.getMessage()); } }
@Override public void sendMessage(TxMessage message) { if(message.getDestination().getType().equals(Resource.Type.ROOM)){ Room room = getRoom(message.getDestination().getAddress()); if(room != null) { room.sendMessage(message); } }else{ XmppRxMessage origin = (XmppRxMessage)message.getRequest(); Chat chat = ChatManager.getInstanceFor(connection).getThreadChat(origin.getThread()); if(chat == null){ logger.trace("chat was null, creating a new chat instance"); chat = ChatManager.getInstanceFor(connection).createChat(message.getDestination().getAddress(),origin.getThread(),null); }else{ logger.trace("an existing chat was found for thread id {}", (origin.getThread())); } try { Message msg = MessageHelper.createXmppMessage(message); msg.addExtension(new DeliveryReceipt(origin.getId())); chat.sendMessage(msg); } catch (SmackException.NotConnectedException e) { logger.warn("Trying to send a message on XMPP binding while connection is closed",e); } } }
public XmppMsg(Chat chat, Message msg) { this.from = chat.getParticipant(); this.msg = msg.getBody(); Message.Type t = msg.getType(); if (t != null) { this.type = msg.getType().toString(); } stanzaId = msg.getStanzaId(); }
/** * sends an image message * * @param serverFile the file on the server * @param description the description of the sent image * @param buddyJID the Buddy to receive the message * @return true if sending was successful */ public boolean sendImageMessage(String serverFile, String description, String buddyJID, long id){ if (buddyJID.indexOf('@') == -1) buddyJID += "@" + service; ChatManager chatManager = ChatManager.getInstanceFor(connection); if (connection != null && connection.isConnected() && chatManager != null){ Chat chat = chatManager.createChat(buddyJID); try{ //generate the message in order to set the type to image Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element msg = doc.createElement("message"); doc.appendChild(msg); msg.setAttribute("type", MessageHistory.TYPE_IMAGE); msg.setAttribute("id", String.valueOf(id)); Element file = doc.createElement("file"); msg.appendChild(file); file.setTextContent(serverFile); Element desc = doc.createElement("description"); msg.appendChild(desc); desc.setTextContent(description); // create the string Transformer t = TransformerFactory.newInstance().newTransformer(); StringWriter writer = new StringWriter(); StreamResult r = new StreamResult(writer); t.transform(new DOMSource(doc), r); String message = writer.toString(); // send the message chat.sendMessage(message); Log.d("DEBUG", "Success: Sent message"); return true; }catch (Exception e){ Log.e("ERROR", "Couldn't send message."); Log.e("ERROR", e.toString()); return false; } } Log.e("ERROR", "Sending failed: No connection."); return false; }
@Override public void processMessage(Chat chat, Message message){ processMessages(message); }
@Override public void chatCreated(Chat chat, boolean createdLocally){ Log.d("CHAT MANAGER", "created a new chat"); if (chat.getListeners().isEmpty()) chat.addMessageListener(new MyChatMessageListener()); }
private void sendMessage(String destinationJID, String message) throws SmackException.NotConnectedException { Logger.debug(TAG, "Sending message to " + destinationJID); Chat chat = ChatManager.getInstanceFor(mConnection).createChat(destinationJID, this); chat.sendMessage(message); }
@Override public void chatCreated(Chat chat, boolean createdLocally) { Logger.debug(TAG, "chat created"); chat.addMessageListener(this); }
public void chatCreated(final Chat chat, boolean createdLocally) { chat.addMessageListener(this); }
@Override public void sendMessage(String userJID, String text) throws NotConnectedException { errorIfClosed(); Chat chat = delegate.createChat(userJID); chat.sendMessage(text); }
@Override public void sendMessage(String userJID, Message message) throws NotConnectedException { errorIfClosed(); Chat chat = delegate.createChat(userJID); chat.sendMessage(message); }
public void chatCreated(Chat chat, boolean locallyCreated) { // test if locallyCreated if (!locallyCreated) { chat.addMessageListener(this); } }
/** * Process received messages */ public void processMessage(Chat chat, Message message) { XmppMsg xmppMsg = new XmppMsg(chat, message); invoke("publishXmppMsg", xmppMsg); Message.Type type = message.getType(); String participant = chat.getParticipant(); String body = message.getBody(); log.info("message of type {} from user {} - {}", type, participant, body); if (type == Message.Type.chat) { if (body.startsWith("/")) { // String pathInfo = String.format("/%s/service%s", // CodecUtils.PREFIX_API, body); FIXME - wow that was horrific String pathInfo = String.format("/%s%s", Api.PREFIX_API, body); try { org.myrobotlab.framework.Message msg = CodecUri.decodePathInfo(pathInfo); Object ret = null; ServiceInterface si = Runtime.getService(msg.name); if (si == null) { ret = Status.error("could not find service %s", msg.name); } else { ret = si.invoke(msg.method, msg.data); } if (ret != null && ret instanceof Serializable) { // configurable use log or system.out ? // FIXME - make getInstance configurable // Encoder // reference !!! sendMessage(CodecUtils.toJson(ret), participant); } } catch (Exception e) { try { Logging.logError(e); sendMessage(e.toString(), participant); } catch (Exception e2) { // give up } } } } else { log.error("don't know how to handle message of type {}", type); } }
/** * Fired when the state of a chat with another user changes. * * @param chat the chat in which the state has changed. * @param state the new state of the participant. */ void stateChanged(Chat chat, ChatState state);