6 $we_have_html_entities_decoder = require 'htmlentities'
8 $we_have_html_entities_decoder = false
25 # extras codes, for future use...
39 'otimes' => '⊗',
48 'Epsilon' => 'Ε',
53 'Upsilon' => 'Υ',
55 'there4' => '∴',
60 'rsaquo' => '›',
84 'lfloor' => '⌊',
105 'sbquo' => '‚',
118 'infin' => '∞',
123 'thinsp' => ' ',
125 'bdquo' => '„',
132 'mdash' => '—',
134 'permil' => '‰',
139 'forall' => '∀',
141 'rceil' => '⌉',
144 'lambda' => 'λ',
148 'dagger' => '†',
151 'image' => 'ℑ',
152 'alefsym' => 'ℵ',
158 'frasl' => '⁄',
160 'lowast' => '∗',
171 'oline' => '‾',
178 'empty' => '∅',
185 'weierp' => '℘',
190 'omicron' => 'ο',
191 'upsilon' => 'υ',
193 'Lambda' => 'Λ',
200 'scaron' => 'š',
201 'lsquo' => '‘',
209 'hellip' => '…',
213 'rfloor' => '⌋',
215 'crarr' => '↵',
217 'notin' => '∉',
218 'exist' => '∃',
221 'Dagger' => '‡',
222 'oplus' => '⊕',
228 'lsaquo' => '‹',
230 'Omicron' => 'Ο',
245 'sigmaf' => 'ς',
247 'minus' => '−',
250 'epsilon' => 'ε',
261 'spades' => '♠',
262 'rsquo' => '’',
266 'thetasym' => 'ϑ',
270 'ldquo' => '“',
271 'hearts' => '♥',
283 # miscellaneous useful functions
286 # turn a number of seconds into a human readable string, e.g
287 # 2 days, 3 hours, 18 minutes, 10 seconds
288 def Utils.secs_to_string(secs)
290 days = (secs / (60 * 60 * 24)).to_i
291 secs = secs % (60 * 60 * 24)
292 hours = (secs / (60 * 60)).to_i
293 secs = (secs % (60 * 60))
294 mins = (secs / 60).to_i
295 secs = (secs % 60).to_i
296 ret += "#{days} days, " if days > 0
297 ret += "#{hours} hours, " if hours > 0 || days > 0
298 ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
299 ret += "#{secs} seconds"
304 def Utils.safe_exec(command, *args)
307 return p.readlines.join("\n")
312 rescue Exception => e
313 puts "exec of #{command} led to exception: #{e.inspect}"
316 puts "exec of #{command} failed"
323 @@safe_save_dir = nil
324 def Utils.set_safe_save_dir(str)
325 @@safe_save_dir = str.dup
328 def Utils.safe_save(file)
329 raise 'No safe save directory defined!' if @@safe_save_dir.nil?
330 basename = File.basename(file)
331 temp = Tempfile.new(basename,@@safe_save_dir)
333 yield temp if block_given?
335 File.rename(temp.path, file)
339 # returns a string containing the result of an HTTP GET on the uri
340 def Utils.http_get(uristr, readtimeout=8, opentimeout=4)
342 # ruby 1.7 or better needed for this (or 1.6 and debian unstable)
343 Net::HTTP.version_1_2
344 # (so we support the 1_1 api anyway, avoids problems)
346 uri = URI.parse uristr
349 query += "?#{uri.query}"
354 if(ENV['http_proxy'] && proxy_uri = URI.parse(ENV['http_proxy']))
355 proxy_host = proxy_uri.host
356 proxy_port = proxy_uri.port
360 http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
361 http.open_timeout = opentimeout
362 http.read_timeout = readtimeout
365 resp = http.get(query)
366 if resp.code == "200"
372 error "Utils.http_get exception: #{e.inspect}, while trying to get #{uristr}"
377 def Utils.decode_html_entities(str)
378 if $we_have_html_entities_decoder
379 return HTMLEntities.decode_entities(str)
381 str.gsub(/(&(.+?);)/) {
383 # remove the 0-paddng from unicode integers
385 symbol = "##{$1.to_i.to_s}"
388 # output the symbol's irc-translated character, or a * if it's unknown
389 UNESCAPE_TABLE[symbol] || '*'