Last active
June 28, 2019 09:20
-
-
Save doyousketch2/086ec19daa9a051f4d980b163f18f772 to your computer and use it in GitHub Desktop.
Translate Minetest players 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
##========================================================= | |
## @Doyousketch2 AGPL-3 Jun 17, 2019 | |
## https://www.gnu.org/licenses/agpl-3.0.en.html | |
# | |
# sudo apt install translate-shell | |
##========================================================= | |
## header | |
__module_name__ = 'Translate' | |
__module_version__ = '1.5' | |
__module_description__ = 'Translate Minetest players in IRC' | |
import hexchat | |
import difflib | |
import subprocess as sp | |
info = ( __module_name__, __module_version__, __module_description__ ) | |
hexchat .prnt( '{}, ver. {} loaded.\n {}'.format( *info ) ) | |
##========================================================= | |
''' notes: https://git.io/fjoWH written in Python 3 | |
translation prints before the original message, so channel names stay intact. | |
order can be reversed with EAT_HEXCHAT, but then it doesn't specify channel. | |
perhaps that info comes from userdata, but wiki doesn't specify how to resolve. | |
https://hexchat.readthedocs.io/en/latest/script_python.html | |
/command NICK Hi there! | |
word[0] is command | |
word[1] is NICK | |
word[2] is Hi | |
word[3] is there! | |
word_eol[0] is command NICK Hi there! | |
word_eol[1] is NICK Hi there! | |
word_eol[2] is Hi there! | |
word_eol[3] is there! | |
strange results found so far, mostly w/ single words: | |
hiya CAMc = Is the thickness | |
minetest = pastor | |
okidoki = okidok | |
mese = month | |
afk = afkan | |
noo = us | |
ye = is ~~ V | |
yes returned error code 2 ..? | |
had to rewrite using a "try, except" to ignore exceptions like this. | |
''' | |
##========================================================= | |
## variable declaration | |
print_debug_msgs = False ## set to True if you want to see excess jibberish. | |
dbg_string = 'dbg{}||{}||' | |
diff = difflib .Differ() | |
IRC_msgs = False ## set to True if you wish to translate all messages, even IRC lurkers. | |
## normally, IRC chatters should know what language is desired, so we don't waste bandwidth on their messages. | |
chmsg = 'Channel Message' | |
hilight = 'Channel Msg Hilight' | |
prmsg = 'Private Message to Dialog' | |
trans = ['trans', '-brief'] | |
channels = [ '#ve-minetest-servers', '#insidethebox', '##minetest-ctf' ] | |
##========================================================= | |
## define functions | |
def translate( word, word_eol, userdata ): | |
chan = hexchat .get_info( 'channel' ) | |
if chan in channels: | |
original_msg = hexchat .strip( word_eol[1] .rstrip( ' +' ), -1, 3 ) | |
## only translate players, not IRC chat, unless specified. | |
if original_msg .startswith( '<' ) or IRC_msgs: | |
## don't translate msg's that begin w/ arrows <--- or web links | |
if original_msg .find( '>', 1 ) and ( original_msg .find( 'http', 1 ) < 0 ): | |
delimiter = original_msg .split('> ') | |
msg = delimiter[1] | |
if print_debug_msgs: hexchat .prnt( dbg_string.format( 1, msg ) ) | |
phrase = trans + [str(msg)] | |
if print_debug_msgs: hexchat .prnt( dbg_string.format( 2, ', '.join(map(str, phrase)) ) ) | |
try: | |
translation = sp .check_output( phrase, universal_newlines=True ) .rstrip() | |
if msg.lower() != translation.lower() and translation != '': | |
name = delimiter[0] .lstrip('<') | |
if print_debug_msgs: | |
hexchat .prnt( dbg_string.format( 3, name ) ) | |
hexchat .prnt( dbg_string.format( 4, translation ) ) | |
differences = list( diff .compare( msg.split(), translation.split()) ) | |
new_msg = '' | |
total_diffs = 0 | |
for difference in differences: | |
if difference[0] != ' ': | |
if total_diffs < 3: | |
new_msg = new_msg +difference +' ' | |
total_diffs += 1 | |
else: | |
new_msg = '\{{}\} {}'.format( name, translation ) | |
hexchat .prnt( new_msg ) | |
except sp .CalledProcessError as err: | |
if print_debug_msgs: | |
hexchat .prnt( 'command: {} returned code: {}'.format( err.cmd, err.returncode ) ) | |
hexchat .prnt( dbg_string.format( 5, err.output ) ) | |
else: | |
hexchat .prnt( 'returned code: {}' .format( err.returncode ) ) | |
##========================================================= | |
## main loop | |
hexchat .hook_print( chmsg, translate, {'msgtype': chmsg} ) | |
## less likely to ever recieve /irc_msg that needs translated, | |
## for the sake of completeness though. | |
hexchat .hook_print( prmsg, translate, {'msgtype': prmsg} ) | |
##========================================================= | |
## eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment