Created
December 21, 2011 23:09
-
-
Save JEG2/1508134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################### | |
### Dependencies ### | |
#################### | |
require "optparse" | |
require "cinch" | |
require "cinch/logger/null_logger" | |
###################### | |
### Option Parsing ### | |
###################### | |
options = { channels: [ ], | |
debug: false, | |
nick: "#{ENV['USER']}s_log_bot", | |
password: nil, | |
time_stamp_frequency: 5 } | |
ARGV.options do |opts| | |
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS] CHANNELS" | |
opts.separator "" | |
opts.separator "Specific Options:" | |
opts.on( "-d", "--[no-]debug", | |
"Set to debug this bot." ) do |boolean| | |
options[:debug] = boolean | |
end | |
opts.on( "-n", "--nick NICK", String, | |
"The IRC nick to login with" ) do |nick| | |
options[:nick] = nick | |
end | |
opts.on( "-p", "--password PASSWORD", String, | |
"Set this to authenticate with NICKSERV." ) do |password| | |
options[:password] = password | |
end | |
opts.on( "-t", "--time-stamp-frequency INT", Integer, | |
"The delay between time stamps in minutes." ) do |minutes| | |
options[:time_stamp_frequency] = minutes | |
end | |
opts.separator "Common Options:" | |
opts.on( "-h", "--help", | |
"Show this message." ) do | |
puts opts | |
exit | |
end | |
begin | |
opts.parse! | |
options[:channels].push(*ARGV) | |
fail "You must list channels" if options[:channels].empty? | |
rescue | |
puts opts | |
exit | |
end | |
end | |
############################# | |
### Optional Dependencies ### | |
############################# | |
require "cinch/plugins/identify" if options[:password] | |
############### | |
### IRC Bot ### | |
############### | |
bot = Cinch::Bot.new do | |
self.logger = Cinch::Logger::NullLogger.new unless options[:debug] | |
configure do |c| | |
c.server = "irc.freenode.org" | |
c.nick = options[:nick] | |
c.channels = options[:channels].map { |n| n.sub(/\A(?!#)/, "#") } | |
if options[:password] | |
c.plugins.plugins = [Cinch::Plugins::Identify] | |
c.plugins.options[Cinch::Plugins::Identify] = { | |
username: options[:nick], | |
password: options[:password], | |
type: :nickserv | |
} | |
end | |
@last_printed_time = nil | |
@logs = { } | |
end | |
on :channel do |message, _| | |
if message.channel and message.user | |
channel_name = message.channel.name | |
message_time = Time.now | |
day_of_message = message_time.strftime("%Y-%m-%d") | |
if not @logs.include?(channel_name) or | |
File.basename( @logs[channel_name].path, | |
".log" ) !~ /-#{day_of_message}\z/ | |
@logs[channel_name].close if @logs[channel_name] | |
clean_channel_name = channel_name.sub(/\A#+/, "") | |
log_file_name = "#{clean_channel_name}-#{day_of_message}.log" | |
@logs[channel_name] = open(log_file_name, "a") | |
@last_printed_time = nil | |
end | |
if @last_printed_time.nil? or | |
message_time - @last_printed_time > options[:time_stamp_frequency] * 60 | |
@logs[channel_name].puts unless @last_printed_time.nil? | |
@logs[channel_name].puts message_time.strftime("%Y-%m-%d %H:%M") | |
@last_printed_time = message_time | |
end | |
@logs[channel_name].puts "#{message.user.nick}: #{message.message}" | |
end | |
end | |
end | |
bot.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment