09-10-2010, 09:56 +0200
Forum: Overviewworkscript announcements
This script download a url link and add it to config file and load (or not, read config)
AuthorPost
HackeMate  04-14-2009, 12:41   | eWebSource.tcl      Quote  Reply
Member since 05/2008
6 Posts
Location: Spain
Configure default options reading the header script, it will usefull for you.
You can download and source a file without access shell, any comment, bug, idea or suggestion is welcomed

1  # eWebSource --
2  # This script downloads and saves a url if it is a tcl file to be loaded
3  
4  
5  global eWS
6  
7  # Path where the files will be saved
8  set eWS(scriptdir) "scripts"
9  
10  # Should I add the downloaded file to config file to be loaded when rehash?
11  # (1) Enable (0) Disable
12  set eWS(addsource) 1
13  
14  # Should I load the file after download? (Does not means rehash)
15  set eWS(load) 1
16  
17  # (1) Enable proxy (0) Disable
18  set eWS(dl.useproxy) 0
19  
20  # Timeout in milliseconds
21  set eWS(dl.timeout) "5000"
22  
23  # Proxy:port to connect trought firewall (will be random select)
24  set eWS(dl.proxy) {
25      216.194.70.3:8118
26      200.209.145.179:3128
27      83.17.123.186:8080
28      212.93.193.83:8082
29      203.162.224.137:80
30      203.162.224.139:80
31      217.17.248.171:80
32  }
33  # Default proxy port if not defined. Do not force script to use this, most of
34  # times will not work.
35  set eWS(dl.defaultport) "8080"
36  
37  
38  set eWS(me.name) "eWebSource"
39  set eWS(me.version) "1.0"
40  set eWS(me.fileName) [file tail [info script]]
41  set eWS(me.author) "HackeMate"
42  set eWS(me.contact) "HackeMate <Sentencia@eggdrop.es>"
43  package require http
44  
45  setudef flag websource
46  
47  bind pub n !websource eWebSource
48  
49  proc eWebSource {nick uhost hand chan text} {
50     
51      if {![channel get $chan websource]} {
52          putserv "NOTICE $nick :This channel has this script disabled; enable it using .chanset $chan +websource via party line"
53          return
54      }
55      set url [lindex [split $text] 0]
56      set source [lindex [split $text] 1]
57      set load [lindex [split $text] 2]
58     
59      # You can use this command directly via tcldebug or .tcl command party line
60      # if you know already what are the right arguments
61      eWSdownload $url $chan $source $load
62     
63  }
64  proc eWSIsIp {arg} {
65     # modify these if you want to check specifi ranges for
66     # each portion - now it look for 0 - 255 in each
67     set ipnum1 {\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]}
68     set ipnum2 {\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]}
69     set ipnum3 {\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]}
70     set ipnum4 {\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]}
71     set fullExp {^($ipnum1)\.($ipnum2)\.($ipnum3)\.($ipnum4)$}
72     set partialExp {^(($ipnum1)(\.(($ipnum2)(\.(($ipnum3)(\.(($ipnum4)?)?)?)?)?)?)?)?$}
73     set fullExp [subst -nocommands -nobackslashes $fullExp]
74     set partialExp [subst -nocommands -nobackslashes $partialExp]
75      if [string equal int focusout] {
76          if [regexp -- $fullExp $arg] {
77              return 1
78          } else {
79              return 0
80          }
81      } else {
82          return [regexp -- $partialExp $arg]
83      }
84  }
85  
86  proc eWSLog {target string} {
87      global eWS
88      if {![validchan $target]} {
89          putlog "$eWS(me.name)\: $string"
90      } else {
91          putserv "PRIVMSG $target :$eWS(me.name)\: $string"
92      }
93  }
94  
95  proc eWSdownload {webtarget {chan "log"} {addSource ""} {rehash ""}} {
96      global eWS config
97      regsub -all -- {17|02|37|26|03(\d{1,2})?(,\d{1,2})?} $webtarget "" target
98      set isName [lindex [split $target "|"] 0]
99      if {[file extension $isName] eq ".tcl"} {
100          set fileName $isName
101          set target [join [lrange [split $target "|"] 1 end] "|"]
102      } else {
103          set fileName [file tail $target]
104      }
105      if {![regexp -nocase {^(http://)?([^/:]+)(:([0-9]+))?(/.*)?$} $target]} {
106          if {$webtarget eq ""} {
107              set target "Put some url link. Usage: !websource 'url link0' 'add to config file (yes or no)' 'load when download (yes or no)'"
108          }
109          eWSLog $chan "Unsupported URL: $target"
110          return
111      }
112      if {([file extension $target] ne ".tcl") && ([file extension $fileName] ne ".tcl")} {
113          eWSLog $chan "That url does not seems a valid tcl file, if you are sure that really it is, Especific the name using: !websource name.tcl\|$target (I will stop now)"
114          return
115      }
116      if {$addSource eq ""} {
117          set addSource $eWS(addsource)
118      }
119      if {$rehash eq ""} {
120          set rehash $eWS(load)
121      }
122      if {$addSource eq "yes"} {
123          set addSource 1
124      }
125      if {$addSource eq "no"} {
126          set addSource 0
127      }
128      if {($addSource != 1) && ($addSource != 0)} {
129          eWSLog $chan "Should I add file to source or not? Use !websource $webtarget 'yes (1) or no (0)' $rehash"
130          return
131      }
132      if {$rehash eq "yes"} {
133          set rehash 1
134      }
135      if {$rehash eq "no"} {
136          set rehash 0
137      }
138      if {($rehash != 1) && ($rehash != 0)} {
139          eWSLog $chan "Should I add load the file after download or not? Use !websource $webtarget $addSource 'yes (1) or no (0)'"
140          return
141      }
142      if {$eWS(dl.useproxy)} {
143          set randProxy [lindex $eWS(dl.proxy) [rand [llength $eWS(dl.proxy)]]]
144          set port [lindex [split $randProxy ":"] 1]
145          set proxy [lindex [split $randProxy ":"] 0]
146          if {![eWSIsIp $proxy]} {
147              eWSLog $chan "$proxy Is Not a valid ip proxy. I will go ahead anyway (sorry)"
148              set proxy {}
149              set port {}
150          }
151          if {![string is digit -strict $port]} {
152              if {![string is digit -strict $eWS(dl.defaultport)]} {
153                  set eWS(dl.defaultport) "8080"
154                  eWSLog $chan "$port and the default $eWS(dl.defaultport) Are Not valid port numbers. I will use the most common proxy port number: 8080 (recheck your config, probably I will break)"
155                  set port "8080"
156              } else {
157                  eWSLog $chan "$port Is Not a valid port number. I will use the default one: $eWS(dl.defaultport) - It may not work (verify your config)"
158              }
159          }
160          if {$proxy ne ""} {
161              eWSLog $chan "Proxy: $proxy\:$port"
162          }
163      } else {
164          set proxy {}
165          set port {}
166      }
167      set fileTcl [file join $eWS(scriptdir) $fileName]
168      if {[file exists $fileTcl]} {
169          eWSLog $chan "$fileName already exists. Last modified [duration [expr [clock seconds]-[file mtime $fileTcl]]] ago"
170          # Use 02!websource $target yes02 to overwrite. ?
171          return
172      }
173      http::config -useragent "$eWS(me.name) - $eWS(me.version)" -proxyhost $proxy -proxyport $port
174      if {![string is digit -strict $eWS(dl.timeout)]} {
175          set eWS(dl.timeout) "5000"
176      }
177      set token [http::geturl $target -binary 1 -timeout $eWS(dl.timeout)]
178      upvar #0 $token state
179      regexp {[0-9]{3}} $state(http) ncode
180      if {$ncode eq ""} {
181          set ncode $state(http)
182      }
183      switch -- $ncode {
184          "200" {
185              eWSLog $chan "File reached $state(currentsize) bytes. MD5sum: [md5 $state(body)]"
186              if {$addSource} {
187                  set fs [open $fileTcl w]
188                  puts -nonewline $fs $state(body)
189                  close $fs
190                  eWSLog $chan "$fileTcl Saved."
191                  set fs [open $config r]
192                  set data [split [read $fs] \n]
193                  close $fs
194                  lappend data "\# File saved from $target and loaded automaticly by $eWS(me.name) $eWS(me.version)"
195                  lappend data "source $fileTcl"
196                  set fs [open $config w]
197                  puts $fs [join $data \n]
198                  close $fs
199                  eWSLog $chan "This file was added to config file."
200              }
201              if {$rehash} {             
202                  if {[catch {source $fileTcl} msg]} {
203                      eWSLog $chan "There is an error loading $fileTcl\:\n $msg"
204                  } else {
205                      eWSLog $chan "File loaded sucessfully."
206                  }
207              }
208          }
209          "404" {
210              eWSLog $chan "No such file or webpage."
211              http::cleanup $token
212              return
213          }
214          "default" {
215              eWSLog $chan "unforeseen circumstance. Server responded: $state(http)"
216          }
217      }
218      http::cleanup $token
219  }
220  
221  eWSLog "log" "$eWS(me.fileName) $eWS(me.version) loaded - by $eWS(me.author)"

I'm the Player2
This post was edited 1 times, last on 04-17-2009, 19:22 by HackeMate
  The user has attached a file: eWebSource.tcl (Save, 7,736 Bytes, downloaded 8 times)
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen