/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.components.stats;
import galena.Kernel;
import galena.addins.modules.database.DB;
import net.sphene.goim.components.ExternalComponentManagerAddin;
import org.dom4j.Element;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
public class StatsComponent implements Component {
public static final String SUBDOMAIN = "stats";
private JID componentJID;
private ExternalComponentManagerAddin manager;
private DB db;
private StatsAddin addin;
public StatsComponent(ExternalComponentManagerAddin manager, DB db, StatsAddin addin) {
this.manager = manager;
this.db = db;
this.addin = addin;
}
public String getName() {
return "GOIM Stats Component";
}
public String getDescription() {
return "Collects Statistics about online games";
}
public void processPacket(Packet packet) {
Kernel.debug(this,"Received packet: " + packet.toString(),6);
if(packet instanceof IQ) {
IQ iq = (IQ)packet;
if(iq.getType() == IQ.Type.get || iq.getType() == IQ.Type.set) {
Element element = iq.getChildElement();
if(element.getName().equals("query")) {
IQ result = new IQ(IQ.Type.result,iq.getID());
result.setTo(iq.getFrom());
result.setFrom(iq.getTo());
Element childResult = result.setChildElement(element.getName(),element.getNamespaceURI());
if(element.getNamespaceURI().equals("http://jabber.org/protocol/disco#info")) {
//Element discoResult = result.setChildElement("query","http://jabber.org/protocol/disco#info");
Element identity = childResult.addElement("identity");
identity.addAttribute("category","gateway");
identity.addAttribute("type","GOIMstats");
identity.addAttribute("name",getName());
Element feature = childResult.addElement("feature");
feature.addAttribute("var","http://jabber.org/protocol/disco");
feature = childResult.addElement("feature");
feature.addAttribute("var","jabber:iq:register");
manager.sendPacket(this,result);
} else if(element.getNamespaceURI().equals("http://jabber.org/protocol/disco#items")) {
//Element discoResult = result.setChildElement("query","http://jabber.org/protocol/disco#items");
} else
result = null;
if(result != null)
manager.sendPacket(this,result);
}
}
} else if(packet instanceof Presence) {
Presence presence = (Presence)packet;
if(presence.getTo().equals(componentJID)) {
Kernel.debug(this,"Presence packet to: " + packet.getTo().toString() + " .. type: " + presence.getType(),3);
if(presence.getType() == null || presence.getType() == Presence.Type.probe) { // available
Presence reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
manager.sendPacket(this,reply);
} else if(presence.getType() == Presence.Type.subscribe) {
Presence reply = new Presence(Presence.Type.subscribed);
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
manager.sendPacket(this,reply);
}
} else {
if(presence.getType() == Presence.Type.subscribe) {
Presence reply = new Presence(Presence.Type.subscribed);
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
manager.sendPacket(this,reply);
//addSubscription(presence.getFrom().toBareJID(),presence.getTo().getNode());
} else if(presence.getType() == null || presence.getType() == Presence.Type.probe) { // available
//Kernel.debug(this,"Available presence packet to: " + presence.getTo().toString(),2);
Presence reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
manager.sendPacket(this,reply);
//addSubscription(presence.getFrom().toBareJID(),presence.getTo().getNode());
}
}
}
}
public void initialize(JID jid, ComponentManager compManager)
throws ComponentException {
if(!jid.toString().startsWith(getSubdomain()))
componentJID = new JID(getSubdomain() + "." + jid);
else
componentJID = jid;
Kernel.debug(this,"Initialize with JID: " + jid.toString() + " custom: " + componentJID.toString(),3);
}
private String getSubdomain() {
return SUBDOMAIN;
}
public void start() {
}
public void shutdown() {
}
}