adding all of botlist, initial add
[botlist.git] / botlistprojects / laughingman / test / ircproxy / src / jbouncer / ServerConnection.java
blob3f7eadcdff6e5fb1478c76187f2e80164ec9578e
1 /*
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/
12 $Author: pjm2 $
13 $Id: ServerConnection.java,v 1.5 2004/03/02 00:32:27 pjm2 Exp $
17 package jbouncer;
19 import org.jibble.pircbot.*;
20 import java.io.*;
21 import java.net.*;
22 import java.util.*;
24 public class ServerConnection extends PircBot {
26 public ServerConnection(String server, int port, String password, String nick, User user) {
27 this.server = server;
28 this.port = port;
29 this.password = password;
30 this.user = user;
32 setName(nick);
33 setLogin("JBouncer");
34 setVersion("JBouncer IRC Proxy 1.0 www.jibble.org");
35 setVerbose(false);
36 setAutoNickChange(true);
38 JBouncerManager.log(user.getLogin() + " initiated new connection to " + server);
41 public void connect() {
42 int tries = 0;
43 boolean trying = true;
44 while (trying) {
45 tries++;
46 try {
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);
50 return;
52 catch (Exception e) {
53 sendToOtherClients(null, ClientConnection.METHOD + "JBouncer could not connect. Trying again in 5 minutes.");
56 try {
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() {
72 connect();
75 public void handleLine(String line) {
76 addToHistory(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()) {
85 try {
86 ClientConnection client = (ClientConnection) it.next();
87 if (client != sendingClient) {
88 client.sendRawLine(line);
91 catch (IOException e) {
92 it.remove();
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) {
111 clients.add(client);
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('!');
128 if (index > 1) {
129 from = from.substring(1, index);
131 String target = parts[2];
132 String message = parts[3].substring(1);
133 message = Colors.removeFormattingAndColors(message);
135 String where = from;
136 if ("#&!+".indexOf(target.charAt(0)) >= 0) {
137 where = target;
140 log(where, "<" + from + "> " + message);
142 history.add(line);
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) {
156 try {
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");
161 writer.flush();
162 writer.close();
165 catch (IOException e) {
166 // Do nothing.
170 private String server;
171 private int port;
172 private String password;
173 private User user;
175 private LinkedList clients = new LinkedList();
177 private String[] serverMessages = new String[6];
178 private LinkedList history = new LinkedList();