package com.wildfire.plugins;
import org.jivesoftware.wildfire.container.Plugin;
import org.jivesoftware.wildfire.container.PluginManager;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.*;
import org.xmpp.muc.JoinRoom;
import org.jivesoftware.wildfire.muc.MUCRoom;
import org.jivesoftware.wildfire.muc.MUCRole;
import org.jivesoftware.wildfire.muc.MultiUserChatServer;
import java.io.File;
import java.util.List;
/**
*/
public class ReplyBotPlugin implements Plugin, Component {
private static final String COMPONENT_NAME = “callcenter”;
public void initializePlugin(PluginManager manager, File pluginDirectory) {
ComponentManager componentManager = getComponentManager();
try {
componentManager.addComponent(COMPONENT_NAME, this);
}
catch (Exception e) {
componentManager.getLog().error(e);
}
}
public void destroyPlugin() {
ComponentManager componentManager = getComponentManager();
try {
componentManager.removeComponent(COMPONENT_NAME);
}
catch (Exception e) {
componentManager.getLog().error(e);
}
}
private ComponentManager getComponentManager() {
return ComponentManagerFactory.getComponentManager();
}
public String getName() {
return COMPONENT_NAME;
}
public String getDescription() {
return “AutoReply Bot”;
}
public void processPacket(Packet packet) {
if (packet instanceof Message) {
// Respond to incoming messages
Message message = (Message) packet;
processMessage(message);
//Component component = Component.initialize(packet.getTo(), getComponentManager()) ;
String conf = packet.getTo().toString();
{color:#ff0000}// Join an existing room or create a new one.
JoinRoom joinRoom = new JoinRoom(“helpdesk1@tzechyntan2004/Spark”, conf);
component.sendPacket(joinRoom);
} else if (packet instanceof Presence) {
// Respond to presence subscription request or presence probe
Presence presence = (Presence) packet;
processPresence(presence);
} else if (packet instanceof IQ) {
// Handle disco packets
IQ iq = (IQ) packet;
// Ignore IQs of type ERROR or RESULT
if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
return;
}
processIQ(iq);
}
}
private void processIQ(IQ iq) {
//todo handle disco packets.
}
private void processPresence(Presence presence) {
try {
if (Presence.Type.subscribe == presence.getType()) {
// Accept all presence requests if user has permissions
// Reply that the subscription request was approved or rejected
Presence reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
reply.setType(Presence.Type.subscribed);
getComponentManager().sendPacket(this, reply);
} else if (Presence.Type.unsubscribe == presence.getType()) {
// Send confirmation of unsubscription
Presence reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
reply.setType(Presence.Type.unsubscribed);
getComponentManager().sendPacket(this, reply);
} else if (Presence.Type.probe == presence.getType()) {
// Send that the service is available
Presence reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
getComponentManager().sendPacket(this, reply);
}
}
catch (ComponentException e) {
getComponentManager().getLog().error(e);
}
}
private void processMessage(Message message) {
//ignore any non-body message. The bot doesn’'t need to know that someone might be typing.
if (message.getBody() != null && message.getBody().length() > 0) {
Message response = new Message();
response.setSubject(message.getSubject());
response.setThread(message.getThread());
response.setType(message.getType());
response.setTo(message.getFrom());
response.setFrom(message.getTo());
response.setBody(“This is a autogenerated reply”);
try {
getComponentManager().sendPacket(this, response);
} catch (ComponentException e) {
getComponentManager().getLog().error(“failed to respond to message.”, e);
}
}
}
public void initialize(JID jid, ComponentManager componentManager) throws ComponentException {
}
public void start() {
}
public void shutdown() {
}
}
Message was edited by: Geordie