diff --git a/scripts/debug.js b/scripts/debug.js index d4ec2b2..c266782 100644 --- a/scripts/debug.js +++ b/scripts/debug.js @@ -1,7 +1,7 @@ const util = require('util'); module.exports = (robot) => { - robot.hear(/^debug user$/i, function (msg) { + robot.hear(/debug user$/i, function (msg) { username = msg.envelope msg.reply("```" + util.inspect(username) + "```") }); diff --git a/scripts/gbanque.js b/scripts/gbanque.js index b3bac15..e7f9d4b 100644 --- a/scripts/gbanque.js +++ b/scripts/gbanque.js @@ -6,19 +6,21 @@ module.exports = (robot) => { if (tool == "telegram"){ room = msg.envelope.user.telegram_chat.title } else if (tool == "axiomchat"){ - room = msg.envelope.user.room.name + room = msg.envelope.user.room } process.env.hb_cmd = msg.envelope.message.text + process.env.hb_isRoom = msg.envelope.user.roomType + idRoom = msg.envelope.room - if (typeof room === 'undefined' || room === null) { - process.env.hb_username = msg.envelope.user.name + "@" + tool + if (typeof room === 'undefined' || room === null || msg.envelope.user.roomType === 'd') { + process.env.hb_username = msg.envelope.user.name + "@" + tool + '%' + idRoom process.env.hb_room = room } else { process.env.hb_username = msg.envelope.user.name - process.env.hb_room = room + "@" + tool + process.env.hb_room = room + "@" + tool + '%' + idRoom } - exec("scripts/shell/gbanque.sh $hb_cmd~~~$hb_username~~~$hb_room", function(err, stdout, stderr) { + exec("scripts/shell/gbanque.sh $hb_cmd~~~$hb_username~~~$hb_room~~~$hb_isRoom", function(err, stdout, stderr) { if (err) { msg.send("Une erreur est survenu ..." + stderr) console.log("Statut d'erreur: " + err) diff --git a/scripts/reload.coffee b/scripts/reload.coffee deleted file mode 100644 index f6d4e42..0000000 --- a/scripts/reload.coffee +++ /dev/null @@ -1,129 +0,0 @@ -# Description: -# Allows Hubot to (re)load scripts without restart -# -# Commands: -# hubot reload - Reloads scripts without restart. Loads new scripts too. (a fork version that works perfectly) -# -# Author: -# spajus -# vinta -# m-seldin - -Fs = require 'fs' -Path = require 'path' - -oldCommands = null -oldListeners = null - -module.exports = (robot) -> - - robot.respond /reload/i, id:'reload-scripts.reload', (msg) -> - try - oldCommands = robot.commands - oldListeners = robot.listeners - - robot.commands = [] - robot.listeners = [] - - reloadAllScripts msg, success, (err) -> - msg.send err - catch error - console.log "Hubot reloader:", error - msg.send "Could not reload all scripts: #{error}" - - success = (msg) -> - # Cleanup old listeners and help - for listener in oldListeners - listener = {} - oldListeners = null - oldCommands = null - msg.send "Reloaded all scripts" - - - walkSync = (dir, filelist) -> - #walk through given directory and collect files - files = Fs.readdirSync(dir) - filelist = filelist || [] - for file in files - fullPath = Path.join(dir,file) - robot.logger.debug "Scanning file : #{fullPath}" - - if (Fs.statSync(fullPath).isDirectory()) - filelist = walkSync(fullPath, filelist) - else - #add full path file to returning collection - filelist.push(fullPath) - return filelist - - # ref: https://github.com/srobroek/hubot/blob/e543dff46fba9e435a352e6debe5cf210e40f860/src/robot.coffee - deleteScriptCache = (scriptsBaseDir) -> - if Fs.existsSync(scriptsBaseDir) - fileList = walkSync scriptsBaseDir - - for file in fileList.sort() - robot.logger.debug "file: #{file}" - if require.cache[require.resolve(file)] - try - cacheobj = require.resolve(file) - console.log "Invalidate require cache for #{cacheobj}" - delete require.cache[cacheobj] - catch error - console.log "Unable to invalidate #{cacheobj}: #{error.stack}" - robot.logger.debug "Finished deleting script cache!" - - reloadAllScripts = (msg, success, error) -> - robot = msg.robot - robot.emit('reload_scripts') - - robot.logger.debug "Deleting script cache..." - - scriptsPath = Path.resolve ".", "scripts" - deleteScriptCache scriptsPath - robot.load scriptsPath - - scriptsPath = Path.resolve ".", "src", "scripts" - deleteScriptCache scriptsPath - robot.load scriptsPath - - robot.logger.debug "Loading hubot scripts..." - - hubotScripts = Path.resolve ".", "hubot-scripts.json" - Fs.exists hubotScripts, (exists) -> - if exists - Fs.readFile hubotScripts, (err, data) -> - if data.length > 0 - try - scripts = JSON.parse data - scriptsPath = Path.resolve "node_modules", "hubot-scripts", "src", "scripts" - robot.loadHubotScripts scriptsPath, scripts - catch err - error "Error parsing JSON data from hubot-scripts.json: #{err}" - return - - robot.logger.debug "Loading hubot external scripts..." - - robot.logger.debug "Deleting cache for apppulsemobile" - deleteScriptCache Path.resolve ".","node_modules","hubot-apppulsemobile","src" - - externalScripts = Path.resolve ".", "external-scripts.json" - Fs.exists externalScripts, (exists) -> - if exists - Fs.readFile externalScripts, (err, data) -> - if data.length > 0 - try - robot.logger.debug "DATA : #{data}" - scripts = JSON.parse data - - if scripts instanceof Array - for pkg in scripts - scriptPath = Path.resolve ".","node_modules",pkg,"src" - robot.logger.debug "Deleting cache for #{pkg}" - robot.logger.debug "Path : #{scripts}" - deleteScriptCache scriptPath - catch err - error "Error parsing JSON data from external-scripts.json: #{err}" - robot.loadExternalScripts scripts - return - robot.logger.debug "step 5" - - success(msg) diff --git a/scripts/reload.js b/scripts/reload.js new file mode 100644 index 0000000..ba47117 --- /dev/null +++ b/scripts/reload.js @@ -0,0 +1,134 @@ +var Fs = require('fs'); +var Path = require('path'); + +module.exports = function(robot) { + robot.respond(/reload/i, { + id: 'reload-scripts.reload' + }, function(msg) { + var error; + try { + oldCommands = robot.commands; + oldListeners = robot.listeners; + robot.commands = []; + robot.listeners = []; + return reloadAllScripts(msg, success, function(err) { + return msg.send(err); + }); + } catch (_error) { + error = _error; + console.log("Hubot reloader:", error); + return msg.send("Could not reload all scripts: " + error); + } + }); + success = function(msg) { + var i, len, listener; + for (i = 0, len = oldListeners.length; i < len; i++) { + listener = oldListeners[i]; + listener = {}; + } + oldListeners = null; + oldCommands = null; + return msg.send("Reloaded all scripts"); + }; + var walkSync = function(dir, filelist) { + var file, files, fullPath, i, len; + files = Fs.readdirSync(dir); + filelist = filelist || []; + for (i = 0, len = files.length; i < len; i++) { + file = files[i]; + fullPath = Path.join(dir, file); + robot.logger.debug("Scanning file : " + fullPath); + if (Fs.statSync(fullPath).isDirectory()) { + filelist = walkSync(fullPath, filelist); + } else { + filelist.push(fullPath); + } + } + return filelist; + }; + deleteScriptCache = function(scriptsBaseDir) { + var cacheobj, error, file, fileList, i, len, ref; + if (Fs.existsSync(scriptsBaseDir)) { + fileList = walkSync(scriptsBaseDir); + ref = fileList.sort(); + for (i = 0, len = ref.length; i < len; i++) { + file = ref[i]; + robot.logger.debug("file: " + file); + if (require.cache[require.resolve(file)]) { + try { + cacheobj = require.resolve(file); + console.log("Invalidate require cache for " + cacheobj); + delete require.cache[cacheobj]; + } catch (_error) { + error = _error; + console.log("Unable to invalidate " + cacheobj + ": " + error.stack); + } + } + } + } + return robot.logger.debug("Finished deleting script cache!"); + }; + return reloadAllScripts = function(msg, success, error) { + var externalScripts, hubotScripts, scriptsPath; + robot = msg.robot; + robot.emit('reload_scripts'); + robot.logger.debug("Deleting script cache..."); + scriptsPath = Path.resolve(".", "scripts"); + deleteScriptCache(scriptsPath); + robot.load(scriptsPath); + scriptsPath = Path.resolve(".", "src", "scripts"); + deleteScriptCache(scriptsPath); + robot.load(scriptsPath); + robot.logger.debug("Loading hubot scripts..."); + hubotScripts = Path.resolve(".", "hubot-scripts.json"); + Fs.exists(hubotScripts, function(exists) { + if (exists) { + return Fs.readFile(hubotScripts, function(err, data) { + var scripts; + if (data.length > 0) { + try { + scripts = JSON.parse(data); + scriptsPath = Path.resolve("node_modules", "hubot-scripts", "src", "scripts"); + return robot.loadHubotScripts(scriptsPath, scripts); + } catch (_error) { + err = _error; + error("Error parsing JSON data from hubot-scripts.json: " + err); + } + } + }); + } + }); + robot.logger.debug("Loading hubot external scripts..."); + robot.logger.debug("Deleting cache for apppulsemobile"); + deleteScriptCache(Path.resolve(".", "node_modules", "hubot-apppulsemobile", "src")); + externalScripts = Path.resolve(".", "external-scripts.json"); + Fs.exists(externalScripts, function(exists) { + if (exists) { + return Fs.readFile(externalScripts, function(err, data) { + var i, len, pkg, scriptPath, scripts; + if (data.length > 0) { + try { + robot.logger.debug("DATA : " + data); + scripts = JSON.parse(data); + if (scripts instanceof Array) { + for (i = 0, len = scripts.length; i < len; i++) { + pkg = scripts[i]; + scriptPath = Path.resolve(".", "node_modules", pkg, "src"); + robot.logger.debug("Deleting cache for " + pkg); + robot.logger.debug("Path : " + scripts); + deleteScriptCache(scriptPath); + } + } + } catch (_error) { + err = _error; + error("Error parsing JSON data from external-scripts.json: " + err); + } + robot.loadExternalScripts(scripts); + } + }); + } + }); + robot.logger.debug("step 5"); + return success(msg); + }; +}; diff --git a/scripts/shell/functions.sh b/scripts/shell/functions.sh index 3f257f8..4565431 100755 --- a/scripts/shell/functions.sh +++ b/scripts/shell/functions.sh @@ -13,8 +13,10 @@ function DEBUG() chanDB() { if [[ "$1" =~ '@' ]]; then chan=$(echo "$1" | awk -F '@' '{ print $1 }') - tool=$(echo "$1" | awk -F '@' '{ print $2 }') + tool=$(echo "$1" | awk -F '@' '{ print $2 }' | awk -F '%' '{ print $1 }') + idRoom=$(echo "$1" | awk -F '%' '{ print $2 }') else +# chan=$(echo "$1" | awk -F '%' '{ print $1 }') chan="$1" fi [[ ! $chan ]] && echo "Please select user." && return 2 @@ -23,9 +25,10 @@ chanDB() { else homedir="$HOME/.bog/ids/$tool/@$chan" fi - DEBUG echo "_${FUNCNAME[0]}_: $chan" - DEBUG echo "_${FUNCNAME[0]}_: $tool" - DEBUG echo "_${FUNCNAME[0]}_: $homedir" + DEBUG echo "_${FUNCNAME[0]}_: args: $1" + DEBUG echo "_${FUNCNAME[0]}_: Chan: $chan" + DEBUG echo "_${FUNCNAME[0]}_: Tool: $tool" + DEBUG echo "_${FUNCNAME[0]}_: Homedir: $homedir" [[ -f $homedir/db ]] && source $homedir/db } @@ -46,9 +49,14 @@ sendmsg() { -H "Content-type:application/json" \ $AC_API_URL/api/v1/chat.postMessage \ -d "{ \"channel\": \"$chan\", \"text\": \"$msg\" }" > /dev/null 2>&1 + elif [[ $tool == "telegram" ]]; then + msg=$(echo "$msg" | sed "s/\\\n/%0a/g") + curl -s "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage?chat_id=$idRoom&parse_mode=Markdown&text=$msg" > /dev/null else echo -e "$msg" fi + + echo -e "arg: $1\nchan: $chan\ntool: $tool\nidRoom:$idRoom" } getPubkey() { @@ -95,8 +103,8 @@ info_account() { if [[ -d $homedir ]]; then sendmsg $chan "Récupération de votre solde en cours sur *$pubkey* ..." - local solde=$($silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2-) - echo "Votre solde est de *$solde*" + soldeAccount=$($silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2-) + echo "Votre solde est de *$soldeAccount*" else echo "Vous n'avez pas de compte chez nous." fi @@ -123,7 +131,7 @@ init_account() { [[ ! -d $homedir ]] && mkdir -p $homedir [[ -f /tmp/secret.dunikey ]] && rm -f $homedir/secret.dunikey && mv /tmp/secret.dunikey $homedir/secret.dunikey - echo -e "salt=\"$salt\"\npepper=\"$pepper\"\npubkey=\"$pubkey\"" > $homedir/db + echo -e "idRoom=\"$idRoom\"\nsalt=\"$salt\"\npepper=\"$pepper\"\npubkey=\"$pubkey\"" > $homedir/db # info_account $chan echo -e "Votre ḠCompte a bien été créé.\nPubkey: *$pubkey*" @@ -175,4 +183,32 @@ history_account() { echo "\`\`\`$result\`\`\`" } +link_account() { + chanDB "$1" + if ! [[ -f "$homedir"/_waitingLink ]]; then + masterChan="$chan@$tool" + chanDB "$2" + [[ ! -d "$homedir" ]] && echo "Le compte $chan@$tool n'existe pas chez nous. $homedir" && return 2 + local codeLink=$(i=0; while [[ $i -le 5 ]]; do echo $((0 + RANDOM % 9)); ((i++)); done | tr -d '\n') + echo -e "masterAccount=$1\ncodeLink=$codeLink" > $homedir/_waitingLink + sendmsg $2 "Une demande de fusion de compte a été émise de la part de $masterChan.\nSi vous êtes à l'origine de cette demande, veuillez répondre:\n\n*gbanque link $codeLink*\n\nà ce message.\nSinon, vous pouvez ignorer ce message." + else + source $homedir/_waitingLink + if [[ "$codeLink" == "$2" ]]; then + info_account $1 + if [[ $soldeAccount == "0.0 Ğ1" ]]; then + rm -r "$homedir" + local slaveHomeDir="$homedir" + local slaveChan="$chan@$tool" + chanDB "$masterAccount" + ln -sf "$homedir" "$slaveHomeDir" + echo "$slaveChan" >> "$homedir/slaves" + sendmsg "$chan@$tool" "Votre compte est désormais fusionné avec le compte $slaveChan" + chanDB "$slaveChan" + sendmsg "$chan@$tool" "Votre compte est désormais fusionné avec $chan" + fi + fi + fi +} + #[[ $(type $1 | grep "est une fonction") ]] && $@ || echo "Veuillez préciser votre commande" diff --git a/scripts/shell/gbanque.sh b/scripts/shell/gbanque.sh index 38a37fe..ee37477 100755 --- a/scripts/shell/gbanque.sh +++ b/scripts/shell/gbanque.sh @@ -5,13 +5,15 @@ MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized source $MY_PATH/functions.sh -args=$(echo "$@" | awk -F '~~~' '{ print $1 }' | tr ' ' '\n' | grep -vEwi 'gbanque|gb|biz|bog' | tr '\n' ' ') +args=$(echo "$@" | awk -F '~~~' '{ print $1 }' | tr ' ' '\n' | grep -vEwi 'gbanque|gb|bog' | tr '\n' ' ') cmd=$(echo "$args" | awk '{ print $1 }') -args=$(echo "$args" | awk '{$1=""; print $0}') +args=$(echo "$args" | awk '{$1=""; print $0}' | cut -d' ' -f2-) user=$(echo "$@" | awk -F '~~~' '{ print $2 }') room=$(echo "$@" | awk -F '~~~' '{ print $3 }') +isRoom=$(echo "$@" | awk -F '~~~' '{ print $4 }') +idRoom$(echo "$@" | awk -F '~~~' '{ print $5 }') -[[ $room == "undefined" ]] && dest="$user" || dest="#$room" +[[ $room == "undefined" || $isRoom == "d" ]] && dest="$user" || dest="#$room" init() { init_account "$dest"; } remove() { rm_account "$dest"; } @@ -19,8 +21,9 @@ info() { info_account "$dest"; } pay() { tx "$dest" $args; } history() { history_account "$dest"; } mp() { sendmsg "$user" "$args"; } +link() { link_account "$dest" "$args"; } -[[ "$cmd" =~ ^(init|remove|info|pay|history|mp)$ ]] && "$cmd" +[[ "$cmd" =~ ^(init|remove|info|pay|history|mp|link)$ ]] && "$cmd" [[ $? == 1 ]] && echo "Veuillez indiquer une commande valide" exit 0