diff options
author | bill-auger <mr.j.spam.me@gmail.com> | 2018-09-06 16:03:47 +0000 |
---|---|---|
committer | bill-auger <mr.j.spam.me@gmail.com> | 2018-10-02 19:02:24 -0400 |
commit | ca99c6d84aefd0303e920c62b3aeb74a5421c4b4 (patch) | |
tree | 5cc4d8301b6b448f213dca68086911f55db56bb2 | |
parent | 52f8573f9f66e7b45f5204f3039f4b747d7b950a (diff) |
refactor translate feature into a module
-rw-r--r-- | bot_settings.sh | 8 | ||||
-rw-r--r-- | modules/m_translate.sh | 152 | ||||
-rw-r--r-- | process_event | 49 |
3 files changed, 178 insertions, 31 deletions
diff --git a/bot_settings.sh b/bot_settings.sh index 9b4830c..fbd5e6c 100644 --- a/bot_settings.sh +++ b/bot_settings.sh @@ -198,7 +198,7 @@ readonly INJECT_NICK='01234-PBOT-BCDEF%' # invalid IRC nick # # The list should normally start with "modules rehash services umodes autojoin" # Some modules should be placed last. "factoids" and "karma are such modules. -config_modules="modules rehash services umodes autojoin ctcp spamfilter" +config_modules="modules rehash services umodes autojoin ctcp translate spamfilter" # Where are modules stored, this can be a relative path from the # main script of the bot, or an absolute path. @@ -271,6 +271,12 @@ config_module_ctcp_version_reply="pbot-ng $envbot_version" # (requires: $BECOME_OP_ON_JOIN and channel in $OP_CHANNELS) config_module_spamfilter_channels='#parabola' +##################################################################### +# Translate module +# +# Channels in which we translate chat. +config_module_translate_channels='#parabola' + ##################################################################### # SQLite3 module diff --git a/modules/m_translate.sh b/modules/m_translate.sh new file mode 100644 index 0000000..31852b3 --- /dev/null +++ b/modules/m_translate.sh @@ -0,0 +1,152 @@ +#!/bin/bash +########################################################################### +# # +# envbot - an IRC bot in bash # +# Copyright (C) 2007-2008 Arvid Norlander # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +########################################################################### +#-------------------------------------------------------------------------# +## This module translates arbitrary text from english to spanish. ## +## TODO: It will also translate previous messages from specified a user ## +## from spanish to english. ## +## Requires: the 'apertium' program and the 'en-es' pair to be installed ## +## Requires: (in bot_settings.sh) ## +## * channel in $config_module_translate_channels ## +#-------------------------------------------------------------------------# + + +readonly TRANSLATE_CHANNELS="${config_module_translate_channels}" + + +## setup/teardown ## + +module_translate_INIT() +{ + modinit_API='2' + modinit_HOOKS='on_PRIVMSG' + helpentry_module_translate_description="Provides support for translating messages." + + which apertium &>/dev/null || log_error "[TRANSLATE]: ERROR: module failed to load - cannot find the \`apertium\` program" + which apertium &>/dev/null +} + +module_translate_UNLOAD() +{ + return 0 +} + +module_translate_REHASH() +{ + return 0 +} + + +## events ## + +module_translate_on_PRIVMSG() # (hostmask , target , query) +{ + local hostmask=$1 + local target=$2 + local query=$3 + local bot_nick=${server_nick_current} + local was_handled + +DBG_TRANSLATE_CRITERIA + + if ! is_translatable_channel "${target}" || + ! is_translate_request "${query}" + then was_handled=0 + + else local message="${query#* translate }" + local nick="$(echo "${message%% *}" | tr -d '/')" + +DBG_TRANSLATE + + if is_known_user ${nick} + then translate_user ${target} ${nick} + else translate_message ${target} "${message}" + fi + + was_handled=1 + fi + + # supress or allow further handling of this message + return ${was_handled} +} + + +## helpers ## + +is_translatable_channel() # (target) +{ + local target=$1 + + [[ " ${TRANSLATE_CHANNELS} " =~ " ${target} " ]] +} + +is_translate_request() # (query) +{ + local query=$1 + local bot_nick=${server_nick_current} + + [[ "${query}" =~ ^"${bot_nick}: translate " ]] +} + +is_known_user() # (nick) +{ + local nick=$1 + + [[ -n "${nick}" ]] && [[ -d announcements/people/${nick} ]] +} + +translate_user() # (target , nick) +{ + local target=$1 + local nick=$2 + +DBG_TRANSLATE_USER + + # translate previous chat from $nick from spanish to english + send_msg "${target}" "(not yet implemented)" +} + +translate_message() # (target , message) +{ + local target=$1 + local message=$2 + + # translate $message from english to spanish + local translated_message=$(echo "${message}" | apertium en-es) + +DBG_TRANSLATE_MSG + + [[ "${translated_message}" ]] && send_msg "${target}" "${person} said: ${translated_message}" +} + + +## DEBUG ## + +# readonly DEBUG=0 +DBG_TRANSLATE_CRITERIA() +{ + (( ${DEBUG} )) || return + + echo -n "[TRANSLATE]: target='${target}'" ; ! is_translatable_channel ${target} && echo -n " => wrong channel - returning" ; echo ; + echo -n "[TRANSLATE]: query='${query}'" ; ! is_translate_request "${query}" && echo -n " => not a translate request - returning" ; echo ; +} +DBG_TRANSLATE() { (( ${DEBUG} )) && echo "[TRANSLATE]: !!!triggered!!! $(is_known_user ${nick} && echo is_known_user nick=${nick} || echo message=${message})" ; } +DBG_TRANSLATE_USER() { (( ${DEBUG} )) && echo "[TRANSLATE]: translate_user() nick=${nick}" ; } +DBG_TRANSLATE_MSG() { (( ${DEBUG} )) && echo "[TRANSLATE]: translate_message() ${person} said: ${translated_message}" ; } diff --git a/process_event b/process_event index 72511ea..413495a 100644 --- a/process_event +++ b/process_event @@ -71,19 +71,6 @@ function forget_fact esac } -function translate_user -{ - send_msg "${channel_it_came_from}" "(not yet implemented)" -} - -function translate_message -{ - # translate $message from english to spanish - local translated_message=$(echo "${message}" | apertium en-es) - - [[ "${translated_message}" ]] && send_msg "${channel_it_came_from}" "$person said: ${translated_message}" -} - function process_event { my_own_name='pbot' @@ -446,28 +433,20 @@ function process_event ;; - ############# - # translate # - ############# - - "${my_own_name}: translate "+([![:space:]])* ) - local message="${sentence#* translate }" - local nick="$(echo "${message%% *}" | tr -d '/')" - - if [[ -n "${nick}" ]] && [[ ${nick} != ${my_own_name} ]] - then if [[ -d announcements/people/${nick} ]] - then translate_user - else translate_message - fi - fi - ;; - - ######################## # unrecognised command # ######################## ${my_own_name}:* | ','* ) + local module + for module in ${config_modules} + do case ${module} in + 'translate' ) + [[ "${sentence}" =~ ^"${my_own_name}: translate " ]] && return 0 + ;; + esac + done + while read line do send_msg "${personoslash}" "${line}" @@ -479,6 +458,16 @@ ${my_own_name}: lemon is yummy ${my_own_name}: lemon isn't yummy ,lemon EOF + for module in ${config_modules} + do case ${module} in + 'translate') + send_msg "${personoslash}" 'translate "Hello, Pedro" to spanish:' + send_msg "${personoslash}" " ${my_own_name}: translate Hello, Pedro" + send_msg "${personoslash}" "translate Pedro's chat from spanish to english:" + send_msg "${personoslash}" " ${my_own_name}: translate Pedro" + ;; + esac + done ;; esac |