Exemplo n.º 1
0
  private void syncUsers() {
    for (RosterEntry entry : conn.getRoster().getEntries()) {
      getBuddyManager()
          .storeBuddy(
              new XMPPBuddy(
                  getBuddyManager(), entry.getUser(), entry.getName(), entry.getGroups(), entry));
      // Facebook does not support presence probes in their XMPP implementation. See
      // http://developers.facebook.com/docs/chat#features
      if (!TransportType.facebook.equals(getTransport().getType())) {
        // ProbePacket probe = new ProbePacket(this.getJID()+"/"+xmppResource, entry.getUser());
        ProbePacket probe = new ProbePacket(null, entry.getUser());
        Log.debug("XMPP: Sending the following probe packet: " + probe.toXML());
        try {
          conn.sendPacket(probe);
        } catch (IllegalStateException e) {
          Log.debug("XMPP: Not connected while trying to send probe.");
        }
      }
    }

    try {
      getTransport().syncLegacyRoster(getJID(), getBuddyManager().getBuddies());
    } catch (UserNotFoundException ex) {
      Log.error("XMPP: User not found while syncing legacy roster: ", ex);
    }

    getBuddyManager().activate();

    // lets repoll the roster since smack seems to get out of sync...
    // we'll let the roster listener take care of this though.
    conn.getRoster().reload();
  }
Exemplo n.º 2
0
 public boolean deleteUser(String jid) {
   try {
     connection.getRoster().removeEntry(connection.getRoster().getEntry(jid));
   } catch (XMPPException e) {
     e.printStackTrace();
   }
   return false;
 }
Exemplo n.º 3
0
 /**
  * @see net.sf.kraken.session.TransportSession#removeContact(net.sf.kraken.roster.TransportBuddy)
  */
 @Override
 public void removeContact(XMPPBuddy contact) {
   RosterEntry user2remove;
   String mail = getTransport().convertJIDToID(contact.getJID());
   user2remove = conn.getRoster().getEntry(mail);
   try {
     conn.getRoster().removeEntry(user2remove);
   } catch (XMPPException ex) {
     Log.debug("XMPP: unable to remove:" + mail);
   }
 }
  @Override
  public boolean login() throws RemoteException {
    if (mAdaptee.isAuthenticated()) return true;
    if (!mAdaptee.isConnected()) return false;
    try {

      this.initFeatures(); // pour declarer les features xmpp qu'on
      // supporte

      PacketFilter filter =
          new PacketFilter() {

            @Override
            public boolean accept(Packet packet) {
              if (packet instanceof Presence) {
                Presence pres = (Presence) packet;
                if (pres.getType() == Presence.Type.subscribe) return true;
              }
              return false;
            }
          };

      mAdaptee.addPacketListener(mSubscribePacketListener, filter);

      filter = new PacketTypeFilter(PingExtension.class);
      mAdaptee.addPacketListener(mPingListener, filter);

      mAdaptee.login(mLogin, mPassword, mResource);
      mUserInfo = new UserInfo(mAdaptee.getUser());

      mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService, mAdaptee.getRoster());
      // nikita: I commented this line because of the logs provided in
      // http://www.beem-project.com/issues/321
      // Also, since the privacylistmanager isn't finished and used, it will be safer to not
      // initialize it
      // mPrivacyListManager = new
      // PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee));
      mService.initJingle(mAdaptee);
      discoverServerFeatures();

      mRoster = new RosterAdapter(mAdaptee.getRoster(), mService, mAvatarManager);
      mApplication.setConnected(true);
      int mode = mPref.getInt(BeemApplication.STATUS_KEY, 0);
      String status = mPref.getString(BeemApplication.STATUS_TEXT_KEY, "");
      changeStatus(mode, status);
      return true;
    } catch (XMPPException e) {
      Log.e(TAG, "Error while connecting", e);
      mErrorMsg = mService.getString(R.string.error_login_authentication);
      return false;
    }
  }
Exemplo n.º 5
0
  /**
   * @see net.sf.kraken.session.TransportSession#addContact(org.xmpp.packet.JID, String,
   *     java.util.ArrayList)
   */
  @Override
  public void addContact(JID jid, String nickname, ArrayList<String> groups) {
    String mail = getTransport().convertJIDToID(jid);
    try {
      conn.getRoster().createEntry(mail, nickname, groups.toArray(new String[groups.size()]));
      RosterEntry entry = conn.getRoster().getEntry(mail);

      getBuddyManager()
          .storeBuddy(new XMPPBuddy(getBuddyManager(), mail, nickname, entry.getGroups(), entry));
    } catch (XMPPException ex) {
      Log.debug("XMPP: unable to add:" + mail);
    }
  }
 /**
  * Creates a new FriendGroup. If this FriendGroup contains no Friends when you logout it will be
  * erased from the server.
  *
  * @param name The name of this FriendGroup
  * @return The new FriendGroup or null if a FriendGroup with this name already exists.
  */
 public FriendGroup addFriendGroup(String name) {
   final RosterGroup g = connection.getRoster().createGroup(name);
   if (g != null) {
     return new FriendGroup(this, connection, g);
   }
   return null;
 }
Exemplo n.º 7
0
  /**
   * @see net.sf.kraken.session.TransportSession#sendChatState(org.xmpp.packet.JID,
   *     net.sf.kraken.type.ChatStateType)
   */
  @Override
  public void sendChatState(JID jid, ChatStateType chatState) {
    final Presence presence = conn.getRoster().getPresence(jid.toString());
    if (presence == null || presence.getType().equals(Presence.Type.unavailable)) {
      // don't send chat state to contacts that are offline.
      return;
    }
    Chat chat = conn.getChatManager().createChat(getTransport().convertJIDToID(jid), listener);
    try {
      ChatState state = ChatState.active;
      switch (chatState) {
        case active:
          state = ChatState.active;
          break;
        case composing:
          state = ChatState.composing;
          break;
        case paused:
          state = ChatState.paused;
          break;
        case inactive:
          state = ChatState.inactive;
          break;
        case gone:
          state = ChatState.gone;
          break;
      }

      Message message = new Message();
      message.addExtension(new ChatStateExtension(state));
      chat.sendMessage(message);
    } catch (XMPPException e) {
      // Ignore
    }
  }
Exemplo n.º 8
0
  public void addRosterListener() {
    connection
        .getRoster()
        .addRosterListener(
            new RosterListener() {

              @Override
              public void entriesAdded(Collection<String> arg0) {
                // TODO Auto-generated method stub
                System.out.println("--------EE:" + "entriesAdded");
              }

              @Override
              public void entriesDeleted(Collection<String> arg0) {
                // TODO Auto-generated method stub
                System.out.println("--------EE:" + "entriesDeleted");
              }

              @Override
              public void entriesUpdated(Collection<String> arg0) {
                // TODO Auto-generated method stub
                System.out.println("--------EE:" + "entriesUpdated");
              }

              @Override
              public void presenceChanged(Presence arg0) {
                // TODO Auto-generated method stub
                System.out.println("--------EE:" + "presenceChanged");
              }
            });
  }
  @Override
  public void disconnected(boolean onError) {
    final XMPPConnection connection = myFacade.getConnection();

    LOG.info("Jabber disconnected: " + connection.getUser());
    connection.removePacketListener(mySubscribeListener);
    mySubscribeListener = null;
    connection.removePacketListener(myMessageListener);
    myMessageListener = null;

    final Roster roster = connection.getRoster();
    if (roster != null) {
      roster.removeRosterListener(myRosterListener);
    }
    myRosterListener = null;

    myIDEtalkUsers.clear();
    myUser2Presence.clear();
    myUser2Thread.clear();

    if (onError && reconnectEnabledAndNotStarted()) {
      LOG.warn(getMsg("jabber.server.was.disconnected", myReconnectTimeout / 1000));
      myReconnectProcess = myIdeFacade.runOnPooledThread(new MyReconnectRunnable());
    }
  }
 /**
  * Get a list of all your FriendGroups.
  *
  * @return A List of all your FriendGroups
  */
 public List<FriendGroup> getFriendGroups() {
   final ArrayList<FriendGroup> groups = new ArrayList<>();
   for (final RosterGroup g : connection.getRoster().getGroups()) {
     groups.add(new FriendGroup(this, connection, g));
   }
   return groups;
 }
 /**
  * Gets a FriendGroup by name, for example "Duo Partners". The name is case sensitive! The
  * FriendGroup will be created if it didn't exist yet.
  *
  * @param name The name of your group (case-sensitive)
  * @return The corresponding FriendGroup
  */
 public FriendGroup getFriendGroupByName(String name) {
   final RosterGroup g = connection.getRoster().getGroup(name);
   if (g != null) {
     return new FriendGroup(this, connection, g);
   }
   return addFriendGroup(name);
 }
 /**
  * Gets a friend based on his XMPPAddress.
  *
  * @param xmppAddress For example [email protected]
  * @return The corresponding Friend or null if user is not found or he is not a friend of you.
  */
 public Friend getFriendById(String xmppAddress) {
   final RosterEntry entry =
       connection.getRoster().getEntry(StringUtils.parseBareAddress(xmppAddress));
   if (entry != null) {
     return new Friend(this, connection, entry);
   }
   return null;
 }
 /** {@inheritDoc} */
 @Override
 public IRoster getRoster() throws RemoteException {
   if (mRoster != null) return mRoster;
   Roster adap = mAdaptee.getRoster();
   if (adap == null) return null;
   mRoster = new RosterAdapter(adap, mService, mAvatarManager);
   return mRoster;
 }
Exemplo n.º 14
0
 /** 更改用户状态 */
 public void setPresence(int code) {
   if (connection == null) return;
   Presence presence;
   switch (code) {
     case 0:
       presence = new Presence(Presence.Type.available);
       connection.sendPacket(presence);
       Log.v("state", "设置在线");
       break;
     case 1:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.chat);
       connection.sendPacket(presence);
       Log.v("state", "设置Q我吧");
       System.out.println(presence.toXML());
       break;
     case 2:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.dnd);
       connection.sendPacket(presence);
       Log.v("state", "设置忙碌");
       System.out.println(presence.toXML());
       break;
     case 3:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.away);
       connection.sendPacket(presence);
       Log.v("state", "设置离开");
       System.out.println(presence.toXML());
       break;
     case 4:
       Roster roster = connection.getRoster();
       Collection<RosterEntry> entries = roster.getEntries();
       for (RosterEntry entry : entries) {
         presence = new Presence(Presence.Type.unavailable);
         presence.setPacketID(Packet.ID_NOT_AVAILABLE);
         presence.setFrom(connection.getUser());
         presence.setTo(entry.getUser());
         connection.sendPacket(presence);
         System.out.println(presence.toXML());
       }
       // 向同一用户的其他客户端发送隐身状态
       presence = new Presence(Presence.Type.unavailable);
       presence.setPacketID(Packet.ID_NOT_AVAILABLE);
       presence.setFrom(connection.getUser());
       presence.setTo(StringUtils.parseBareAddress(connection.getUser()));
       connection.sendPacket(presence);
       Log.v("state", "设置隐身");
       break;
     case 5:
       presence = new Presence(Presence.Type.unavailable);
       connection.sendPacket(presence);
       Log.v("state", "设置离线");
       break;
     default:
       break;
   }
 }
 /**
  * Gets a friend based on a given filter.
  *
  * @param filter The filter defines conditions that your Friend must meet.
  * @return The first Friend that meets the conditions or null if not found.
  */
 public Friend getFriend(Filter<Friend> filter) {
   for (final RosterEntry e : connection.getRoster().getEntries()) {
     final Friend f = new Friend(this, connection, e);
     if (filter.accept(f)) {
       return f;
     }
   }
   return null;
 }
 /** Disconnects from chatserver and releases all resources. */
 public void disconnect() {
   connection.getRoster().removeRosterListener(leagueRosterListener);
   try {
     connection.disconnect();
   } catch (final NotConnectedException e) {
     e.printStackTrace();
   }
   stop = true;
 }
Exemplo n.º 17
0
 public boolean addUser(String jid, String name) {
   String[] groupsStrings = new String[] {"1", "2", "3"};
   try {
     connection.getRoster().createEntry(jid, name, groupsStrings);
   } catch (XMPPException e) {
     e.printStackTrace();
   }
   return false;
 }
Exemplo n.º 18
0
 public boolean renameFriend(String userID, String name) {
   if (sConnection != null && sConnection.isConnected()) {
     Roster roster = sConnection.getRoster();
     if (roster.contains(userID)) {
       RosterEntry entry = roster.getEntry(userID);
       entry.setName(name);
     }
   }
   return false;
 }
 /**
  * Gets a list of your friends based on a given filter.
  *
  * @param filter The filter defines conditions that your Friends must meet.
  * @return A List of your Friends that meet the condition of your Filter
  */
 public List<Friend> getFriends(Filter<Friend> filter) {
   final ArrayList<Friend> friends = new ArrayList<>();
   for (final RosterEntry e : connection.getRoster().getEntries()) {
     final Friend f = new Friend(this, connection, e);
     if (filter.accept(f)) {
       friends.add(f);
     }
   }
   return friends;
 }
Exemplo n.º 20
0
  public List<RosterEntry> getRosterEntries() {
    List<RosterEntry> result = new ArrayList<RosterEntry>();

    Roster roster = con.getRoster();
    Collection<RosterGroup> groups = roster.getGroups();
    for (RosterGroup group : groups) {
      result.addAll(group.getEntries());
    }

    return result;
  }
  public void presenceChanged(Presence p) {
    String from = p.getFrom();
    if (from != null) {
      p = connection.getRoster().getPresence(p.getFrom());
      from = StringUtils.parseBareAddress(from);
      final Friend friend = api.getFriendById(from);
      if (friend != null) {
        for (final FriendListener l : api.getFriendListeners()) {
          final Presence.Type previousType = typeUsers.get(from);
          if (p.getType() == Presence.Type.available
              && (previousType == null || previousType != Presence.Type.available)) {
            l.onFriendJoin(friend);
          } else if (p.getType() == Presence.Type.unavailable
              && (previousType == null || previousType != Presence.Type.unavailable)) {
            l.onFriendLeave(friend);
          }

          final Presence.Mode previousMode = modeUsers.get(from);
          if (p.getMode() == Presence.Mode.chat
              && (previousMode == null || previousMode != Presence.Mode.chat)) {
            l.onFriendAvailable(friend);
          } else if (p.getMode() == Presence.Mode.away
              && (previousMode == null || previousMode != Presence.Mode.away)) {
            l.onFriendAway(friend);
          } else if (p.getMode() == Presence.Mode.dnd
              && (previousMode == null || previousMode != Presence.Mode.dnd)) {
            l.onFriendBusy(friend);
          }

          if (p.getStatus() != null) {
            try {
              final LolStatus previousStatus = statusUsers.get(from);
              final LolStatus newStatus = new LolStatus(p.getStatus());
              if (previousStatus != null && !newStatus.equals(previousStatus)) {
                l.onFriendStatusChange(friend);
              }
            } catch (JDOMException | IOException e) {
            }
          }
        }

        typeUsers.put(from, p.getType());
        modeUsers.put(from, p.getMode());
        if (p.getStatus() != null) {
          try {
            statusUsers.put(from, new LolStatus(p.getStatus()));
          } catch (JDOMException | IOException e) {
          }
        }
      }
    }
  }
Exemplo n.º 22
0
  public int retrieveState(String userID) {
    int userState = XmppFriend.OFFLINE; // default return value
    Presence userFromServer = null;

    try {
      userFromServer = sConnection.getRoster().getPresence(userID);
      userState = retrieveState(userFromServer.getMode(), userFromServer.isAvailable());
    } catch (NullPointerException e) {
      Log.e("retrieveState(): Invalid connection or user - NPE", e);
    }

    return userState;
  }
Exemplo n.º 23
0
 /**
  * @see net.sf.kraken.session.TransportSession#updateContact(net.sf.kraken.roster.TransportBuddy)
  */
 @Override
 public void updateContact(XMPPBuddy contact) {
   RosterEntry user2Update;
   String mail = getTransport().convertJIDToID(contact.getJID());
   user2Update = conn.getRoster().getEntry(mail);
   user2Update.setName(contact.getNickname());
   Collection<String> newgroups = contact.getGroups();
   if (newgroups == null) {
     newgroups = new ArrayList<String>();
   }
   for (RosterGroup group : conn.getRoster().getGroups()) {
     if (newgroups.contains(group.getName())) {
       if (!group.contains(user2Update)) {
         try {
           group.addEntry(user2Update);
         } catch (XMPPException e) {
           Log.debug("XMPP: Unable to add roster item to group.");
         }
       }
       newgroups.remove(group.getName());
     } else {
       if (group.contains(user2Update)) {
         try {
           group.removeEntry(user2Update);
         } catch (XMPPException e) {
           Log.debug("XMPP: Unable to delete roster item from group.");
         }
       }
     }
   }
   for (String group : newgroups) {
     RosterGroup newgroup = conn.getRoster().createGroup(group);
     try {
       newgroup.addEntry(user2Update);
     } catch (XMPPException e) {
       Log.debug("XMPP: Unable to add roster item to new group.");
     }
   }
 }
Exemplo n.º 24
0
 public boolean removeFriend(String userID) {
   if (sConnection != null && sConnection.isConnected()) {
     Roster roster = sConnection.getRoster();
     if (roster.contains(userID)) {
       try {
         roster.removeEntry(roster.getEntry(userID));
         return true;
       } catch (XMPPException e) {
         System.err.println("Error in removing friend " + e.getMessage());
       }
     }
   }
   return false;
 }
Exemplo n.º 25
0
  /**
   * returns the status message for a given bare or full JID
   *
   * @param userID
   * @return
   */
  public String retrieveStatusMessage(String userID) {
    String userStatus = ""; // default return value

    try {
      userStatus = sConnection.getRoster().getPresence(userID).getStatus();
    } catch (NullPointerException e) {
      Log.e("Invalid connection or user in retrieveStatus() - NPE", e);
      userStatus = "";
    }
    // Server may set their status to null; we want empty string
    if (userStatus == null) {
      userStatus = "";
    }

    return userStatus;
  }
 void addFriendImpl(String id, String name) throws FriendException {
   synchronized (this) {
     try {
       checkLoggedIn();
       Roster roster = connection.getRoster();
       if (roster != null) {
         // TODO smack enhancement
         // TODO to support notifications when
         // TODO the Roster is created
         roster.createEntry(id, name, null);
       }
     } catch (org.jivesoftware.smack.XMPPException e) {
       throw new FriendException(e);
     }
   }
 }
Exemplo n.º 27
0
  // TODO: figure out what determines the presence of a RosterEntry's name field
  public void setActiveChatByName(String name) {
    Roster roster = connection.getRoster();

    String activeUser = null;
    if (activeChat != null) {
      activeUser = activeChat.getParticipant();
      int index = activeUser.indexOf("/");
      // Log.d("Jane", "index: " + index);
      if (index >= 0) {
        activeUser = activeUser.substring(0, index);
      }

      if (!chatCache.containsKey(activeUser)) {
        Log.d("Jane", "Caching " + activeUser);
        chatCache.put(activeUser, activeChat);
      }
    }

    for (RosterEntry entry : roster.getEntries()) {
      String potentialName = entry.getName();
      String email = entry.getUser();
      Log.i("Chat", "Checking desired recipient: " + name + " against: " + potentialName);
      if (potentialName != null && potentialName.regionMatches(true, 0, name, 0, name.length())) {
        Log.i("Chat", "Setting active chat to " + potentialName + "/" + email);
        speak("Now talking to " + potentialName);
        if (chatCache.containsKey(email)) {
          activeChat = chatCache.get(email);
        } else {
          Log.d("Chat", "Chat cache did not contain key " + email);
          activeChat = connection.getChatManager().createChat(email, null);
        }
        return;
      } else if (email.contains(name)) { // maybe emails will be ok...
        Log.i("Chat", "Setting active chat to " + email);
        speak("Now talking to " + email);
        if (chatCache.containsKey(email)) {
          activeChat = chatCache.get(email);
        } else {
          activeChat = connection.getChatManager().createChat(email, null);
        }
        return;
      }
    }
    Log.i("Chat", "No friend matches " + name);
    speak("Sorry, but I couldn't find a friend named " + name);
  }
 private void xmppRosterPost(XMPPConnection connection) {
   List<String> jidList = new LinkedList<String>();
   System.err.println("INFO: Reading roster.");
   for (RosterEntry entry : connection.getRoster().getEntries()) {
     jidList.add(entry.getUser());
     System.err.println("INFO: Got " + entry.getUser() + " from roster.");
   }
   System.err.println("INFO: Sending " + jidList.size() + " roster entries to server.");
   BuddylistIQ blIQ = new BuddylistIQ();
   blIQ.setFrom(connection.getUser());
   blIQ.setTo(this.androidbuddy);
   blIQ.setBuddies(jidList);
   blIQ.setIdentity(XMPPUtil.jidWithoutRessource(connection.getUser()));
   blIQ.setNetwork("roster");
   blIQ.setType(IQ.Type.SET);
   connection.sendPacket(blIQ);
 }
  private void removeFriendImpl(String id) throws FriendException {
    synchronized (this) {
      try {
        checkLoggedIn();
        Roster roster = connection.getRoster();
        if (roster != null) {
          // TODO smack enhancement
          // TODO to support notifications when
          // TODO the Roster is created

          RosterEntry entry = roster.getEntry(id);
          if (entry != null) {
            roster.removeEntry(entry);
          }
        }
      } catch (org.jivesoftware.smack.XMPPException e) {
        throw new FriendException(e);
      }
    }
  }
Exemplo n.º 30
0
 public String getNameForEmail(String email) {
   Log.i("Chat", "Getting name for " + email);
   if (nameCache.containsKey(email)) {
     return nameCache.get(email);
   } else {
     Roster roster = connection.getRoster();
     for (RosterEntry entry : roster.getEntries()) {
       Log.i("Chat", entry.getUser() + "," + entry.getName());
       if (entry.getUser().equals(email)) {
         String name = entry.getName();
         Log.i("Chat", "Got name for " + email + " as " + name);
         if (name == null || name.equals("")) {
           return email;
         } else {
           nameCache.put(email, name);
           return name;
         }
       }
     }
     return email;
   }
 }