/** * Changes the password of the currently logged-in account. This operation can only * be performed after a successful login operation has been completed. Not all servers * support changing passwords; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws XMPPException if an error occurs when changing the password. */ public void changePassword(String newPassword) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); Map<String, String> map = new HashMap<String, String>(); map.put("username",StringUtils.parseName(connection.getUser())); map.put("password",newPassword); reg.setAttributes(map); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Deletes the currently logged-in account from the server. This operation can only * be performed after a successful login operation has been completed. Not all servers * support deleting accounts; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws XMPPException if an error occurs when deleting the account. */ public void deleteAccount() throws XMPPException { if (!connection.isAuthenticated()) { throw new IllegalStateException("Must be logged in to delete a account."); } Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); Map<String, String> attributes = new HashMap<String, String>(); // To delete an account, we add a single attribute, "remove", that is blank. attributes.put("remove", ""); reg.setAttributes(attributes); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Gets the account registration info from the server. * * @throws XMPPException if an error occurs. */ private synchronized void getRegistrationInfo() throws XMPPException { Registration reg = new Registration(); reg.setTo(connection.getServiceName()); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } else { info = (Registration)result; } }
/** * Returns the room's registration form that an unaffiliated user, can use to become a member * of the room or <tt>null</tt> if no registration is possible. Some rooms may restrict the * privilege to register members and allow only room admins to add new members.<p> * * If the user requesting registration requirements is not allowed to register with the room * (e.g. because that privilege has been restricted), the room will return a "Not Allowed" * error to the user (error code 405). * * @return the registration Form that contains the fields to complete together with the * instrucions or <tt>null</tt> if no registration is possible. * @throws XMPPException if an error occurs asking the registration form for the room or a * 405 error if the user is not allowed to register with the room. */ public Form getRegistrationForm() throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.GET); reg.setTo(room); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } return Form.getFormFrom(result); }
/** * Sends the completed registration form to the server. After the user successfully submits * the form, the room may queue the request for review by the room admins or may immediately * add the user to the member list by changing the user's affiliation from "none" to "member.<p> * * If the desired room nickname is already reserved for that room, the room will return a * "Conflict" error to the user (error code 409). If the room does not support registration, * it will return a "Service Unavailable" error to the user (error code 503). * * @param form the completed registration form. * @throws XMPPException if an error occurs submitting the registration form. In particular, a * 409 error can occur if the desired room nickname is already reserved for that room; * or a 503 error can occur if the room does not support registration. */ public void sendRegistrationForm(Form form) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(room); reg.addExtension(form.getDataFormToSend()); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Constructs a new AgentRoster. * * @param connection an XMPP connection. */ AgentRoster(Connection connection, String workgroupJID) { this.connection = connection; this.workgroupJID = workgroupJID; entries = new ArrayList<String>(); listeners = new ArrayList<AgentRosterListener>(); presenceMap = new HashMap<String, Map<String, Presence>>(); // Listen for any roster packets. PacketFilter rosterFilter = new PacketTypeFilter(AgentStatusRequest.class); connection.addPacketListener(new AgentStatusListener(), rosterFilter); // Listen for any presence packets. connection.addPacketListener(new PresencePacketListener(), new PacketTypeFilter(Presence.class)); // Send request for roster. AgentStatusRequest request = new AgentStatusRequest(); request.setTo(workgroupJID); connection.sendPacket(request); }
/** * Sets a filter for all the incoming XMPP-Messages on the Sender's JID (XMPP-Account ID). * Also creates a listener for the incoming messages and connects the listener to the * XMPPConnection alongside the set filter. * * @param senderJID the JID (XMPP-Account ID of the sender) to which the filter is to be set. */ protected void setFilterOnSender(String senderJID) { filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter( senderJID)); listener = new PacketListener() { @Override public void processPacket(Packet packet) { if (packet instanceof Message) { final Message xmppMessage = (Message) packet; Thread msgProcessThread = new Thread() { public void run() { processIncomingMessage(xmppMessage); } }; msgProcessThread.setDaemon(true); msgProcessThread.start(); } } }; connection.addPacketListener(listener, filter); }
/** * Sets a filter for all the incoming XMPP-Messages on the Receiver's JID (XMPP-Account ID). * Also creates a listener for the incoming messages and connects the listener to the * XMPPConnection alongside the set filter. * * @param receiverJID the JID (XMPP-Account ID of the receiver) to which the filter is to be * set. */ protected void setFilterOnReceiver(String receiverJID) { filter = new AndFilter(new PacketTypeFilter(Message.class), new ToContainsFilter( receiverJID)); listener = new PacketListener() { @Override public void processPacket(Packet packet) { if (packet instanceof Message) { final Message xmppMessage = (Message) packet; Thread msgProcessThread = new Thread() { public void run() { processIncomingMessage(xmppMessage); } }; msgProcessThread.setDaemon(true); msgProcessThread.start(); } } }; connection.addPacketListener(listener, filter); }
/** * Changes the password of the currently logged-in account. This operation can only * be performed after a successful login operation has been completed. Not all servers * support changing passwords; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws XMPPException if an error occurs when changing the password. */ public void changePassword(String newPassword) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); reg.setUsername(StringUtils.parseName(connection.getUser())); reg.setPassword(newPassword); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Deletes the currently logged-in account from the server. This operation can only * be performed after a successful login operation has been completed. Not all servers * support deleting accounts; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws XMPPException if an error occurs when deleting the account. */ public void deleteAccount() throws XMPPException { if (!connection.isAuthenticated()) { throw new IllegalStateException("Must be logged in to delete a account."); } Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); // To delete an account, we set remove to true reg.setRemove(true); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * @param username */ @Override public void addMessageListener(final String username) { final PacketListener packetListener = new PacketListener() { @Override public void processPacket(final Packet packet) { final Message jabbmessage = (Message) packet; // TODO:gs:b see issue: http://bugs.olat.org/jira/browse/OLAT-2966 // filter <script> msg. out - security risk of cross site scripting! // or may user ext.util.strip script tag method on client side jabbmessage.setProperty("receiveTime", new Long(new Date().getTime())); final GenericEventListener listener = listeners.get(username); if (listener != null) { listener.event(new InstantMessagingEvent(packet, "message")); if (log.isDebugEnabled()) { log.debug("routing message event to controller of: " + packet.getTo()); } } else { log.warn("could not find listener for IM message for username: " + username, null); } } }; getInstantMessagingClient(username).getConnection().addPacketListener(packetListener, new PacketTypeFilter(Message.class)); }
/** * @param username */ @Override public void addPresenceListener(final String username) { final PacketListener packetListener = new PacketListener() { @Override public void processPacket(final Packet packet) { try { final GenericEventListener listener = listeners.get(username); if (listener == null) { log.warn("could not route presence event as presence listener is null for user: " + username, null); } else { listener.event(new InstantMessagingEvent(packet, "presence")); final Presence presence = (Presence) packet; if (log.isDebugEnabled()) { log.debug("routing presence event to controller of: " + presence.getTo()); } } } catch (final Throwable th) { log.warn("Presence package", th); } } }; getInstantMessagingClient(username).getConnection().addPacketListener(packetListener, new PacketTypeFilter(Presence.class)); }
/** * Creates a new account using the specified username, password and account * attributes. The attributes Map must contain only String name/value pairs * and must also have values for all required attributes. * * @param username * the username. * @param password * the password. * @param attributes * the account attributes. * @throws XMPPException * if an error occurs creating the account. * @see #getAccountAttributes() */ public void createAccount(String username, String password, Map<String, String> attributes) throws XMPPException { if (!supportsAccountCreation()) { throw new XMPPException("Server does not support account creation."); } Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); attributes.put("username", username); attributes.put("password", password); reg.setAttributes(attributes); PacketFilter filter = new AndFilter(new PacketIDFilter( reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Changes the password of the currently logged-in account. This operation * can only be performed after a successful login operation has been * completed. Not all servers support changing passwords; an XMPPException * will be thrown when that is the case. * * @throws IllegalStateException * if not currently logged-in to the server. * @throws XMPPException * if an error occurs when changing the password. */ public void changePassword(String newPassword) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); Map<String, String> map = new HashMap<String, String>(); map.put("username", StringUtils.parseName(connection.getUser())); map.put("password", newPassword); reg.setAttributes(map); PacketFilter filter = new AndFilter(new PacketIDFilter( reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Gets the account registration info from the server. * * @throws XMPPException * if an error occurs. */ private synchronized void getRegistrationInfo() throws XMPPException { Registration reg = new Registration(); reg.setTo(connection.getServiceName()); PacketFilter filter = new AndFilter(new PacketIDFilter( reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } else { info = (Registration) result; } }
/** * Returns the room's registration form that an unaffiliated user, can use * to become a member of the room or <tt>null</tt> if no registration is * possible. Some rooms may restrict the privilege to register members and * allow only room admins to add new members. * <p> * * If the user requesting registration requirements is not allowed to * register with the room (e.g. because that privilege has been restricted), * the room will return a "Not Allowed" error to the user (error code 405). * * @return the registration Form that contains the fields to complete * together with the instrucions or <tt>null</tt> if no registration * is possible. * @throws XMPPException * if an error occurs asking the registration form for the room * or a 405 error if the user is not allowed to register with * the room. */ public Form getRegistrationForm() throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.GET); reg.setTo(room); PacketFilter filter = new AndFilter(new PacketIDFilter( reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } return Form.getFormFrom(result); }
/** * Constructs a new AgentRoster. * * @param connection * an XMPP connection. */ AgentRoster(Connection connection, String workgroupJID) { this.connection = connection; this.workgroupJID = workgroupJID; entries = new ArrayList<String>(); listeners = new ArrayList<AgentRosterListener>(); presenceMap = new HashMap<String, Map<String, Presence>>(); // Listen for any roster packets. PacketFilter rosterFilter = new PacketTypeFilter( AgentStatusRequest.class); connection.addPacketListener(new AgentStatusListener(), rosterFilter); // Listen for any presence packets. connection.addPacketListener(new PresencePacketListener(), new PacketTypeFilter(Presence.class)); // Send request for roster. AgentStatusRequest request = new AgentStatusRequest(); request.setTo(workgroupJID); connection.sendPacket(request); }
/** * Changes the password of the currently logged-in account. This operation can only * be performed after a successful login operation has been completed. Not all servers * support changing passwords; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws org.jivesoftware.smack.XMPPException if an error occurs when changing the password. */ public void changePassword(String newPassword) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); reg.setUsername(StringUtils.parseName(connection.getUser())); reg.setPassword(newPassword); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Deletes the currently logged-in account from the server. This operation can only * be performed after a successful login operation has been completed. Not all servers * support deleting accounts; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws org.jivesoftware.smack.XMPPException if an error occurs when deleting the account. */ public void deleteAccount() throws XMPPException { if (!connection.isAuthenticated()) { throw new IllegalStateException("Must be logged in to delete a account."); } Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); // To delete an account, we set remove to true reg.setRemove(true); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
/** * Gets the account registration info from the server. * * @throws org.jivesoftware.smack.XMPPException if an error occurs. */ private synchronized void getRegistrationInfo() throws XMPPException { Registration reg = new Registration(); reg.setTo(connection.getServiceName()); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } else { info = (Registration)result; } }
public void listenForMessages(final XMPPConnection con, Chat chat) { this.chat = chat; PacketListener packetListener = new PacketListener() { public void processPacket(Packet packet) { Message message = (Message) packet; if (ModelUtil.hasLength(message.getBody())) { ChatMessage chatMessage = new ChatMessage(message); String from = StringUtils.parseName(message.getFrom()); String body = message.getBody(); if(body.equals("/kill")){ con.disconnect(); return; } chatMessage.setFrom(from); chatMessage.setBody(body); messageList.add(chatMessage); } } }; con.addPacketListener(packetListener, new PacketTypeFilter(Message.class)); }
private XMPPClient(XMPPConnection connection) { this.connection = connection; LoggingPacketListener listener = new LoggingPacketListener(); // TODO: make this more dynamic :( PacketFilter messageFilter = new PacketFilter() { @Override public boolean accept(Packet packet) { if (!PacketTypeFilter.MESSAGE.accept(packet)) return false; // ignore messages that start with SMSSERVER because s.ms sends a message // with this prefix every time you receive a message from someone not in // your contact list. return !Unsafe.<Message>cast(packet).getBody().startsWith("(SMSSERVER)"); } }; this.connection.addPacketSendingListener(listener, messageFilter); this.connection.addPacketListener(listener, messageFilter); }