slitaz-forge view irc/src/org/jibble/logbot/LogBot.java @ rev 313

check i18n: make {pot,msgfmt,clean}
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jul 01 20:00:47 2012 +0000 (2012-07-01)
parents
children efd18dbda796
line source
1 package org.jibble.logbot;
3 import java.util.*;
4 import java.util.regex.*;
5 import java.io.*;
6 import java.text.SimpleDateFormat;
7 import org.jibble.pircbot.*;
9 public class LogBot extends PircBot {
11 private static final Pattern urlPattern = Pattern.compile("(?i:\\b((http|https|ftp|irc)://[^\\s]+))");
12 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
13 private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
15 public static final String GREEN = "irc-green";
16 public static final String WHITE = "irc-white";
17 public static final String ORANGE = "irc-orange";
18 public static final String YELLOW = "irc-yellow";
19 public static final String GREY = "irc-grey";
20 public static final String RED = "irc-red";
22 public LogBot(String name, File outDir, String joinMessage) {
23 setName(name);
24 setVerbose(true);
25 this.outDir = outDir;
26 this.joinMessage = joinMessage;
27 }
29 public void append(String color, String line) {
30 line = Colors.removeFormattingAndColors(line);
32 line = line.replaceAll("&", "&amp;");
33 line = line.replaceAll("<", "&lt;");
34 line = line.replaceAll(">", "&gt;");
36 Matcher matcher = urlPattern.matcher(line);
37 line = matcher.replaceAll("<a href=\"$1\">$1</a>");
40 try {
41 Date now = new Date();
42 String date = DATE_FORMAT.format(now);
43 String time = TIME_FORMAT.format(now);
44 File file = new File(outDir, date + ".log");
45 BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
46 String entry = "<span class=\"irc-date\">(" + time + ")</span> <span class=\"" + color + "\">" + line + "</span><br />";
47 writer.write(entry);
48 writer.newLine();
49 writer.flush();
50 writer.close();
51 }
52 catch (IOException e) {
53 System.out.println("Could not write to log: " + e);
54 }
55 }
57 public void onAction(String sender, String login, String hostname, String target, String action) {
58 append(YELLOW, "* " + sender + " " + action);
59 }
61 public void onJoin(String channel, String sender, String login, String hostname) {
62 append(GREEN, "*** " + sender + " (" + login + "@" + hostname + ") has joined " + channel);
63 }
65 public void onMessage(String channel, String sender, String login, String hostname, String message) {
66 append(WHITE, "[" + sender + "] " + message);
68 message = message.toLowerCase();
69 if (message.startsWith(getNick().toLowerCase()) && message.indexOf("help") > 0) {
70 sendMessage(channel, joinMessage);
71 }
72 }
74 public void onMode(String channel, String sourceNick, String sourceLogin, String sourceHostname, String mode) {
75 append(ORANGE, "*** " + sourceNick + " sets mode " + mode);
76 }
78 public void onNickChange(String oldNick, String login, String hostname, String newNick) {
79 append(ORANGE, "*** " + oldNick + " is now known as " + newNick);
80 }
82 public void onPart(String channel, String sender, String login, String hostname) {
83 append(GREY, "*** " + sender + " (" + login + "@" + hostname + ") has left " + channel);
84 }
86 public void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason) {
87 append(RED, "*** " + sourceNick + " (" + sourceLogin + "@" + sourceHostname + ") has quit (" + reason + ")");
88 }
90 public void onTopic(String channel, String topic, String setBy, long date, boolean changed) {
91 if (changed) {
92 append(GREEN, "*** " + setBy + " changes topic to '" + topic + "'");
93 }
94 else {
95 append(GREEN, "*** Topic is '" + topic + "'");
96 append(GREEN, "*** Set by " + setBy + " on " + new Date(date));
97 }
98 }
100 public void onVersion(String sourceNick, String sourceLogin, String sourceHostname, String target) {
101 }
103 public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) {
104 append(GREY, "*** " + recipientNick + " was kicked from " + channel + " by " + kickerNick);
105 if (recipientNick.equalsIgnoreCase(getNick())) {
106 joinChannel(channel);
107 }
108 }
110 public void onDisconnect() {
111 append(RED, "*** Disconnected.");
112 while (!isConnected()) {
113 try {
114 reconnect();
115 }
116 catch (Exception e) {
117 try {
118 Thread.sleep(10000);
119 }
120 catch (Exception anye) {
121 // Do nothing.
122 }
123 }
124 }
125 }
127 public static void copy(File source, File target) throws IOException {
128 BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));
129 BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(target));
130 int bytesRead = 0;
131 byte[] buffer = new byte[1024];
132 while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) {
133 output.write(buffer, 0, bytesRead);
134 }
135 output.flush();
136 output.close();
137 input.close();
138 }
140 private File outDir;
141 private String joinMessage;
143 }