4 $we_have_html_entities_decoder = require 'htmlentities'
6 $we_have_html_entities_decoder = false
21 # extras codes, for future use...
35 'otimes' => '⊗',
44 'Epsilon' => 'Ε',
49 'Upsilon' => 'Υ',
51 'there4' => '∴',
56 'rsaquo' => '›',
80 'lfloor' => '⌊',
101 'sbquo' => '‚',
114 'infin' => '∞',
119 'thinsp' => ' ',
121 'bdquo' => '„',
128 'mdash' => '—',
130 'permil' => '‰',
135 'forall' => '∀',
137 'rceil' => '⌉',
140 'lambda' => 'λ',
144 'dagger' => '†',
147 'image' => 'ℑ',
148 'alefsym' => 'ℵ',
154 'frasl' => '⁄',
156 'lowast' => '∗',
167 'oline' => '‾',
174 'empty' => '∅',
181 'weierp' => '℘',
186 'omicron' => 'ο',
187 'upsilon' => 'υ',
189 'Lambda' => 'Λ',
196 'scaron' => 'š',
197 'lsquo' => '‘',
205 'hellip' => '…',
209 'rfloor' => '⌋',
211 'crarr' => '↵',
213 'notin' => '∉',
214 'exist' => '∃',
217 'Dagger' => '‡',
218 'oplus' => '⊕',
224 'lsaquo' => '‹',
226 'Omicron' => 'Ο',
241 'sigmaf' => 'ς',
243 'minus' => '−',
246 'epsilon' => 'ε',
257 'spades' => '♠',
258 'rsquo' => '’',
262 'thetasym' => 'ϑ',
266 'ldquo' => '“',
267 'hearts' => '♥',
276 # miscellaneous useful functions
279 # turn a number of seconds into a human readable string, e.g
280 # 2 days, 3 hours, 18 minutes, 10 seconds
281 def Utils.secs_to_string(secs)
283 days = (secs / (60 * 60 * 24)).to_i
284 secs = secs % (60 * 60 * 24)
285 hours = (secs / (60 * 60)).to_i
286 secs = (secs % (60 * 60))
287 mins = (secs / 60).to_i
288 secs = (secs % 60).to_i
289 ret += "#{days} days, " if days > 0
290 ret += "#{hours} hours, " if hours > 0 || days > 0
291 ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
292 ret += "#{secs} seconds"
297 def Utils.safe_exec(command, *args)
300 return p.readlines.join("\n")
305 rescue Exception => e
306 puts "exec of #{command} led to exception: #{e.inspect}"
309 puts "exec of #{command} failed"
315 # returns a string containing the result of an HTTP GET on the uri
316 def Utils.http_get(uristr, readtimeout=8, opentimeout=4)
318 # ruby 1.7 or better needed for this (or 1.6 and debian unstable)
319 Net::HTTP.version_1_2
320 # (so we support the 1_1 api anyway, avoids problems)
322 uri = URI.parse uristr
325 query += "?#{uri.query}"
330 if(ENV['http_proxy'] && proxy_uri = URI.parse(ENV['http_proxy']))
331 proxy_host = proxy_uri.host
332 proxy_port = proxy_uri.port
336 http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
337 http.open_timeout = opentimeout
338 http.read_timeout = readtimeout
341 resp = http.get(query)
342 if resp.code == "200"
348 error "Utils.http_get exception: #{e.inspect}, while trying to get #{uristr}"
353 def Utils.decode_html_entities(str)
354 if $we_have_html_entities_decoder
355 return HTMLEntities.decode_entities(str)
357 str.gsub(/(&(.+?);)/) {
359 # remove the 0-paddng from unicode integers
361 symbol = "##{$1.to_i.to_s}"
364 # output the symbol's irc-translated character, or a * if it's unknown
365 UNESCAPE_TABLE[symbol] || '*'