001: /**
002: * $Revision$
003: * $Date$
004: *
005: * Copyright (C) 2007 Jive Software. All rights reserved.
006: *
007: * This software is published under the terms of the GNU Public License (GPL),
008: * a copy of which is included in this distribution.
009: */package org.jivesoftware.openfire.gateway.protocols.irc;
010:
011: import org.jivesoftware.openfire.gateway.muc.MUCTransportSession;
012: import org.jivesoftware.openfire.gateway.type.PresenceType;
013: import org.jivesoftware.openfire.gateway.session.TransportSession;
014: import org.xmpp.packet.Presence;
015: import org.xmpp.packet.Message;
016: import org.dom4j.Element;
017: import org.apache.log4j.Logger;
018:
019: import java.lang.ref.WeakReference;
020: import java.util.ArrayList;
021:
022: import f00f.net.irc.martyr.services.AutoJoin;
023: import f00f.net.irc.martyr.commands.*;
024:
025: /**
026: * @author Daniel Henninger
027: */
028:
029: public class IRCMUCSession extends MUCTransportSession {
030:
031: static Logger Log = Logger.getLogger(IRCMUCSession.class);
032:
033: public IRCMUCSession(TransportSession session, String roomname,
034: String nickname, IRCMUCTransport transport) {
035: super (session, roomname, nickname, transport);
036: ircSessionRef = new WeakReference<IRCSession>(
037: (IRCSession) session);
038: }
039:
040: /* IRC session for conveniences. */
041: WeakReference<IRCSession> ircSessionRef = null;
042:
043: AutoJoin autoJoin;
044:
045: public IRCSession getSession() {
046: return ircSessionRef.get();
047: }
048:
049: /* List of known contacts in this room. */
050: public ArrayList<String> contacts = new ArrayList<String>();
051:
052: /**
053: * Retrieve list of contacts from this chat room.
054: *
055: * @return List of contacts in this room.
056: */
057: public ArrayList<String> getContacts() {
058: return contacts;
059: }
060:
061: public void enterRoom() {
062: autoJoin = new AutoJoin(getSession().getConnection(), roomname);
063: }
064:
065: public void leaveRoom() {
066: try {
067: getSession().getConnection().sendCommand(
068: new PartCommand(roomname));
069: } catch (Exception e) {
070: Log.debug("IRC: Error while trying to part chat room:", e);
071: }
072: try {
073: autoJoin.disable();
074: } catch (Exception e) {
075: // Ignore
076: }
077: autoJoin = null;
078: for (String contact : getContacts()) {
079: Presence p = new Presence();
080: p.setType(Presence.Type.unavailable);
081: p.setTo(session.getJID());
082: p.setFrom(transport.convertIDToJID(roomname, contact));
083: Element elem = p.addChildElement("x",
084: "http://jabber.org/protocol/muc#user");
085: Element item = elem.addElement("item");
086: item.addAttribute("affiliation", "none");
087: item.addAttribute("role", "none");
088: transport.sendPacket(p);
089: }
090: Presence p = new Presence();
091: p.setType(Presence.Type.unavailable);
092: p.setTo(session.getJID());
093: p.setFrom(transport.convertIDToJID(roomname, nickname));
094: Element elem = p.addChildElement("x",
095: "http://jabber.org/protocol/muc#user");
096: Element item = elem.addElement("item");
097: item.addAttribute("affiliation", "none");
098: item.addAttribute("role", "none");
099: Element status = elem.addElement("status");
100: status.addAttribute("code", "110");
101: transport.sendPacket(p);
102: }
103:
104: public void sendMessage(String message) {
105: getSession().getConnection().sendCommand(
106: new MessageCommand(roomname, message));
107: // Return the favor.
108: transport.sendMessage(session.getJID(), transport
109: .convertIDToJID(roomname, nickname), message,
110: Message.Type.groupchat);
111: }
112:
113: public void sendPrivateMessage(String nickname, String message) {
114: getSession().sendMessage(
115: getSession().getTransport().convertIDToJID(nickname),
116: message);
117: }
118:
119: public void updateStatus(PresenceType presenceType) {
120: // This should be taken care of by the main transport.
121: }
122:
123: public void updateTopic(String topic) {
124: getSession().getConnection().sendCommand(
125: new TopicCommand(roomname, topic));
126: }
127:
128: public void kickUser(String nickname, String reason) {
129: getSession().getConnection().sendCommand(
130: new KickCommand(roomname, nickname, reason));
131: }
132:
133: public void grantVoice(String nickname) {
134: // TODO: Map into IRC
135: }
136:
137: public void revokeVoice(String nickname) {
138: // TODO: Map into IRC
139: }
140:
141: public void banUser(String nickname, String reason) {
142: // TODO: Map into IRC
143: }
144:
145: public void grantMembership(String nickname) {
146: // TODO: Does this map into IRC?
147: }
148:
149: public void revokeMembership(String nickname) {
150: // TODO: Does this map into IRC?
151: }
152:
153: public void grantModerator(String nickname) {
154: // TODO: Map into IRC
155: }
156:
157: public void revokeModerator(String nickname) {
158: // TODO: Map into IRC
159: }
160:
161: }
|