]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/Source.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / Source.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\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
14 /*  the License.                                     */\r
15 /*                                                   */\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
21 /*                                                   */\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
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 /**\r
33  * The root source.\r
34  */\r
35 public abstract class Source extends IRCObject {\r
36         /**\r
37          * The server.\r
38          */\r
39         protected Server _server;\r
40         private ListenerGroup _listeners;\r
41         /**\r
42          * The interpretor.\r
43          */\r
44         protected Interpretor _in;\r
45 \r
46         /**\r
47          * Create a new Source.\r
48          * \r
49          * @param config\r
50          *          the global configuration.\r
51          * @param s\r
52          *          the bound server.\r
53          */\r
54         public Source(IRCConfiguration config, Server s) {\r
55                 super(config);\r
56                 _listeners = new ListenerGroup();\r
57                 _in = new NullInterpretor(config);\r
58                 _server = s;\r
59         }\r
60 \r
61         @Override\r
62         public void release() {\r
63                 _in = new NullInterpretor(_ircConfiguration);\r
64                 super.release();\r
65         }\r
66 \r
67         /**\r
68          * Get this source name.\r
69          * \r
70          * @return source name.\r
71          */\r
72         public abstract String getName();\r
73 \r
74         /**\r
75          * Get this source type.\r
76          * \r
77          * @return source type.\r
78          */\r
79         public abstract String getType();\r
80 \r
81         /**\r
82          * Test wether this source can accept messages from user.\r
83          * \r
84          * @return true if this source accepts user input, false otherwise.\r
85          */\r
86         public abstract boolean talkable();\r
87 \r
88         /**\r
89          * Request to leave (close) this source.\r
90          */\r
91         public abstract void leave();\r
92 \r
93         /**\r
94          * Set this source's interpretor.\r
95          * \r
96          * @param in\r
97          *          new interpretor.\r
98          */\r
99         public void setInterpretor(Interpretor in) {\r
100                 _in = in;\r
101         }\r
102 \r
103         /**\r
104          * Send a string to this source.\r
105          * \r
106          * @param str\r
107          *          user input.\r
108          */\r
109         public void sendString(String str) {\r
110                 _in.sendString(this, str);\r
111         }\r
112 \r
113         /**\r
114          * Send a string from user input to this source. The string is filtered\r
115          * against unauthorized commands.\r
116          * \r
117          * @param str\r
118          *          user input.\r
119          */\r
120         public void sendUserString(String str) {\r
121                 if (!str.startsWith("/"))\r
122                         sendString(str);\r
123                 else {\r
124                         String cmd = str.substring(1).trim();\r
125                         int pos = cmd.indexOf(' ');\r
126                         if (pos >= 0)\r
127                                 cmd = cmd.substring(0, pos);\r
128                         if (_ircConfiguration.mayCommand(cmd))\r
129                                 sendString(str);\r
130                 }\r
131         }\r
132 \r
133         /**\r
134          * Get this source's interpretor.\r
135          * \r
136          * @return this source's interpretor.\r
137          */\r
138         public Interpretor getInterpretor() {\r
139                 return _in;\r
140         }\r
141 \r
142         /**\r
143          * Request this source to clear all the history it could have.\r
144          */\r
145         public void clear() {\r
146                 _listeners.sendEvent("clear", this);\r
147         }\r
148 \r
149         /**\r
150          * Notify this source a new message has arrived.\r
151          * \r
152          * @param source\r
153          *          the source of the message. It can be a user nickname or a channel\r
154          *          name.\r
155          * @param msg\r
156          *          the message.\r
157          */\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
162 \r
163                         String cmd = "";\r
164                         String param = "";\r
165                         int pos = msg.indexOf(' ');\r
166                         if (pos == -1) {\r
167                                 cmd = msg.toLowerCase(java.util.Locale.ENGLISH);\r
168                         } else {\r
169                                 cmd = msg.substring(0, pos).toLowerCase(java.util.Locale.ENGLISH);\r
170                                 param = msg.substring(pos + 1);\r
171                         }\r
172 \r
173                         if (cmd.equals("action"))\r
174                                 action(source, param);\r
175                         else\r
176                                 getServer().sendStatusMessage(\r
177                                                 "\2\3" + "4" + "[" + source + " " + cmd.toUpperCase(java.util.Locale.ENGLISH) + "]");\r
178                 } else {\r
179                         _listeners.sendEvent("messageReceived", source, msg, this);\r
180                 }\r
181         }\r
182 \r
183         /**\r
184          * Notify this source a new notice message has arrived.\r
185          * \r
186          * @param source\r
187          *          the source of the message. It can be a user nickname or a channel\r
188          *          name.\r
189          * @param msg\r
190          *          the message.\r
191          */\r
192         public void noticeReceived(String source, String msg) {\r
193                 _listeners.sendEvent("noticeReceived", source, msg, this);\r
194         }\r
195 \r
196         /**\r
197          * Notify this source a new action message has arrived.\r
198          * \r
199          * @param nick\r
200          *          the user who sent the action.\r
201          * @param msg\r
202          *          the message.\r
203          */\r
204         public void action(String nick, String msg) {\r
205                 _listeners.sendEvent("action", nick, msg, this);\r
206         }\r
207 \r
208         /**\r
209          * Notify this source a new report has arrived.\r
210          * \r
211          * @param msg\r
212          *          the report message.\r
213          */\r
214         public void report(String msg) {\r
215                 _listeners.sendEvent("reportReceived", msg, this);\r
216         }\r
217 \r
218         /**\r
219          * Add a new SourceListener.\r
220          * \r
221          * @param lis\r
222          *          listener to add.\r
223          */\r
224         public void addSourceListener(SourceListener lis) {\r
225                 _listeners.addListener(lis);\r
226         }\r
227 \r
228         /**\r
229          * Remove a SourceListener.\r
230          * \r
231          * @param lis\r
232          *          the listener to remove.\r
233          */\r
234         public void removeSourceListener(SourceListener lis) {\r
235                 _listeners.removeListener(lis);\r
236         }\r
237 \r
238         /**\r
239          * Get the source server.\r
240          * \r
241          * @return the source server.\r
242          */\r
243         public Server getServer() {\r
244                 return _server;\r
245         }\r
246 \r
247         /**\r
248          * Test whether this source may be used as a default source for system event\r
249          * output.\r
250          * \r
251          * @return true if this source may be used as a default source, false\r
252          *         otherwise.\r
253          */\r
254         public boolean mayDefault() {\r
255                 return true;\r
256         }\r
257 \r
258 }\r