Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

132
LINES

< > BotCompany Repo | #1002677 // IRC Log Bot (WORKS, with auto-reconnect, does not work with bouncer)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 75K of libraries. Click here for Pure Java version (1932L/12K/41K).

!752

!1002672 // pircbot library

import org.jibble.pircbot.*;

static boolean useBouncer = false;

static S server = useBouncer ? "second.tinybrain.de" : "irc.freenode.net";
static L<S> channels = litlist("##linux", "#pircbot");
static S name = "Lookie";
static int reconnectTimeout1 = 60; // seconds after which to send a ping to server
static int reconnectTimeout2 = 80; // seconds after which we try to reconnect

static Map<S, PersistentLog<Map>> bulk = new TreeMap; // key = channel (with "#")

static MyBot bot;

p {
  thread "IRC Log-In" {
    bot = new MyBot;
    bot.setVerbose(true);
    print("PircBot connecting to " + server);
    bot.connect(server);
    print("PircBot connect issued");
    
    autoReconnect(bot);
  }
}

static class MyBot extends PircBot {
  long lastReceived;
  
  MyBot() {
    setName(name);
    //setRealName(name); // newer version of PircBot only
    setLogin(name);
    setVersion(name);
    setFinger("");
    setAutoNickChange(true);
  }
  
  public void handleLine(String line) {
    getLog("freenode").add(litmap("time", smartNow(), "line", line));
    super.handleLine(line);
    lastReceived = now();
  }
  
  public void onConnect() {
    print("PircBot onConnect useBouncer=" + useBouncer);
    if (useBouncer) {
      S bouncerLogin = loadSecretTextFileMandatory("bouncer-login").trim();
      print("Logging in to bouncer");
      bot.sendMessage("root", bouncerLogin.trim());
      
      // session should already be there, so resume it
      print("Resuming bouncer session");
      bot.sendMessage("root", "connect freenode");
    }
    
    for (S channel : channels)
      bot.joinChannel(channel);
  }
}

static void autoReconnect(final MyBot bot) {
  thread "PircBot Auto-Reconnect" {
    while (licensed()) {
      sleepSeconds(10);
      
      if (now() >= bot.lastReceived + reconnectTimeout2*1000)
        try {
          print("PircBot: Trying to reconnect...");
          hardClose(bot);
          
          sleepSeconds(1); // allow for input thread to halt
          
          bot.reconnect();
        } catch (Exception e) {
          print("Reconnect fail: " + e);
          // Couldn't reconnect!
        }
      else if (now() >= bot.lastReceived + reconnectTimeout1*1000)
        bot.sendRawLine("TIME"); // send something
    }
  }
}

static Socket getSocket(PircBot bot) {
  ret (Socket) get(get(bot, "_inputThread"), "_socket");
}

answer {
  if "irc log size *"
    exceptionToUser { ret lstr(getLog(m.unq(0))); }
}

static synchronized PersistentLog<Map> getLog(S channelName) {
  checkChannelName(channelName);
  PersistentLog log = bulk.get(channelName);
  if (log == null)
    bulk.put(channelName, log = new PersistentLog(channelName + ".log"));
  ret log;
}

static S checkChannelName(S s) {
  assertTrue(nempty(s));
  for (int i = 0; i < l(s); i++)
    if (!(
      "#!".indexOf(s.charAt(i)) >= 0 || Character.isLetterOrDigit(s.charAt(i))))
      fail(s);
  ret s;
}

static void hardClose(PircBot bot) {
  if (bot == null) ret;
  
  try {
    //bot.disconnect();
    getSocket(bot).close();
  }
  catch (Exception e) { print(str(e)); }
}

static void cleanMeUp() {
  hardClose(bot);
}

static long smartNow_last;

static long smartNow() {
  ret smartNow_last = max(smartNow_last + 1, now());
}

Author comment

Began life as a copy of #1002676

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002677
Snippet name: IRC Log Bot (WORKS, with auto-reconnect, does not work with bouncer)
Eternal ID of this version: #1002677/1
Text MD5: e0b4845295b378345a9576b68e72b4a2
Transpilation MD5: 595eea6f424d1c390792279cb4febbe3
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-13 03:02:46
Source code size: 3413 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 625 / 957
Referenced in: [show references]