-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClient.java
More file actions
251 lines (231 loc) · 8.46 KB
/
Copy pathTestClient.java
File metadata and controls
251 lines (231 loc) · 8.46 KB
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
package com.legrand.iln;
import java.io.*;
import java.util.*;
/* $Id: TestClient.java,v 1.4.2.1 2004/01/14 16:24:12 legrand Exp legrand $ */
/**
* Progetto ILN
* Copyright (C) 2003 Monsieur Legrand
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the license, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/**
* Classe che implementa il client specifico per la connessione
* ad un servizio IRC.
*
* @author Monsieur Legrand
* @version 1.4 rev 3
* @date 08 aprile 2004
*/
public class TestClient {
String nickName;
boolean debug = false;
String motd = new String();
Analyzer analisi;
String tipoResponse;
String visitor;
BufferedReader in;
Hashtable learningTable = new Hashtable();
/**
* Costruttore. Viene invocato automaticamente alla partenza del programma.
* Non c'e' bisogno di effettuare chiamate esplicite.
*
* @param aNick nickname dell'istanza del programma
* @param deb un booleano che indica il livello di debug:
* true = debug alto, false = debug basso.
* @param aResponse una stringa contenente la personalita' dell'istanza del programma.
*/
public TestClient(String aNick, boolean deb){
nickName = aNick;
debug = deb;
in = new BufferedReader(new InputStreamReader(System.in));
analisi = new Analyzer(nickName, debug);
//Eventuale interfaccia grafica
System.out.print("chi sei? ");
try{
String line = in.readLine();
visitor = ":" + line + "!localhost";
} catch (IOException ex){
System.out.println("Errore di I/O: " + ex.getMessage());
ex.printStackTrace();
System.exit(1);
}
cicloClient();
}
/**
* Fa partire il client.<BR>
* Uso:
* <code>java TestClient nick debugLevel</code><br>
*/
public static void main(String[] args) {
boolean ldeb;
if (args.length < 2)
misuse();
String nick = args[0];
int aDeb = Integer.parseInt(args[1]);
if (aDeb == 1)
ldeb = true;
else ldeb = false;
new TestClient(nick, ldeb);
}
/**
* Funzione di utilita' invocata se il programma viene fatto partire con un numero
* insufficiente di argomenti.
*/
private static void misuse(){
System.out.println("[local:] - Uso: java TestClient <nick> <debug> ");
System.out.println("[local:] - debug = 1 - più messaggi in log. debug = 0 - meno messaggi");
System.exit(0);
}
/**
* Funzione principale del client locale, contiene il ciclo da eseguire durante un dialogo.
*/
public void cicloClient() {
String line = null;
String prefix, tredig, comando, params;
String[] response;
IrcServerMessage lineMess;
motd = analisi.getMotd();
// saluto
if (motd.length()==0)
motd = "Ciao io sono il kernel dei gatti";
System.out.println(motd);
while(true){
try{
System.out.print(">> ");
line = in.readLine();
} catch(IOException ex){
System.out.println("[local:] - Impossibile leggere dal socket: " + ex.getMessage());
System.exit(1);
}
prefix = visitor;
params = nickName + " :" + line.toLowerCase().trim();
trattaPrivMsg(prefix, params);
}
}
/**
* La documentazione di questa funzione e' la stessa della funzione omonima in @see IrcClient
*/
public void trattaPrivMsg(String prefix, String params) {
StringTokenizer tok = new StringTokenizer(params, " ");
String k = tok.nextToken();
String[] response;
int nRes = 1;
boolean command = false;
if (k.equals(nickName)) {
String toNick = trovaNick(prefix);
if (learningTable.containsKey(toNick))
if (((String)learningTable.get(toNick)).equals("begin_learn"))
learn2(toNick, params);
else
learnEnd(toNick, params);
else {
String k2 = tok.nextToken();
if (k2.equals(":versione")){
response = analisi.version();
for (int i=0; i<response.length; i++)
System.out.println(response[i]);
}
else if (k2.trim().equals(":chi sei?")){
response = analisi.chisei();
for (int i=0; i<response.length; i++)
System.out.println(response[i]);
}
else if (k2.substring(1).trim().equals(nickName) ||
k2.substring(1).trim().equals(nickName.toLowerCase())){
System.out.println("Si?");
}
else if (k2.equals(":stop")){
response = new String[1];
response[0] = "C'è tanto da imparare nella vita";
System.out.println("Ordine di arresto programma. Ciao a presto");
System.out.println("Mi fermo qui. Ciao a tutti");
for (int i = 0; i < 20000; i++);
System.out.println("QUIT");
System.exit(0);
}
else {
response = analisi.generation(toNick, params, 1);
if (response != null){
if (response[0].indexOf("CMD") > -1){
nRes = 2;
response = analisi.esegueComando(response[0], params, nRes);
command = true;
}
if (!response[0].equals("learn")){
for (int i = 0; i < nRes; i++){
System.out.println(response[i]);
}
if (command){
nRes = 1;
command = false;
}
}
else {
learningTable.put(toNick, "begin_learn");
System.out.println(toNick + " Non so cosa significhi. La parola chiave quale è?");
}
}
}
}
}
}
/**
* La documentazione di questa funzione e' la stessa di quella di @see IrcClient
*/
private String trovaNick(String stringa){
StringBuffer rispostina = new StringBuffer();
int i = 1;
while (stringa.charAt(i) != '!'){
rispostina.append(stringa.charAt(i));
i++;
}
return rispostina.toString();
}
/**
* La documentazione di questa funzione e' la stessa di quella di @see IrcClient
*/
private void learn2(String toNick, String params) {
int k = params.indexOf(":");
String newKeyWord = params.substring(k+1).trim();
if (newKeyWord.indexOf(nickName) >-1 ){ //okkio! Stanno tentando l'hack del nome!
System.out.println("No no non puoi darmi come chiave qualcosa con il mio nome...");
System.out.println("poi va a finire che quando la gente mi invoca io rispondo cosi'");
System.out.println("e mica ci casco... sce'!");
learningTable.remove(toNick);
}
else {
learningTable.put(toNick, newKeyWord);
System.out.println("Mi dici una bella frase che riguarda ciò? Io la imparo.");
}
}
/**
* La documentazione di questa funzione e' la stessa di quella di @see IrcClient
*/
private void learnEnd(String toNick, String params){
int k = params.indexOf(":");
String newPhraseLearned = params.substring(k+1).trim();
String newKeyWord = (String)learningTable.get(toNick);
//Controllo bug di gi0
if (newKeyWord.equals("") || newPhraseLearned.equals("")){
System.out.println("Ehi mica vorrai fregarmi con qualche stupido hack... sei un lamer :))");
}
else {
System.out.println("Ho capito: " + newKeyWord + " " + newPhraseLearned);
System.out.println("Grazie davvero. Non si finisce mai di imparare :-)");
analisi.learning(newKeyWord, newPhraseLearned);
}
learningTable.remove(toNick);
}
}