1 ### vim:ft=zsh:foldmethod=marker
2 ## gmane.org backend for lookup
3 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
5 LOOKUP_guard || return 1
6 [[ -n ${lookup_describe} ]] && printf '%s' 'query gmane.org' && return 0
8 local lookup_context bookmarks
9 local -A known_sorts known_opers
10 lookup_context="$(LOOKUP_context)"
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"
20 revdate 'sort by reverse date'
21 relevance 'sort by relevance'
25 and 'require all words'
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'
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}
78 LOOKUP_help && return 0
80 if [[ -n ${lookup_complete} ]] ; then
81 # gmane backend completion code {{{
84 LOOKUP_guard -fd __lookup_${backend}_known_operations ||
85 function __lookup_${backend}_known_operations() {
90 for o in ${(k)known_opers}; do
91 os+=("$o:${known_opers[$o]}")
93 _describe -t gmane_opers 'known operations' os
96 LOOKUP_guard -fd __lookup_${backend}_known_sorts ||
97 function __lookup_${backend}_known_sorts() {
102 for s in ${(k)known_sorts}; do
103 as+=("$s:${known_sorts[$s]}")
105 _describe -t gmane_sorts 'known sorting methods' as
108 LOOKUP_guard -fd __lookup_${backend}_group_bookmarks ||
109 function __lookup_${backend}_group_bookmarks() {
112 if [[ -r ${bookmarks} ]] ; then
113 bms=(${(f)"$(< $bookmarks)"})
115 _describe -t gmane_bookmarks 'bookmarked gmane groups' bms
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'
130 _arguments -s -w -A '-*' ${comp_args} && return 0
131 _message 'gmane query'
136 local author group operation sort ret list_mode
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'
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=''
164 # set QUERY and export parameters for query handlers via $lookup_communicate[]
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]}"
177 if [[ -z ${(Mk)known_opers:#$operation} ]] ; then
178 printf 'Unknown operation: '\''%s'\''.\n\n' ${mode}
180 elif [[ -z ${(Mk)known_sorts:#$sort} ]] ; then
181 printf 'Unknown sorting method: '\''%s'\''.\n\n' ${mode}
184 if [[ -z ${QUERY} ]] &&
185 ( [[ ${opts[-l]} == 'yes' ]] || [[ ${opts[-s]} == 'yes' ]] || [[ ${opts[-m]} == 'yes' ]] )
191 if [[ -z ${QUERY} ]] && [[ -z ${group} ]] && [[ -z ${author} ]] ; then
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"
203 LOOKUP_guard -fd tcp_open || autoload -Uz tcp_open
204 LOOKUP_guard -fd tcp_send || autoload -Uz tcp_send
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'
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'
222 tcp_send -s gmane -- 'Connection: close'
223 tcp_send -s gmane -- ''
226 tcp_read -b -d -s gmane
229 tcp_close -q -s gmane
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
246 # make the actual queries
248 if [[ ${opts[-l]} == 'yes' ]] ; then
249 if [[ ${list_mode} == 'yes' ]] ; then
250 LOOKUP_beh_${backend}_list 'list' ${QUERY}
253 LOOKUP_browser "http://gmane.org/find.php?list=${QUERY}"
256 elif [[ ${opts[-s]} == 'yes' ]] ; then
257 if [[ ${list_mode} == 'yes' ]] ; then
258 LOOKUP_beh_${backend}_list 'match' ${QUERY}
261 LOOKUP_browser "http://dir.gmane.org/search.php?match=${QUERY}"
264 elif [[ ${opts[-m]} == 'yes' ]] ; then
265 LOOKUP_browser "http://mid.gmane.org/${QUERY}"
268 LOOKUP_browser "http://search.gmane.org/?query=${QUERY}&author=${author}&group=${group}&DEFAULTOP=${operation}&sort=${sort}"