]> jfr.im git - irc/Ozafy/borknet_p10_irc_services.git/blob - core/modules/v/Server.java
First commit
[irc/Ozafy/borknet_p10_irc_services.git] / core / modules / v / Server.java
1 /**
2 #
3 # BorkNet Services Core
4 #
5
6 #
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #
23 */
24
25 /*
26 Handles all raw data.
27
28 This one only parses private messages and relays them to commands.
29
30 However, it can parse any message on the network, and could infact
31 be adjusted to support diffrent protocols (in theory ;)
32 */
33
34 import java.util.*;
35 import java.net.*;
36 import java.security.*;
37 import borknet_services.core.*;
38
39 /**
40 * The server communication class of the Q IRC Bot.
41 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
42 */
43 public class Server
44 {
45 /** the main bot */
46 private Core C;
47 /** Core commands */
48 private Commands CC;
49 /** the bot's nick */
50 private String nick;
51 /** the bot's host */
52 private String host;
53 /** the server's numeric */
54 private String numeric;
55 /** the bot's numeric */
56 private String corenum;
57 /** the channel we report to */
58 private String reportchan;
59 /** our version reply */
60 private String version;
61 /** counts the number of received pings, used as a timer for channel limits */
62 private int limit = 0;
63
64 private V Bot;
65
66
67 /**
68 * Constructs a Server communicator.
69 * @param B The main bot
70 * @param dbc The connection to the database
71 */
72 public Server(Core C, V Bot)
73 {
74 this.C = C;
75 this.Bot = Bot;
76 CC = new Commands(C,Bot);
77 nick = C.get_nick();
78 host = C.get_host();
79 numeric = C.get_numeric();
80 corenum = C.get_corenum();
81 version = C.get_version();
82 reportchan = C.get_reportchan();
83 }
84
85 public void parse(ArrayList<String> params)
86 {
87 String source = params.get(0);
88 String command = params.get(1);
89 if(command.equals("P"))
90 {
91 privmsg(params);
92 }
93 else if(command.equals("N"))
94 {
95 if(Bot.get_qwebirc() || Bot.get_automatic())
96 {
97 nickchange(params);
98 }
99 }
100 }
101
102 /**
103 * Handles a privmsg
104 * @param me Server it's going to.
105 * @param username numeric of the user talking to me
106 * @param message the message we got from the user
107 */
108 public void privmsg(ArrayList<String> params)
109 {
110 CC.privmsg(params);
111 }
112
113 /**
114 * Handles N lines, these can be a user nickchange, or new clients connecting
115 *
116 * @param usernumeric the user's numeric
117 * @param params raw irc data
118 */
119 public void nickchange(ArrayList<String> params)
120 {
121 //AB N Ozafy 1 1119649303 ozafy bob.be.borknet.org +oiwkgrxXnIh Ozafy Darth@Vader B]AAAB ABAXs :Laurens Panier
122 String source = params.get(0);
123 if(source.length() < 3)
124 {
125 try
126 {
127 String ident = params.get(5);
128 String ip;
129 try
130 {
131 ip = InetAddress.getByName(params.get(6)).getHostAddress();
132 }
133 catch(UnknownHostException e)
134 {
135 ip = "0.0.0.0";
136 }
137 String numeric = params.get(params.size()-2);
138 if(Bot.get_qwebirc() && params.get(6).equals(Bot.get_qhost()))
139 {
140 try
141 {
142 long realip = Long.parseLong(ident.substring(1),16);
143 setHost(numeric, Bot.get_qident(), C.longToHost(realip));
144 }
145 catch(Exception longex)
146 {
147 //do nothing
148 }
149 }
150 else if(Bot.get_automatic())
151 {
152 String vhost = encrypt(ip) + "." + Bot.get_vhost();
153 setHost(numeric, ident, vhost);
154 }
155 }
156 catch(ArrayIndexOutOfBoundsException e)
157 {
158 C.printDebug("ArrayIndexOutOfBoundsException in srv_nickchange! (1)");
159 C.debug(e);
160 C.report("ArrayIndexOutOfBoundsException in srv_nickchange! (1)");
161 }
162 }
163 }
164
165 public void setHost(String numeric, String ident, String vhost)
166 {
167 C.cmd_sethost(numeric, ident, vhost);
168 }
169
170 public String encrypt(String str)
171 {
172 long hash = 0;
173 long x = 0;
174 for(int i = 0; i < str.length(); i++)
175 {
176 hash = (hash << 4) + str.charAt(i);
177 if((x = hash & 0xF0000000L) != 0)
178 {
179 hash ^= (x >> 24);
180 }
181 hash &= ~x;
182 }
183 return hash+"";
184 }
185 }