From 0a8145685d8d13da0c12e61b034a2323a22f46e2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 29 Aug 2014 17:58:28 +0200 Subject: Support multipastes Signed-off-by: Florian Pritz --- fb-helper.c | 27 +++++++++++++++++++++------ fb.1 | 6 ++++++ fb.in | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/fb-helper.c b/fb-helper.c index b550887..261d3b8 100644 --- a/fb-helper.c +++ b/fb-helper.c @@ -221,11 +221,12 @@ void display_help() printf("Usage: fb-helper \n"); printf("\n"); printf("Options:\n"); - printf(" -D Print debugging information\n"); - printf(" -h This help\n"); - printf(" -u URL of pastebin or URL to download\n"); - printf(" -f File to upload to URL\n"); - printf(" -a Path to API key file\n"); + printf(" -D Print debugging information\n"); + printf(" -h This help\n"); + printf(" -u URL of pastebin or URL to download\n"); + printf(" -f File to upload to URL\n"); + printf(" -F key=value Post key=value\n"); + printf(" -a Path to API key file\n"); } int main(int argc, char *argv[]) @@ -264,7 +265,7 @@ int main(int argc, char *argv[]) exit(0); } - while ((opt = getopt(argc, argv, "Du:f:m:a:h")) != -1) { + while ((opt = getopt(argc, argv, "Du:f:F:m:a:h")) != -1) { switch (opt) { case 'D': options.debug = 1; break; @@ -272,6 +273,20 @@ int main(int argc, char *argv[]) case 'f': options.file = optarg; break; + case 'F': + { + char *save = NULL; + char *key = strtok_r(optarg, "=", &save); + char *value = strtok_r(save, "=", &save); + + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, key, + CURLFORM_PTRCONTENTS, value, + CURLFORM_END); + } + break; + case 'a': options.apikeyfile = optarg; break; case 'h': diff --git a/fb.1 b/fb.1 index 4a07538..918ae84 100644 --- a/fb.1 +++ b/fb.1 @@ -83,6 +83,12 @@ terminal. You can no longer upload files in this mode. If the argument is a URL, .Nm will try to extract the ID. +.It Fl m +Create a multipaste of the IDs/files/directories/URLs. This uploads files as +always, but then creates a multipaste combining all of them. URLs starting with +the pastebin URL will have their ID extracted and will not be downloaded. Only +the multipaste URL will be copied to the clipboard, but all URLs will be +displayed while uploading. .It Fl h Display a short help message. .It Fl t diff --git a/fb.in b/fb.in index 3aefba8..930ce1c 100644 --- a/fb.in +++ b/fb.in @@ -44,6 +44,8 @@ delete= extension="" filename="stdin" get= +multipaste= +multipaste_ids=() tar= compress=0 display_history= @@ -67,13 +69,14 @@ base64_encode() { } request_helper() { + # available modes are: d, u, m mode=$1 url=$2 file=$3 # if available use the external helper, else fall back to calling curl if [ -x "$libdir/fb-helper" ]; then - helperopts=() + helperopts=(-u "$url") if [ "$debug" ]; then helperopts+=(-D) @@ -83,13 +86,16 @@ request_helper() { helperopts+=(-a $apikey_file) fi - if [ "$mode" = "d" ]; then - $libdir/fb-helper "${helperopts[@]}" -u "$url" + if [ "$mode" = "u" ]; then + helperopts+=(-f "$file") fi - if [ "$mode" = "u" ]; then - $libdir/fb-helper "${helperopts[@]}" -u "$url" -f "$file" + if [ "$mode" = "m" ]; then + for id in "${multipaste_ids[@]}"; do + helperopts+=(-F "ids[]=$id") + done fi + $libdir/fb-helper "${helperopts[@]}" else curlopts=(${default_curlopts[@]}) @@ -105,6 +111,12 @@ request_helper() { curlopts+=("-v") fi + if [ "$mode" = "m" ]; then + for id in "${multipaste_ids[@]}"; do + curlopts+=(-F "ids[]=$id") + done + fi + if [ "$mode" = "d" ]; then curlopts+=("-s") fi @@ -141,6 +153,14 @@ is_url() { return 1 } +is_pastebin_url() { + if printf "%s" "$i" | grep -qE "^$pastebin.+"; then + return 0 + fi + + return 1 +} + do_tar_upload() { if [ "$compress" = "1" ]; then file="$tmpdir/$filename.tar.gz" @@ -284,6 +304,7 @@ usage: [cat |] `basename "$0"` [switches] [options] [] Switches: -d delete the IDs -g download the IDs and output on stdout (use with care!) + -m create a multipaste -h this help -v show the client version -H display an upload history @@ -305,11 +326,12 @@ if ! type getopts >/dev/null 2>&1; then exit 1 fi -while getopts "e:n:gdhHatcvD" option; do +while getopts "e:n:gmdhHatcvD" option; do case $option in e) extension="$OPTARG";; n) filename="$OPTARG";; g) get=1;; + m) multipaste=1;; c) compress=`expr $compress + 1`;; t) tar=1;; d) delete=1;; @@ -382,6 +404,13 @@ else else for i in "$@"; do if is_url "$i"; then + if [ "$multipaste" ]; then + if is_pastebin_url "$i"; then + multipaste_ids+=("$(id_from_arg "$i")") + continue + fi + fi + cd $tmpdir if ! request_helper d "$i" > "`basename "$i"`"; then exitcode=1 @@ -398,6 +427,23 @@ else fi done fi + + if [ "$multipaste" ]; then + for url in $clipboard; do + id="$(id_from_arg "$url")" + multipaste_ids+=("$id") + done + + tmpfile=`mktemp "$tmpdir/data.XXXXXX"` + request_helper m "$pastebin/file/do_multipaste" > $tmpfile || exitcode=1 + + sed '$d' $tmpfile >&2 + url=`tail -1 $tmpfile`"$extension" + printf "%s\n" "$url" + if printf "%s" "$url" | grep -qE "^https?://"; then + clipboard="$url" + fi + fi fi if [ "$clipboard" != "" ]; then -- cgit v1.2.3-24-g4f1b