Skip to content

Instantly share code, notes, and snippets.

@singpolyma
Last active April 8, 2020 00:37
Show Gist options
  • Save singpolyma/e757d312ef8d4ef0da6a238163eed9cf to your computer and use it in GitHub Desktop.
Save singpolyma/e757d312ef8d4ef0da6a238163eed9cf to your computer and use it in GitHub Desktop.
Add cell numbers from a set of vcards to your XMPP roster (through Cheogram)
source "https://rubygems.org"
gem "blather"
gem "vcard"
require 'blather/client'
require 'vcard'
# USAGE: ruby rosterAdd.rb jid@server.tld password path/to/vcf [path/to/second/vcf etc]
#
# One way to install dependencies and run this is the following:
#
# $ bundle install --path=.gems
# $ bundle exec ruby rosterAdd.rb [args]
tels = ARGV.slice!(2..-1).flat_map do |file|
Vcard::Vcard.decode(open(file).read).flat_map do |vcard|
vcard.telephones.select {|tel| tel.location.include?('cell') }.map { |tel|
tel = tel.to_s.gsub(/[^0-9\+]/, '')
tel = "+1#{tel}" if tel.length == 10 && tel[0] != '+'
{ tel: tel, fn: vcard.name.fullname }
}
end
end
Blather.logger = Class.new {
def send(level, message)
warn message
end
}.new
subscription :request? do |s|
if s.from.domain == "cheogram.com"
puts "Approving #{s.from}"
write_to_stream s.approve!
end
end
when_ready do
puts "Connected..."
tels.each do |tel|
item = Blather::RosterItem.new(Blather::JID.new(tel[:tel], "cheogram.com"))
item.name = tel[:fn]
item.groups = ['SMS']
puts "Adding #{item.jid}"
my_roster << item
self << Blather::Stanza::Presence::Subscription.new(item.jid, :subscribe)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment