#!/bin/bash # # This source code 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 2 of the License, # or (at your option) any later version. # # (c) Copyright 2006 AlpT (@freaknet.org) # To add your actions see what's already here and copy the style ;) # # The `CB_ACTION' array is scanned by `]', while `CB_ACTION_II' by `][' # Note that the order of each CB_ACTION[] is relevant. # # If your function deals with filename, remember to use "$@" and not simply $@. # Doing so you will support filenames with spaces too. # # That's all, have fun ################### CONFIGURE HERE ################# SHELL_LIST_FILE="$CLOSEBRACKET_DIR/shells" FILE_EXTENSIONS_LIST="$CLOSEBRACKET_DIR/file_extensions" CB_EDITOR='vim' CB_BROWSER='firefox' CB_HTTP_DOWNLOADER='wget' CB_TEXT_BROWSER='links' CB_IMAGE_VIEWER='gqview' CB_IMAGE_TTY_VIEWER='seejpeg' CB_IMAGE_EDITOR='gimp' CB_MEDIA_PLAYER='mplayer' CB_CALC='bc -l' CB_MAIL_AGENT='mutt' CB_REMOTE_COPY='scp' CB_REMOTE_COPY_DIR='scp -r' CB_REMOTE_COPY_PREFIX='scp://' #CB_REMOTE_COPY='rsync' #CB_REMOTE_COPY_DIR='rsync -a' #CB_REMOTE_COPY_PREFIX='rsync://' CB_BITTORRENT='bittorrent' CB_BITTORRENT_TTY='bittorrent-curses' CB_PDF_VIEWER='xpdf_hack' # see xpdf_hack() below CB_SHELL_GREP="$CLOSEBRACKET_DIR/scripts/shell" CB_UNTARRA="$CLOSEBRACKET_DIR/scripts/untarra" ################### CONFIGURE END ################# CB_OK=79 CB_SKIP=0 cbi=0 ## ### Filters based on regular expressions ## ((cbi++)) CB_ACTION[$cbi]="cb_http_browser" # Open an URI with the browser CB_ACTION_II[$cbi]="cb_http_wget" # Download the URI with wget ((cbi++)) CB_ACTION[$cbi]="cb_remote_edit" # Open with vim a remote file CB_ACTION_II[$cbi]="cb_remote_copy" # scp ((cbi++)) CB_ACTION[$cbi]="cb_file_colon_line" # ] file:line ==> vim file +line CB_ACTION_II[$cbi]="cb_donothing" # Open mutt to send a new mail when "$1" is a mail address ((cbi++)) CB_ACTION[$cbi]="cb_mail_agent" CB_ACTION_II[$cbi]="cb_donothing" #((cbi++)) #CB_ACTION[$cbi]="cb_calc" # Use bc -l: ] 3+3.14*(21^2) #CB_ACTION_II[$cbi]="cb_donothing" ## ### Filters based on file type ## # File extensions match ((cbi++)) CB_ACTION[$cbi]="cb_file_exts" CB_ACTION_II[$cbi]="cb_file_exts_II" ## ### General filters ## ## When we have no argument ((cbi++)) CB_ACTION[$cbi]="cb_list_dir" # do `ls' when "$1" is null CB_ACTION_II[$cbi]="cb_cdhome" # go $HOME/ ## When the first argument is a directory ((cbi++)) CB_ACTION[$cbi]="cb_cdcd" # change dir if "$1" is a dir CB_ACTION_II[$cbi]="cb_lsdir" # ls dir/ # Vim ((cbi++)) CB_ACTION[$cbi]="cb_vim" # vim file CB_ACTION_II[$cbi]="cb_cat" # cat file # If the file exists but it hasn't any associated action, just do an ls of it ((cbi++)) CB_ACTION[$cbi]="cb_unknown_file" # ls file CB_ACTION_II[$cbi]="cb_donothing" # non existent file ((cbi++)) CB_ACTION[$cbi]="cb_donothing" CB_ACTION_II[$cbi]="cb_notexistent" # vim new_file ((cbi++)) CB_ACTION[$cbi]="cb_shell" # shell $1 CB_ACTION_II[$cbi]="cb_donothing" ############ FUNCTIONS ############### cb_list_dir() { if [ -z "$1" ] then eval ls return $CB_OK fi } cb_cdhome() { if [ -z "$1" ] then eval cd $HOME return $CB_OK fi } cb_cdcd() { if [ -d "$1" ] then eval 'cd "$1"' return $CB_OK fi } cb_lsdir() { if [ -d "$1" ] then eval 'ls "$@"' return $CB_OK fi } __cb_match_extension() { local fname fname="$1" shift for i in "$@"; do i="`echo $i | sed -e 's#\.#\\\\.#'`" res=`expr match "$fname" ".*$i$"` if [ $res -ne 0 ]; then return 0 fi done return 1 } cb_file_exts() { local idx i idx=1 if [ -z "$1" ]; then return; fi while [ ! -z "${CB_EXT_PROG[$idx]}" ] do if __cb_match_extension "$1" ${CB_EXT_EXTS[$idx]} then ${CB_EXT_PROG[$idx]} "$@" return $CB_OK fi ((idx++)) done } cb_file_exts_II() { local idx i idx=1 if [ -z "$1" ]; then return; fi while [ ! -z "${CB_EXT_PROG[$idx]}" ] do if __cb_match_extension "$1" ${CB_EXT_EXTS[$idx]} then ${CB_EXT_PROG_II[$idx]} "$@" return $CB_OK fi ((idx++)) done } cb_tarra() { if [ -z "$1" ]; then return; fi # TODO return $CB_SKIP } cb_untarra() { local base tf if [ -z "$1" ]; then return; fi echo untarra... tf=$(tempfile) $CB_UNTARRA "$@" > $tf base=`cat $tf | line` rm $tf if [ -d "$base" ] then echo cd $base cd $base fi return $CB_OK } cb_vim() { if [ -z "$1" ]; then return; fi if [ -f "$1" ] then $CB_EDITOR "$@" return $CB_OK fi } cb_cat() { if [ -z "$1" ]; then return; fi if [ -f "$1" ] then cat "$@" return $CB_OK fi } cb_htm() { if [ -z "$1" ]; then return; fi if [ -z "$DISPLAY" ] then $CB_TEXT_BROWSER "$@" else echo ] $CB_BROWSER... $CB_BROWSER "$@" fi return $CB_OK } cb_img_viewer() { if [ -z "$1" ]; then return; fi if [ -z "$DISPLAY" ] then echo ] $CB_IMAGE_TTY_VIEWER... $CB_IMAGE_TTY_VIEWER "$@" else echo ] $CB_IMAGE_VIEWER... $CB_IMAGE_VIEWER "$@" fi return $CB_OK } cb_img_editor() { if [ -z "$1" ]; then return; fi if [ -z "$DISPLAY" ] then echo ] $CB_IMAGE_TTY_VIEWER... $CB_IMAGE_TTY_VIEWER "$@" else echo ] $CB_IMAGE_EDITOR... $CB_IMAGE_EDITOR "$@" fi return $CB_OK } cb_bittorrent() { if [ -z "$1" ]; then return; fi if [ -z "$DISPLAY" ] then echo ] $CB_BITTORRENT_TTY... $CB_BITTORRENT_TTY "$@" else echo ] $CB_BITTORRENT... $CB_BITTORRENT "$@" fi return $CB_OK } cb_pdf() { local tmp if [ -z "$1" ]; then return; fi if [ -z "$DISPLAY" ] then echo "] $1 -> $tmp.html" tmp=$(tempfile) pdftohtml "$1" -stdout > $tmp.html if [ $? -eq 0 ]; then $CB_TEXT_BROWSER $tmp.html rm $tmp.html elif [ $? -eq 127 ]; then echo "Please install pdftohtml. It's a nice utility" else echo "pdftohtml "$1": failed" fi else echo ] $CB_PDF_VIEWER... $CB_PDF_VIEWER "$@" fi return $CB_OK } cb_mail_agent() { if [ -z "$1" ]; then return; fi echo "$1" | perl -e \ 'if(<>=~/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) { exit 0; } exit 1;' if [ $? -eq 0 ] then $CB_MAIL_AGENT "$@" return $CB_OK fi } cb_shell() { if [ -z "$1" ]; then return; fi $CB_SHELL_GREP "$@" if [ $? -eq 0 ] then return $CB_OK fi } cb_unknown_file() { if [ -e "$1" ] then ls "$@" return $CB_OK fi } cb_donothing() { return $CB_SKIP } cb_notexistent() { local cbf; if [ -z "$1" ]; then return; fi cbf="`echo $1`" if [ ! -e "$cbf" ] then $CB_EDITOR "$@" return $CB_OK fi } cb_http_browser() { if [ -z "$1" ]; then return; fi case $1 in http://*|https://*|ftp://*) if [ -z "$DISPLAY" ] then $CB_TEXT_BROWSER "$@" else echo $CB_BROWSER... $CB_BROWSER "$@" fi return $CB_OK esac } cb_http_wget() { if [ -z "$1" ]; then return; fi case $1 in http://*|https://*|ftp://*) $CB_HTTP_DOWNLOADER "$@" return $CB_OK;; esac } cb_remote_edit() { local host rpath user local uno uno="$1" if [ -z "$1" ]; then return; fi case $1 in scp://*|rsync://*) $CB_EDITOR "$@" return $CB_OK;; esac case $uno in *:*) case $uno in *@*) user=$(echo "$uno" | cut -d '@' -f 1) host=$(echo "$uno" | cut -d '@' -f 2 | cut -d ':' -f 1) rpath=$(echo "$uno" | cut -d '@' -f 2 | cut -d ':' -f 2-) ;; *) host=$(echo "$uno" | cut -d ':' -f 1) rpath=$(echo "$uno" | cut -d ':' -f 2-) ;; esac if grep -q $host $SHELL_LIST_FILE; then host=$(grep $host $SHELL_LIST_FILE | line) if [ ! -z $user ]; then user="$user@"; fi $CB_EDITOR scp://$user$host/$rpath return $CB_OK fi;; esac } cb_remote_copy() { local host rpath user lpath last_arg remotelocal if [ -z "$1" ]; then return; fi remote_copy_parse_uri() { case $1 in *:*) case $1 in *@*) user=$(echo "$1" | cut -d '@' -f 1) host=$(echo "$1" | cut -d '@' -f 2- | cut -d ':' -f 1) rpath=$(echo "$1" | cut -d '@' -f 2- | cut -d ':' -f 2-) ;; *) host=$(echo "$1" | cut -d ':' -f 1) rpath=$(echo "$1" | cut -d ':' -f 2-) ;; esac;; esac } if (( $# == 1 )) then case $1 in $CB_REMOTE_COPY_PREFIX) rpath=$(echo "$1" | cut -d '/' -f 3-) host=$(echo $rpath | cut -d '/' -f 1) rpath=$(echo $rpath | cut -d '/' -f 2-) $CB_REMOTE_COPY_DIR $host:$rpath . return $CB_OK;; esac case $1 in *:*) remote_copy_parse_uri "$1" lpath="." remotelocal="from_remote";; esac else case $1 in *:*) remote_copy_parse_uri "$1" shift while (($#)); do lpath="$lpath \"$1\""; shift; done remotelocal="from_remote";; *) while (($#)); do if [ ! -z "$last_arg" ]; then lpath="$lpath \"$last_arg\"" fi last_arg="$1" shift done remote_copy_parse_uri $last_arg remotelocal="from_local";; esac fi if [ ! -z "$host" -a ! -z "$lpath" ]; then if grep -q $host $SHELL_LIST_FILE; then host=$(grep $host $SHELL_LIST_FILE | line) if [ ! -z $user ]; then user="$user@"; fi if [ "$remotelocal" = "from_remote" ]; then eval $CB_REMOTE_COPY_DIR $user$host:$rpath $lpath else eval $CB_REMOTE_COPY_DIR $lpath $user$host:$rpath fi return $CB_OK fi fi } cb_file_colon_line() { local f l if [ -z "$1" ]; then return; fi local IFS=: set -- $1 f=$1 l=$2 if [ ! -f "$f" ] || [ -z "$l" ] || echo $l | grep '[^0-9]'; then return; fi vim $f +$l return $CB_OK } cb_calc() { if [ -z "$1" ]; then return; fi echo "$@" | perl -e \ 'if(<>=~/^[0-9]+[\.]*[0-9]*\s*[\+\-\^\*%]\s*[0-9]+[\.]*[0-9]*/) { exit 0; } exit 1;' if [ $? -eq 0 ] then echo $@ | $CB_CALC return $CB_OK fi } xpdf_hack() { #xpdf doesn't allow multiple file arguments. for i in "$@" do xpdf "$i"& done }