require 'net/http' require 'cgi' class Compat attr_reader :plat, :arch PLAT_WIN = 1 PLAT_LINUX = 2 ARCH_X86 = 1 ARCH_X86_64 = 2 def initialize arch,plat = RUBY_PLATFORM.split("-") @arch = Compat::ARCH_X86 if [ "i386", "i486", "i586", "i686" ].index(arch) != nil @arch = Compat::ARCH_X86_64 if [ "x86_64" ].index(arch) != nil @plat = Compat::PLAT_WIN if [ "mingw32", "mswin32" ].index(plat) != nil @plat = Compat::PLAT_LINUX if [ "linux", "freebsd8", "freebsd7" ].index(plat) != nil end end class BadConfig < Exception def initialize(str) @str = str end def what @str end end class Configuration attr_accessor :user, :pass def initialize(compat, fn) @loginconf = fn + "/login.conf" @phonebook = fn + "/phonebook.conf" @phones = { } @compat = compat user = "" pass = "" begin st = File.stat(@loginconf) raise BadConfig.new("Plik nie jest twoja wlasnoscia: #{@loginconf}!") unless st.owned? raise BadConfig.new("Nie moge odczytac pliku: #{@loginconf}!") unless st.readable? raise BadConfig.new("Inni maja dostep do pliku #{@loginconf}!") unless ((st.mode & 4) == 0 and (st.mode & 040) == 0) || compat.plat == Compat::PLAT_WIN IO.readlines(@loginconf).each { |line| user,pass = line.split(",").map { |x| x.strip } } rescue Errno::ENOENT raise BadConfig.new("Nie moge otworzyc pliku do odczytu: #{@loginconf}") end @user = user @pass = pass begin st = File.stat(@phonebook) raise BadConfig.new("Plik nie jest twoja wlasnoscia: #{@phonebook}!") unless st.owned? raise BadConfig.new("Nie moge odczytac pliku: #{@phonebook}!") unless st.readable? raise BadConfig.new("Inni maja dostep do pliku #{@phonebook}!") unless ((st.mode & 4) == 0 and (st.mode & 040) == 0) || compat.plat == Compat::PLAT_WIN IO.readlines(@phonebook).each { |line| num,name = line.split("=").map { |x| x.strip } @phones[num] = name } rescue Errno::ENOENT raise BadConfig.new("Nie moge otworzyc pliku do odczytu: #{@phonebook}") end end def map_recipient(num) to = @phones[num] return (to == nil || to.size == 0) ? nil : to end end class Protocol def initialize(conf) @config = conf end def login http = Net::HTTP.new("www.orange.pl", 80) res = http.request_get("/start.phtml") raise BadStep1.new(res) if res.code != '200' @cookies = res["Set-Cookie"] puts " -> sesja java: #{get_session_id()}" path = "/start.phtml;jsessionid=#{get_session_id()}?_DARGS=/gear/infoportal/header/user-box.jsp" data = get_login_data(@config.user, @config.pass) @config.user = "" @config.pass = "" header = get_headers() header['Referer'] = "http://www.orange.pl/start.phtml" header['Content-Type'] = "application/x-www-form-urlencoded" header['Content-Length'] = data.length.to_s res = http.request_post(path, data, header) data = "" raise BadStep2.new(res) if res.code != '302' raise BadStep3.new(res) if res['Location'].index("_requestid") == nil puts " -> lokacja docelowa: #{res['Location']}" @cookies += "; " + res["Set-Cookie"] res = http.request_get(res['Location'], get_headers()) if res["Set-Cookie"] == nil raise BadStep3a.new(res) if res["Location"] == nil || res["Location"].index("start.phtml?_request") == -1 puts " -> przekierowanie do #{res["Location"]}..." header = get_headers() header['Referer'] = "http://www.orange.pl/start.phtml" res = http.request_get(res['Location'], header) end body = res.read_body @cookies += "; " + res["Set-Cookie"] if body =~ /span class="error-box"/i puts " -> BLAD: wykryto error-box na stronie." puts "" puts "Logowanie nie powiodlo sie, zly login i haslo?" return false end if body.index("/portal/map/map/edit") == nil && body.index("jsp.logoutForm") == nil puts " -> BLAD: nie wykryto przycisku edycji profilu na stronie." puts "" puts "Logowanie nie powiodlo sie, strona zostala zmodyfikowana?" return false end if get_name(body) == nil puts " -> BLAD: nie wykryto imienia ustawionego w profilu." puts "" puts "Logowanie nie powiodlo sie, zmieniono strukture strony?" return false end puts " -> logowanie ok, otrzymano powitanie: #{get_name(body)}!" return true end def sendsms(num, text) smspage = "/portal/map/map/message_box" http = Net::HTTP.new("www.orange.pl", 80) path = "/portal/map/map/message_box?mbox_edit=new&mbox_view=newsms" res = http.request_get(path, get_headers()) puts("---") puts(get_headers.inspect()) puts(path) puts(res.inspect) puts(res.read_body) puts("---") raise BadStep4.new(res) if res.code != '200' body = res.read_body raise BadStep5.new(res) if get_sms_count(body) == nil sms_count = get_sms_count(body) puts " -> pozostalo smsow: #{sms_count}." if sms_count == nil || sms_count == 0 puts " -> BLAD: niewystarczajaca ilosc darmowych SMS." return false end token = get_token(body) if token == nil || token.length == 0 puts " -> BLAD: nie moge namierzyc tokena." return false end puts " -> token: #{token}" path = "/portal/map/map/message_box?_DARGS=/gear/mapmessagebox/smsform.jsp" data = get_sms_form_data(num, text, token) headers = get_headers() headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Length'] = data.length.to_s headers['Referer'] = 'http://www.orange.pl/portal/map/map/message_box?mbox_edit=new&mbox_view=newsms' res = http.request_post(path, data, headers) if res['Location'].index("messageslist") == nil puts " -> BLAD: niepoprawny link powrotny, wiadomosc byc moze nie zostala wyslana." puts " Moze jest za dluga?" return false end return true end def get_headers() { "Host" => "www.orange.pl", "User-Agent" => "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.2.10) Gecko/20100915 Linux Mint/8 (Helena) Firefox/3.6.10", "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language" => "pl,en-us;q=0.7,en;q=0.3", "Accept-Charset" => "ISO-8859-2,utf-8;q=0.7,*;q=0.7", "Keep-Alive" => "115", "Connection" => "keep-alive", "Cookie" => @cookies } end def get_login_data(user, pass) "_dyncharset=UTF-8&%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Fformhandler%2FProxyProfileFormhandler." + "userLogin=#{CGI.escape(user)}&_D%3A%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Fformhandler%2FProxyPr" + "ofileFormhandler.userLogin=+&%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Fformhandler%2FProxyProfileF" + "ormhandler.userPassword=#{CGI.escape(pass)}&_D%3A%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Fformhan" + "dler%2FProxyProfileFormhandler.userPassword=+&%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Fformhandle" + "r%2FProxyProfileFormhandler.successUrl=%2Fstart.phtml&_D%3A%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader" + "%2Fformhandler%2FProxyProfileFormhandler.successUrl=+&%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2Ffo" + "rmhandler%2FProxyProfileFormhandler.errorUrl=%2Fstart.phtml&_D%3A%2Fptk%2Fmap%2Finfoportal%2Fportlet%2F" + "header%2Fformhandler%2FProxyProfileFormhandler.errorUrl=+&%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%" + "2Fformhandler%2FProxyProfileFormhandler.login=loguj&_D%3A%2Fptk%2Fmap%2Finfoportal%2Fportlet%2Fheader%2" + "Fformhandler%2FProxyProfileFormhandler.login=+&x=0&y=0&_DARGS=%2Fgear%2Finfoportal%2Fheader%2Fuser-box." + "jsp" end def get_sms_form_data(number, text, token) "_dyncharset=UTF-8&%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.type=sms&_D%3A%2F" + "amg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.type=+&enabled=false&%2Famg%2Fptk%2Fma" + "p%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.errorURL=%2Fportal%2Fmap%2Fmap%2Fmessage_box%3Fmbox_" + "view%3Dnewsms&_D%3A%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.errorURL=+&%2Fam" + "g%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.successURL=%2Fportal%2Fmap%2Fmap%2Fmessa" + "ge_box%3Fmbox_view%3Dmessageslist&_D%3A%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHand" + "ler.successURL=+&%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.to=#{number}&_D%3A" + "%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.to=+&_D%3A%2Famg%2Fptk%2Fmap%2Fmess" + "agebox%2Fformhandlers%2FMessageFormHandler.body=+&%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMess" + "ageFormHandler.body=#{CGI.escape(text)}&%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHan" + "dler.token=#{token}&_D%3A%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.token=+&%2" + "Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFormHandler.create.x=66&%2Famg%2Fptk%2Fmap%2Fmess" + "agebox%2Fformhandlers%2FMessageFormHandler.create.y=14&%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2" + "FMessageFormHandler.create=Wy%C5%9Blij&_D%3A%2Famg%2Fptk%2Fmap%2Fmessagebox%2Fformhandlers%2FMessageFor" + "mHandler.create=+&_DARGS=%2Fgear%2Fmapmessagebox%2Fsmsform.jsp" end def get_session_id() get_cookie("JSESSIONID") end def get_cookie(token) if @cookies =~ /.*#{token}=([^,;]*)/i $1.strip else raise NoToken.new(token) end end def get_name(body) body = body.gsub(/\n/, "").gsub(/\t/, " ") if body =~ /