Created
July 28, 2012 22:36
-
-
Save mendelgusmao/3195031 to your computer and use it in GitHub Desktop.
Symbian9 - Nono dígito para Symbian & programas que usam vCard (Outlook p.ex)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10/08/2012 | |
- Adicionada opção para o formato 0XX11987654321, com ou sem zero no sufixo (Contribuição: Carlos Luizetto / Roberto Akama) | |
- Adicionada verificação da "gem vcard" para os casos em que o componente responsável pela leitura e edição de vCards não está instalado | |
02/08/2012 | |
- Correção de bugs | |
- Lista de prefixos da Nextel (SME/trunking não faz parte da mudança) (Contribuição: Maurício Iwata) | |
- Adição do formato 9-XXXX-XXXX (Contribuição: Carlos Luizetto) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'vcard' | |
rescue LoadError | |
puts "ERRO! Um componente importante para o funcionamento do script nao foi encontrado!" | |
puts "Voce deve executar 'gem install vcard' e tentar novamente." | |
exit 1 | |
end | |
MODE_AUTOMATIC = 1 | |
MODE_SEMIAUTOMATIC = 2 | |
MODE_MANUAL = 3 | |
MODES = [MODE_AUTOMATIC, MODE_SEMIAUTOMATIC, MODE_MANUAL] | |
FORMAT_KEEP = 1 | |
FORMAT_NONE = 2 | |
FORMAT_54 = 3 | |
FORMAT_45 = 4 | |
FORMAT_333 = 5 | |
FORMAT_144 = 6 | |
FORMATS = [FORMAT_KEEP, FORMAT_54, FORMAT_45, FORMAT_333, FORMAT_144, FORMAT_NONE] | |
SKIP_RANGES = { | |
:nextel => [ | |
7900..7999, | |
7700..7799, | |
7000..7099 | |
] | |
} | |
TESTING = false | |
class Symbian9 | |
attr_accessor :mode, :ddd, :format, :force_ddd, :ld_operator | |
def process() | |
vcards = [] | |
if TESTING | |
vcards = Dir.glob("teste_*.vcf") | |
else | |
vcards = Dir.glob("*.vcf") | |
end | |
vcards.each do |vcard_path| | |
content = open(vcard_path, "r") { |f| f.read } | |
vcard = Vpim::Vcard.decode(content).first | |
name = vcard.name.given | |
new_vcard = Vpim::Vcard::Maker.make2 do |maker| | |
maker.copy(vcard) do |field| | |
field = field.copy | |
if field.name? 'TEL' | |
number = field.value | |
new_number, prefix = parse_number(number) | |
eligible = false | |
changed = false | |
if prefix > 0 and not is_in_skip_ranges(prefix) | |
eligible = true | |
if (@mode == MODE_MANUAL) or (@mode == MODE_SEMIAUTOMATIC and is_exception(prefix)) | |
print "Corrigir o numero #{number} de #{name} para #{new_number}? (s/n) " | |
STDOUT.flush | |
if gets.chomp.downcase == "s" | |
field.value = new_number | |
changed = true | |
end | |
else | |
field.value = new_number | |
changed = true | |
end | |
end | |
if TESTING | |
puts "%s (%s) -> %s %s %s" % [number, prefix, new_number, eligible ? "[eligible]" : "", changed ? "[changed]" : ""] | |
end | |
end | |
field | |
end | |
end | |
unless TESTING | |
open(vcard_path, "w") { |f| f.write(new_vcard) } | |
end | |
end | |
end | |
def parse_number(number) | |
# 1199999999 | 01199999999 | 119999-9999 | 0119999-9999 | |
# 11 99999999 | 011 99999999 | 11 9999-9999 | 011 9999-9999 | |
# (11) 99999999 | (11) 9999-9999 | (011) 99999999 | (011) 9999-9999 | |
# (11)99999999 | (11)9999-9999 | (011)99999999 | (011)9999-9999 | |
# 99999999 | 9999-9999 | |
# 151199999999 | 0151199999999 | 15119999-9999 | 015119999-9999 | |
# 1511 99999999 | 01511 99999999 | 1511 9999-9999 | 01511 9999-9999 | |
# (1511) 99999999 | (1511) 9999-9999 | (01511) 99999999 | (01511) 9999-9999 | |
# (1511)99999999 | (1511)9999-9999 | (01511)99999999 | (01511)9999-9999 | |
if phone = /^(?<preprefixo>\(?0?(?<operadora>\d{2})?11\)?\s?)?(?<numero>(?<prefixo>[5-9]\d{3})\-?\d{4})$/.match(number) | |
if phone["preprefixo"].nil? and @ddd != 11 | |
return [number, 0] | |
end | |
if @format == FORMAT_KEEP | |
new_number = phone["preprefixo"].to_s + "9" + phone["numero"] | |
else | |
new_number = apply_format("9" + phone["numero"], phone["operadora"]) | |
end | |
prefix = phone["prefixo"].to_i | |
return [new_number, prefix] | |
# +551199999999 | |
elsif phone = /^(?<preprefixo>\+5511)(?<numero>(?<prefixo>[5-9]\d{3})\d{4})$/.match(number) | |
if @format == FORMAT_KEEP | |
new_number = phone["preprefixo"].to_s + apply_format("9" + phone["numero"]) | |
else | |
new_number = "+55 " + apply_format("9" + phone["numero"], "") | |
end | |
prefix = phone["prefixo"].to_i | |
return [new_number, prefix] | |
end | |
return ["0", 0] | |
end | |
def apply_format(number, ld_operator) | |
number.gsub! /[^0-9]/, "" | |
ld_operator = @ld_operator.to_i or ld_operator.to_i | |
return case @format | |
when FORMAT_54 then "%s%s-%s" % [ prefixed_ddd(ld_operator), number[0..4], number[5..8] ] | |
when FORMAT_45 then "%s%s-%s" % [ prefixed_ddd(ld_operator), number[0..3], number[4..8] ] | |
when FORMAT_333 then "%s%s-%s-%s" % [ prefixed_ddd(ld_operator), number[0..2], number[3..5], number[6..8] ] | |
when FORMAT_144 then "%s%s-%s-%s" % [ prefixed_ddd(ld_operator), number[0..0], number[1..4], number[5..8] ] | |
when FORMAT_NONE then "%s%s" % [ prefixed_ddd(ld_operator), number ] | |
else number | |
end | |
end | |
def is_in_skip_ranges(prefix) | |
SKIP_RANGES.each do |operator, ranges| | |
return operator unless ranges.select { |range| range.include? prefix }.empty? | |
end | |
return false | |
end | |
def is_exception(prefix) | |
# Parte dos 5xxx ainda sao fixos | |
# 78xx - faixa de prefixos da Nextel usada pela Oi | |
((prefix / 1000).to_i == 5) or ((prefix / 100).to_i == 78) | |
end | |
def prefixed_ddd(ld_operator, force_no_parenthesis = false) | |
prefixed = "" | |
if @force_ddd != "" or @ddd != 11 | |
prefixed = "(11) " | |
prefixed = "(011) " if @force_ddd == "z" | |
prefixed = "(0%s11) " % ld_operator if ld_operator > 0 | |
end | |
prefixed.gsub!(/[\(\)\s]/, "") if @format == FORMAT_NONE or force_no_parenthesis | |
prefixed | |
end | |
def ask_mode | |
print "1 - Atualiza todos os contatos com prefixos 5xxx a 9xxx\n" | |
print "2 - Atualiza todos os contatos com prefixos 6xxx a 9xxx mas pergunta o que fazer com os 5xxx e 78xx\n" | |
print "3 - Pergunta o que fazer para todos os contatos com prefixos 5xxx a 9xxx\n" | |
print "Qual modo utilizar? " | |
STDOUT.flush | |
gets.chomp.to_i | |
end | |
def ask_ddd | |
print "\nQual e seu DDD? " | |
gets.chomp.to_i | |
end | |
def ask_format | |
print "\nQual formato utilizar?\n" | |
print "1 - Nao alterar\n" | |
print "2 - %s987654321\n" % prefixed_ddd(@ld_operator, true) | |
print "3 - %s98765-4321\n" % prefixed_ddd(@ld_operator) | |
print "4 - %s9876-54321\n" % prefixed_ddd(@ld_operator) | |
print "5 - %s987-654-321\n" % prefixed_ddd(@ld_operator) | |
print "6 - %s9-8765-4321\n" % prefixed_ddd(@ld_operator) | |
gets.chomp.to_i | |
end | |
def ask_force_ddd | |
print "\nColocar (11) na frente do numero mesmo que o telefone seja da mesma area?\n" | |
print "s = (11) / z = (011) / vazio = nao colocar \n" | |
gets.chomp.downcase.strip | |
end | |
def ask_ld_operator | |
print "\nDigite o codigo da sua operadora de longa distancia preferida\n0 para nenhuma ou manter \n" | |
gets.chomp.to_i | |
end | |
def run! | |
@ddd = 0 | |
@format = nil | |
@force_ddd = false | |
@ld_operator = 0 | |
@mode = ask_mode() | |
unless MODES.include? @mode | |
print "\nModo incorreto\n" | |
@mode = ask_mode() | |
end | |
@ddd = ask_ddd() | |
unless (11..99).include? @ddd | |
print "\nDDD incorreto\n" | |
@ddd = ask_ddd() | |
end | |
@force_ddd = "" | |
if @ddd == 11 | |
@force_ddd = ask_force_ddd() | |
unless ["s", "z", ""].include? @force_ddd | |
print "\nOpcao incorreta\n" | |
@force_ddd = ask_force_ddd() | |
end | |
else | |
@ld_operator = ask_ld_operator() | |
unless (10..99).include? @ld_operator | |
print "\nCodigo de operadora de longa distancia incorreto\n" | |
@ld_operator = ask_ld_operator() | |
end | |
end | |
@format = ask_format() | |
unless FORMATS.include? @format | |
print "\nFormato incorreto\n" | |
@format = ask_format() | |
end | |
process() | |
end | |
end | |
Symbian9.new.run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment