Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created December 15, 2010 01:59
Show Gist options
  • Save FiXato/741520 to your computer and use it in GitHub Desktop.
Save FiXato/741520 to your computer and use it in GitHub Desktop.
A simple prototype script to parse the unrealircd ip.conf file.
#!/usr/bin/env ruby
#Simple mockup script for Chat4All to parse the UnrealIRCd ip.conf file which holds the 'ban ip'-blocks.
#LICENSE:
#Copyright 2010 Filip H.F. "FiXato" Slagter
#You may use this work without restrictions, as long as this notice is included.
#The work is provided "as is" without warranty of any kind, neither express nor implied.
# Get the lines from the Unreal ban ips.conf file. Strip leading and trailing whitespaces and repeated spaces.
# Compact so there are no empty lines / nil lines left in the array.
filepath = File.expand_path("~/script_resources/ips.conf")
lines = File.readlines(filepath).map{|l|
l.strip!
l = l.squeeze(' ')
l == '' ? nil : l
}.compact
# Initialise the bans hash
bans = {}
# Go through all the lines and their index
lines.each_with_index do |line,idx|
# Don't start parsing unless the line is the start of an Unreal ban ip block
next unless line == "ban ip {";
# The IP is the first line after the 'ban ip {' line. Strip the Unreal arguments
ip = lines[idx+1].gsub('mask ','').gsub(';','');
# The Unreal reason is the second line of the block. Also strip the Unreal arguments
reason = lines[idx+2].gsub('reason "','').gsub('";','');
# Check the line before the block. If it starts with a # then use that comment as a custom reason, otherwise leave the custom reason an empty string
custom_reason = (lines[idx-1][0..0] == '#' ? lines[idx-1][1..-1].strip : '')
# Construct a hash with the IP as key. Using the IP as key will prevent duplicate entries,
# because if it encounters another block with the same IP, it will just overwrite the reason and custom reason.
bans[ip] = {
:ip => ip,
:reason => reason,
:custom_reason => custom_reason,
}
end
# Write away the generated config file
File.open(File.expand_path("~/script_resources/parsed_ips.conf"),"w") do |f|
bans.each do |key,hash|
# Only print the custom reason if there is one
f.puts '#%s' % hash[:custom_reason] if hash[:custom_reason].to_s.size > 0
f.puts 'ban ip {'
f.puts ' mask %s;' % hash[:ip]
f.puts ' reason "%s";' % hash[:reason]
f.puts '};'
f.puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment