diff --git a/bashbot.sh b/bashbot.sh index ef59191..25066f5 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-pre-4-gd1a3372 +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d # # Exit Codes: # - 0 sucess (hopefully) @@ -132,6 +132,20 @@ fi # internal functions +# $1 URL, $2 filename in TMPDIR +# outputs final filename +download() { + local empty="no.file" file="${2:-${empty}}" + if [[ "$file" = *"/"* ]] || [[ "$file" = "."* ]]; then file="${empty}"; fi + while [ -f "${TMPDIR:-.}/${file}" ] ; do file="$RAMDOM-${file}"; done + getJson "$1" >"${TMPDIR:-.}/${file}" || return + printf '%s\n' "${TMPDIR:-.}/${file}" +} + +# easy handling of users: +_is_botadmin() { + user_is_botadmin "${USER[ID]}" +} # $1 postfix, e.g. chatid # $2 prefix, back- or startbot- procname(){ diff --git a/doc/6_reference.md b/doc/6_reference.md index 636f56c..63923b2 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -544,6 +544,20 @@ You must include ```source modules/aliases.sh``` in 'commands.sh' to have the f ### Helper functions +##### download +Download the fiven URL ans returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a +random number. filename is not allowed to contain '/' or '..'. + +*usage:* download URL filename + +*example:* +```bash +file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" +echo "$file" -> ./data-bot-bash/avatar.jpg +file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" +echo "$file" -> ./data-bot-bash/12345-avatar.jpg +``` + ##### _exists Returns true if the given function exist, can be used to check if a module is loaded. @@ -629,5 +643,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d diff --git a/modules/aliases.sh b/modules/aliases.sh index 859a447..1d18b43 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -5,14 +5,10 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d # # source from commands.sh to use the aliases -# easy handling of users: -_is_botadmin() { - user_is_botadmin "${USER[ID]}" -} _is_admin() { user_is_admin "${CHAT[ID]}" "${USER[ID]}" }