6 $we_have_html_entities_decoder = require 'htmlentities'
8 $we_have_html_entities_decoder = false
31 # extras codes, for future use...
45 'otimes' => '⊗',
54 'Epsilon' => 'Ε',
58 'Upsilon' => 'Υ',
60 'there4' => '∴',
65 'rsaquo' => '›',
87 'lfloor' => '⌊',
102 'Scaron' => 'Š',
108 'sbquo' => '‚',
121 'infin' => '∞',
126 'thinsp' => ' ',
128 'bdquo' => '„',
135 'mdash' => '—',
137 'permil' => '‰',
142 'forall' => '∀',
144 'rceil' => '⌉',
147 'lambda' => 'λ',
151 'dagger' => '†',
154 'image' => 'ℑ',
155 'alefsym' => 'ℵ',
161 'frasl' => '⁄',
163 'lowast' => '∗',
174 'oline' => '‾',
181 'empty' => '∅',
188 'weierp' => '℘',
193 'omicron' => 'ο',
194 'upsilon' => 'υ',
196 'Lambda' => 'Λ',
203 'scaron' => 'š',
204 'lsquo' => '‘',
212 'hellip' => '…',
216 'rfloor' => '⌋',
218 'crarr' => '↵',
220 'notin' => '∉',
221 'exist' => '∃',
224 'Dagger' => '‡',
225 'oplus' => '⊕',
231 'lsaquo' => '‹',
233 'Omicron' => 'Ο',
248 'sigmaf' => 'ς',
250 'minus' => '−',
253 'epsilon' => 'ε',
264 'spades' => '♠',
265 'rsquo' => '’',
269 'thetasym' => 'ϑ',
273 'ldquo' => '“',
274 'hearts' => '♥',
286 # miscellaneous useful functions
289 # turn a number of seconds into a human readable string, e.g
290 # 2 days, 3 hours, 18 minutes, 10 seconds
291 def Utils.secs_to_string(secs)
293 days = (secs / (60 * 60 * 24)).to_i
294 secs = secs % (60 * 60 * 24)
295 hours = (secs / (60 * 60)).to_i
296 secs = (secs % (60 * 60))
297 mins = (secs / 60).to_i
298 secs = (secs % 60).to_i
299 ret += "#{days} days, " if days > 0
300 ret += "#{hours} hours, " if hours > 0 || days > 0
301 ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
302 ret += "#{secs} seconds"
307 def Utils.safe_exec(command, *args)
310 return p.readlines.join("\n")
315 rescue Exception => e
316 puts "exec of #{command} led to exception: #{e.inspect}"
319 puts "exec of #{command} failed"
326 @@safe_save_dir = nil
327 def Utils.set_safe_save_dir(str)
328 @@safe_save_dir = str.dup
331 def Utils.safe_save(file)
332 raise 'No safe save directory defined!' if @@safe_save_dir.nil?
333 basename = File.basename(file)
334 temp = Tempfile.new(basename,@@safe_save_dir)
336 yield temp if block_given?
338 File.rename(temp.path, file)
342 # returns a string containing the result of an HTTP GET on the uri
343 def Utils.http_get(uristr, readtimeout=8, opentimeout=4)
345 # ruby 1.7 or better needed for this (or 1.6 and debian unstable)
346 Net::HTTP.version_1_2
347 # (so we support the 1_1 api anyway, avoids problems)
349 uri = URI.parse uristr
352 query += "?#{uri.query}"
357 if(ENV['http_proxy'] && proxy_uri = URI.parse(ENV['http_proxy']))
358 proxy_host = proxy_uri.host
359 proxy_port = proxy_uri.port
363 http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
364 http.open_timeout = opentimeout
365 http.read_timeout = readtimeout
368 resp = http.get(query)
369 if resp.code == "200"
375 error "Utils.http_get exception: #{e.inspect}, while trying to get #{uristr}"
380 def Utils.decode_html_entities(str)
381 if $we_have_html_entities_decoder
382 return HTMLEntities.decode_entities(str)
384 str.gsub(/(&(.+?);)/) {
386 # remove the 0-paddng from unicode integers
388 symbol = "##{$1.to_i.to_s}"
391 # output the symbol's irc-translated character, or a * if it's unknown
392 UNESCAPE_TABLE[symbol] || '*'