Example #1
0
  @Override
  public synchronized void connect(String username, String password) throws RemoteException {
    if (isConnected()) {
      notifyLogin(1, "already connected");
      return;
    }
    ConnectionConfiguration config = new ConnectionConfiguration(serveraddress, serverPort);
    config.setCompressionEnabled(true);
    config.setSASLAuthenticationEnabled(true);
    try {
      connection = new XMPPConnection(config);
      connection.connect();
      connection.login(username, password, "smack");

      assert connection.isSecureConnection();
      notifyLogin(0, "login succeeded");
    } catch (XMPPException e) {
      notifyLogin(-1, "login failed, reason : " + e.getMessage());
    }
  }
Example #2
0
  @Override
  public void run() {
    connection = new XMPPConnection("webmail.1723.org");

    try {
      PacketFilter filter = new FromContainsFilter("webmail.1723.org");

      connection.connect();
      connection.login("sal", "server", "Server Connection");
      connection.addPacketListener(this, filter);

      while (!done) {
        Thread.sleep(10000);
        System.out.println("XMPPModule - Alive");
      }

      connection.disconnect();
    } catch (XMPPException e) {
      System.out.println("XMPP Exception: " + e);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Example #3
0
  @Override
  public void LoginUser(String user, String pass) throws XMPPException {
    if (connection == null) {
      try {
        this.Init();
      } catch (XMPPException e) {
        throw e;
      }
    }
    // if (connection.isAuthenticated() == true) return;

    this.login = user;
    this.password = pass;
    connection.connect();
    connection.login(user + "@afaneor-comp", pass);
    // Assume we've created a Connection name "connection".
    ChatManager chatmanager = connection.getChatManager();
    chatmanager.addChatListener(
        new ChatManagerListener() {
          @Override
          public void chatCreated(Chat chat, boolean createdLocally) {
            if (!createdLocally)
              chat.addMessageListener(
                  new MessageListener() {

                    public void processMessage(Chat chat, Message message) {
                      System.out.println(
                          "Received message: "
                              + message
                              + (message != null ? message.getBody() : "NULL"));
                      ServletContextClass.myManager.GetMessage(
                          message.getFrom(), message.getBody());
                    }
                  });
          }
        });
  }
Example #4
0
 /**
  * Logs in to the server using the strongest authentication mode supported by the server, then
  * sets presence to available. If the server supports SASL authentication then the user will be
  * authenticated using SASL if not Non-SASL authentication will be tried. If more than five
  * seconds (default timeout) elapses in each step of the authentication process without a response
  * from the server, or if an error occurs, a XMPPException will be thrown.
  *
  * <p>Before logging in (i.e. authenticate) to the server the connection must be connected.
  *
  * <p>It is possible to log in without sending an initial available presence by using {@link
  * ConnectionConfiguration#setSendPresence(boolean)}. If this connection is not interested in
  * loading its roster upon login then use {@link
  * ConnectionConfiguration#setRosterLoadedAtLogin(boolean)}. Finally, if you want to not pass a
  * password and instead use a more advanced mechanism while using SASL then you may be interested
  * in using {@link
  * ConnectionConfiguration#setCallbackHandler(javax.security.auth.callback.CallbackHandler)}. For
  * more advanced login settings see {@link ConnectionConfiguration}.
  *
  * @param username the username.
  * @param password the password or <tt>null</tt> if using a CallbackHandler.
  * @throws XMPPException if an error occurs.
  */
 public void login(String username, String password) throws XMPPException {
   login(username, password, "Smack");
 }
Example #5
0
  public static void main(String[] args) {
    Scanner inputReader = new Scanner(System.in);
    XMPPConnection connection = null;
    System.out.println("Welcome to the Multi User Chat Desktop Application.");
    boolean notConnected = true;
    while (notConnected) {
      System.out.println("Enter the XMPP server you would like to connect to (e.g. myserver.org):");
      String xmppServer = inputReader.nextLine();
      try {
        System.out.println("Processing... Please wait");
        connection = new XMPPConnection(xmppServer); // connects to server address provided
        connection.connect();
        System.out.println("Connection Successful!\n");
        notConnected = false;
      } catch (Exception e) {
        System.out.println(
            "There was an issue connecting to the XMPP server '"
                + xmppServer
                + "' (We recommend jabber.org)");
      }
    }
    boolean validUserAndPass = false;
    String userName = null;
    while (!validUserAndPass) {
      System.out.println("Please enter your username: "******"Please enter your password: "******"Validating your information...");
        connection.login(userName, password); // attempts to login to the server
        validUserAndPass = true;
        System.out.println("Login Successful!\n");
      } catch (Exception e) {
        System.out.println(
            "Error logging into server - Your username or password may be incorrect");
      }
    }
    Connection connection2 = new XMPPConnection("jabber.org");
    MultiUserChat groupChat2 = null;
    try {
      connection2.connect();
      connection2.login("*****@*****.**", "opencommdesign");
    } catch (XMPPException e1) {
      System.out.println("Hardcoded opencommdesign failed to log in");
    }
    System.out.println("Enter a command to begin (or 'help' to see available commands)");
    MultiUserChat groupChat = null;
    ArrayList<RosterEntry> onlineUsersInRoom =
        new ArrayList<RosterEntry>(); // updated when a user accepts the chat invitation
    boolean chatRoomGenerated =
        false; // checked against to make sure room is not regenerated each time a user is invited
    ChatManager chatmanager = null;
    Chat chat = null;
    boolean programTerminated = false;
    while (!programTerminated) {
      String input = inputReader.nextLine();
      input =
          input
              .trim(); // ignores spaces before and after the command if the command itself is
                       // correct - does not remove spaces mixed into the command
      if (input.startsWith(HELP_VERB) && input.length() == HELP_VERB.length()) {
        System.out.println(COMMAND_OPTIONS); // prints list of available commands
      } else if (input.equals(VIEW_ROOM_VERB)) {
        if (groupChat == null) {
          System.out.println("You are not currently in any chat rooms");
        } else {
          System.out.println("You are currently in the '" + DEFAULT_ROOM_NAME + "' chatroom");
        }
      } else if (input.startsWith(INVITATION_VERB)) {
        String userToInvite =
            input.substring(
                INVITATION_VERB.length()
                    + 1); // +1 accounts for space after verb, isolates the username

        try {
          if (!chatRoomGenerated) {
            System.out.println("Initializing a default chat room...");
            groupChat = new MultiUserChat(connection, DEFAULT_ROOM_NAME + "@conference.jabber.org");
            groupChat.create(
                DEFAULT_ROOM_NAME); // initializes a default instant room, automatically placing the
                                    // user in it
            groupChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
            System.out.println("Default chat room initialized!");
            // listen for invitation rejections
            groupChat.addInvitationRejectionListener(
                new InvitationRejectionListener() {
                  public void invitationDeclined(String invitee, String reason) {
                    System.out.println("User '" + invitee + "' declined your chat invitation.");
                    System.out.println("Reason: " + reason);
                  }
                });
            groupChat2 =
                new MultiUserChat(connection, DEFAULT_ROOM_NAME + "@conference.jabber.org");
            groupChat2.join(DEFAULT_ROOM_NAME); // hardcoded second user joins the room
            chatRoomGenerated = true;
            chatmanager = connection.getChatManager();
            chat =
                chatmanager.createChat(
                    userToInvite,
                    new MessageListener() {
                      public void processMessage(Chat chat, Message message) {
                        System.out.println("Received message: " + message);
                      }
                    });
          }
          groupChat.invite(
              userToInvite, "User '" + userName + "' has invited you to join a chat room");

        } catch (XMPPException e) {
          System.out.println("Error occured in creating the chat room");
        }
      } else if (input.equals(VIEW_BUDDY_VERB)) { // if user enters viewBuddyList
        Roster roster = connection.getRoster(); // gets other users on this connection
        Collection<RosterEntry> entries = roster.getEntries();
        ArrayList<RosterEntry> onlineUsers = new ArrayList<RosterEntry>();
        ArrayList<RosterEntry> offlineUsers = new ArrayList<RosterEntry>();
        for (RosterEntry entry : entries) {
          if (entry
              .toString()
              .contains(
                  "[Buddies]")) { // if other users are marked as buddies, print them to the list
            String user = entry.getUser();
            if (roster.getPresence(user) == null) { // if user is offline, add them to offlineUsers
              offlineUsers.add(entry);
            } else {
              onlineUsers.add(entry);
            }
          }
        }

        System.out.println("Online Buddies in your chat room:");
        if (groupChat.getOccupantsCount() == 1) {
          System.out.println("There are 0 buddies in your chat room");
        } else {
          @SuppressWarnings("unchecked")
          Collection<String> users = (Collection<String>) groupChat.getOccupants();
          for (@SuppressWarnings("unused") String user : users) {
            System.out.println(user);
          }
        }
        System.out.println("Online Buddies:");
        if (onlineUsers.size() == 0) {
          System.out.println("There are 0 buddies online");
        } else {
          for (RosterEntry entry : onlineUsers) {
            String user = entry.toString().substring(0, entry.toString().indexOf("[Buddies]"));
            System.out.println(user);
          }
        }
        System.out.println("Offline Buddies:");
        if (offlineUsers.size() == 0) {
          System.out.println("There are 0 buddies offline");
        } else {
          for (RosterEntry entry : offlineUsers) {
            String user = entry.toString().substring(0, entry.toString().indexOf("[Buddies]"));
            System.out.println(user);
          }
        }
        System.out.println("");
      } else {
        System.out.println(
            "Command not recognized.  Type 'help' to see a list of available commands");
      }

      if (!programTerminated) {
        System.out.println("Enter a command: ");
      }
    }
  }