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).

1  
!752
2  
!1002672 // pircbot library
3  
4  
import org.jibble.pircbot.*;
5  
6  
static boolean useBouncer = false;
7  
8  
static S server = useBouncer ? "second.tinybrain.de" : "irc.freenode.net";
9  
static L<S> channels = litlist("##linux", "#pircbot");
10  
static S name = "Lookie";
11  
static int reconnectTimeout1 = 60; // seconds after which to send a ping to server
12  
static int reconnectTimeout2 = 80; // seconds after which we try to reconnect
13  
14  
static Map<S, PersistentLog<Map>> bulk = new TreeMap; // key = channel (with "#")
15  
16  
static MyBot bot;
17  
18  
p {
19  
  thread "IRC Log-In" {
20  
    bot = new MyBot;
21  
    bot.setVerbose(true);
22  
    print("PircBot connecting to " + server);
23  
    bot.connect(server);
24  
    print("PircBot connect issued");
25  
    
26  
    autoReconnect(bot);
27  
  }
28  
}
29  
30  
static class MyBot extends PircBot {
31  
  long lastReceived;
32  
  
33  
  MyBot() {
34  
    setName(name);
35  
    //setRealName(name); // newer version of PircBot only
36  
    setLogin(name);
37  
    setVersion(name);
38  
    setFinger("");
39  
    setAutoNickChange(true);
40  
  }
41  
  
42  
  public void handleLine(String line) {
43  
    getLog("freenode").add(litmap("time", smartNow(), "line", line));
44  
    super.handleLine(line);
45  
    lastReceived = now();
46  
  }
47  
  
48  
  public void onConnect() {
49  
    print("PircBot onConnect useBouncer=" + useBouncer);
50  
    if (useBouncer) {
51  
      S bouncerLogin = loadSecretTextFileMandatory("bouncer-login").trim();
52  
      print("Logging in to bouncer");
53  
      bot.sendMessage("root", bouncerLogin.trim());
54  
      
55  
      // session should already be there, so resume it
56  
      print("Resuming bouncer session");
57  
      bot.sendMessage("root", "connect freenode");
58  
    }
59  
    
60  
    for (S channel : channels)
61  
      bot.joinChannel(channel);
62  
  }
63  
}
64  
65  
static void autoReconnect(final MyBot bot) {
66  
  thread "PircBot Auto-Reconnect" {
67  
    while (licensed()) {
68  
      sleepSeconds(10);
69  
      
70  
      if (now() >= bot.lastReceived + reconnectTimeout2*1000)
71  
        try {
72  
          print("PircBot: Trying to reconnect...");
73  
          hardClose(bot);
74  
          
75  
          sleepSeconds(1); // allow for input thread to halt
76  
          
77  
          bot.reconnect();
78  
        } catch (Exception e) {
79  
          print("Reconnect fail: " + e);
80  
          // Couldn't reconnect!
81  
        }
82  
      else if (now() >= bot.lastReceived + reconnectTimeout1*1000)
83  
        bot.sendRawLine("TIME"); // send something
84  
    }
85  
  }
86  
}
87  
88  
static Socket getSocket(PircBot bot) {
89  
  ret (Socket) get(get(bot, "_inputThread"), "_socket");
90  
}
91  
92  
answer {
93  
  if "irc log size *"
94  
    exceptionToUser { ret lstr(getLog(m.unq(0))); }
95  
}
96  
97  
static synchronized PersistentLog<Map> getLog(S channelName) {
98  
  checkChannelName(channelName);
99  
  PersistentLog log = bulk.get(channelName);
100  
  if (log == null)
101  
    bulk.put(channelName, log = new PersistentLog(channelName + ".log"));
102  
  ret log;
103  
}
104  
105  
static S checkChannelName(S s) {
106  
  assertTrue(nempty(s));
107  
  for (int i = 0; i < l(s); i++)
108  
    if (!(
109  
      "#!".indexOf(s.charAt(i)) >= 0 || Character.isLetterOrDigit(s.charAt(i))))
110  
      fail(s);
111  
  ret s;
112  
}
113  
114  
static void hardClose(PircBot bot) {
115  
  if (bot == null) ret;
116  
  
117  
  try {
118  
    //bot.disconnect();
119  
    getSocket(bot).close();
120  
  }
121  
  catch (Exception e) { print(str(e)); }
122  
}
123  
124  
static void cleanMeUp() {
125  
  hardClose(bot);
126  
}
127  
128  
static long smartNow_last;
129  
130  
static long smartNow() {
131  
  ret smartNow_last = max(smartNow_last + 1, now());
132  
}

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: 629 / 963
Referenced in: [show references]