#!/bin/sh # # sh-tool for using mldonkey # # Copyright (c) 2003,2004 Mathias Gumz # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # globals # NETCAT=/usr/bin/nc WGET=/usr/bin/wget PREVIEW=mplayer PREFIX=$HOME/.mldonkey D_HOST=127.0.0.1 D_PORT=4000 D_USER=foo D_PASS=bar RC_FILE=$HOME/.donkeyshrc ACTION="help" SORT="percentage" REVERSE="" # # functions # display_version() { cat << EOF donkeysh - 0.3 2003 (c) by m.gumz 030728 EOF } display_help() { cat << EOF NAME donkeysh - small and handy shelltool for mldonkey SYNOPSIS donkeysh [-hHv] [-f rcfile] [-s mode] [-r] [-a action] [nr] DESCRIPTION donkeysh is a small and hopefully handy script to handle and control the mldonkey-core. it was written to avoid the graphical userinterfaces and provide a simple tool to do a lot of stuff via the console. mldonkey version >= 2.5.11 OPTIONS -h display help -v display version -a action h | help display help c | command submit a command ms | ministats show ministats s | stats show stats (with a number of that download) i | info displays some detailed info to a file co | connect connect to the mldonkey-console r | resume resume a download p | pause pauses a download | cancel cancel a download d | download start to download a ed2k-link pr | preview preview file with \$PREVIEW srm | sharereactor multiple selection srs | sharereactor single file ln | listnew lists all files in \$PREFIX/incoming la | listall lists all files in \$PREFIX -s mode sort of the output p | percantage default n | name nr | number s | size sp | speed d | dur duration -r reverse order -f rcfile use of rcfile for settings EOF } display_addhelp() { cat << EOF FILES \$HOME/.donkeyshrc CONFIGSYNTAX host = mldonkey-host port = port user = user pass = password netcat = netcat-binary wget = wget-binary preview = videoplayer, default is mplayer prefix = path to the files EXAMPLES $> donkeysh -s n -a stats display named orderd stats $> donkeysh -a info 24 display info about file 24 $> donkeysh -f myconfig -a d ed2k://blafoo download blafoo with settings in myconfig TIPS usefull aliases (bash-syntax) alias donkeyms='donkeysh -a ms -r' -> ministats alias donkeys='donkeysh -a s -r' -> normalstats alias donkeyd='donkeysh -a d' -> download BUGS not yet known AUTHOR mathias gumz EOF } read_rcfile() { if [ -f "$RC_FILE" ] then t=`mktemp` cat "$RC_FILE" | sed '/^[[:space:]]*$/d' | sed '/^[#!]/d' > $t while read l do local opt=$(echo $l | awk '{print $1;};') local val=$(echo $l | awk '{print $3;};') case "$opt" in host ) D_HOST=$val;; port ) D_PORT=$val;; user ) D_USER=$val;; pass ) D_PASS=$val;; preview ) PREVIEW=$val;; prefix ) PREFIX=$val;; netcat ) NETCAT=$val;; wget ) WGET=$val;; esac done < $t rm $t fi } send_command() { ( [ -n "$D_USER" ] && echo "auth $D_USER $D_PASS" echo "ansi false" echo "$@" echo "q" ) | \ $NETCAT $D_HOST $D_PORT | \ sed '1,9d;$d' } filter_network_tag() { sed -n '/^\[[[:alpha:]]\+/p' | \ sed -e ' s/^\[[[:alpha:]]/ / s/\]/ / s/|/@/g ' } insert_bars() { sed -e ' s/\(.\{7\}\).\(.*\)/\1| \2/ # 1 number s/\(.\{60\}\).\(.*\)/\1| \2/ # 2 name s/\(.\{67\}\)..\(.*\)/\1| \2/ # 3 percentage s/\(.\{77\}\).\(.*\)/\1| \2/ # 4 done s/\(.\{87\}\).\(.*\)/\1| \2/ # 5 size s/\(.\{98\}\).\(.*\)/\1 | \2/ # 6 left s/\(.\{104\}\).\(.*\)/\1 |\2/ # 7 old s/\(.\{112\}\).\(.*\)/\1 |\2/ # 8 active s/\(.\{120\}\).\(.*\)/\1|\2/ # 9 rate # 10 priority ' } # insert some bars in the normal output # # $1 - f -> fullmode # m -> minimode display_fields() { case "$1" in full ) cut -d '|' -f 1,2,3,4,5,6,7,8,9,10 ;; mini ) cut -d '|' -f 1,2,3,9 ;; esac } # # $1 - sort order # $2 - m -> mini # f -> full sort_output() { case "$1" in n | name ) sort -t '|' -k 2 $2;; p | percentage ) sort -t '|' -k 3 -n $2;; sp | speed ) sort -t '|' -k 9 -n $2;; nr | number ) sort -t '|' -k 1 -n $2;; s | size ) sort -t '|' -n -k 5 $2;; d | dur ) sort -t '|' -n -k 7 $2;; esac } # # real mainloo # read_rcfile while getopts ":hHrs:a:f:" opts do case "$opts" in h ) display_help; exit 0;; H ) display_help; display_addhelp; exit 0;; v ) display_version; exit 0;; a ) ACTION="$OPTARG";; f ) RC_FILE="$OPTARG"; read_rcfile;; s ) SORT="$OPTARG";; r ) REVERSE="-r";; esac done shift $(($OPTIND - 1)) case "$ACTION" in h | help ) display_help; exit 0;; s | stats ) send_command "vd" | \ filter_network_tag | insert_bars | sort_output $SORT $REVERSE | \ display_fields "full";; raw ) send_command "vd" | filter_network_tag | insert_bars;; ms | ministats ) send_command "vd" | \ filter_network_tag | insert_bars | sort_output $SORT $REVERSE | \ display_fields "mini" ;; monitor ) watch -d "$0" -a ms -r -s p ;; i | info ) if [ -z "$1" ] then echo "error: number is essential" exit 1 fi tmp=`mktemp` send_command "vd" | \ sed -n "/^\[[[:alpha:]]\+ \<$1\>/p" | \ filter_network_tag | insert_bars | display_fields "mini" send_command "vd $1" > $tmp cat $tmp | sed -n '/^\[Donkey/p' | \ sed -e 's/^.\{1,15\}/ TmpName /' cat $tmp | sed -n '/^Chunks/p' | \ sed -e 's/^Chunks/ Progress/' rm $tmp ;; co | connect ) $NETCAT $D_HOST $D_PORT;; cancel ) if [ -z "$1" ]; then echo "error: number is essential."; exit 1; fi echo -n "realy cancel $1 [yes]/no " read a if [ -z "$a" -o "$a" == "yes" ]; then send_command "cancel $1" fi ;; p | pause ) if [ -z "$1" ]; then echo "error: number is essential."; exit 1; fi; send_command "pause $1" ;; r | resume ) if [ -z "$1" ]; then echo "error: number is essential."; exit 1; fi; send_command "resume $1" ;; d | download ) if [ -z "$1" ]; then echo "error: number is essential."; exit 1; fi; for link in "$@"; do send_command "dllink $link" done ;; prev | preview ) if [ -z "$1" ] then echo "error: number is essential." exit 1 fi for p in $@ do f=`send_command "vd $p" | \ sed -n '/^Chunks/,+1p' | \ sed 1d | \ sed -e ' s/^.*(// s/).*// ' ` if [ -n "$f" -a -e "$PREFIX/tmp/$f" -a -n $PREVIEW ]; then $PREVIEW $PREFIX/tmp/$f fi done ;; c | cmd ) if [ -z "$1" ]; then echo "error: number is essential."; fi send_command "$@";; t ) send_command "vd" ;; srm ) if [ -n "$1" ]; then wget -q -O - "$1" | \ grep "ed2k://" | \ sed -e 's/^.*=//g;s/..$//g' > tmp.$$ && \ while read line do echo -n " >> $line :" send_command "dllink $line" done < tmp.$$ && \ rm tmp.$$ else echo "mising link" fi ;; srs ) if [ -n "$1" ]; then ed2k=`wget -q -O - "$1" | grep "ed2k://" | sed -e 's/^[[:space:]]*> $ed2k :" send_command "dllink $ed2k" else echo "mising link" fi ;; listnew | ln ) ls -1sh $PREFIX/incoming ;; listall | la ) ls -R $PREFIX/ ;; esac