#!/usr/bin/env ruby $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'logger' require 'getoptlong' require 'rubygems' require 'racoon' require 'base64' require 'socket' require 'yajl' require 'csv' require 'beanstalk-client' def usage puts "Usage: racoon-send [switches] (--b64-token | --hex-token) " puts " --beanstalk <127.0.0.1:11300> csv of ip:port for beanstalk servers" puts " --pem the path to the pem file, if a pem is supplied the server defaults to gateway.push.apple.com:2195" puts " --alert the message to send" puts " --sound the sound to play, defaults to 'default'" puts " --badge the badge number" puts " --custom a custom json string to be added to the main object" puts " --b64-token a base 64 encoded device token" puts " --hex-token a hex encoded device token" puts " --help this message" end opts = GetoptLong.new( ["--beanstalk", "-b", GetoptLong::REQUIRED_ARGUMENT], ["--pem", "-c", GetoptLong::REQUIRED_ARGUMENT], ["--alert", "-a", GetoptLong::REQUIRED_ARGUMENT], ["--sound", "-S", GetoptLong::REQUIRED_ARGUMENT], ["--badge", "-n", GetoptLong::REQUIRED_ARGUMENT], ["--custom", "-j", GetoptLong::REQUIRED_ARGUMENT], ["--b64-token", "-B", GetoptLong::REQUIRED_ARGUMENT], ["--hex-token", "-H", GetoptLong::REQUIRED_ARGUMENT], ["--help", "-h", GetoptLong::NO_ARGUMENT] ) beanstalks = ["127.0.0.1:11300"] certificate = nil notification = Racoon::Notification.new opts.each do |opt, arg| case opt when '--help' usage exit when '--beanstalk' beanstalks = CSV.parse(arg)[0] when '--pem' certificate = File.read(arg) when '--alert' notification.alert = arg when '--sound' notification.sound = arg when '--badge' notification.badge = arg.to_i when '--custom' notification.custom = Yajl::Parser.parse(arg) when '--b64-token' notification.device_token = Base64::decode64(arg) when '--hex-token' notification.device_token = arg.scan(/[0-9a-f][0-9a-f]/).map {|s| s.hex.chr}.join end end if notification.device_token.nil? usage exit else bs = Beanstalk::Pool.new beanstalks %w{use watch}.each { |s| bs.send(s, "racoon-apns") } bs.ignore("default") project = { :name => "test", :certificate => certificate } bs.yput({ :project => project, :notification => notification.payload, :device_token => notification.device_token, :sandbox => true }) end