# utils.rb - utilities # # Copyright (C) 2002 Yoshinori K. Okuji # # This file is part of BugCommunicator. # # BugCommunicator is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # BugCommunicator is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with BugCommunicator; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA require 'bugcomm/main' require 'rmail/utils' module BugCommunicator # Regular expressions. PROJECT_RE = /\A[a-zA-Z_+-]{1,32}\z/ FIELD_RE = /\A[a-zA-Z_]{1,16}\z/ # Other constants. RESERVED_KEYWORDS = ['any', 'none'] # Print an error message. def error(str) if @@log.respond_to?(:err) @@log.err('%s', str) elsif @@log.respond_to?(:puts) @@log.puts("bugcommd [#{Process.pid}]: error: #{str}") end end module_function :error # Print an informational message. def info(str) if @@verbosity > 0 if @@log.respond_to?(:info) @@log.info('%s', str) elsif @@log.respond_to?(:puts) @@log.puts("bugcommd [#{Process.pid}]: info: #{str}") end end end module_function :info # Print a debug message. def debug(str) if @@verbosity > 1 if @@log.respond_to?(:debug) @@log.debug('%s', str) elsif @@log.respond_to?(:puts) @@log.puts("bugcommd [#{Process.pid}]: debug: #{str}") end end end module_function :debug # Disable signals temporarily. def critical_region t = Thread.current begin t[:critical] = true yield ensure t[:critical] = false end end module_function :critical_region # Check if STR is all US-ASCII. def us_ascii?(str) if /[^\x21-\x7e \t\r\n]/ =~ str then false else true end end module_function :us_ascii? # Convert LF to CRLF. def lf_to_crlf(str) str.gsub(/(^|[^\r])\n/, "\\1\r\n") end module_function :lf_to_crlf # Decode "encoded word"s according to RFC 2047. def decode_encoded_words(str) str.gsub(/=\?([^\?]+)\?(q|b)\?([^\?]*)\?=/i) do |matched| charset, encoding, encoded_text = $1.upcase, $2.downcase, $3 decoded_text = '' begin case encoding when 'q' until encoded_text.empty? if encoded_text.sub!(/\A=([0-9a-f][0-9a-f])/i, '') decoded_text << $1.hex.chr elsif encoded_text.sub!(/\A_/, '') decoded_text << 32.chr else encoded_text.sub!(/\A(.)/, '') decoded_text << $1 end end when 'b' decoded_texxt = RMail::Utils.base64_decode(encoded_text) end if charset == 'UTF-8' decoded_text else Iconv.iconv('UTF-8', charset, decoded_text) end rescue # If anything wrong happens, don't replace anything. matched end end end module_function :decode_encoded_words end