Skip to content
This repository has been archived by the owner on Feb 4, 2019. It is now read-only.

Commit

Permalink
Add netfault tolerance, allow sever scrub (#1)
Browse files Browse the repository at this point in the history
* Add netfault tolerance

* Rejoin channel on reconnect

* Change error messages

* Sleep after nick change

* Catch InterruptedException
  • Loading branch information
TheOtherUnknown authored Jan 31, 2019
1 parent 4d70feb commit 3076a0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/org/iuscsg/ccdclogger/IRCBotl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
package org.iuscsg.ccdclogger;
import org.jibble.pircbot.*;

public class IRCBotl extends PircBot{
public class IRCBotl extends PircBot {
private boolean exit = false;
public IRCBotl() {
this.setName("Logger-" + (int) (Math.random() * 500));
}
protected void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason)
{
if (recipientNick.equals(this.getNick()))
{
System.out.println("Bot was kicked from #events! Rejoining right away");
this.joinChannel("#events");
}
}

public void setExit(boolean status)
{
exit = status;
}

protected void onDisconnect()
{
if (!exit)
{
System.out.println("Connection lost! Reconnecting right away");
try
{
this.reconnect();
this.joinChannel("#events");
} catch (Exception e) {
System.out.println("Failed to reconnect. Exiting...");
System.exit(2);
}
}
}
}
15 changes: 14 additions & 1 deletion src/org/iuscsg/ccdclogger/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Main {
private static IRCBotl bot = new IRCBotl();
private static Scanner scan = new Scanner(System.in);
private static String chan = "#events";


public static void main(String[] args) {
// bot.setVerbose(true); // testing
Expand All @@ -16,13 +17,14 @@ public static void main(String[] args) {
System.out.print("What machine are you working on? This can be changed later: ");
machine = scan.nextLine();
System.out.print("Enter an IRC server address without port number (using 6667): ");
bot.setAutoNickChange(true);
String server = scan.nextLine();
try {
System.out.println("Connecting...");
bot.connect(server);
} catch (Exception e) {
System.out.println(
"Unable to connect to IRC server! Check your server settings, and make sure your nick is not in use.");
"Unable to connect to IRC server! Check your server settings, and try again later.");
System.exit(1);
}
bot.joinChannel(chan);
Expand All @@ -36,6 +38,7 @@ public static void main(String[] args) {
System.out.println("3. Change machines (Current: " + machine + ")");
System.out.println("4. Change name (Current: " + name + ")");
System.out.println("5. Change IRC bot nick (Current: " + bot.getNick() + ")");
System.out.println("6. Scrub server");
System.out.println("-1. Quit the program");
int option = Integer.parseInt(scan.nextLine());

Expand All @@ -60,8 +63,18 @@ public static void main(String[] args) {
case 5:
System.out.print("Enter a new nick: ");
bot.changeNick(scan.nextLine().replaceAll("\\s", ""));
try
{
Thread.sleep(2000); // Let the server catch up to the change
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
case 6:
bot.sendMessage(chan, "[" + machine + "] [" + name + "] Scrub started :(");
break;
case -1:
bot.setExit(true);
bot.quitServer("Exited logger");
System.exit(0);
default:
Expand Down

0 comments on commit 3076a0a

Please sign in to comment.