1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
35 public abstract class Source extends IRCObject {
\r
39 protected Server _server;
\r
40 private ListenerGroup _listeners;
\r
44 protected Interpretor _in;
\r
47 * Create a new Source.
\r
50 * the global configuration.
\r
54 public Source(IRCConfiguration config, Server s) {
\r
56 _listeners = new ListenerGroup();
\r
57 _in = new NullInterpretor(config);
\r
62 public void release() {
\r
63 _in = new NullInterpretor(_ircConfiguration);
\r
68 * Get this source name.
\r
70 * @return source name.
\r
72 public abstract String getName();
\r
75 * Get this source type.
\r
77 * @return source type.
\r
79 public abstract String getType();
\r
82 * Test wether this source can accept messages from user.
\r
84 * @return true if this source accepts user input, false otherwise.
\r
86 public abstract boolean talkable();
\r
89 * Request to leave (close) this source.
\r
91 public abstract void leave();
\r
94 * Set this source's interpretor.
\r
99 public void setInterpretor(Interpretor in) {
\r
104 * Send a string to this source.
\r
109 public void sendString(String str) {
\r
110 _in.sendString(this, str);
\r
114 * Send a string from user input to this source. The string is filtered
\r
115 * against unauthorized commands.
\r
120 public void sendUserString(String str) {
\r
121 if (!str.startsWith("/"))
\r
124 String cmd = str.substring(1).trim();
\r
125 int pos = cmd.indexOf(' ');
\r
127 cmd = cmd.substring(0, pos);
\r
128 if (_ircConfiguration.mayCommand(cmd))
\r
134 * Get this source's interpretor.
\r
136 * @return this source's interpretor.
\r
138 public Interpretor getInterpretor() {
\r
143 * Request this source to clear all the history it could have.
\r
145 public void clear() {
\r
146 _listeners.sendEvent("clear", this);
\r
150 * Notify this source a new message has arrived.
\r
153 * the source of the message. It can be a user nickname or a channel
\r
158 public void messageReceived(String source, String msg) {
\r
159 if (msg.startsWith("\1")) {
\r
160 msg = msg.substring(1);
\r
161 msg = msg.substring(0, msg.length() - 1);
\r
165 int pos = msg.indexOf(' ');
\r
167 cmd = msg.toLowerCase(java.util.Locale.ENGLISH);
\r
169 cmd = msg.substring(0, pos).toLowerCase(java.util.Locale.ENGLISH);
\r
170 param = msg.substring(pos + 1);
\r
173 if (cmd.equals("action"))
\r
174 action(source, param);
\r
176 getServer().sendStatusMessage(
\r
177 "\2\3" + "4" + "[" + source + " " + cmd.toUpperCase(java.util.Locale.ENGLISH) + "]");
\r
179 _listeners.sendEvent("messageReceived", source, msg, this);
\r
184 * Notify this source a new notice message has arrived.
\r
187 * the source of the message. It can be a user nickname or a channel
\r
192 public void noticeReceived(String source, String msg) {
\r
193 _listeners.sendEvent("noticeReceived", source, msg, this);
\r
197 * Notify this source a new action message has arrived.
\r
200 * the user who sent the action.
\r
204 public void action(String nick, String msg) {
\r
205 _listeners.sendEvent("action", nick, msg, this);
\r
209 * Notify this source a new report has arrived.
\r
212 * the report message.
\r
214 public void report(String msg) {
\r
215 _listeners.sendEvent("reportReceived", msg, this);
\r
219 * Add a new SourceListener.
\r
224 public void addSourceListener(SourceListener lis) {
\r
225 _listeners.addListener(lis);
\r
229 * Remove a SourceListener.
\r
232 * the listener to remove.
\r
234 public void removeSourceListener(SourceListener lis) {
\r
235 _listeners.removeListener(lis);
\r
239 * Get the source server.
\r
241 * @return the source server.
\r
243 public Server getServer() {
\r
248 * Test whether this source may be used as a default source for system event
\r
251 * @return true if this source may be used as a default source, false
\r
254 public boolean mayDefault() {
\r