001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package chatserver;
016:
017: import java.io.*;
018: import java.net.*;
019: import org.quickserver.net.server.ClientHandler;
020: import java.util.*;
021: import java.util.logging.*;
022:
023: /**
024: *
025: * @author Akshathkumar Shetty
026: */
027: public class ChatMessaging {
028: private static final Logger logger = Logger
029: .getLogger(ChatMessaging.class.getName());
030:
031: public static void sendInfoMessage2Room(ClientHandler handler,
032: String room, String msg) throws SocketTimeoutException,
033: IOException {
034: if (room == null || msg == null)
035: return;
036: Iterator iterator = handler.getServer().findAllClientByKey(
037: ".+@" + room);
038: ClientHandler toHandler = null;
039: if (iterator.hasNext()) {
040: while (iterator.hasNext()) {
041: if (msg.equals("LoggedOut") == false)
042: handler.isConnected();//check if src is still alive
043: toHandler = (ClientHandler) iterator.next();
044: if (toHandler == handler)
045: continue;
046: sendInfoMessage(handler, toHandler, msg);
047: }
048: } /*else {
049: handler.sendClientMsg("{system.error} MessageNotSent No Clients in that room");
050: }*/
051: }
052:
053: public static void sendInfoMessage(ClientHandler handler,
054: ClientHandler toHandler, String message) throws IOException {
055: if (message == null)
056: return;
057: logger.fine("From: " + handler + "To: " + toHandler + ", "
058: + message);
059: if (toHandler == null)
060: return;
061:
062: ChatData data = (ChatData) handler.getClientData();
063: try {
064: toHandler.sendClientMsg("{user.info:" + data.getClientKey()
065: + "} " + message);
066: } catch (Exception e) {
067: logger.fine("Error sending msg: " + e);
068: }
069: }
070:
071: public static void sendMessageBwUsers(ClientHandler handler,
072: ClientHandler toHandler, String message) throws IOException {
073: logger.fine("From: " + handler + "To: " + toHandler + ", "
074: + message);
075: if (toHandler == null)
076: return;
077:
078: ChatData data = (ChatData) handler.getClientData();
079: try {
080: ChatData toCd = (ChatData) toHandler.getClientData();
081: toHandler.sendClientMsg("{user.msg:" + data.getClientKey()
082: + ":" + toCd.getClientKey() + "} " + message);
083: handler.sendClientMsg("{system.debug} SentMessageTo "
084: + toCd.getClientKey());
085: } catch (Exception e) {
086: logger.warning("Error sending msg: " + e);
087: handler.sendClientMsg("{system.error} MessageNotSent "
088: + e.getMessage());
089: }
090: }
091:
092: public static void sendMessage2Room(ClientHandler handler,
093: ClientHandler toHandler, String message) throws IOException {
094: logger.fine("From: " + handler + "To: " + toHandler + ", "
095: + message);
096: if (toHandler == null)
097: return;
098:
099: ChatData data = (ChatData) handler.getClientData();
100: try {
101: ChatData toCd = (ChatData) toHandler.getClientData();
102: toHandler.sendClientMsg("{user.msg:" + data.getClientKey()
103: + ":@" + toCd.getRoom() + "} " + message);
104: handler.sendClientMsg("{system.debug} SentMessageTo "
105: + toCd.getClientKey());
106: } catch (Exception e) {
107: logger.warning("Error sending msg: " + e);
108: handler.sendClientMsg("{system.error} MessageNotSent "
109: + e.getMessage());
110: }
111: }
112:
113: public static void printHelp(ClientHandler handler, String command)
114: throws IOException {
115: if (command != null)
116: handler.sendClientMsg("{system.error} BadCommand "
117: + command);
118: handler.sendClientMsg("{system.help} Sending Message Format:");
119: handler
120: .sendClientMsg("{system.help} sendMsg <<username>> <<message>>");
121: handler
122: .sendClientMsg("{system.help} sendMsg <<username@room>> <<message>>");
123: handler
124: .sendClientMsg("{system.help} sendMsgToRoom <<room_name>> <<message>>");
125: handler.sendClientMsg("{system.help} userList");
126: handler.sendClientMsg("{system.help} changeRoom <<room_name>>");
127: }
128:
129: public static void sendMsgToRoom(ClientHandler handler,
130: String command) throws SocketTimeoutException, IOException {
131: String room = null;
132: String message = null;
133: int i = command.indexOf(" ");
134: if (i == -1) {
135: handler.sendClientMsg("{system.error} BadCommand "
136: + command);
137: return;
138: }
139: int j = command.indexOf(" ", i + 1);
140: if (j == -1)
141: j = command.length();
142: room = command.substring(i + 1, j);
143:
144: if (j != command.length())
145: message = command.substring(j + 1);
146: else
147: message = "";
148:
149: Iterator iterator = handler.getServer().findAllClientByKey(
150: ".+@" + room);
151: if (iterator.hasNext()) {
152: while (iterator.hasNext()) {
153: handler.isConnected();//check if src is still alive
154: ChatMessaging.sendMessage2Room(handler,
155: (ClientHandler) iterator.next(), message);
156: }
157: } else {
158: handler
159: .sendClientMsg("{system.error} MessageNotSent No Clients in that room");
160: }
161: }
162:
163: public static void sendMsg(ClientHandler handler, String command)
164: throws SocketTimeoutException, IOException {
165: String id = null;
166: String message = null;
167: int i = command.indexOf(" ");
168: if (i == -1) {
169: handler.sendClientMsg("{system.error} BadCommand "
170: + command);
171: return;
172: }
173: int j = command.indexOf(" ", i + 1);
174: if (j == -1)
175: j = command.length();
176: id = command.substring(i + 1, j);
177: if (j != command.length())
178: message = command.substring(j + 1);
179: else
180: message = "";
181:
182: ClientHandler toHandler = null;
183: Iterator iterator = null;
184: if (id.indexOf("@") != -1) {
185: toHandler = handler.getServer().findClientByKey(id);
186: List list = new ArrayList();
187: if (toHandler != null) {
188: logger.finest("Found to by Key: " + toHandler);
189: list.add(toHandler);
190: }
191: iterator = list.iterator();
192: } else {
193: logger.finest("Finding to by Id");
194: iterator = handler.getServer().findAllClientById(id);
195: }
196:
197: if (iterator.hasNext()) {
198: while (iterator.hasNext()) {
199: handler.isConnected();//check if src is still alive
200: toHandler = (ClientHandler) iterator.next();
201: ChatMessaging.sendMessageBwUsers(handler, toHandler,
202: message);
203: }//end of while
204: } else {
205: handler
206: .sendClientMsg("{system.error} MessageNotSent No Client by that id");
207: }
208: }
209:
210: public static void sendRoomList(ClientHandler handler)
211: throws SocketTimeoutException, IOException {
212: ChatData data = (ChatData) handler.getClientData();
213: Iterator iterator = handler.getServer().findAllClientByKey(
214: ".+@" + data.getRoom());
215: if (iterator.hasNext()) {
216: while (iterator.hasNext()) {
217: handler.isConnected();//check if src is still alive
218: sendList(handler, (ClientHandler) iterator.next());
219: }
220: }
221: }
222:
223: public static void sendList(ClientHandler toHandler,
224: ClientHandler handler) throws IOException {
225: logger.finest("From: " + handler + "To: " + toHandler);
226: if (handler == null)
227: return;
228:
229: ChatData data = (ChatData) handler.getClientData();
230: try {
231: toHandler.sendClientMsg("{user.list} "
232: + data.getClientKey());
233: } catch (Exception e) {
234: logger.finest("Error sending msg: " + e);
235: }
236: }
237: }
|