Commit 373a8f53 authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

[GATE-14] Updated IRC support quite a bit.

[GATE-5] Yahoo nickname support updated via pseudo rosters.
Lots of new functionality with pseudo rosters.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@5350 b35dd754-fafc-0310-a699-88a17e54d16e
parent 333cc735
......@@ -159,7 +159,14 @@ public abstract class BaseTransport implements Component, RosterEventListener {
if (to.getNode() == null) {
// Message to gateway itself. Throw away for now.
// TODO: Repsond with a message at some point?
try {
TransportSession session = sessionManager.getSession(from);
session.sendServerMessage(packet.getBody());
}
catch (NotFoundException e) {
// TODO: Should return an error packet here
Log.debug("Unable to find session.");
}
}
else {
try {
......@@ -592,6 +599,19 @@ public abstract class BaseTransport implements Component, RosterEventListener {
Element response = DocumentHelper.createElement(QName.get("query", IQ_REGISTER));
IQ result = IQ.createResultIQ(packet);
String curUsername = null;
String curPassword = null;
String curNickname = null;
Boolean registered = false;
Collection<Registration> registrations = registrationManager.getRegistrations(from, this.transportType);
if (registrations.iterator().hasNext()) {
Registration registration = registrations.iterator().next();
curUsername = registration.getUsername();
curPassword = registration.getPassword();
curNickname = registration.getNickname();
registered = true;
}
DataForm form = new DataForm(DataForm.Type.form);
form.addInstruction(getTerminologyRegistration());
......@@ -599,11 +619,17 @@ public abstract class BaseTransport implements Component, RosterEventListener {
usernameField.setLabel(getTerminologyUsername());
usernameField.setVariable("username");
usernameField.setType(FormField.Type.text_single);
if (curUsername != null) {
usernameField.addValue(curUsername);
}
FormField passwordField = form.addField();
passwordField.setLabel(getTerminologyPassword());
passwordField.setVariable("password");
passwordField.setType(FormField.Type.text_private);
if (curPassword != null) {
passwordField.addValue(curPassword);
}
String nicknameTerm = getTerminologyNickname();
if (nicknameTerm != null) {
......@@ -611,19 +637,30 @@ public abstract class BaseTransport implements Component, RosterEventListener {
nicknameField.setLabel(nicknameTerm);
nicknameField.setVariable("nick");
nicknameField.setType(FormField.Type.text_single);
if (curNickname != null) {
nicknameField.addValue(curNickname);
}
}
response.add(form.getElement());
response.addElement("instructions").addText(getTerminologyRegistration());
Collection<Registration> registrations = registrationManager.getRegistrations(from, this.transportType);
if (registrations.iterator().hasNext()) {
Registration registration = registrations.iterator().next();
if (registered) {
response.addElement("registered");
response.addElement("username").addText(registration.getUsername());
response.addElement("password").addText(registration.getPassword());
response.addElement("username").addText(curUsername);
if (curPassword == null) {
response.addElement("password");
}
else {
response.addElement("password").addText(curPassword);
}
if (nicknameTerm != null) {
response.addElement("nick").addText(registration.getNickname());
if (curNickname == null) {
response.addElement("nick");
}
else {
response.addElement("nick").addText(curNickname);
}
}
}
else {
......@@ -1229,6 +1266,10 @@ public abstract class BaseTransport implements Component, RosterEventListener {
// Not ours, not our problem.
return;
}
if (item.getJid().getNode() == null) {
// Gateway itself, don't care.
return;
}
try {
TransportSession session = sessionManager.getSession(roster.getUsername());
session.updateContact(item);
......
......@@ -13,12 +13,10 @@ package org.jivesoftware.wildfire.gateway;
import org.jivesoftware.util.Log;
import org.jivesoftware.database.DbConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
/**
* Representation of an entire roster associated with a registration id.
......@@ -28,10 +26,12 @@ import java.util.ArrayList;
public class PseudoRoster {
private static final String GET_ALL_USER_ROSTER_ITEMS =
"SELECT username FROM gatewayPseudoRoster WHERE registrationID=?";
"SELECT username,nickname,groups FROM gatewayPseudoRoster WHERE registrationID=?";
private static final String REMOVE_ROSTER_ITEM =
"DELETE FROM gatewayPseudoRoster WHERE registrationID=? AND username=?";
private long registrationID;
private List<String> pseudoRosterItems = new ArrayList<String>();
private ConcurrentHashMap<String,PseudoRosterItem> pseudoRosterItems = new ConcurrentHashMap<String,PseudoRosterItem>();
/**
* Loads an existing pseudo roster.
......@@ -55,12 +55,80 @@ public class PseudoRoster {
/**
* Returns the list of roster items associated with this registration ID.
*
* @return List of roster item usernames.
* @return Map of roster item usernames to PseudoRosterItems.
*/
public List<String> getRosterItems() {
public ConcurrentHashMap<String,PseudoRosterItem> getRosterItems() {
return pseudoRosterItems;
}
/**
* Returns a set of just the usernames of contacts from this roster.
*
* @return Set of usernames.
*/
public Set<String> getContacts() {
return pseudoRosterItems.keySet();
}
/**
* Returns true or false if a pseudo roster item exists for a username.
*
* @param username Username to locate.
* @return Whether a roster item exists with the username.
*/
public Boolean hasItem(String username) {
return pseudoRosterItems.containsKey(username);
}
/**
* Retrieves a pseudo roster item for a username.
*
* @param username Username to locate.
* @return A PseudoRosterItem for the user specified.
*/
public PseudoRosterItem getItem(String username) {
return pseudoRosterItems.get(username);
}
/**
* Removes a pseudo roster item for a username.
*
* @param username Username to remove.
*/
public void removeItem(String username) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(REMOVE_ROSTER_ITEM);
pstmt.setLong(1, registrationID);
pstmt.setString(2, username);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
Log.error(sqle);
}
finally {
DbConnectionManager.closeConnection(pstmt, con);
}
}
/**
* Creates a new pseudo roster item for a username, nickname, and list of groups.
*
* @param username Username to add.
* @param nickname Nickname for roster item.
* @param groups List of groups for roster item.
*/
public PseudoRosterItem createItem(String username, String nickname, String groups) {
PseudoRosterItem rosterItem = new PseudoRosterItem(registrationID, username, nickname, groups);
pseudoRosterItems.put(username, rosterItem);
return rosterItem;
}
/**
* Load pseudo roster from database.
*/
private void loadFromDb() {
Connection con = null;
PreparedStatement pstmt = null;
......@@ -71,7 +139,10 @@ public class PseudoRoster {
pstmt.setLong(1, registrationID);
rs = pstmt.executeQuery();
while (rs.next()) {
pseudoRosterItems.add(rs.getString(1));
String username = rs.getString(1);
String nickname = rs.getString(2);
String groups = rs.getString(3);
pseudoRosterItems.put(username, new PseudoRosterItem(registrationID, username, nickname, groups));
}
}
catch (SQLException sqle) {
......
......@@ -13,7 +13,6 @@ package org.jivesoftware.wildfire.gateway;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.NotFoundException;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SequenceManager;
import java.sql.*;
......@@ -211,7 +210,6 @@ public class PseudoRosterItem {
* Inserts a new registration into the database.
*/
private void insertIntoDb() throws SQLException {
this.registrationID = SequenceManager.nextID(this);
Connection con = null;
PreparedStatement pstmt = null;
boolean abortTransaction = false;
......
......@@ -10,6 +10,11 @@
package org.jivesoftware.wildfire.gateway;
import org.xmpp.packet.JID;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import java.util.Collection;
/**
* Handles retrieving pseudo rosters and other related tasks.
*
......@@ -17,4 +22,47 @@ package org.jivesoftware.wildfire.gateway;
*/
public class PseudoRosterManager {
/**
* Manages registration information.
* @see org.jivesoftware.wildfire.gateway.RegistrationManager
*/
public final RegistrationManager registrationManager = new RegistrationManager();
/**
* Retrieves a pseudo roster based off of a registration ID.
*
* @param registrationID To retrieve the roster for.
* @return A Pseudo roster
*/
public PseudoRoster getPseudoRoster(Long registrationID) {
return new PseudoRoster(registrationID);
}
/**
* Retrieves a pseudo roster based off of a registration.
*
* @param registration To retrieve the roster for.
* @return A Pseudo roster
*/
public PseudoRoster getPseudoRoster(Registration registration) {
return getPseudoRoster(registration.getRegistrationID());
}
/**
* Retrieves a pseudo roster based off of a registration.
*
* @param jid To retrieve the roster for.
* @param type TransportType the roster is for.
* @return A Pseudo roster
*/
public PseudoRoster getPseudoRoster(JID jid, TransportType type) throws UserNotFoundException {
Collection<Registration> registrations = registrationManager.getRegistrations(jid, type);
if (registrations.isEmpty()) {
// User is not registered with us.
throw new UserNotFoundException("Unable to find registration.");
}
Registration registration = registrations.iterator().next();
return getPseudoRoster(registration);
}
}
......@@ -324,6 +324,11 @@ public class Registration {
}
}
/**
* Load registration from database.
*
* @throws NotFoundException if registration was not found in database.
*/
private void loadFromDb() throws NotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
......
......@@ -279,6 +279,15 @@ public abstract class TransportSession implements Runnable {
*/
public abstract void sendMessage(JID jid, String message);
/**
* Sends an outgoing message directly to the legacy serivce.
*
* Doesn't -have- to do anything. Only occasionally useful.
*
* @param message Message to be sent.
*/
public abstract void sendServerMessage(String message);
/**
* Asks the legacy service to send a presence packet for a contact.
*
......
......@@ -13,11 +13,14 @@ package org.jivesoftware.wildfire.gateway.protocols.irc;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCUser;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUtil;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
import org.xmpp.packet.JID;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.gateway.PresenceType;
import java.util.Date;
import java.util.*;
/**
* Handles listening for IRC events.
......@@ -30,11 +33,31 @@ public class IRCListener implements IRCEventListener {
this.session = session;
}
/**
* Timer to check for online status.
*/
private Timer timer = new Timer();
/**
* Interval at which status is checked.
*/
private int timerInterval = 60000; // 1 minute
/**
* Session this listener is associated with.
*/
IRCSession session;
/**
* Effectively enables or disables the listener functionality, without burying it yet.
*/
Boolean silenced = false;
/**
* Status checker.
*/
StatusCheck statusCheck;
/**
* Retrieves the session this listener is associated with.
*/
......@@ -43,22 +66,29 @@ public class IRCListener implements IRCEventListener {
}
public void onRegistered() {
Log.debug("IRC registered");
getSession().getRegistration().setLastLogin(new Date());
Presence p = new Presence();
p.setFrom(getSession().getTransport().getJID());
p.setTo(getSession().getJID());
getSession().getTransport().sendPacket(p);
statusCheck = new StatusCheck();
timer.schedule(statusCheck, timerInterval, timerInterval);
}
public void onDisconnected() {
Log.debug("IRC disconnected");
Presence p = new Presence(Presence.Type.unavailable);
p.setTo(getSession().getJID());
p.setFrom(getSession().getTransport().getJID());
getSession().getTransport().sendPacket(p);
getSession().getConnection().close();
timer.cancel();
}
public void onError(String string) {
Log.debug("IRC error: "+string);
if (silenced) { return; }
Message m = new Message();
m.setType(Message.Type.error);
m.setFrom(getSession().getTransport().getJID());
......@@ -68,6 +98,8 @@ public class IRCListener implements IRCEventListener {
}
public void onError(int i, String string) {
Log.debug("IRC error: "+i+", "+string);
if (silenced) { return; }
Message m = new Message();
m.setType(Message.Type.error);
m.setFrom(getSession().getTransport().getJID());
......@@ -89,7 +121,7 @@ public class IRCListener implements IRCEventListener {
}
public void onMode(String string, IRCUser ircUser, IRCModeParser ircModeParser) {
Log.debug("IRC mode: "+string+", "+ircUser+", "+ircModeParser);
Log.debug("IRC mode: "+string+", "+ircUser+", "+ircModeParser);
}
public void onMode(IRCUser ircUser, String string, String string1) {
......@@ -102,6 +134,24 @@ public class IRCListener implements IRCEventListener {
public void onNotice(String string, IRCUser ircUser, String string1) {
Log.debug("IRC notice: "+string+", "+ircUser+", "+string1);
if (silenced) { return; }
String username = ircUser.getNick();
if (username == null) {
username = ircUser.getUsername();
}
JID from;
if (username == null) {
from = getSession().getTransport().getJID();
}
else {
from = getSession().getTransport().convertIDToJID(username);
}
Message m = new Message();
m.setType(Message.Type.chat);
m.setFrom(from);
m.setTo(getSession().getJIDWithHighestPriority());
m.setBody(string1);
getSession().getTransport().sendPacket(m);
}
public void onPart(String string, IRCUser ircUser, String string1) {
......@@ -113,6 +163,12 @@ public class IRCListener implements IRCEventListener {
}
public void onPrivmsg(String chan, IRCUser ircUser, String msg) {
Log.debug("IRC privmsg: "+chan+", "+ircUser+", "+msg);
if (silenced) { return; }
if (msg.equals("VERSION")) {
// TODO: Investigate if I should respond to this. What do other clients do?
return;
}
Message m = new Message();
m.setType(Message.Type.chat);
m.setFrom(getSession().getTransport().convertIDToJID(ircUser.getNick()));
......@@ -122,6 +178,7 @@ public class IRCListener implements IRCEventListener {
}
public void onQuit(IRCUser ircUser, String string) {
Log.debug("IRC quit: "+ircUser+", "+string);
Presence p = new Presence(Presence.Type.unavailable);
p.setTo(getSession().getJID());
p.setFrom(getSession().getTransport().getJID());
......@@ -131,6 +188,30 @@ public class IRCListener implements IRCEventListener {
public void onReply(int i, String string, String string1) {
Log.debug("IRC reply: "+i+", "+string+", "+string1);
if (silenced) { return; }
if (i == IRCUtil.RPL_ISON) {
String[] onlineContacts = string.split(" ");
ArrayList<String> onlineContactList = new ArrayList<String>();
// Lets see who all is on
for (String contact : onlineContacts) {
onlineContactList.add(contact);
getSession().setBuddyStatus(contact, PresenceType.available);
}
// Now lets compare with who all is not on
for (String contact : getSession().getBuddyStatuses().keySet()) {
if (!onlineContactList.contains(contact)) {
getSession().setBuddyStatus(contact, PresenceType.unavailable);
}
}
}
else {
Message m = new Message();
m.setType(Message.Type.chat);
m.setFrom(getSession().getTransport().getJID());
m.setTo(getSession().getJIDWithHighestPriority());
m.setBody(string1);
getSession().getTransport().sendPacket(m);
}
}
public void onTopic(String string, IRCUser ircUser, String string1) {
......@@ -141,4 +222,23 @@ public class IRCListener implements IRCEventListener {
Log.debug("Unknown IRC message: "+string+", "+string1+", "+string2+", "+string3);
}
public void setSilenced(Boolean setting) {
silenced = setting;
}
private class StatusCheck extends TimerTask {
/**
* Send ISON to IRC to check on status of contacts.
*/
public void run() {
String buddyList = "";
for (String buddy : getSession().getBuddyStatuses().keySet()) {
buddyList = buddyList + " " + buddy;
}
if (!buddyList.equals("")) {
getSession().getConnection().doIson(buddyList);
}
}
}
}
......@@ -10,15 +10,15 @@
package org.jivesoftware.wildfire.gateway.protocols.irc;
import org.jivesoftware.wildfire.gateway.TransportSession;
import org.jivesoftware.wildfire.gateway.PresenceType;
import org.jivesoftware.wildfire.gateway.Registration;
import org.jivesoftware.wildfire.gateway.*;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.schwering.irc.lib.IRCConnection;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
/**
* Represents an IRC session.
......@@ -30,6 +30,8 @@ import java.io.IOException;
*/
public class IRCSession extends TransportSession {
final private PseudoRosterManager pseudoRosterManager = new PseudoRosterManager();
/**
* Create a MSN Session instance.
*
......@@ -41,6 +43,11 @@ public class IRCSession extends TransportSession {
public IRCSession(Registration registration, JID jid, IRCTransport transport, Integer priority) {
super(registration, jid, transport, priority);
pseudoRoster = pseudoRosterManager.getPseudoRoster(registration);
for (String contact : pseudoRoster.getContacts()) {
buddyStatuses.put(contact, PresenceType.unavailable);
}
String server = "irc.freenode.net";
int[] ports = new int[] { 7000, 6667 };
String username = registration.getUsername();
......@@ -52,14 +59,32 @@ public class IRCSession extends TransportSession {
conn.setPong(true);
conn.setDaemon(false);
conn.setColors(false);
conn.addIRCEventListener(new IRCListener(this));
ircListener = new IRCListener(this);
conn.addIRCEventListener(ircListener);
}
/**
* Our pseudo roster.
*
* No server side buddy list, so we track it all here.
*/
private PseudoRoster pseudoRoster;
/**
* IRC connection.
*/
public IRCConnection conn;
/**
* IRC listener.
*/
IRCListener ircListener;
/**
* Tracks status of 'buddy list'.
*/
ConcurrentHashMap<String, PresenceType> buddyStatuses = new ConcurrentHashMap<String, PresenceType>();
/**
* Logs the session into IRC.
*
......@@ -79,9 +104,45 @@ public class IRCSession extends TransportSession {
* Logs the session out of IRC.
*/
public void logOut() {
ircListener.setSilenced(true);
conn.doQuit();
}
/**
* Retrieves the buddy status list.
*/
public ConcurrentHashMap<String, PresenceType> getBuddyStatuses() {
return buddyStatuses;
}
/**
* Gets the current presence status of a buddy.
*
* @param username Username to look up.
*/
public PresenceType getBuddyStatus(String username) {
return buddyStatuses.get(username);
}
/**
* Updates the current presence status of a buddy.
*
* @param username Username to set presence of.
* @param presenceType New presence type.
*/
public void setBuddyStatus(String username, PresenceType presenceType) {
if (!buddyStatuses.get(username).equals(presenceType)) {
Presence p = new Presence();
if (presenceType.equals(PresenceType.unavailable)) {
p.setType(Presence.Type.unavailable);
}
p.setTo(getJID());
p.setFrom(getTransport().convertIDToJID(username));
getTransport().sendPacket(p);
}
buddyStatuses.put(username, presenceType);
}
/**
* Returns the IRC connection associated with this session.
*/
......@@ -113,21 +174,45 @@ public class IRCSession extends TransportSession {
* @see org.jivesoftware.wildfire.gateway.TransportSession#addContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void addContact(RosterItem item) {
// TODO: Handle this
String contact = getTransport().convertJIDToID(item.getJid());
if (pseudoRoster.hasItem(contact)) {
PseudoRosterItem rosterItem = pseudoRoster.getItem(contact);
rosterItem.setNickname(item.getNickname());
rosterItem.setGroups(item.getGroups().toString());
conn.doIson(contact);
}
else {
pseudoRoster.createItem(contact, item.getNickname(), item.getGroups().toString());
buddyStatuses.put(contact, PresenceType.unavailable);
conn.doIson(contact);
}
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#removeContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void removeContact(RosterItem item) {
// TODO: Handle this
String contact = getTransport().convertJIDToID(item.getJid());
pseudoRoster.removeItem(contact);
buddyStatuses.remove(contact);
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#updateContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void updateContact(RosterItem item) {
// TODO: Handle this
String contact = getTransport().convertJIDToID(item.getJid());
if (pseudoRoster.hasItem(contact)) {
PseudoRosterItem rosterItem = pseudoRoster.getItem(contact);
rosterItem.setNickname(item.getNickname());
rosterItem.setGroups(item.getGroups().toString());
conn.doIson(contact);
}
else {
pseudoRoster.createItem(contact, item.getNickname(), item.getGroups().toString());
buddyStatuses.put(contact, PresenceType.unavailable);
conn.doIson(contact);
}
}
/**
......@@ -137,18 +222,40 @@ public class IRCSession extends TransportSession {
conn.doPrivmsg(getTransport().convertJIDToID(jid), message);
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#sendServerMessage(String)
*/
public void sendServerMessage(String message) {
conn.send(message);
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#retrieveContactStatus(org.xmpp.packet.JID)
*/
public void retrieveContactStatus(JID jid) {
// TODO: Handle this
String contact = getTransport().convertJIDToID(jid);
Presence p = new Presence();
if (buddyStatuses.get(contact).equals(PresenceType.unavailable)) {
p.setType(Presence.Type.unavailable);
}
p.setTo(jid);
p.setFrom(getTransport().convertIDToJID(contact));
getTransport().sendPacket(p);
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#resendContactStatuses(org.xmpp.packet.JID)
*/
public void resendContactStatuses(JID jid) {
// TODO: Handle this
for (String contact : buddyStatuses.keySet()) {
Presence p = new Presence();
if (buddyStatuses.get(contact).equals(PresenceType.unavailable)) {
p.setType(Presence.Type.unavailable);
}
p.setTo(jid);
p.setFrom(getTransport().convertIDToJID(contact));
getTransport().sendPacket(p);
}
}
}
......@@ -252,6 +252,13 @@ public class MSNSession extends TransportSession {
msnMessenger.sendText(Email.parseStr(getTransport().convertJIDToID(jid)), message);
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#sendServerMessage(String)
*/
public void sendServerMessage(String message) {
// We don't care.
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#retrieveContactStatus(org.xmpp.packet.JID)
*/
......@@ -274,10 +281,10 @@ public class MSNSession extends TransportSession {
if (isLoggedIn()) {
msnMessenger.getOwner().setStatus(((MSNTransport)getTransport()).convertJabStatusToMSN(presenceType));
}
// else {
// // Hrm, not logged in? Lets fix that.
// msnMessenger.login();
// }
else {
// Hrm, not logged in? Lets fix that.
msnMessenger.login();
}
}
/**
......
......@@ -236,6 +236,13 @@ public class OSCARSession extends TransportSession {
request(new SendImIcbm(getTransport().convertJIDToID(jid), message));
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#sendServerMessage(String)
*/
public void sendServerMessage(String message) {
// We don't care.
}
/**
* Opens/creates a new BOS connection to a specific server and port, given a cookie.
*
......@@ -286,7 +293,8 @@ public class OSCARSession extends TransportSession {
snacMgr.addRequest(request);
request(new ServiceRequest(family));
} else {
Log.error("eep! can't find a service redirector server.");
// TODO: Why does this occur a lot and yet not cause problems?
// Log.error("eep! can't find a service redirector server.");
}
}
}
......
......@@ -14,10 +14,7 @@ import java.io.IOException;
import java.util.*;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.gateway.PresenceType;
import org.jivesoftware.wildfire.gateway.Registration;
import org.jivesoftware.wildfire.gateway.TransportBuddy;
import org.jivesoftware.wildfire.gateway.TransportSession;
import org.jivesoftware.wildfire.gateway.*;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.xmpp.packet.JID;
......@@ -38,6 +35,8 @@ import ymsg.network.YahooUser;
*/
public class YahooSession extends TransportSession {
final private PseudoRosterManager pseudoRosterManager = new PseudoRosterManager();
/**
* Create a Yahoo Session instance.
*
......@@ -49,10 +48,19 @@ public class YahooSession extends TransportSession {
public YahooSession(Registration registration, JID jid, YahooTransport transport, Integer priority) {
super(registration, jid, transport, priority);
pseudoRoster = pseudoRosterManager.getPseudoRoster(registration);
yahooSession = new Session();
yahooSession.addSessionListener(new YahooSessionListener(this));
}
/**
* Our pseudo roster.
*
* We only really use it for nickname tracking.
*/
private PseudoRoster pseudoRoster;
/**
* Are we logged in?
*/
......@@ -192,22 +200,28 @@ public class YahooSession extends TransportSession {
* @see org.jivesoftware.wildfire.gateway.TransportSession#addContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void addContact(RosterItem item) {
// TODO: Sync nickname (local storage)
// Syncing will take are of add.
String contact = getTransport().convertJIDToID(item.getJid());
syncContactGroups(contact, item.getGroups());
if (pseudoRoster.hasItem(contact)) {
PseudoRosterItem rosterItem = pseudoRoster.getItem(contact);
rosterItem.setNickname(item.getNickname());
}
else {
pseudoRoster.createItem(contact, item.getNickname(), null);
}
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#removeContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void removeContact(RosterItem item) {
// TODO: Clear local stored nickname.
String contact = getTransport().convertJIDToID(item.getJid());
for (YahooGroup yahooGroup : yahooSession.getGroups()) {
if (yahooGroup.getIndexOfFriend(contact) != -1) {
try {
yahooSession.removeFriend(contact, yahooGroup.getName());
pseudoRoster.removeItem(contact);
}
catch (IOException e) {
Log.error("Failed to remove yahoo user.");
......@@ -220,9 +234,15 @@ public class YahooSession extends TransportSession {
* @see org.jivesoftware.wildfire.gateway.TransportSession#updateContact(org.jivesoftware.wildfire.roster.RosterItem)
*/
public void updateContact(RosterItem item) {
// TODO: Sync nickname (local storage)
String contact = getTransport().convertJIDToID(item.getJid());
syncContactGroups(contact, item.getGroups());
String contact = getTransport().convertJIDToID(item.getJid());
syncContactGroups(contact, item.getGroups());
if (pseudoRoster.hasItem(contact)) {
PseudoRosterItem rosterItem = pseudoRoster.getItem(contact);
rosterItem.setNickname(item.getNickname());
}
else {
pseudoRoster.createItem(contact, item.getNickname(), null);
}
}
/**
......@@ -289,6 +309,13 @@ public class YahooSession extends TransportSession {
}
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#sendServerMessage(String)
*/
public void sendServerMessage(String message) {
// We don't care.
}
/**
* @see org.jivesoftware.wildfire.gateway.TransportSession#updateStatus(org.jivesoftware.wildfire.gateway.PresenceType, String)
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment