-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.java
292 lines (241 loc) · 9.3 KB
/
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
GeekShed-Logger Log Server - V1.4
User Class File
GeekShed Will -NOT- Provide Support With This Code!
Copyright (c) 2008 Phil Lavin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Libs
import java.util.*;
import java.sql.*;
// User class
public class User {
// Vars
private String nick, ident, gecos, ip, server, hostname, flags; // User info
private long timestamp; // Last NICK timestamp
private int sport, dport; // Source and dest ports
private Vector<Character> modesOn = new Vector<Character>(20); // Modes set on
private Vector<Character> modesOff = new Vector<Character>(20); // Modes set off
private Vector<String> channels = new Vector<String>(60); // Channels joined
private Vector<String> nicknames = new Vector<String>(60); // Nicknames used
private MySQL mysql; // Mysql connection
private long mysqlRecordId = 0; // Mysql insert id
// Constructor
public User(String nick, String ident, String gecos, String server, String hostname, long timestamp, MySQL mysql) {
// Store data
this.nick = nick;
this.ident = ident;
this.gecos = gecos;
this.server = server;
this.hostname = hostname;
this.timestamp = timestamp;
this.mysql = mysql;
// Save current nickname to nicknames table
this.nicknames.add(nick);
this.insertNicknames();
}
// Accessors and mutators
public void setMySqlRecordId(long id) {
this.mysqlRecordId = id;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getNick() {
return this.nick;
}
public void setIdent(String ident) {
this.ident = ident;
}
public void setGecos(String gecos) {
this.gecos = gecos;
}
public void setIP(String ip) {
this.ip = ip;
}
public void setServer(String server) {
this.server = server;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public void setFlags(String flags) {
this.flags = flags;
}
public void setSPort(int sport) {
this.sport = sport;
}
public void setDPort(int dport) {
this.dport = dport;
}
public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}
public void addModeOn(char mode) {
if (modesOn.capacity() == modesOn.size()) {
modesOn.ensureCapacity(modesOn.size() + 100);
}
modesOn.add(new Character(mode));
}
public void addModeOff(char mode) {
modesOff.add(new Character(mode));
}
public void addChannel(String channel) {
channels.add(channel);
}
public void addNickname(String nickname) {
nicknames.add(nickname);
this.nick = nickname;
}
// Overridden method to give sane class debug output
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("Nick = " + this.nick + "\n");
buf.append("Ident = " + this.ident + "\n");
buf.append("Gecos = " + this.gecos + "\n");
buf.append("IP = " + this.ip + "\n");
buf.append("Server = " + this.server + "\n");
buf.append("Hostname = " + this.hostname + "\n");
buf.append("Flags = " + this.flags + "\n");
buf.append("Source Port = " + this.sport + "\n");
buf.append("Destination Port = " + this.dport + "\n");
buf.append("Modes On = " + this.modesOn + "\n");
buf.append("Modes Off = " + this.modesOff + "\n");
buf.append("Channels = " + this.channels + "\n");
buf.append("Nicknames = " + this.nicknames + "\n");
buf.append("SQL ID = " + this.mysqlRecordId + "\n");
return buf.toString();
}
// Create the user record in the database
public void createInDB() {
String sql = "INSERT INTO `logs` (`nick`, `ident`, `gecos`, `server`, `dport`, `ip`, `sport`, `flags`, `userhost`, `timestamp`, `con_timestamp`) VALUES ('" + MySQL.sqlEscape(this.nick) + "', '" + MySQL.sqlEscape(this.ident) + "', '" + MySQL.sqlEscape(this.gecos) + "', '" + MySQL.sqlEscape(this.server) + "', '" + MySQL.sqlEscape(this.dport) + "', '" + MySQL.sqlEscape(this.ip) + "', '" + MySQL.sqlEscape(this.sport) + "', '" + MySQL.sqlEscape(this.flags) + "', '" + MySQL.sqlEscape(this.hostname) + "', '" + MySQL.sqlEscape(this.timestamp) + "', '" + MySQL.sqlEscape(this.timestamp) + "')";
// Attempt to insert into logs table
try {
this.mysql.executeUpdate(sql);
// Get mysql insert id
ResultSet rs = this.mysql.executeQuery("SELECT LAST_INSERT_ID() AS insid");
rs.next();
this.mysqlRecordId = rs.getLong("insid");
}
// On failure
catch (Exception e) {
// If not dup key error
if (e.getMessage().length() >= 68 && !e.getMessage().substring(0, 68).equals("Duplicate key or integrity constraint violation message from server:")) {
e.printStackTrace();
}
else { // If dup key error
// Get the ID of row causing clash and remember
sql = "SELECT `id` FROM `logs` WHERE `nick`='" + MySQL.sqlEscape(this.nick) + "' AND `ip`='" + MySQL.sqlEscape(this.ip) + "' AND `timestamp`='" + MySQL.sqlEscape(this.timestamp) + "'";
try {
ResultSet rs = this.mysql.executeQuery(sql);
rs.next();
this.mysqlRecordId = rs.getLong("id");
}
catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
// Update data in logs table row
public void updateInDB() {
if (this.mysqlRecordId != 0) {
// Query
String sql = "UPDATE IGNORE `logs` SET `nick`='" + MySQL.sqlEscape(this.nick) + "', `ident`='" + MySQL.sqlEscape(this.ident) + "', `gecos`='" + MySQL.sqlEscape(this.gecos) + "', `server`='" + MySQL.sqlEscape(this.server) + "', `dport`='" + MySQL.sqlEscape(this.dport) + "', `ip`='" + MySQL.sqlEscape(this.ip) + "', `sport`='" + MySQL.sqlEscape(this.sport) + "', `flags`='" + MySQL.sqlEscape(this.flags) + "', `userhost`='" + MySQL.sqlEscape(this.hostname) + "', `timestamp`='" + MySQL.sqlEscape(this.timestamp) + "' WHERE `id`=" + MySQL.sqlEscape(this.mysqlRecordId);
try {
this.mysql.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Add nicknames from vector to table
public void insertNicknames() {
if (nicknames.size() > 0 && this.mysqlRecordId != 0) {
String sql = "INSERT INTO `nicknames` (`lid`, `nickname`, `timestamp`) VALUES ";
// Loop vector and build query
for (int i = 0; i < nicknames.size(); i++) {
sql = sql + "('" + this.mysqlRecordId + "', '" + MySQL.sqlEscape(nicknames.get(i)) + "', UNIX_TIMESTAMP()), ";
}
// Trim query
sql = sql.substring(0, sql.length() - 2);
// Clear vector
nicknames.clear();
// Do Query
try {
this.mysql.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Add modes from Vector "v" to table "table"
public void insertModes(String table, Vector v) {
if (v.size() > 0 && this.mysqlRecordId != 0) {
String sql = "INSERT INTO `" + table + "` (`lid`, `mode`, `timestamp`) VALUES ";
// Loop vector and build query
for (int i = 0; i < v.size(); i++) {
sql = sql + "('" + this.mysqlRecordId + "', '" + MySQL.sqlEscape(v.get(i).toString()) + "', UNIX_TIMESTAMP()), ";
}
// Trim query
sql = sql.substring(0, sql.length() - 2);
// Clear vector
v.clear();
// Do query
try {
this.mysql.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Overloaded method to add both modes on and off to db
public void insertModes() {
insertModes("modes_on", this.modesOn);
insertModes("modes_off", this.modesOff);
}
// Insert channels from vector into db
public void insertChannels() {
if (channels.size() > 0 && this.mysqlRecordId != 0) {
String sql = "INSERT IGNORE INTO `channels` (`lid`, `channel`, `timestamp`) VALUES ";
// Loop vector and build query
for (int i = 0; i < channels.size(); i++) {
sql = sql + "('" + this.mysqlRecordId + "', '" + MySQL.sqlEscape(channels.get(i)) + "', UNIX_TIMESTAMP()), ";
}
// Trim query
sql = sql.substring(0, sql.length() - 2);
// Clear vector
channels.clear();
// Do query
try {
this.mysql.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}