PageRenderTime 97ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/musmap.tcl

http://egghelp.googlecode.com/
TCL | 341 lines | 179 code | 77 blank | 85 comment | 36 complexity | 22b9e2afafdd00870512e3ce22b274f3 MD5 | raw file
  1. #----------------------------------------------------------------------------
  2. # musmap -????? ??????? ??????? ??? ??????
  3. # ?????????: .chanset #chan +musmap
  4. # ??????: !musmap <name>
  5. # !movmap <name>
  6. # ??????: !musicmap !moviemap
  7. # ???????: anaesthesia #eggdrop@Rusnet
  8. # ???????: http://egghelp.ru
  9. #----------------------------------------------------------------------------
  10. package require Tcl 8.5
  11. package require http 2.5
  12. namespace eval musmap {
  13. #----------------------------------------------------------------------------
  14. # ????????? ????????? ???????????? (Suzi / http.tcl)
  15. #----------------------------------------------------------------------------
  16. # ???????? ? ???????????? ???????, ??????, ???? ????????? ???????????
  17. variable author "anaesthesia"
  18. variable version "01.02"
  19. variable date "29-Dec-2007"
  20. # ??? ?????????? ??? ??????? ?????????
  21. variable unamespace [namespace tail [namespace current]]
  22. # ??????? ??? ????????? ?????? (????? ???? ?????? ???????)
  23. variable pubprefix {!}
  24. variable pubflag {-|-}
  25. # ??????? ??????
  26. variable musbnd {musmap musicmap}
  27. variable movbnd {movmap moviemap}
  28. # pubcmd:???_??????????? "???????1 ???????2 ..."
  29. # ??????? ? ?? ????????? ????????, ?????? ? ??????? ???????? ????????? ????????
  30. variable pub:musmap "$musbnd $movbnd"
  31. # ???? ??? ? ????, ??? ????????? ??????
  32. variable msgprefix $pubprefix
  33. variable msgflag {-|-}
  34. # ????? ?? ??????? ??? ??? ????????? ???????
  35. variable msg:musmap ${pub:musmap}
  36. # ????? ????????? ????????? ??? ????????? ???????, ?????? ? ???????? ??????? ?????? ??????
  37. # ??? ??????????????? ?????????? variable [pub|msg]:handler "string ..."
  38. # ????? ?????????????? ???????????? ??? ?????????? ????????
  39. # ???????? $unick, $uhost, $uchan
  40. # ??????? tcl ?????????, ??????????? ???????????? ?????????? id ???
  41. # ????????????? ???????.
  42. variable requserid {$uhost}
  43. # ???????????? ????? ????????? ?????????? ???????? ??? ?????? id
  44. variable maxreqperuser 1
  45. # ???????????? ????? ????????? ?????????? ????????
  46. variable maxrequests 5
  47. # ????? ????? ?????????, ? ??????? ??????? ?????? ?????????? ??? ?????????????, ??????
  48. variable pause 30
  49. # ????? ??????-???????
  50. # ?????? ???? "proxyhost.dom:proxyport" ??? ?????? ??????, ???? ??????-?????? ?? ????????????
  51. variable proxy {}
  52. # ????????? ?????????? ?????, ???? ???????? "" -- ????? ???????????
  53. # ????????, ?? ???? ???? ???? ???? ?????????? ?? ?????? -- ?????? ????????
  54. # ???? "no" ???????? ???? ?????????? ????????? ??? ???? ????? ???????????
  55. # ???????? ? ?????? ?????????? ?? ?????? ????????? ?????? ???????
  56. # (??? ???? ?????? ???????? ?? ???? ???????, ??? ?? ??????????? ???? ????)
  57. variable flagactas ""
  58. # ??? ?????????? ?????, ????????? ??? ?????????/?????????? ??????? ?? ??????
  59. # ?? ????????? ??????????? ?? ?????? ?????? ????? ? ????? ??????????
  60. # ? ?????? ?????? ????? ?????? ???????????
  61. # ??? ????????? ?? ?????? ????????? ??????
  62. variable chflag "$flagactas$unamespace"
  63. setudef flag $chflag
  64. #----------------------------------------------------------------------------
  65. # ????????? ????????? ????????????
  66. #----------------------------------------------------------------------------
  67. # ????? ??? ???????? -- ?????? ?????? ??? ?? ???????
  68. # ????? ??????????????? ????? ? ???
  69. variable logrequests {'$unick', '$uhost', '$handle', '$uchan', '$ustr'}
  70. # ??????? ?????? ??? ?????????? ???????, ?? ????????? -- ?? ?????
  71. # ???????? $uchan & $unick
  72. variable pubsend {PRIVMSG $uchan :}
  73. # ??????? ?????? ??? ?????????? ???????, ?? ????????? -- ????????? ?????????
  74. # ???????? ?????? $unick ($uchan == $unick)
  75. variable msgsend {PRIVMSG $unick :}
  76. # ??????? ?????? ??? ??????/????????????? ???????
  77. # ???????? $unick
  78. variable errsend {NOTICE $unick :}
  79. # ???????????? ????? ?????????? ? ??????????? ????????
  80. variable maxredir 2
  81. # ??????? ??????? ? ?????????????, ?? ???? 30 ??????
  82. variable timeout 30000
  83. # ????????? ? ???????? ???????
  84. variable err_ok {??? ?????? ??????}
  85. # ????????? ? ????????????? ???????? ??????, ??????? ? ??????? ?? ????????
  86. # ?????? ?????????? ? ????????????? ?? ????????
  87. variable err_fail {? ????????? ??? ?????? ?? ????????. ???????? ?? ??????? ????????? ? ????????-????????.}
  88. # ????????? ? ????????????? ??????? ????????
  89. variable err_queue_full {? ?????? ????? ??????? ??????? ????????? ? ?? ????? ????????? ??? ??????. ????????? ??????? ?????.}
  90. # ????????? ? ????????????? ??????? ??? ??????????? id
  91. variable err_queue_id {?????????? ????????? ????????? ?????????? ????????.}
  92. # ????????? ? ??? ??? ????? ????? ??????????????? ??????? ?? ???????
  93. # ???????? ?????????? $timewait -- ?????????? ?????, ?? ????????? ???????? ?????? ????? ????????
  94. variable err_queue_time {?????????? ????????? ??????? ?????. ?????? ????? ???????? ??? ????????????? ????? $timewait ???.}
  95. #----------------------------------------------------------------------------
  96. # ?????????? ?????????? ? ???
  97. #----------------------------------------------------------------------------
  98. # ?????, ? ???????? ?????????? ????????? ??????????
  99. variable furlmus "http://www.music-map.com"
  100. variable furlmov "http://www.movie-map.com"
  101. # ?????????? ????????? ???????????
  102. variable maxres 1
  103. # ??????? ????????
  104. variable reqqueue
  105. array unset reqqueue
  106. # ????????? ??????????
  107. variable laststamp
  108. array unset laststamp
  109. variable updinprogress 0
  110. variable updatetimeout 60000
  111. #---body---
  112. proc tolow {strr} {return [string tolower [string map {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} $strr]]}
  113. proc sspace {strr} {return [string trim [regsub -all {[\t\s]+} $strr { }]]}
  114. proc chkrusl {symb} {if {[lsearch -exact {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} $symb] > -1} {return 1} {return 0}}
  115. proc chkrus {strr} {
  116. set len [string length $strr]
  117. set cnt 0 ; while {$cnt < $len} {if {[chkrusl [string index $strr $cnt]]} {return 1} ; incr cnt}
  118. return 0
  119. }
  120. proc msg:musmap {unick uhost handle str} {
  121. pub:musmap $unick $uhost $handle $unick $str
  122. return
  123. }
  124. proc pub:musmap {unick uhost handle uchan str} {
  125. variable requserid ; variable chflag ; variable flagactas ; variable errsend
  126. variable maxres ; variable pubprefix ; variable pubsend ; variable msgsend ; variable unamespace ; variable logrequests
  127. variable musbnd ; variable movbnd ; variable fetchurl ; variable furlmov ; variable furlmus
  128. set id [subst -noc $requserid]
  129. set prefix [subst -noc $errsend]
  130. if {$unick ne $uchan} {if {![channel get $uchan $chflag] ^ $flagactas eq "no"} {return}}
  131. set why [queue_isfreefor $id]
  132. if {$why != ""} {lput puthelp $why $prefix ; return}
  133. #---?????????
  134. set ustr [string map { " " "+" } [tolow $str]]
  135. if {$ustr == "" || [chkrus $ustr]} {
  136. if {$uchan eq $unick} {set prefix [subst -noc $msgsend]} {set prefix [subst -noc $pubsend]}
  137. lput puthelp "\002??????\002: \002${pubprefix}[lindex $musbnd 0]\002 <name> ??? \002${pubprefix}[lindex $movbnd 0]\002 <name> \(??? ?????? \037?????????\037\)" $prefix
  138. lput puthelp "????? ??????? ??????????? ????? ??? ???????. \002??????:\002 ${pubprefix}[lindex $musbnd 0] nine inch nails" $prefix
  139. return
  140. }
  141. if {[string trimleft $::lastbind $pubprefix] in $musbnd} {set fetchurl $furlmus} {set fetchurl $furlmov}
  142. if {$logrequests ne ""} {set logstr [subst -noc $logrequests] ; lput putlog $logstr "$unamespace: "}
  143. ::http::config -useragent "Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)"
  144. if {[queue_add "${fetchurl}/map-search.php?f=${ustr}" $id "[namespace current]::musmap:parser" [list $unick $uhost $uchan {}]]} {
  145. variable err_ok ; if {$err_ok ne ""} {lput puthelp "$err_ok." $prefix}
  146. } {
  147. variable err_fail ; if {$err_fail ne ""} {lput puthelp $err_fail $prefix}
  148. }
  149. return
  150. }
  151. #---parser
  152. proc musmap:parser {errid errstr body extra} {
  153. upvar $errid lerrid $errstr lerrstr $body lbody $extra lextra
  154. variable err_fail ; variable pubsend ; variable msgsend ; variable errsend ; variable useurl ; variable maxres
  155. foreach {unick uhost uchan ustr} $lextra {break}
  156. if {$lerrid ne {ok}} {lput putserv [subst -noc $err_fail] [subst -noc $errsend] ; return}
  157. if {$uchan eq $unick} {set prefix [subst -noc $msgsend]} {set prefix [subst -noc $pubsend]}
  158. #--suzi-patch
  159. if {[info exists ::sp_version]} {set str $lbody} {set str $lbody}
  160. #----------------------------------------------------------------------------
  161. ##---parser-specific------
  162. #----------------------------------------------------------------------------
  163. regsub -all -- "\n|\r" $str {} str
  164. if {[regexp {gnodMapSettings.*?</script>(.*?)<script>} $str -> res]} {
  165. regsub -all -- "<a href.*?>" $res " \002*\002 " res
  166. regsub -all -- "<.*?>" $res {} res
  167. lput putserv "[sspace $res]" $prefix
  168. } {
  169. lput putserv "MusicMap: \037?????? ?? ???????\037." $prefix
  170. return
  171. }
  172. return
  173. }
  174. #----------------------------------------------------------------------------
  175. ##---ok------
  176. #----------------------------------------------------------------------------
  177. proc lput {cmd str {prefix {}} {maxchunk 420}} {
  178. set buf1 "" ; set buf2 [list]
  179. foreach word [split $str] {append buf1 " " $word ; if {[string length $buf1]-1 >= $maxchunk} {lappend buf2 [string range $buf1 1 end] ; set buf1 ""}}
  180. if {$buf1 != ""} {lappend buf2 [string range $buf1 1 end]}
  181. foreach line $buf2 {$cmd $prefix$line}
  182. return
  183. }
  184. #---queue
  185. proc queue_isfreefor {{id {}}} {
  186. variable reqqueue ; variable maxreqperuser ; variable maxrequests ; variable laststamp ; variable pause
  187. variable err_queue_full ; variable err_queue_id ; variable err_queue_time
  188. if {[info exists laststamp(stamp,$id)]} {
  189. set timewait [expr {$laststamp(stamp,$id) + $pause - [unixtime]}]
  190. if {$timewait > 0} {return [subst -noc $err_queue_time]}
  191. }
  192. if {[llength [array names reqqueue -glob "*,$id"]] >= $maxreqperuser} {return $err_queue_id}
  193. if {[llength [array names reqqueue]] >= $maxrequests} {return $err_queue_full}
  194. return
  195. }
  196. #---add-to-queue
  197. proc queue_add {newurl id parser extra {redir 0}} {
  198. variable reqqueue ; variable proxy ; variable timeout ; variable laststamp
  199. ::http::config -proxyfilter "[namespace current]::queue_proxy"
  200. if {![catch {
  201. set token [::http::geturl $newurl -command "[namespace current]::queue_done" -binary true -timeout $timeout]} errid]} {
  202. set reqqueue($token,$id) [list $parser $extra $redir]
  203. set laststamp(stamp,$id) [unixtime]
  204. } {return false}
  205. return true
  206. }
  207. #---proxy
  208. proc queue_proxy {url} {
  209. variable proxy
  210. if {$proxy ne {}} {return [split $proxy {:}]}
  211. return [list]
  212. }
  213. #---callback
  214. proc queue_done {token} {
  215. upvar #0 $token state
  216. variable reqqueue ; variable maxredir
  217. set errid [::http::status $token]
  218. set errstr [::http::error $token]
  219. set id [array names reqqueue "$token,*"]
  220. foreach {parser extra redir} $reqqueue($id) {break}
  221. regsub -- "^$token," $id {} id
  222. while (1) {
  223. if {$errid == "ok" && [::http::ncode $token] == 302} {
  224. if {$redir < $maxredir} {
  225. array set meta $state(meta)
  226. if {[info exists meta(Location)]} {variable fetchurl ; queue_add "$fetchurl$meta(Location)" $id $parser $extra [incr redir] ; break}
  227. } {set errid "error" ; set errstr "Maximum redirects reached"}
  228. }
  229. if {[catch {$parser {errid} {errstr} {state(body)} {extra}} errid]} {lput putlog $errid "[namespace current] "}
  230. break
  231. }
  232. array unset reqqueue "$token,*"
  233. ::http::cleanup $token
  234. return
  235. }
  236. #---clear
  237. proc queue_clear_stamps {} {
  238. variable laststamp ; variable timeout ; variable timerID
  239. set curr [expr { [unixtime] - 2 * $timeout / 1000 }]
  240. foreach {id} [array names laststamp] {if {$laststamp($id) < $curr} {array unset laststamp $id}}
  241. set timerID [timer 10 "[info level 0]"]
  242. }
  243. #---command aliases & bnd
  244. proc cmdaliases {{action {bind}}} {
  245. foreach {bindtype} {pub msg dcc} {
  246. foreach {bindproc} [info vars "[namespace current]::${bindtype}:*"] {
  247. variable "${bindtype}prefix"
  248. variable "${bindtype}flag"
  249. foreach {alias} [set $bindproc] {
  250. catch {$action $bindtype [set ${bindtype}flag] [set ${bindtype}prefix]$alias $bindproc}
  251. }
  252. }
  253. }
  254. return
  255. }
  256. #---killtimers
  257. if {[info exists timerID]} {catch {killtimer $timerID} ; catch {unset timerID}}
  258. #---rest
  259. [namespace current]::queue_clear_stamps
  260. [namespace current]::cmdaliases
  261. variable sfil [lindex [split [info script] "/"] end]
  262. variable modf [clock format [file mtime [info script]] -format "%d-%b-%Y : %H:%M:%S"]
  263. if {[info exists ::sp_version]} {putlog "[namespace current] v$version (suzi_$sp_version) :: file:$sfil / rel:\[$date\] / mod:\[$modf\] :: by $author :: loaded."} {putlog "[namespace current] v$version :: file:$sfil / rel:\[$date\] / mod:\[$modf\] :: by $author :: loaded."}
  264. }