-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrcBot.java
More file actions
271 lines (249 loc) · 7.26 KB
/
IrcBot.java
File metadata and controls
271 lines (249 loc) · 7.26 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import java.io.*;
import java.net.*;
import java.util.*;
public class IrcBot {
// Constants
private String propertiesFile = "settings.properties";
// Fields
private Socket socket;
private PrintStream serverout;
private Scanner serverin;
private PrintStream clientout = new PrintStream(System.out);
private Scanner clientin = new Scanner(System.in);
private Properties props = new Properties();
private HashSet<Module> modules = new HashSet<Module>();
private Admins admins;
private String permaadmin = "";
private boolean listening = false;
private Ignores ignores;
private String password;
private Voting voting;
/**
* Creates a new IrcBot
*/
public IrcBot(){
readConfig();
permaadmin = props.getProperty("permaadmin");
admins = new Admins();
modules.add(admins);
ignores = new Ignores();
modules.add(ignores);
voting = new Voting();
modules.add(voting);
modules.add(new Bots());
//modules.add(new HtmlParser());
modules.add(new Rules());
modules.add(new Intros());
modules.add(new Cucks());
modules.add(new Quote());
modules.add(new Help());
modules.add(new Version());
modules.add(new Administration());
modules.add(new Triggers());
modules.add(new Ping());
modules.add(new Fortune());
modules.add(new Rainbow());
modules.add(new SadFrog());
if(admins == null){
clientout.println("Error reading admins file");
return;
}
if(props.get("server") == null){
clientout.println("Error, server not set");
clientout.println("Please make sure an entry for server is in settings.properties");
}
else if(props.get("port") == null){
clientout.println("Error, port not set");
clientout.println("Please make sure an entry for port is in settings.properties");
}
else if(props.get("nick") == null){
clientout.println("Error, nick not set");
clientout.println("Please make sure an entry for nick is in settings.properties");
}
else if(props.get("user") == null){
clientout.println("Error, user not set");
clientout.println("Please make sure an entry for user is in settings.properties");
}
else{
clientout.println("Config File Read");
clientout.println("Enter login");
while(true){
if (clientin.hasNextLine()) {
password = clientin.nextLine();
break;
}
}
connect();
}
}
/**
* Connects the IrcBot to the socket
*/
public void connect(){
String server = props.getProperty("server");
String port = props.getProperty("port");
try {
clientout.println("Creating socket on " + props.getProperty("server") + ":" + props.getProperty("port"));
socket = new Socket(server, Integer.parseInt(port));
clientout.println("Creating streams");
serverin = new Scanner(socket.getInputStream());
serverout = new PrintStream(socket.getOutputStream());
clientout.println("Sending login information");
send("NICK " + props.getProperty("nick"));
send("USER " + props.getProperty("user"));
while(true){
if(serverin.hasNextLine()){
String line = serverin.nextLine();
//clientout.println(line);
Message m = new Message(line);
if(m.getCommand().equals("PING")) send("PONG :" + m.getTrailing());
if(m.getCommand().equals("002")) clientout.println("Connected");
if(m.getCommand().equals("433")) clientout.println("Nick in use");
if(m.getCommand().equals("451")) clientout.println("Register first");
if(m.getCommand().equals("376")) break;
}
}
clientout.println("Logged in successfully");
send("PRIVMSG Nickserv :IDENTIFY " + password);
new Thread(new Runnable(){
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
listenToServer();
}
}).start();
new Thread(new Runnable() {
public void run() {
inputListener();
}
}).start();
listening = true;
}
catch (NumberFormatException | IOException e) {
}
}
/**
* Reads the config file
*/
public void readConfig(){
InputStream stream = getClass().getClassLoader().getResourceAsStream(propertiesFile);
Properties newprops = new Properties();
try {
newprops.load(stream);
props = newprops;
}
catch (IOException e) {
clientout.println("Error reading settings.properties, does it exist?");
}
}
public void send(String message){
serverout.println(message + "\r\n");
serverout.flush();
}
public void pm(String target, String message){
send("PRIVMSG " + target + " :" + message);
}
private void listenToServer(){
clientout.println("Listening to server");
send("JOIN " + props.getProperty("channel"));
send("JOIN #/g/bots");
send("PRIVMSG " + props.getProperty("channel") + " :Daily reminder that java is the best programming language");
while(true){
if(serverin.hasNextLine()){
String line = serverin.nextLine();
clientout.println(line);
Message m = new Message(line);
clientout.println(m.getTrailing());
final Message message = m;
final boolean senderisadmin = (admins.has(message.getSender()) || permaadmin.equals(message.getSender()));
if(m.getCommand().equals("PRIVMSG") && ignores.has(m.getSender()) && !senderisadmin){
clientout.println("ignoring message");
continue;
}
for(final Module module : modules){
new Thread(new Runnable(){
public void run(){
module.parse(message, senderisadmin);
String[] outputs = module.outputs();
if(outputs != null){
for(int i = 0; i < outputs.length; i++){
clientout.println(outputs[i]);
send(outputs[i]);
try {
Thread.sleep(500);
}
catch (InterruptedException e) {}
}
if(module.getClass().getName().equals("Admins")){
admins = (Admins)module;
}
if(module.getClass().getName().equals("Ignores")){
ignores = (Ignores)module;
}
if(module.getClass().getName().equals("Voting")){
voting.giveStream(serverout);
voting = (Voting)module;
}
}
}
}).start();
}
}
}
}
/**
* Listens to the system in
*/
public void inputListener() {
while (listening) {
if (clientin.hasNextLine()) {
String command = clientin.nextLine();
String[] splitcom = command.split(" ");
if (command.startsWith("/")) {
if (splitcom[0].equals("/join")) {
if (splitcom.length == 2) {
send("JOIN " + splitcom[1]);
}
else {
clientout.println("Error, not enough parameters");
}
}
else if (splitcom[0].equals("/leave")) {
if (splitcom.length == 2) {
send("PART " + splitcom[1]);
}
else {
clientout.println("Error, not enough parameters");
}
}
else if (splitcom[0].equals("/raw")) {
if (splitcom.length > 1) {
String tosend = "";
for(int i = 1; i < splitcom.length; i++){
tosend += (splitcom[i] + " ");
}
send(tosend);
}
else {
clientout.println("Error, not enough parameters");
}
}
else {
clientout.println("Error, command not recognized");
}
}
else if (command.startsWith("#")) {
String message = "";
for (int i = 1; i < splitcom.length; i++) {
message += (splitcom[i] + " ");
}
pm(splitcom[0], message);
}
}
}
}
public static void main(String[] args){
new IrcBot();
}
}