Created
August 22, 2015 02:56
-
-
Save irisfofs/7d586be384d4fbbcfcc6 to your computer and use it in GitHub Desktop.
Ruby script to convert lines from my IRC-Skype relay bot to be as though they were spoken in IRC.
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
require 'set' | |
TIMESTAMP = /^(?<timestamp>[-0-9 :]{19})/ | |
BOT_NAME = /\tSkyBot\t/ | |
SKYPE_MSG = /#{TIMESTAMP}#{BOT_NAME}<(?<nick>[^>]+)>(?<text>.*)$/ | |
SKYPE_ACTION = /#{TIMESTAMP}#{BOT_NAME}\* (?<text>.*)$/ | |
# NICK = /([\w\-\\\[\]\{\}\^\`\|]+)/ | |
# Characters that can't be in a nick | |
NOT_NICK = /[^\w\-\\\[\]\{\}\^\`\|]/ | |
def clean(nick) | |
nick.gsub(NOT_NICK,'_') | |
end | |
nicks = Set.new | |
updated_files = {} | |
# Reformat all normal messages and collect a list of nicks who speak | |
Dir.glob("vore_merge/*").each { |filename| | |
File.open(filename) { |file| | |
updated_files[filename] = [] | |
file.each_line { |line| | |
if line =~ SKYPE_MSG | |
nicks << $~[:nick] | |
nick = clean($~[:nick]) | |
modified_line = "#{$~[:timestamp]}\t#{nick}\t#{$~[:text]}\n" | |
updated_files[filename] << modified_line | |
else | |
updated_files[filename] << line | |
end | |
} | |
} | |
} | |
puts nicks.inspect | |
# Transform all actions from nicks who have spoken. | |
updated_files.each { |filename, contents| | |
contents.map! { |line| | |
if line =~ SKYPE_ACTION | |
# If no nicks match, we want to return nil | |
modified_line = nil | |
nicks.each { |nick| | |
if $~[:text].start_with? nick | |
timestamp = $~[:timestamp] | |
text = $~[:text].sub(nick, clean(nick)) | |
modified_line = "#{timestamp}\t *\t#{text}\n" | |
break | |
end | |
} | |
modified_line | |
else | |
line | |
end | |
} | |
# Remove nils. | |
contents.compact! | |
# Write all files out again | |
File.open(filename, "w") { |f| | |
f.write(contents.join) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment