Skip to content

Instantly share code, notes, and snippets.

@UnknownEntity634
Created April 18, 2013 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save UnknownEntity634/5415063 to your computer and use it in GitHub Desktop.
Save UnknownEntity634/5415063 to your computer and use it in GitHub Desktop.
=begin
Type /parsebotlist and go get some noms depending on bot list length while it collects info.
Requires Ruby 1.9+ due to (?<variable>) and =~ usage
TODO:
- Make it wait between commands as to add the headings at the proper location instead of as the first 2 lines (this is literally my only timing problem left)
- Probably rewrite the regex matching (=~ buffer method) with a method for backwards compatibility with older Ruby
=end
require 'fileutils'
def weechat_init
Weechat.register("weebots", "Unknown Entity", "1.0", "GPL3", "Acquire /bs info on bots and parse it out", "", "")
Weechat.hook_signal("*,irc_in2_notice", "notice_cb", "")
Weechat.hook_signal("*,irc_in2_319", "cb_319", "") # Channel list from /whiois nick
Weechat.hook_signal("*,irc_in2_317", "cb_317", "") # Idle time from /whois nick nick
hook = Weechat.hook_command("parsebotlist", "Parse botlist info retrieved after having done /bs botlist", "", "", "", "parsebotlist_cb", "")
# Check path option, if nonexistent create it
if Weechat.config_is_set_plugin('outpath') == 0
Weechat.config_set_plugin('outpath', File.expand_path(File.join('~', 'weebotslist')))
end
outpath = Weechat.config_get_plugin('outpath')
Weechat.print('', 'weebots Output path set to ' + outpath)
# Make the directory if it doesn't exist
if ! File.directory?(outpath)
Dir.mkdir(outpath)
end
return Weechat::WEECHAT_RC_OK
end
# grab only botserv notices we need
def notice_cb(data, signal, signal_data)
outpath = Weechat.config_get_plugin('outpath')
fp = File.open(File.expand_path(File.join(outpath,'botlist')),'a+')
fpd = File.open(File.expand_path(File.join(outpath,'botlistdetail')),'a+')
nick = Weechat.info_get("irc_nick_from_host", signal_data)
# Limit the split to 3 matches. The raw data has 2 colons in it; one at the very start and one at the start of the message proper. Anything after that is not to be counted.
#buffer = signal_data.split(":",3)[-1]
buffer = signal_data.split(":",3)[-1]
if (buffer && nick == 'BotServ') # Filter out stuff like server notices; we don't care about those here
#/bs botlist checking first
if /^Bot list:/ =~ buffer
fp.puts('=== Public Bot List ===')
elsif /^\s{3}(?<botname>\S*)\s+\(\S+@\S+\)/ =~ buffer
fp.puts(botname)
elsif /^Bots reserved to IRC operators:/ =~ buffer
fp.puts('=== Private Bot List ===')
# And now for /bs info checking
elsif /^Information for bot (?<botname>.*):/ =~ buffer
fpd.print("Found #{botname}!")
elsif /^ Mask : (?<mask>.*)/ =~ buffer
fpd.print("#{mask} ")
elsif /^ Real name : (?<realname>.*)/ =~ buffer
fpd.print("(#{realname})")
elsif /^ Used on : (?<numchans>\d+) channel\(s\)/ =~ buffer
if numchans.to_i > 0
fpd.print("; used on #{numchans} channel(s)")
else
fpd.puts("; used on #{numchans} channel(s)")
end
elsif (/\s{1,2}(#\S+)+/.match(buffer))
fpd.print ' => '
# .match *would* be used instead of having to rescan with the same regex, but Regex.match stops after one match.
# String.scan does not, nor does it return nil or any indication of no results to use in the elsif
matches = buffer.scan(/\s{1,2}(#\S+)+/) { |m| fpd.print("#{m[0]} ") }
fpd.puts("")
end
end
fp.close
fpd.close
return Weechat::WEECHAT_RC_OK
end
# Now we need to grab raw /whois output; to save some time we'll only pick up numerics 319 (channel list from /whois nick) and 317 (idle timefrom /whois nick nick)
def cb_317(data, signal, signal_data)
outpath = Weechat.config_get_plugin('outpath')
fp = File.open(File.expand_path(File.join(outpath,'botlistdetail')),'a+') #botwhois
nick = signal_data.split(' ')[3]
params = signal_data.split(' ')
time = params[4].to_i
hours = (time/3600).to_i
minutes = (time/60 - hours * 60).to_i
seconds = (time - (minutes * 60 + hours * 3600))
fp.printf("%s idle %02d:%02d:%02d\n", nick, hours, minutes, seconds)
fp.close
return Weechat::WEECHAT_RC_OK
end
def cb_319(data, signal, signal_data)
outpath = Weechat.config_get_plugin('outpath')
fp = File.open(File.expand_path(File.join(outpath,'botlistdetail')),'a+')
nick = signal_data.split(' ')[3]
fp.puts(nick + ' on ' + signal_data.split(':')[-1])
fp.close
return Weechat::WEECHAT_RC_OK
end
def parser_cb(data, remaining_calls)
sbuf = Weechat.info_get("irc_buffer", Weechat.buffer_get_string(Weechat.current_buffer(), 'localvar_server'))
outpath = Weechat.config_get_plugin('outpath')
botlist = File.expand_path(File.join(outpath,'botlist'))
botdetail = File.expand_path(File.join(outpath,'botlistdetail'))
fpd = File.open(File.expand_path(File.join(outpath,'botlistdetail')),'a+')
File.readlines(File.expand_path(File.join(outpath,'botlist'))).each do |bn|
bn = bn.strip
if /^===/.match(bn)
fpd.puts(bn)
else
# The 3 second 'wait' could probably be knocked down to 1 or 2, but I want to test this first
Weechat.hook_timer(3*1000, 0, 1, 'botparse_cb', bn)
end
end
fpd.close
Weechat.print(sbuf, 'Bot List dump complete')
return Weechat::WEECHAT_RC_OK
end
def botparse_cb(data, remaining_calls)
sbuf = Weechat.info_get("irc_buffer", Weechat.buffer_get_string(Weechat.current_buffer(), 'localvar_server'))
Weechat.command(sbuf, "/quote BotServ info #{data} all")
Weechat.command(sbuf, "/quote whois #{data}")
Weechat.command(sbuf, "/quote whois #{data} #{data}")
return Weechat::WEECHAT_RC_OK
end
def parsebotlist_cb(data, buffer, args)
sbuf = Weechat.info_get("irc_buffer", Weechat.buffer_get_string(Weechat.current_buffer(), 'localvar_server'))
outpath = Weechat.config_get_plugin('outpath')
botlist = File.expand_path(File.join(outpath,'botlist'))
botdetail = File.expand_path(File.join(outpath,'botlistdetail'))
# Clear out all of the files
FileUtils.touch(botlist)
FileUtils.touch(botdetail)
File.truncate(botlist, 0)
File.truncate(botdetail, 0)
Weechat.command(sbuf, "/quote BotServ botlist")
Weechat.print(sbuf, 'Waiting 5 seconds for /bs botlist to finish...')
Weechat.hook_timer(5*1000, 0, 1, 'parser_cb', '') # Alternatively, as close to 'wait' as we can get
return Weechat::WEECHAT_RC_OK
end
=begin
Reference raw output here
00:38:24 Chat4All <-- | WHOIS watchdog
00:38:24 Chat4All --> | :eu.chat4all.org 311 o_o WatchdoG BotServ irc.chat4all.org * :Chat4All Bot:\02 /join #help \02for more information
00:38:24 Chat4All --> | :eu.chat4all.org 379 o_o WatchdoG :is using modes +Sq
00:38:24 Chat4All --> | :eu.chat4all.org 378 o_o WatchdoG :is connecting from *@irc.chat4all.org
00:38:24 Chat4All --> | :eu.chat4all.org 319 o_o WatchdoG :&#trivia &#lounge &#ircops &#idlerpg &#help &#chat4all
00:38:24 Chat4All --> | :eu.chat4all.org 312 o_o WatchdoG services.chat4all.org :Chat4All Services
00:38:24 Chat4All --> | :eu.chat4all.org 313 o_o WatchdoG :is a Network Service
00:38:24 Chat4All --> | :eu.chat4all.org 318 o_o watchdog :End of /WHOIS list.
00:38:26 Chat4All <-- | WHOIS watchdog watchdog
00:38:27 Chat4All --> | :services.chat4all.org 311 o_o WatchdoG BotServ irc.chat4all.org * :Chat4All Bot:\02 /join #help \02for more information
00:38:27 Chat4All --> | :services.chat4all.org 307 o_o WatchdoG :is a registered nick
00:38:27 Chat4All --> | :services.chat4all.org 312 o_o WatchdoG services.chat4all.org :Chat4All Services
00:38:27 Chat4All --> | :services.chat4all.org 317 o_o WatchdoG 20356 1334736301 :seconds idle, signon time
00:38:27 Chat4All --> | :services.chat4all.org 318 o_o WatchdoG :End of /WHOIS list.
00:47:57 Chat4All <-- | bs info watchdog
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o :Information for bot \02WatchdoG\02:
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : Mask : BotServ@irc.chat4all.org
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : Real name : Chat4All Bot:\02 /join #help \02for more information
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : Created : Dec 04 16:10:45 2003 CET
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : Options : Private
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : Used on : 14 channel(s)
00:47:58 Chat4All --> | :BotServ!services@chat4all.org NOTICE o_o : #beginner #casual #chat4all #class #cservice #help #HelpDesk #idleRPG #irchelp #ircops #lounge #mirc #trivia #worldchat
17:24:04 Chat4All <-- | bs botlist
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity :Bot list:
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : aike (aike@is.mijn.naam)
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : AkhadaQueen (info@indiancyberarmy.org)
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : Anna (Anna@Boten.Anna)
...
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity :Bots reserved to IRC operators:
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : _Bruce_ (Benzje@TossRadio.nl)
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : Aladdin (Aladdin@Thousand.And.One.Tales)
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : buttkicker (kick@kickchannel.rpg)
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity : Buzz (Buzz@infinity.and.beyond)
...
17:24:05 Chat4All --> | :BotServ!services@chat4all.org NOTICE Unknown_Entity :75 bots available.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment