Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_rfc
1 ### vim:ft=zsh:foldmethod=marker
2 ## RFC backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 # TODO: This is currently just a real lookup. rfc-editor.com supports searching.
6 #       ...would be nice...
7
8 LOOKUP_guard || return 1
9 [[ -n ${lookup_describe} ]] &&
10     printf '%s' 'RFC lookup' &&
11     return 0
12
13 local lookup_context
14 local -a comp_args
15
16 lookup_context="$(LOOKUP_context)"
17
18 LOOKUP_guard -fd LOOKUP_help_${backend} ||
19 function LOOKUP_help_${backend}() {
20     LOOKUP_guard || return 1
21            #-80characters-----------------------------------------------------------------#
22     printf 'usage: %s [-{d,w}] <RFC number>\n' ${backend}
23     printf '  -d    ask to download RFC before reading\n'
24     printf '  -w    just watch. do not ask to download\n'
25     printf '\n Read RFCs by number.\n'
26     printf ' Supports local files and remote files (via rfc-editor.com).\n'
27     printf '\n First, it looks for a local version of the RFC (within a given path, see\n'
28     printf ' Styles below); if a local version is found, it is opened in a browser. If it\n'
29     printf ' cannot be found (per default) the user is asked if we should try to download\n'
30     printf ' the RFC in question via FTP from rfc-editor.com (only works if the zsh/zftp\n'
31     printf ' module can be loaded). If the answer is yes, the file is downloaded. If that\n'
32     printf ' was successful the *local* file is viewed. If the answer is no, the remote\n'
33     printf ' URL is opened in a browser.\n'
34     printf '\nStyles:\n'
35     printf ' The context in which styles are looked up is:\n'
36     printf '        %s\n' ${lookup_context}
37     LOOKUP_context -l local-files
38     lookup_context="$(LOOKUP_context)"
39     printf ' If a local file is viewed, that changes to:\n'
40     printf '        %s\n\n' ${lookup_context}
41     printf ' search-path: a list style, defining where to look for local RFCs\n'
42     printf '              (Default: one entry: $HOME - "%s")\n' ${HOME}
43     printf '   save-path: when downloading RFCs, save them to this directory\n'
44     printf '              (Default: $HOME - "%s")\n' ${HOME}
45     printf '    download: boolean; changes the default downloading behaviour\n'
46     printf '              (Default: true: always ask to download non-local RFCs)\n'
47     printf '\n When looking for local RFCs, this backend looks for files named like this:\n'
48     printf '    rfc<->.txt(|.gz|.bz2) (eg: rfc123.txt, rfc234.txt.gz or rfc345.txt.bz2)\n'
49     printf '\nExamples:\n'
50     printf ' %% zstyle '\'':lookup:*:%s:*'\'' search-path /usr/share/doc/RFC/links ~/myrfcs\n' ${backend}
51     printf ' %% zstyle '\'':lookup:*:%s:*'\'' save-path   ~/myrfcs\n' ${backend}
52     printf ' %% zstyle '\'':lookup:*:%s:*'\'' download    false\n' ${backend}
53     printf ' %% lookup %s 881\n' ${backend}
54     printf ' %% lookup %s -w 881\n\n' ${backend}
55 }
56 LOOKUP_help && return 0
57
58 if [[ -n ${lookup_complete} ]] ; then
59     comp_args=(
60         '-d[attempt do download RFCs]:'
61         '-w[never attempt do download RFCs]:'
62         '*:RFC number:true'
63     )
64
65     _arguments -s -w -A '-*' ${comp_args} && return 0
66     _message 'RFC number'
67     return 0
68 fi
69
70 local dir file save_path search_path yesno
71 local -i justwatch
72 local -A opts
73 local -x QUERY
74
75 zstyle -s "${lookup_context}" save-path   save_path   || save_path=${HOME}
76 zstyle -a "${lookup_context}" search-path search_path || search_path=(${HOME})
77
78 if zstyle -t "${lookup_context}" download ; then
79     justwatch='0'
80 else
81     justwatch='1'
82 fi
83
84 lu_parseopts_args=( d bool w bool )
85 LOOKUP_parseopts "$@" || return 1
86 [[ ${opts[-d]} == 'yes' ]] && justwatch=0
87 [[ ${opts[-w]} == 'yes' ]] && justwatch=1
88
89 QUERY="${args[*]}"
90 LOOKUP_query_handler || return 1
91 if [[ -z ${QUERY} ]] ; then
92     LOOKUP_help -f
93     return 1
94 fi
95
96 for dir in ${search_path} ; do
97     file="${dir}/rfc${QUERY}.txt"
98     if [[ -r ${file} ]] ; then
99         LOOKUP_browser ${file}
100         return $?
101     elif [[ -r ${file}.gz ]]  ; then
102         LOOKUP_context -l local-files
103         LOOKUP_browser ${file}.gz
104         return $?
105     elif [[ -r ${file}.bz2 ]] ; then
106         LOOKUP_context -l local-files
107         LOOKUP_browser ${file}.bz2
108         return $?
109     fi
110 done
111
112 if (( lookup_printout == 0 )) && (( justwatch == 0 )) && [[ -n ${save_path} ]] ; then
113     if [[ -z ${builtins[zftp]} ]] ; then
114         if ! zmodload zsh/zftp ; then
115             printf 'Module zsh/zftp could not be loaded, cannot download.\n'
116             return 1
117         fi
118     fi
119     printf 'RFC: %s, save-path: %s\n' ${QUERY} ${save_path}
120     printf 'Shall I download the rfc in question? [y/n] '
121     read -q yesno
122     if [[ ${yesno} == 'y' ]] ; then
123         LOOKUP_context -l local-files
124         file="${save_path}/rfc${QUERY}.txt"
125
126         zftp open ftp.rfc-editor.org "anonymous" "zsh_lookup@somewhere.tld"
127         zftp cd "/in-notes"
128         zftp binary
129         zftp get "rfc${QUERY}.txt" > ${file}
130         quit
131
132         LOOKUP_browser ${file}
133         return $?
134     fi
135 fi
136
137 LOOKUP_browser "ftp://ftp.rfc-editor.org/in-notes/rfc${QUERY}.txt"
138 return $?