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

133
LINES

< > BotCompany Repo | #1002681 // IRC Log Bot (working on bouncer)

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

Uses 75K of libraries. Compilation Failed (4974L/33K).

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

Author comment

Began life as a copy of #1002677

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: #1002681
Snippet name: IRC Log Bot (working on bouncer)
Eternal ID of this version: #1002681/1
Text MD5: 173de7540cadc7c59263f40ddf073087
Transpilation MD5: c9a5cf3890a1d74f79be96295782b00e
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-13 02:51:52
Source code size: 3407 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 606 / 639
Referenced in: [show references]