#!/usr/bin/env ruby # # This requires RubyGems and mechanize installed via RubyGems. For # the config file we're using a simple YAML file that will load into # ruby Hash. (I'm being totally lazy, here.) In practice it looks # like this: # # ---- file start ---- # --- # :logfile: # :extention: 1234 # :password: 4321 # ---- file end ---- # # On windows this file is in your userprofile directory and called # nar_phone.yaml. On a unix its ~/.nar_phone # #$VERBOSE=1 require 'optparse' require 'yaml' require 'rubygems' require 'mechanize' require 'logger' START_URL = 'https://path.to.your.system/' rtconfig = { :number => nil, :forward => false } def get_config(filename) if File.exist?(filename) YAML.load_file(filename) else raise IOError, filename + " does not exist" end end default_config = nil if (PLATFORM =~ /win32/) default_config = ENV['USERPROFILE'] + '\nar_phone.yaml' else default_config = ENV['HOME'] + '/.nar_phone' end begin config = get_config(default_config) rescue IOError config = { :logfile => nil, :extention => nil, :password => nil } end opts = OptionParser.new opts.on('-cFILE', '--config FILE', 'Use this config file') do |val| begin config = get_config(val) rescue IOError => e puts "Error loading config file: " + e exit 1 end end opts.on('-eEXT', '--extention EXT') { |val| config[:extention] = val } opts.on('-f', '--forward', 'Forward. Without forwarding is turned off') do rtconfig[:forward] = true end opts.on('-lFILE', '--log FILE', 'Log the output from the http agent') { |val| config[:logfile] = val } opts.on('-pPASSWD', '--password PASSWD') { |val| config[:password] = val } opts.on('-tNUM', '--to NUM', 'Forward phone to this number') { |val| rtconfig[:number] = val } opts.on('-h', '--help', 'Show this message') do puts opts exit end begin opts.parse(ARGV) rescue OptionParser::ParseError => e puts "Incorrect command line usage: " + e.reason + " for " + e.args.to_s exit 1 end if config[:extention].nil? || config[:password].nil? puts "Must have the extention and password configured" exit 1 end # Initialize our web agent and turn on optional logging agent = WWW::Mechanize.new do |a| if (!config[:logfile].nil?) a.log = Logger.new(config[:logfile]) end end # Login page = agent.get(START_URL) login_form = page.forms.name("theForm").first login_form.fields.name("userID").value = config[:extention].to_s login_form.fields.name("password").value = config[:password].to_s page = agent.submit(login_form) # Did we login, or did it fail and put us back at the first screen? if page.title == "Cisco CallManager User Options Log On" puts "Error logging in" exit 1 end # Load the call forwarding page link = page.links.find { |l| l.node.text =~ /all calls to a different number/ } page = agent.click(link) call_forward_form = page.forms.name("callForwardForm").first # Assume numPlanID_0 is our main line, it seems to be true in my # limited testing. This means that the value of numPlanID_0 is what # we append to the checkboxes and what not. line_id = call_forward_form.field("numPlanID_0").value # Turn the fowarding checkbox on call_forward_form.checkboxes.name("forward_#{line_id}").first.checked = rtconfig[:forward] if rtconfig[:number].nil? # Set the radio button to Voice Mail call_forward_form.radiobuttons.name("forwardToVM_#{line_id}").value = "1" else # Set the radio button to "this number" call_forward_form.radiobuttons.name("forwardToVM_#{line_id}").value = "0" # Fill in the text box with the phone number to forward to call_forward_form.field("destination_#{line_id}").value = rtconfig[:number] end page = agent.submit(call_forward_form) # Logout link = page.links.find { |l| l.node.text =~ /^Log Off/ } page = agent.click(link)