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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/* Banana-Chat - The first Open Source Knuddels Emulator
* Copyright (C) 2011 Flav <http://banana-coding.com>
*
* Diese Datei unterliegt dem Copyright von Banana-Coding und
* darf verändert, aber weder in andere Projekte eingefügt noch
* reproduziert werden.
*
* Der Emulator dient - sofern der Client nicht aus Eigenproduktion
* stammt - nur zu Lernzwecken, das Hosten des originalen Clients
* ist untersagt und wird der Knuddels GmbH gemeldet.
*/
package knuddels;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import tools.IntegerUtil;
import tools.KCodeParser;
import tools.PacketCreator;
import tools.Pair;
import tools.database.ConnectionPool;
import tools.database.PoolConnection;
/**
*
* @author Flav
*/
public class Client {
private final List<Channel> channels;
private final Socket socket;
private OutputStream out;
private List<Pair<String, Integer>> icons;
private String name;
private byte gender;
private String age;
private String birthday;
private String starsign;
private String forgiven;
private String realname;
private String stadt;
private String land;
private String hobbys;
private String job;
private String email;
private String motto;
private int roses;
private byte rank;
private int kisses;
private int knuddels;
private int onlineTime;
private long loginTimestamp;
private String registrationDate;
private String like;
private String registrationTime;
private String ipAddress;
private int ban;
private String appletVersion;
private String readme;
private boolean away;
private int spam;
private long lastAction;
private float wordmixPoints;
private float quizPoints;
private float mafia;
//Familymitglied
private byte famcheck;
private String famcheckdate;
private String famseit;
private Client kdice_user;
private int kdice_id;
private int kdice_count;
private String kdice_dice;
public Client(Socket socket) {
channels = new ArrayList<Channel>();
this.socket = socket;
if (socket == null) {
return;
}
try {
out = socket.getOutputStream();
} catch (IOException e) {
}
}
public List<Pair<String, Integer>> getIcons() {
return icons;
}
private void addIcon(String icon, int size) {
Pair<String, Integer> pair = new Pair<String, Integer>(icon, size);
icons.add(pair);
for (Channel channel : getChannels()) {
String add = PacketCreator.addIcon(channel.getName(), name, pair);
for (Client target : channel.getClients()) {
target.send(add);
}
}
}
private void removeIcon(String icon) {
for (Pair<String, Integer> pair : icons) {
if (pair.getLeft().equals(icon)) {
icons.remove(pair);
break;
}
}
for (Channel channel : getChannels()) {
String remove = PacketCreator.removeIcon(channel.getName(), name, icon);
for (Client target : channel.getClients()) {
target.send(remove);
}
}
}
public String getName() {
return name;
}
public String getAppletVersion() {
return appletVersion;
}
public String getRealName() {
return realname;
}
public String getBirthday() {
return birthday;
}
public String getStadt() {
return stadt;
}
public String getLand() {
return land;
}
public String getHobbys() {
return hobbys;
}
public String getJob() {
return job;
}
public String getMotto() {
return motto;
}
public String getAge() {
return age;
}
public String getStarsign() {
return starsign;
}
public String getForgiven() {
return forgiven;
}
public String getEmail(){
return email;
}
public void setRealName(String realname) {
this.realname = realname;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void setStadt(String stadt) {
this.stadt = stadt;
}
public void setLand(String land) {
this.land = land;
}
public void setHobbys(String hobbys) {
this.hobbys = hobbys;
}
public void setJob(String job) {
this.job = job;
}
public void setMotto(String motto) {
this.motto = motto;
}
public void setAge(String age){
this.age = age;
}
public void setStarsign(String starsign){
this.starsign = starsign;
}
public void setForgiven(String forgiven){
this.forgiven = forgiven;
}
public void setEmail(String email){
this.email = email;
}
public String getReadme() {
return readme;
}
public void setReadme(String text) {
this.readme = text;
}
public byte getRank() {
return rank;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public boolean isVIP() {
return rank >= 2;
}
public boolean isModerator() {
return rank >= 3;
}
public boolean isAdministrator() {
return rank >= 4;
}
//Familymitglied
public boolean isFam() {
return rank >= 1;
}
public byte isFamcheck() {
return famcheck;
}
public String getFamseit() {
return famseit;
}
public String getFamcheckdate() {
return famcheckdate;
}
public byte getGender() {
return gender;
}
public int getKisses() {
return kisses;
}
public void increaseMafia(float mafia){
this.mafia += mafia;
}
public float getMafia(){
return mafia;
}
public void increaseWordMixPoints(float wordmixPoints) {
this.wordmixPoints += wordmixPoints;
}
public float getWordMixPoints() {
return wordmixPoints;
}
public void increaseQuizPoints(float quizPoints){
this.quizPoints += quizPoints;
}
public float getQuizPoints(){
return quizPoints;
}
public void increaseRoses(int roses){
this.roses += roses;
}
public int getRoses(){
return roses;
}
public void increaseKisses() {
kisses++;
}
public int getKnuddels() {
return knuddels;
}
public void setKnuddels(int knuddels) {
this.knuddels = knuddels;
}
public void setAppletVersion(String version) {
this.appletVersion = version;
}
public int getOnlineTime() {
return onlineTime + (int) ((System.currentTimeMillis() - loginTimestamp) / 1000);
}
public String getRegistrationDate() {
return registrationDate;
}
public String getRegistrationTime() {
return registrationTime;
}
public String getIPAddress() {
return ipAddress;
}
public int getBan() {
return ban;
}
public boolean isAway() {
return away;
}
public void setAway(boolean away) {
this.away = away;
if (away) {
addIcon("pics/away.png", 20);
} else {
removeIcon("pics/away.png");
}
}
public int getDiceID() {
return kdice_id;
}
public String getDice() {
return kdice_dice;
}
public Client getDiceUser() {
return kdice_user;
}
public int getDiceCount() {
return kdice_count;
}
public void setDiceUser(Client user, int count, String dice) {
kdice_user = user;
kdice_id = IntegerUtil.rand(100000, 999999);
kdice_count = count;
kdice_dice = dice;
}
public void increaseKnuddels(int num) {
knuddels += num;
}
public void decreaseKnuddels(int num) {
knuddels -= num;
}
public boolean checkSpam(String channel) {
long now = System.currentTimeMillis();
if (lastAction > now - 2000) {
spam++;
if (spam == 2) {
sendButlerMessage(channel, "Bitte _NICHT spammen_ und fluten.");
} else if (spam == 3) {
disconnect();
return true;
}
} else {
spam -= (now - lastAction) / 2000;
if (spam < 0) {
spam = 0;
}
}
lastAction = now;
return false;
}
public void login(String nickname) throws SQLException {
if (name != null && !name.equals(nickname)) { // logging in using another account
logout("Ausgeloggt");
}
synchronized (channels) {
if (channels.isEmpty()) { // is logged out
Client client = Server.get().getClient(nickname);
if (client != null) { // someone else is logged into that account
client.disconnect();
}
loadStats(nickname);
}
}
}
public void logout(String message) {
synchronized (channels) {
if (!channels.isEmpty()) { // is logged in
for (Channel channel : channels) {
channel.leave(this);
if (message != null) {
send(PacketCreator.kick(channel.getName(), message));
}
}
channels.clear();
saveStats();
Server.get().removeClient(name);
sendHello();
}
}
}
public Channel getChannel() {
if (name.equals(Server.get().getButler().getName())) {
return null;
}
synchronized (channels) {
return channels.get(channels.size() - 1);
}
}
public List<Channel> getChannels() {
synchronized (channels) {
return channels;
}
}
public void joinChannel(Channel channel) {
synchronized (channels) {
if (channels.isEmpty()) { // logged in
Server.get().addClient(this);
}
channels.add(channel);
}
}
public void leaveChannel(Channel channel) {
synchronized (channels) {
channels.remove(channel);
if (channels.isEmpty()) { // logged out
saveStats();
Server.get().removeClient(name);
sendHello();
}
}
}
public void loadStats(String name) {
PoolConnection pcon = ConnectionPool.getConnection();
PreparedStatement ps = null;
try {
Connection con = pcon.connect();
ps = con.prepareStatement("SELECT * FROM `accounts` WHERE `name` = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
this.name = rs.getString("name");
age = rs.getString("age");
birthday = rs.getString("birthday");
starsign = rs.getString("starsign");
forgiven = rs.getString("forgiven");
realname = rs.getString("realname");
stadt = rs.getString("stadt");
land = rs.getString("land");
hobbys = rs.getString("hobbys");
job = rs.getString("job");
email = rs.getString("email");
motto = rs.getString("motto");
rank = rs.getByte("rank");
icons = new ArrayList<Pair<String, Integer>>();
gender = rs.getByte("gender");
if (gender == 1) {
addIcon("pics/male.png", 16);
} else if (gender == 2) {
addIcon("pics/female.png", 14);
}
if (getRank() == 2) {
addIcon("pics/icon_vip.png", 22);
}
kisses = rs.getInt("kisses");
roses = rs.getInt("roses");
knuddels = rs.getInt("knuddels");
like = rs.getString("like");
onlineTime = rs.getInt("onlineTime");
mafia = rs.getFloat("mafia");
quizPoints = rs.getFloat("quizpoints");
wordmixPoints = rs.getFloat("wordmixpoints");
readme = rs.getString("readme");
loginTimestamp = System.currentTimeMillis();
String[] registration = rs.getString("registration").split(" ");
String[] date = registration[0].split("-");
registrationDate = String.format("%s.%s.%s", date[2], date[1], date[0]);
registrationTime = registration[1].split("\\.")[0];
//Familymitglied
String famrech = rs.getString("familydate");
String[] fdate = famrech.split("-");
famseit = String.format("%s.%s.%s", fdate[2], fdate[1], fdate[0]);
famcheck = rs.getByte("famcheck");
String dfamcheck = rs.getString("famcheckdate");
String[] fmdate = dfamcheck.split("-");
famcheckdate = String.format("%s.%s.%s", fmdate[2], fmdate[1], fmdate[0]);
if (isModerator()) {
ipAddress = "1.3.3.7";
} else {
if (socket == null) {
ipAddress = rs.getString("ipAddress");
} else {
ipAddress = socket.getInetAddress().getHostAddress();
}
}
away = false;
ban = rs.getInt("ban");
spam = 0;
lastAction = 0;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
pcon.close();
}
}
private void saveStats() {
PoolConnection pcon = ConnectionPool.getConnection();
PreparedStatement ps = null;
try {
Connection con = pcon.connect();
ps = con.prepareStatement("UPDATE `accounts` SET `kisses` = ?, `knuddels` = ?, `onlineTime` = ?, `ipAddress` = ?, `readme` = ?, `age` = ?, `birthday` = ?, `starsign` = ?, `forgiven` = ?, `realname` = ?, `stadt` = ?, `land` = ?, `hobbys` = ?, `job` = ?, `email` = ?, `motto` = ?, `wordmixPoints` = ?, `mafia` = ?, `quizpoints` = ?, `like` = ?, `roses` = ? WHERE `name` = ?");
ps.setInt(1, kisses);
ps.setInt(2, knuddels);
ps.setInt(3, getOnlineTime());
ps.setString(4, ipAddress);
ps.setString(5, readme);
ps.setString(6, age);
ps.setString(7, birthday);
ps.setString(8, starsign);
ps.setString(9, forgiven);
ps.setString(10, realname);
ps.setString(11, stadt);
ps.setString(12, land);
ps.setString(13, hobbys);
ps.setString(14, job);
ps.setString(15, email);
ps.setString(16, motto);
ps.setFloat(17, wordmixPoints);
ps.setFloat(18, mafia);
ps.setFloat(19, quizPoints);
ps.setString(20, like);
ps.setInt(21, roses);
ps.setString(22, name);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
pcon.close();
}
}
public void sendHello() {
Collection<Channel> channelList = Server.get().getChannels();
send(PacketCreator.hello(((Channel) channelList.toArray()[0]).getName()));
send(PacketCreator.channelList(channelList));
}
public void sendPrivateMessage(List<Client> targets, String message, Channel channel, boolean fromGame) {
if (!fromGame && channel.getGame() != null && !channel.getGame().parsePrivateMessage(targets, message, this)) {
return;
}
message = KCodeParser.parse(message, !isModerator(), 5, 10, 20);
message = Server.get().parseSmileys(message);
if (message.isEmpty()) {
return;
}
StringBuilder recipients = new StringBuilder();
for (Client target : targets) {
recipients.append(target.getName());
recipients.append(",");
}
String recipient = recipients.substring(0, recipients.length() - 1);
if (!targets.contains(this)) {
targets.add(this);
}
for (Client target : targets) {
if (channel.getClients().contains(target)) {
target.send(PacketCreator.privateMessage(name, recipient, channel.getName(), message, " "));
} else {
for (Channel ch : target.getChannels()) {
target.send(PacketCreator.privateMessage(name, recipient, ch.getName(), message, channel.getName()));
}
}
}
}
public void sendButlerMessage(String channel, String message) {
send(PacketCreator.privateMessage(Server.get().getButler().getName(), name, channel, message, " "));
}
public void send(String message) {
if (socket != null && socket.isConnected()) {
try {
out.write(Protocol.encode(message.getBytes("UTF8")));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void disconnect() {
logout(null);
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void updateReadme() {
PoolConnection pcon = ConnectionPool.getConnection();
PreparedStatement ps = null;
try {
Connection con = pcon.connect();
ps = con.prepareStatement("UPDATE `accounts` SET `readme` = ? WHERE `name` = ?");
ps.setString(1, readme);
ps.setString(2, name);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
}
}
pcon.close();
}
}
}
|