Using skype to get machine ip (windows)

This started as a need to get an IP of a remote computer, that gets new dynamic IP frequently, so i could Remote Desktop into it.

With Skype configured as a startup program, whenever the pc is booted, Skype manages to connect itself. So is a potentially good way to get machine's IP.

After a bit of digging in google and a some customization, i ended up with a small ruby script that instantiates an win32ole skype object, and can listen to incoming events like chats or calls, in this case i'm listening to a chat, whenever gets a text message with "IP", it runs the ipconfig command and returns the output as an answer to the chat.

Ruby Code:

# This simple example echos whatever chat message that is sent to you
require 'win32ole'

#make it wait a bit before, skype connects
#sleep(60)
puts "Ready..."

# create the new Skype automation object
skype = WIN32OLE.new 'Skype4COM.Skype'

# some constants for easy viewing
$RECEIVED= skype.Convert.TextToChatMessageStatus("RECEIVED")
$SAID = skype.Convert.TextToChatMessageType("SAID")

# uncomment the below if you want to run in silent mode. Only works from Skype 2.6 and above
# skype.SilentMode = true

# attach to Skype
skype.attach

# A default handler for any events. You can use this to handle all miscellanous events or if
# you don't have any specific behaviors you want to trigger on
def default_handler(event, *args)

case event
# When someone calls
when "CallStatus"
# do nothing for now

# When any online status changes
when "OnlineStatus"
# do nothing
end

end

# Or you can use a specific method to catch this event
def message_status(msg,status)

# echo whatever is send to you in a chat message
if status == $RECEIVED && msg.Type == $SAID

case msg.body

#request for IP
when "IP"
puts "request for IP Address from " + msg.FromDisplayName
s = `ipconfig`.scan(/IP Add[^\r\n]*/).to_s
#msg.chat.SendMessage(s.scan(/IP Add[^\r\n]*/))
msg.chat.SendMessage(s)
end



end
#msg.chat.SendMessage msg.body if status == $RECEIVED && msg.Type == $SAID
end

# register to receive events from Skype
event = WIN32OLE_EVENT.new(skype, '_ISkypeEvents')

# on any event at all, go to the default handler
event.on_event {|*args| default_handler(*args)}
# for this specific event
event.on_event("MessageStatus") { | *args| message_status args[0], args[1]}

# loop forever
catch(:done) do
loop do
WIN32OLE_EVENT.message_loop
end
end

Skype on Quicksilver

This thing started after I found out how nice is quicksilver giving access to applications, files, commands in your computer, etc ... so i thought: When i want to chat to my Skype contacts, i want to be able to do it just by typing their names in quicksilver, why not?


First, the Address Book contacts are searchable by Quicksilver, and we can even get their phone numbers from there.


Then, the Skype client has some services available for Quicksilver access:
  • Skype/Send Message
  • Skype/Send SMS
  • Skype/Call
So, if we have a contact on Address book we can search by this contact name, get contact phone number and use one of these services, all from inside quicksilver. Even more, if we add to an Address book contact the skype username as a phone number, voilá! all accessible from Quicksilver!
How to get Skype contacts into the Address book ? do something like this:
  1. Use Skype Windows Client to export all contacts into vcards.
  2. Use a little ruby scripting to make some transformations on the vcards.
  3. Import the resulting vcards into Address Book.

End Result

Control-Space and search for a contact:








Select the "other phone"(that contains Skype username)







Tab->write "send m"->enter









Have a Skype chat open.




















Nice Side Effects
  • First of course, is having the Address book filled with all Skype contacts information, their emails, their phones, etc ... All searchable by quicksilver.
  • Because mac has great application integration we can have Mail aware of Address Book contacts.
  • Having a isync compatible phone, means that you can have all your Skype contacts with numbers, addresses even notes, inside the phone.

Ruby Code


#!/usr/bin/ruby -w

require 'vpim/vcard'

$FINAL=""
vcf = open(ARGV[0] || 'al3x.martins.vcf')

cards = Vpim::Vcard.decode(vcf)
tempdir = "all_contacts"
`mkdir #{tempdir}`

#separate vcards
iter = 1
cards.each do |card|
open( tempdir +"/" + iter.to_s + "_temp.vcf", 'w').write card.to_s
iter +=1
end

# Extract the Photos
Dir[tempdir+"/*vcf"].each do |file|
vcard = open(file).read
card = Vpim::Vcard.decode(vcard)[0]
photo = card.photos[0]
photodata = [photo.to_s].pack('m').to_s
photodata = photodata.gsub(/[ \n]/, '').scan(/.{1,76}/).join("\n ")
vcard.sub!('END:VCARD', "PHOTO;BASE64:\n " + photodata + "\nEND:VCARD")

vcard.sub!('X-SKYPE-USERNAME:', 'TEL;SKYPE:')

value = vcard.scan(/FN:(.*)/).flatten.to_s
val2 = value.split(' ')
if val2.size <= 1
vcard.sub!(/^N:(.*)/, 'N:;'+val2.flatten.to_s+';;')
else
final = val2.last + ';'
final += val2[0] + ';'
final += val2[1...-1].join(' ') + ';'
vcard.sub!(/^N:(.*)/, 'N:'+final)
end

vcard.sub!('X-SKYPE-SEX:2', 'Sex: female')
vcard.sub!('X-SKYPE-SEX:1', 'Sex: male')
vcard.sub!('X-SKYPE-LANGUAGE', 'Language')
vcard.sub!('X-SKYPE-COUNTRY', 'Country')
vcard.sub!('X-SKYPE-CITY', 'City')
vcard.sub!('X-SKYPE-MOOD', 'Mood Message')
vcard.sub!('X-SKYPE-PROVINCE', 'Province')
vcard.sub!('X-SKYPE-NUMBER-OF-BUDDIES', "Number of Skype Buddies")

`rm -f #{file}`
open(tempdir+"/"+value.sub('/','_').sub('/','_').sub('/','_') + '.vcf', 'w').write vcard.to_s

end

`say its all done`