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 List<String> keys = new ArrayList<String>(); 034 private String hostname; 035 private String password; 036 private String nickname; 037 private String realname; 038 private String username; 039 private SSLTrustManager trustManager = new SSLDefaultTrustManager(); 040 private boolean usingSSL; 041 private boolean persistent = true; 042 private boolean colors = true; 043 private boolean onNick = true; 044 private boolean onQuit = true; 045 private boolean onJoin = true; 046 private boolean onKick = true; 047 private boolean onMode = true; 048 private boolean onPart = true; 049 private boolean onReply; 050 private boolean onTopic = true; 051 private boolean onPrivmsg = true; 052 private int[] ports = {6667, 6668, 6669}; 053 054 public IrcConfiguration() { 055 } 056 057 public IrcConfiguration(String hostname, String nickname, String displayname, List<String> channels) { 058 this.channels = channels; 059 this.hostname = hostname; 060 this.nickname = nickname; 061 this.username = nickname; 062 this.realname = displayname; 063 } 064 065 public IrcConfiguration(String hostname, String username, String password, String nickname, String displayname, List<String> channels) { 066 this.channels = channels; 067 this.hostname = hostname; 068 this.username = username; 069 this.password = password; 070 this.nickname = nickname; 071 this.realname = displayname; 072 } 073 074 public IrcConfiguration copy() { 075 try { 076 return (IrcConfiguration) clone(); 077 } catch (CloneNotSupportedException e) { 078 throw new RuntimeCamelException(e); 079 } 080 } 081 082 public String getCacheKey() { 083 return hostname + ":" + nickname; 084 } 085 086 public String getListOfChannels() { 087 String retval = ""; 088 for (String channel : channels) { 089 retval += channel + " "; 090 } 091 return retval.trim(); 092 } 093 094 public void configure(String uriStr) throws URISyntaxException { 095 // fix provided URI and handle that we can use # to indicate the IRC room 096 if (uriStr.startsWith("ircs")) { 097 setUsingSSL(true); 098 if (!uriStr.startsWith("ircs://")) { 099 uriStr = uriStr.replace("ircs:", "ircs://"); 100 } 101 } else if (!uriStr.startsWith("irc://")) { 102 uriStr = uriStr.replace("irc:", "irc://"); 103 } 104 105 if (uriStr.contains("?")) { 106 uriStr = ObjectHelper.before(uriStr, "?"); 107 } 108 109 URI uri = new URI(uriStr); 110 111 setNickname(uri.getUserInfo()); 112 setUsername(uri.getUserInfo()); 113 setRealname(uri.getUserInfo()); 114 setHostname(uri.getHost()); 115 116 if (uri.getFragment() != null && uri.getFragment().length() != 0) { 117 String channel = "#" + uri.getFragment(); 118 addChannel(channel); 119 } 120 } 121 122 public void addChannel(String channel) { 123 boolean alreadyHave = false; 124 for (String aChannel : channels) { 125 if (channel.contentEquals(aChannel)) { 126 alreadyHave = true; 127 } 128 } 129 if (!alreadyHave) { 130 channels.add(channel); 131 } 132 } 133 134 public void setChannels(String channels) { 135 String[] args = channels.split(","); 136 137 for (String channel : args) { 138 channel = channel.trim(); 139 if (channel.startsWith("#")) { 140 addChannel(channel); 141 } 142 } 143 } 144 145 public void setKeys(String keys) { 146 String[] s = keys.split(","); 147 for (String key : s) { 148 this.keys.add(key); 149 } 150 } 151 152 public void setKeys(List<String> keys) { 153 this.keys = keys; 154 } 155 156 public List<String> getKeys() { 157 return keys; 158 } 159 160 public void setTrustManager(SSLTrustManager trustManager) { 161 this.trustManager = trustManager; 162 } 163 164 public SSLTrustManager getTrustManager() { 165 return trustManager; 166 } 167 168 public boolean getUsingSSL() { 169 return usingSSL; 170 } 171 172 private void setUsingSSL(boolean usingSSL) { 173 this.usingSSL = usingSSL; 174 } 175 176 public String getHostname() { 177 return hostname; 178 } 179 180 public void setHostname(String hostname) { 181 this.hostname = hostname; 182 } 183 184 public String getPassword() { 185 return password; 186 } 187 188 public void setPassword(String password) { 189 this.password = password; 190 } 191 192 public String getNickname() { 193 return nickname; 194 } 195 196 public void setNickname(String nickname) { 197 this.nickname = nickname; 198 } 199 200 public String getRealname() { 201 return realname; 202 } 203 204 public void setRealname(String realname) { 205 this.realname = realname; 206 } 207 208 public String getUsername() { 209 return username; 210 } 211 212 public void setUsername(String username) { 213 this.username = username; 214 } 215 216 public int[] getPorts() { 217 return ports; 218 } 219 220 public void setPorts(int[] ports) { 221 this.ports = ports; 222 } 223 224 public List<String> getChannels() { 225 return channels; 226 } 227 228 public boolean isPersistent() { 229 return persistent; 230 } 231 232 public void setPersistent(boolean persistent) { 233 this.persistent = persistent; 234 } 235 236 public boolean isColors() { 237 return colors; 238 } 239 240 public void setColors(boolean colors) { 241 this.colors = colors; 242 } 243 244 public boolean isOnNick() { 245 return onNick; 246 } 247 248 public void setOnNick(boolean onNick) { 249 this.onNick = onNick; 250 } 251 252 public boolean isOnQuit() { 253 return onQuit; 254 } 255 256 public void setOnQuit(boolean onQuit) { 257 this.onQuit = onQuit; 258 } 259 260 public boolean isOnJoin() { 261 return onJoin; 262 } 263 264 public void setOnJoin(boolean onJoin) { 265 this.onJoin = onJoin; 266 } 267 268 public boolean isOnKick() { 269 return onKick; 270 } 271 272 public void setOnKick(boolean onKick) { 273 this.onKick = onKick; 274 } 275 276 public boolean isOnMode() { 277 return onMode; 278 } 279 280 public void setOnMode(boolean onMode) { 281 this.onMode = onMode; 282 } 283 284 public boolean isOnPart() { 285 return onPart; 286 } 287 288 public void setOnPart(boolean onPart) { 289 this.onPart = onPart; 290 } 291 292 public boolean isOnReply() { 293 return onReply; 294 } 295 296 public void setOnReply(boolean onReply) { 297 this.onReply = onReply; 298 } 299 300 public boolean isOnTopic() { 301 return onTopic; 302 } 303 304 public void setOnTopic(boolean onTopic) { 305 this.onTopic = onTopic; 306 } 307 308 public boolean isOnPrivmsg() { 309 return onPrivmsg; 310 } 311 312 public void setOnPrivmsg(boolean onPrivmsg) { 313 this.onPrivmsg = onPrivmsg; 314 } 315 316 public String toString() { 317 return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", target: " + target + ", username=" + username + "]"; 318 } 319 }