Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / Backends / LOOKUP_be_gmane
1 ### vim:ft=zsh:foldmethod=marker
2 ## gmane.org backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
4
5 LOOKUP_guard || return 1
6 [[ -n ${lookup_describe} ]] && printf '%s' 'query gmane.org' && return 0
7
8 local lookup_context bookmarks
9 local -A known_sorts known_opers
10 lookup_context="$(LOOKUP_context)"
11
12 # Set up the path name of the bookmark file. Looked up in the :lookup:*
13 # context because it may not be limited to compsys in the future
14 # (maybe someone will write bookmark managing via zsh/curses...). :-)
15 zstyle -s "${lookup_context}" group-bookmarks bookmarks ||
16     bookmarks="${HOME}/.zgmane.bookmarks"
17
18 known_sorts=(
19     date        'sort by date'
20     revdate     'sort by reverse date'
21     relevance   'sort by relevance'
22 )
23
24 known_opers=(
25     and     'require all words'
26     or      'require one word'
27 )
28
29 LOOKUP_guard -fd LOOKUP_help_${backend} ||
30 function LOOKUP_help_${backend}() {
31     # gmane backend help {{{
32     LOOKUP_guard || return 1
33     printf 'usage: %s <query>\n' ${backend}
34     printf '  -l        search for original list names (like zsh-workers)\n'
35     printf '  -s        match gmane group names (like comp.shell)\n'
36     printf '  -L        list-mode; do not open a browser, just print the results\n'
37     printf '            valid with -l and -s.\n'
38     printf '  -B        browser-mode; opposite of -L\n'
39     printf '  -m        search for Message-Id: header\n'
40     printf '  -a <arg>  match messages only by a certain author\n'
41     printf '  -g <arg>  match messages only from a certain gmane group\n'
42     printf '  -o <arg>  use defined operation in search (%s)\n' "${(j:/:)${(@kon)known_opers}}"
43     printf '  -S <arg>  use given sorting method (%s)\n' "${(j:/:)${(@kon)known_sorts}}"
44     printf '\n Search the archives of the gmane.org mail2news gateway.\n'
45     printf '\n Default sort method: relevance\n'
46     printf ' Default query logic: and\n'
47     printf ' List-mode (-L and -B options) defaults to: off\n'
48     printf ' Default group bookmark file: %s\n' "${HOME}/.zgmane.bookmarks"
49     printf ' All defaults may be altered by the use of styles.\n'
50     printf '\nStyles:\n'
51     printf ' All styles are looked up in this context:\n'
52     printf '    %s\n\n' ${lookup_context}
53     printf ' group-bookmarks:   file that contains group bookmarks (see below)\n'
54     printf '    list-default:   boolean, use list-mode by default if true\n'
55     printf '    default-sort:   change default sorting method\n'
56     printf '    default-oper:   change default query logic\n'
57     printf '\nBookmarks:\n'
58     printf '\n Often you will want to limit your queries to a certain gmane group.\n'
59     printf ' Since these are lengthy at times, it is awful to type them out.\n'
60     printf ' Therefore, this backend gives you a bookmark file. Its format is:\n'
61     printf '    gname.group.name:description\n'
62     printf ' The '\'':description'\'' part is optional.\n'
63     printf ' That way you can search for group names (using -L, -l and -s) and put\n'
64     printf ' your prefered groups into that file. That way you get rid of a lot of\n'
65     printf ' typing by using completion: %% lu gmane -g <tab>\n'
66     printf '\nExamples:\n'
67     printf ' %% zstyle '\'':lookup:*:%s:*'\'' list-default    true\n' ${backend}
68     printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-sort    date\n' ${backend}
69     printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-oper    or\n' ${backend}
70     printf ' %% zstyle '\'':lookup:*:%s:*'\'' group-bookmarks ~/.my_gmane_bookmarks\n' ${backend}
71     printf ' %% lookup %s zsh\n' ${backend}
72     printf ' %% lookup %s -m 20090215095848.GA21661@schmehl.info\n' ${backend}
73     printf ' %% lookup %s -g gmane.comp.shells.fish.user zsh\n' ${backend}
74     printf ' %% lookup %s -L -l zsh-workers\n' ${backend}
75     printf ' %% lookup %s -L -s openbsd\n' ${backend}
76     #}}}
77 }
78 LOOKUP_help && return 0
79
80 if [[ -n ${lookup_complete} ]] ; then
81     # gmane backend completion code {{{
82     local -a comp_args
83
84     LOOKUP_guard -fd __lookup_${backend}_known_operations ||
85     function __lookup_${backend}_known_operations() {
86         local o
87         local -a os
88
89         os=()
90         for o in ${(k)known_opers}; do
91             os+=("$o:${known_opers[$o]}")
92         done
93         _describe -t gmane_opers 'known operations' os
94     }
95
96     LOOKUP_guard -fd __lookup_${backend}_known_sorts ||
97     function __lookup_${backend}_known_sorts() {
98         local s
99         local -a as
100
101         as=()
102         for s in ${(k)known_sorts}; do
103             as+=("$s:${known_sorts[$s]}")
104         done
105         _describe -t gmane_sorts 'known sorting methods' as
106     }
107
108     LOOKUP_guard -fd __lookup_${backend}_group_bookmarks ||
109     function __lookup_${backend}_group_bookmarks() {
110         local -a bms
111
112         if [[ -r ${bookmarks} ]] ; then
113             bms=(${(f)"$(< $bookmarks)"})
114         fi
115         _describe -t gmane_bookmarks 'bookmarked gmane groups' bms
116     }
117
118     comp_args=(
119         '-l[search for original list names]:list name:'
120         '-s[match gmane group names]:group name:'
121         '-L[switch to list-mode]'
122         '-m[query for message ids]:Message-Id\::'
123         '-a[author name]:author:'
124         '-g[group name]:group:__lookup_'${backend}'_group_bookmarks'
125         '-o[operations]:operation:__lookup_'${backend}'_known_operations'
126         '-S[sorting method]:sorting method:__lookup_'${backend}'_known_sorts'
127         '*:wikipedia search:true'
128     )
129
130     _arguments -s -w -A '-*' ${comp_args} && return 0
131     _message 'gmane query'
132     return 0
133     #}}}
134 fi
135
136 local author group operation sort ret list_mode
137 local -x QUERY
138
139 # set some defaults via zstyle
140 zstyle -t "${lookup_context}" list-default              && list_mode='yes'
141 zstyle -s "${lookup_context}" default-sort sort         || sort='relevance'
142 zstyle -s "${lookup_context}" default-oper operation    || operation='and'
143
144 # parse options
145 lu_parseopts_args=(
146     l   bool
147     m   bool
148     s   bool
149     B   bool
150     L   bool
151     a   string
152     g   string
153     o   string
154     S   string
155 )
156 LOOKUP_parseopts "$@" || return 1
157 [[ -n ${opts[-a]} ]] && author="$(LOOKUP_encode ${opts[-a]})"
158 [[ -n ${opts[-g]} ]] && group="$(LOOKUP_encode ${opts[-g]})"
159 [[ -n ${opts[-o]} ]] && operation="${opts[-o]}"
160 [[ -n ${opts[-S]} ]] && sort="${opts[-S]}"
161 [[ -n ${opts[-L]} ]] && list_mode='yes'
162 [[ -n ${opts[-B]} ]] && list_mode=''
163
164 # set QUERY and export parameters for query handlers via $lookup_communicate[]
165 QUERY="${args[*]}"
166 lookup_communicate[author]="${author}"
167 lookup_communicate[group]="${group}"
168 lookup_communicate[operation]="${operation}"
169 lookup_communicate[sort]="${sort}"
170 LOOKUP_query_handler || return 1
171 [[ ${lookup_communicate[author]} != ${author} ]] && author="${lookup_communicate[author]}"
172 [[ ${lookup_communicate[group]} != ${group} ]] && group="${lookup_communicate[group]}"
173 [[ ${lookup_communicate[operation]} != ${operation} ]] && operation="${lookup_communicate[operation]}"
174 [[ ${lookup_communicate[sort]} != ${sort} ]] && sort="${lookup_communicate[sort]}"
175
176 # validate arguments
177 if [[ -z ${(Mk)known_opers:#$operation} ]] ; then
178     printf 'Unknown operation: '\''%s'\''.\n\n' ${mode}
179     QUERY=''
180 elif [[ -z ${(Mk)known_sorts:#$sort} ]] ; then
181     printf 'Unknown sorting method: '\''%s'\''.\n\n' ${mode}
182     QUERY=''
183 fi
184 if [[ -z ${QUERY} ]] &&
185    ( [[ ${opts[-l]} == 'yes' ]] || [[ ${opts[-s]} == 'yes' ]] || [[ ${opts[-m]} == 'yes' ]] )
186    then
187
188     LOOKUP_help -f
189     return 1
190 fi
191 if [[ -z ${QUERY} ]] && [[ -z ${group} ]] && [[ -z ${author} ]] ; then
192     LOOKUP_help -f
193     return 1
194 fi
195
196 LOOKUP_guard -fd LOOKUP_beh_${backend}_list ||
197 function LOOKUP_beh_${backend}_list() {
198     # list mode via tcp_{open,send,close} {{{
199     LOOKUP_guard || return 1
200     setopt localoptions no_warncreateglobal
201     local mode="$1" q="$2"
202
203     LOOKUP_guard -fd tcp_open || autoload -Uz tcp_open
204     LOOKUP_guard -fd tcp_send || autoload -Uz tcp_send
205
206     case ${mode} in
207     (list)
208         tcp_open -q gmane.org 80 gmane || return 1
209         tcp_send -s gmane -- "GET /find.php?list=${q} HTTP/1.1"
210         tcp_send -s gmane -- 'HOST: gmane.org'
211         ;;
212     (match)
213         tcp_open -q dir.gmane.org 80 gmane || return 1
214         tcp_send -s gmane -- "GET /search.php?match=${q} HTTP/1.1"
215         tcp_send -s gmane -- 'HOST: dir.gmane.org'
216         ;;
217     (*)
218         return 1
219         ;;
220     esac
221
222     tcp_send -s gmane -- 'Connection: close'
223     tcp_send -s gmane -- ''
224
225     TCP_SILENT='yes'
226     tcp_read -b -d -s gmane
227     TCP_SILENT=''
228
229     tcp_close -q -s gmane
230
231     results=()
232     for line in ${tcp_lines} ; do
233         [[ ${line} != *'dir.gmane.org'* ]] && continue
234         line=${line//(#b)*\"(http:\/\/dir.gmane.org\/[^\"]#)\"*/${match[1]}}
235         line=${line#http://dir.gmane.org/}
236         [[ -z ${line} ]] && continue
237         [[ ${line} != gmane.* ]] && continue
238         results+=( ${line} )
239     done
240
241     print -l ${results}
242     return 0
243     #}}}
244 }
245
246 # make the actual queries
247 LOOKUP_encode -q
248 if [[ ${opts[-l]} == 'yes' ]] ; then
249     if [[ ${list_mode} == 'yes' ]] ; then
250         LOOKUP_beh_${backend}_list 'list' ${QUERY}
251         ret="$?"
252     else
253         LOOKUP_browser "http://gmane.org/find.php?list=${QUERY}"
254         ret="$?"
255     fi
256 elif [[ ${opts[-s]} == 'yes' ]] ; then
257     if [[ ${list_mode} == 'yes' ]] ; then
258         LOOKUP_beh_${backend}_list 'match' ${QUERY}
259         ret="$?"
260     else
261         LOOKUP_browser "http://dir.gmane.org/search.php?match=${QUERY}"
262         ret="$?"
263     fi
264 elif [[ ${opts[-m]} == 'yes' ]] ; then
265     LOOKUP_browser "http://mid.gmane.org/${QUERY}"
266     ret="$?"
267 else
268     LOOKUP_browser "http://search.gmane.org/?query=${QUERY}&author=${author}&group=${group}&DEFAULTOP=${operation}&sort=${sort}"
269     ret="$?"
270 fi
271 return ${ret}