001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.irc;
018
019 import java.net.URI;
020 import java.net.URISyntaxException;
021 import java.util.ArrayList;
022 import java.util.Arrays;
023 import java.util.List;
024
025 import org.apache.camel.RuntimeCamelException;
026 import org.apache.camel.util.ObjectHelper;
027 import org.schwering.irc.lib.ssl.SSLDefaultTrustManager;
028 import org.schwering.irc.lib.ssl.SSLTrustManager;
029
030 public class IrcConfiguration implements Cloneable {
031 private String target;
032 private List<String> channels = new ArrayList<String>();
033 private String hostname;
034 private String password;
035 private String nickname;
036 private String realname;
037 private String username;
038 private SSLTrustManager trustManager = new SSLDefaultTrustManager();
039 private boolean usingSSL;
040 private boolean persistent = true;
041 private boolean colors = true;
042 private boolean onNick = true;
043 private boolean onQuit = true;
044 private boolean onJoin = true;
045 private boolean onKick = true;
046 private boolean onMode = true;
047 private boolean onPart = true;
048 private boolean onReply;
049 private boolean onTopic = true;
050 private boolean onPrivmsg = true;
051 private int[] ports = {6667, 6668, 6669};
052
053 public IrcConfiguration() {
054 }
055
056 public IrcConfiguration(String hostname, String nickname, String displayname, List<String> channels) {
057 this.channels = channels;
058 this.hostname = hostname;
059 this.nickname = nickname;
060 this.username = nickname;
061 this.realname = displayname;
062 }
063
064 public IrcConfiguration(String hostname, String username, String password, String nickname, String displayname, List<String> channels) {
065 this.channels = channels;
066 this.hostname = hostname;
067 this.username = username;
068 this.password = password;
069 this.nickname = nickname;
070 this.realname = displayname;
071 }
072
073 public IrcConfiguration copy() {
074 try {
075 return (IrcConfiguration) clone();
076 } catch (CloneNotSupportedException e) {
077 throw new RuntimeCamelException(e);
078 }
079 }
080
081 public String getCacheKey() {
082 return hostname + ":" + nickname;
083 }
084
085 public String getListOfChannels() {
086 String retval = "";
087 for (String channel : channels) {
088 retval += channel + " ";
089 }
090 return retval.trim();
091 }
092
093 public void configure(String uriStr) throws URISyntaxException {
094 // fix provided URI and handle that we can use # to indicate the IRC room
095 if (uriStr.startsWith("ircs")) {
096 setUsingSSL(true);
097 if (!uriStr.startsWith("ircs://")) {
098 uriStr = uriStr.replace("ircs:", "ircs://");
099 }
100 } else if (!uriStr.startsWith("irc://")) {
101 uriStr = uriStr.replace("irc:", "irc://");
102 }
103
104 if (uriStr.contains("?")) {
105 uriStr = ObjectHelper.before(uriStr, "?");
106 }
107
108 URI uri = new URI(uriStr);
109
110 setNickname(uri.getUserInfo());
111 setUsername(uri.getUserInfo());
112 setRealname(uri.getUserInfo());
113 setHostname(uri.getHost());
114
115 if (uri.getFragment() != null && uri.getFragment().length() != 0) {
116 String channel = "#" + uri.getFragment();
117 addChannel(channel);
118 }
119 }
120
121 public void addChannel(String channel) {
122 boolean alreadyHave = false;
123 for (String aChannel : channels) {
124 if (channel.contentEquals(aChannel)) {
125 alreadyHave = true;
126 }
127 }
128 if (!alreadyHave) {
129 channels.add(channel);
130 }
131 }
132
133 public void setChannels(String channels) {
134 String[] args = channels.split(",");
135
136 for (String channel : args) {
137 channel = channel.trim();
138 if (channel.startsWith("#")) {
139 addChannel(channel);
140 }
141 }
142 }
143
144 public void setTrustManager(SSLTrustManager trustManager) {
145 this.trustManager = trustManager;
146 }
147
148 public SSLTrustManager getTrustManager() {
149 return trustManager;
150 }
151
152 public boolean getUsingSSL() {
153 return usingSSL;
154 }
155
156 private void setUsingSSL(boolean usingSSL) {
157 this.usingSSL = usingSSL;
158 }
159
160 public String getHostname() {
161 return hostname;
162 }
163
164 public void setHostname(String hostname) {
165 this.hostname = hostname;
166 }
167
168 public String getPassword() {
169 return password;
170 }
171
172 public void setPassword(String password) {
173 this.password = password;
174 }
175
176 public String getNickname() {
177 return nickname;
178 }
179
180 public void setNickname(String nickname) {
181 this.nickname = nickname;
182 }
183
184 public String getRealname() {
185 return realname;
186 }
187
188 public void setRealname(String realname) {
189 this.realname = realname;
190 }
191
192 public String getUsername() {
193 return username;
194 }
195
196 public void setUsername(String username) {
197 this.username = username;
198 }
199
200 public int[] getPorts() {
201 return ports;
202 }
203
204 public void setPorts(int[] ports) {
205 this.ports = ports;
206 }
207
208 public List<String> getChannels() {
209 return channels;
210 }
211
212 public boolean isPersistent() {
213 return persistent;
214 }
215
216 public void setPersistent(boolean persistent) {
217 this.persistent = persistent;
218 }
219
220 public boolean isColors() {
221 return colors;
222 }
223
224 public void setColors(boolean colors) {
225 this.colors = colors;
226 }
227
228 public boolean isOnNick() {
229 return onNick;
230 }
231
232 public void setOnNick(boolean onNick) {
233 this.onNick = onNick;
234 }
235
236 public boolean isOnQuit() {
237 return onQuit;
238 }
239
240 public void setOnQuit(boolean onQuit) {
241 this.onQuit = onQuit;
242 }
243
244 public boolean isOnJoin() {
245 return onJoin;
246 }
247
248 public void setOnJoin(boolean onJoin) {
249 this.onJoin = onJoin;
250 }
251
252 public boolean isOnKick() {
253 return onKick;
254 }
255
256 public void setOnKick(boolean onKick) {
257 this.onKick = onKick;
258 }
259
260 public boolean isOnMode() {
261 return onMode;
262 }
263
264 public void setOnMode(boolean onMode) {
265 this.onMode = onMode;
266 }
267
268 public boolean isOnPart() {
269 return onPart;
270 }
271
272 public void setOnPart(boolean onPart) {
273 this.onPart = onPart;
274 }
275
276 public boolean isOnReply() {
277 return onReply;
278 }
279
280 public void setOnReply(boolean onReply) {
281 this.onReply = onReply;
282 }
283
284 public boolean isOnTopic() {
285 return onTopic;
286 }
287
288 public void setOnTopic(boolean onTopic) {
289 this.onTopic = onTopic;
290 }
291
292 public boolean isOnPrivmsg() {
293 return onPrivmsg;
294 }
295
296 public void setOnPrivmsg(boolean onPrivmsg) {
297 this.onPrivmsg = onPrivmsg;
298 }
299
300 public String toString() {
301 return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", target: " + target + ", username=" + username + "]";
302 }
303 }