Skip to content

Instantly share code, notes, and snippets.

@Baael
Created January 5, 2009 12:14
Show Gist options
  • Save Baael/43370 to your computer and use it in GitHub Desktop.
Save Baael/43370 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'rexml/document'
class GG
attr_accessor :token, :server, :port, :uin, :login, :pass, :content_type
def initialize(options={})
self.uin=options[:uin] unless options[:uin].nil?
self.pass= options[:pass] unless options[:pass].nil?
self.login= options[:login] unless options[:login].nil?
self.content_type= options[:content_type] || 'text/xml; charset=utf-8'
end
def authorize
res=Net::HTTP.new('external-services.gadu-gadu.pl', 80)
response = res.start() {|http|
req = Net::HTTP::Get.new("/botmaster/getToken/#{self.uin}")
req.basic_auth self.login, self.pass
http.read_timeout = 600
http.request(req)
}
return false if response.body.include?("Forbidden")
elm=(REXML::Document.new(response.body.gsub(/[\r|\n|\s+]/, ''))).root.elements
self.token=elm['token'].text
self.server=elm['server'].text
self.port=elm['port'].text
true
end
def push_internal(wiadomosc)
self.authorize || (return 'blad polaczenia')
http = Net::HTTP.new(self.server, self.port)
headers = {
'from' => self.uin,
'token' => self.token,
'Content-Length' => wiadomosc.length.to_s,
'Content-Type' => self.content_type
}
resp = http.post('/',wiadomosc, headers )
end
def message(hash)
ar=[]
hash[:text]=Iconv.conv('ISO-8859-2', 'UTF-8', hash[:text])
ar << "text_message"
ar << Array(hash[:to]).join(";")+";"
ar << (hash[:parts_count] || 1)
ar << (hash[:send_to_offline] || 1)
ar << hash[:text].size
ar << ""
ar << hash[:text]
self.push_internal ar.join(";")
end
def status(hash)
ar=[]
hash[:text]=Iconv.conv('ISO-8859-2', 'UTF-8', hash[:text])
ar << "status_message"
ar << (['away','back','invisible'].include?(hash[:status].to_s)? hash[:status].to_s : 'away')
ar << hash[:text].size
ar << hash[:text]
self.push_internal ar.join(";")
end
end
#in config/environment.rb
GADU=GG.new(:login => 'login', :pass => 'pass', :uin => 'bot_uin')
#sending status (can be :away,:back,:invisible)
GADU.status :status => :back, :text => 'app logger "blog2"'
#sending message
GADU.message :to=> desired_uin, :text => "sample message body"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment