2 Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/
4 This file is part of JBouncer.
6 This software is dual-licensed, allowing you to choose between the GNU
7 General Public License (GPL) and the www.jibble.org Commercial License.
8 Since the GPL may be too restrictive for use in a proprietary application,
9 a commercial license is also provided. Full license information can be
10 found at http://www.jibble.org/licenses/
13 $Id: ServerConnection.java,v 1.5 2004/03/02 00:32:27 pjm2 Exp $
19 import org
.jibble
.pircbot
.*;
24 public class ServerConnection
extends PircBot
{
26 public ServerConnection(String server
, int port
, String password
, String nick
, User user
) {
29 this.password
= password
;
34 setVersion("JBouncer IRC Proxy 1.0 www.jibble.org");
36 setAutoNickChange(true);
38 JBouncerManager
.log(user
.getLogin() + " initiated new connection to " + server
);
41 public void connect() {
43 boolean trying
= true;
47 sendToOtherClients(null, ClientConnection
.METHOD
+ "JBouncer is trying to connect to " + server
+ " (retry #" + tries
+ ")");
48 connect(server
, port
, password
);
49 sendToOtherClients(null, ClientConnection
.METHOD
+ "JBouncer is connected to " + server
);
53 sendToOtherClients(null, ClientConnection
.METHOD
+ "JBouncer could not connect. Trying again in 5 minutes.");
57 Thread
.sleep(5 * 60 * 1000);
59 catch (InterruptedException e
) {
60 // This should not happen.
65 public void onServerResponse(int code
, String message
) {
66 if (code
>= 0 && code
<= 5) {
67 serverMessages
[code
] = message
;
71 public void onDisconnect() {
75 public void handleLine(String line
) {
77 sendToOtherClients(null, line
);
78 super.handleLine(line
);
81 public void sendToOtherClients(ClientConnection sendingClient
, String line
) {
82 synchronized (clients
) {
83 Iterator it
= clients
.iterator();
84 while (it
.hasNext()) {
86 ClientConnection client
= (ClientConnection
) it
.next();
87 if (client
!= sendingClient
) {
88 client
.sendRawLine(line
);
91 catch (IOException e
) {
98 public void removeClient(ClientConnection client
) {
99 synchronized (clients
) {
100 clients
.remove(client
);
102 JBouncerManager
.log(user
.getLogin() + " detached from session on " + server
);
105 public int getClientCount() {
106 return clients
.size();
109 public void add(ClientConnection client
) {
110 synchronized (clients
) {
113 JBouncerManager
.log(user
.getLogin() + " attached to session on " + server
);
114 JBouncerManager
.log(user
.getLogin() + " is now using " + getClientCount() + " sessions.");
117 public String
[] getServerMessages() {
118 return serverMessages
;
121 public void addToHistory(String line
) {
122 String
[] parts
= line
.split("\\s+", 4);
123 if (parts
.length
== 4) {
124 String type
= parts
[1];
125 if (type
.equals("PRIVMSG")) {
126 String from
= parts
[0];
127 int index
= from
.indexOf('!');
129 from
= from
.substring(1, index
);
131 String target
= parts
[2];
132 String message
= parts
[3].substring(1);
133 message
= Colors
.removeFormattingAndColors(message
);
136 if ("#&!+".indexOf(target
.charAt(0)) >= 0) {
140 log(where
, "<" + from
+ "> " + message
);
145 if (history
.size() > JBouncerMain
.HISTORY_LIMIT
) {
146 history
.removeFirst();
150 // Returns a new collection to avoid blocking or thread safety issues.
151 public LinkedList
getHistory() {
152 return new LinkedList(history
);
155 private synchronized void log(String where
, String line
) {
157 File dir
= new File("./logs/" + user
.getLogin());
158 if (dir
.isDirectory() || dir
.mkdirs()) {
159 BufferedWriter writer
= new BufferedWriter(new FileWriter(new File(dir
, where
+ "." + server
+ ".log"), true));
160 writer
.write(line
+ "\r\n");
165 catch (IOException e
) {
170 private String server
;
172 private String password
;
175 private LinkedList clients
= new LinkedList();
177 private String
[] serverMessages
= new String
[6];
178 private LinkedList history
= new LinkedList();