-
Notifications
You must be signed in to change notification settings - Fork 0
/
man.tcl
231 lines (215 loc) · 7.41 KB
/
man.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
encoding system utf-8
::ck::require cmd 0.4
# mans array:
# index : lowered, uncolored keyword
# elem0 : lowered, uncolored keyword
# elem1 : original keyword
# elem2 : man body
# elem3 : man author
# elem4 : list of linked mans
namespace eval mans {
variable version 1.1
variable author "Chpock <chpock@gmail.com>"
variable mans
namespace import -force ::ck::cmd::*
namespace import -force ::ck::files::datafile
}
proc ::mans::init { } {
datafile register man -bot -net
etimer -interval 10 ::mans::dbcleanup
cmd register mans ::mans::run \
-bind "man|ual" -bind "ман|уал" -config "mans"
config register -id "admflags" -type str -default "o|" \
-desc "Flags for access to change mans db." -access "m" -folder "mans"
cmd doc -alias [list man.search] -link [list man.add man.link man.clear man.seealso] "man" \
{*!man* <word> - view man page&K; *!man* [num] <mask> - search man pages.}
cmd doc -link [list man] "man.add" \
{*!man* <word> = <description> - add new man page.}
cmd doc -link [list man] "man.clear" \
{*!man* <word> = - clear man page.}
cmd doc -link [list man] "man.seealso" \
{*!man* <original_man> =+ <other_man> - Add link for page <original_man> as "see also".}
cmd doc -link [list man] "man.link" \
{*!man* <word> = <original_man> - Add man page for <word> as synonym of <original_man>.}
msgreg {
err.title.mask &rMan title can't contains mask symbol like &K"&B*&K"&r and &K"&B?&K"&r.
err.few.page &rSeveral man pages match your request. Select one of them. &K(&n%s&K)
err.few.page.j ", "
err.no.page &rNo such man page.
del.page &BMan page deleted.
err.bad.key &rOriginal keyword is wrong.
err.key.nofnd &rMan page for keyword &K"&R%s&K"&r not found.
err.already.ln &BThese mans already linked.
tnx.for.info &Bthx for information.
err.bad.req &rNo mans match your request.
err.nolocal &rCan't delete this man page.
main.count "&K[&B%s&K/&b%s&K]&n "
main.author " &n[%s]"
main.orig.page &K(&n%s&K)
main.seealso " &K(&nsee also:&g %s&K)"
main.seealso.j "&n,&g "
main &R*&n %s%s%s&K =&n %s%s%s
}
}
proc ::mans::regman { args } {
variable mans
getargs -link str [list] -author str ""
set id [string stripcolor [string tolower [lindex $args 0]]]
set_ [list $id [lindex $args 0] [lindex $args 1] $(author) $(link)]
if { $id == "" } return
if { ![array exists mans] } { datafile getarray man mans }
set mans() [clock seconds]
set mans($id) $_
datafile putarray man mans
}
proc ::mans::dbsearch { args } {
variable mans
getargs -exact flag -local flag
if { ![array exists mans] } { datafile getarray man mans }
set mans() [clock seconds]
set mid [string stripcolor [lindex $args 0]]
if { $(exact) } {
if { ![info exists mans($mid)] } {
if { ![info exists ::ck::cmd::cmddoc($mid)] } {
return ""
}
return $::ck::cmd::cmddoc($mid)
}
return $mans($mid)
}
set result [list]
if { $(local) } {
set mlist [array names mans]
} {
set mlist [luniq [concat [array names mans] [array names ::ck::cmd::cmddoc]]]
}
foreach_ [lsort -dictionary $mlist] {
if { $_ == "" } continue
if { [string match -nocase $mid $_] } {
if { [info exists mans($_)] } {
lappend result $mans($_)
} {
lappend result $::ck::cmd::cmddoc($_)
}
}
}
return $result
}
proc ::mans::dbcount { args } {
variable mans
if { ![array exists mans] } { datafile getarray man mans }
set mans() [clock seconds]
set cnt [llength [luniq [concat [array names mans] [array names ::ck::cmd::cmddoc]]]]
# вычитаем 1 на "пустой ман" в котором время апдейта базы
return [incr cnt -1]
}
proc ::mans::dbupdate { } {
variable mans
if { ![array exists mans] } return
set mans() [clock seconds]
datafile putarray man mans
}
proc ::mans::dbcleanup { } {
variable mans
if { ![array exists mans] } return
if { ([clock seconds] - $mans()) < 300 } return
unset mans
}
proc ::mans::list2titles { mlist } {
set result ""
foreach_ $mlist { lappend result [lindex_ 1] }
return $result
}
proc ::mans::run { sid } {
session import
if { [regexp -- {^\S+\s+(.+?)\s*([\+\-]?=[\+\-]?)\s*(.*)$} [string trim $Text] - var isadd val] } {
session insert CmdAccess [config get "admflags"]
checkaccess -return
# запрос на удаление
if { $val == "" } {
set mid [dbsearch -local -- $var]
if { ![llength $mid] } { reply -err err.no.page }
if { [llength $mid] > 1 } { reply -err err.few.page [cjoin [list2titles $mid] err.few.page.j] }
set mid [lindex [lindex $mid 0] 0]
if { [info exists ::mans::mans($mid)] } {
unset ::mans::mans($mid)
dbupdate
reply -return del.page
} {
reply -err nolocal
}
# запрос на добавление линка
} elseif { [string exists "+" $isadd] } {
set mid [dbsearch -- $var]
if { ![llength $mid] } { reply -err err.bad.key }
if { [llength $mid] > 1 } { reply -err err.few.page [cjoin [list2titles $mid] err.few.page.j] }
set mid [lindex $mid 0]
set linkto [dbsearch -exact -- $val]
if { ![llength $linkto] } { reply -err err.key.nofnd $val }
if { [lexists [lindex $linkto 4] [lindex $mid 0]] } { reply -err err.already.ln }
regman -author [lindex $linkto 3] -link [concat [lindex $linkto 4] [lindex $mid 0]] \
[lindex $linkto 1] [lindex $linkto 2]
reply -return tnx.for.info
}
# добавление нового мана
if { [string first "*" $var] != -1 || [string first "?" $var] != -1 } {
reply -err err.title.mask
}
regman -author $Nick $var $val
reply -return tnx.for.info
}
# просмотр манов
set text [join [lrange $StdArgs 1 end] { }]
if { [regexp {^-?(\d+)(\s+|$)} $text - num] } {
regsub {^-?\d+\s*} $text {} text
if { $num < 1 } { set num 1 }
} {
if { [string trim $text *] eq "" } {
set num [rand [dbcount]]
incr num
} {
set num 1
}
}
if { $text == "" } { set text "*" }
set mlist [dbsearch -- $text]
if { ![llength $mlist] } { reply -err err.bad.req }
if { $num > [llength $mlist] } { set num [llength $mlist] }
set man [lindex $mlist [expr { $num - 1 }]]
# сохраняем val оригинального мана
set oval [lindex $man 1]
# находим слинкованный ман
set stoplist [list [lindex $man 0]]
while { [llength [set_ [dbsearch -exact -- [lindex $man 2]]]] } {
if { [lexists $stoplist [lindex_ 2]] } break
set man $_
lappend stoplist [lindex $man 2]
}
# man содержит последний ман в цепочке
# если оригинальный ман и полученый не совпадают
if { [lindex $man 1] != $oval } {
set oval [cformat main.orig.page $oval]
} {
set oval ""
}
# если есть автор
if { [lindex $man 3] != "" } {
set ath [cformat main.author [string nohighlight [lindex $man 3]]]
} {
set ath ""
}
# если есть see also
if { [llength [lindex $man 4]] } {
set see [cformat main.seealso [cjoin [lindex $man 4] main.seealso.j]]
} {
set see ""
}
# если несколько манов совпало
if { [llength $mlist] > 1 } {
set num [cformat main.count $num [llength $mlist]]
} {
set num ""
}
reply -noperson main $num [lindex $man 1] $oval [lindex $man 2] $see $ath
}
::mans::init