1 ### vim:ft=zsh:foldmethod=marker
2 ## RFC backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
5 # TODO: This is currently just a real lookup. rfc-editor.com supports searching.
8 LOOKUP_guard || return 1
9 [[ -n ${lookup_describe} ]] &&
10 printf '%s' 'RFC lookup' &&
16 lookup_context="$(LOOKUP_context)"
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'
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}
56 LOOKUP_help && return 0
58 if [[ -n ${lookup_complete} ]] ; then
60 '-d[attempt do download RFCs]:'
61 '-w[never attempt do download RFCs]:'
65 _arguments -s -w -A '-*' ${comp_args} && return 0
70 local dir file save_path search_path yesno
75 zstyle -s "${lookup_context}" save-path save_path || save_path=${HOME}
76 zstyle -a "${lookup_context}" search-path search_path || search_path=(${HOME})
78 if zstyle -t "${lookup_context}" download ; then
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
90 LOOKUP_query_handler || return 1
91 if [[ -z ${QUERY} ]] ; then
96 for dir in ${search_path} ; do
97 file="${dir}/rfc${QUERY}.txt"
98 if [[ -r ${file} ]] ; then
99 LOOKUP_browser ${file}
101 elif [[ -r ${file}.gz ]] ; then
102 LOOKUP_context -l local-files
103 LOOKUP_browser ${file}.gz
105 elif [[ -r ${file}.bz2 ]] ; then
106 LOOKUP_context -l local-files
107 LOOKUP_browser ${file}.bz2
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'
119 printf 'RFC: %s, save-path: %s\n' ${QUERY} ${save_path}
120 printf 'Shall I download the rfc in question? [y/n] '
122 if [[ ${yesno} == 'y' ]] ; then
123 LOOKUP_context -l local-files
124 file="${save_path}/rfc${QUERY}.txt"
126 zftp open ftp.rfc-editor.org "anonymous" "zsh_lookup@somewhere.tld"
129 zftp get "rfc${QUERY}.txt" > ${file}
132 LOOKUP_browser ${file}
137 LOOKUP_browser "ftp://ftp.rfc-editor.org/in-notes/rfc${QUERY}.txt"