home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / plugins / facebook / fbconnectlogin.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  15.0 KB  |  447 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from time import clock
  5. from threading import RLock
  6. import wx.webview as wx
  7. from util.primitives.mapping import dictdiff
  8. from permchecks import PermCheck
  9. import sys
  10. import threading
  11. import types
  12. import facebookapi
  13. import traceback
  14. from util.net import UrlQuery
  15. from logging import getLogger
  16. from util.callbacks import named_callbacks
  17. import simplejson
  18. log = getLogger('fb20.connect')
  19. HTTP_FBLOGIN = 'http://www.facebook.com/login.php'
  20. INVITE_URL = 'http://www.digsby.com/fbinvite'
  21. INVITE_URL_FAKE = 'http://apps.facebook.com/digsbyim/invite.php'
  22. DIGSBY_LOGIN_URL = 'https://accounts.digsby.com/login.php'
  23. FACEBOOK_URL = 'http://www.facebook.com/'
  24. ADD_APP_URL = FACEBOOK_URL + 'add.php'
  25. LOGIN_SUCCESS_PAGE = 'http://apps.facebook.com/digsbyim/wait.php'
  26. DIGSBY_API_KEY = 'da7f445feeec8e704ddc36a517a650e8'
  27. WIDGET_API_KEY = '6dd2ef04610590229f5b036f08c35097'
  28. DIGSBY_ACHIEVEMENTS_API_KEY = '86656aa55878222a1c29bf7263c85550'
  29. FB_CONNECT_OPTS = dict(fbconnect = 'true', v = '1.0', connect_display = 'popup', return_session = 'true')
  30. DPERMS_DA = [
  31.     'read_stream',
  32.     'offline_access'] + [
  33.     'publish_stream'] + [
  34.     'user_events'] + [
  35.     'xmpp_login']
  36. DPERMS_D = DPERMS_DA
  37. DPERMS_REQUIRED = [
  38.     'read_stream'] + [
  39.     'user_events'] + [
  40.     'xmpp_login']
  41. APERMS = [
  42.     'publish_stream',
  43.     'offline_access']
  44. APERMS_REQUIRED = APERMS[:]
  45. DIGSBY_LOGIN_PERMS = UrlQuery(HTTP_FBLOGIN, api_key = DIGSBY_API_KEY, **FB_CONNECT_OPTS)
  46. DIGSBY_ACHIEVEMENTS_LOGIN_PERMS = UrlQuery(HTTP_FBLOGIN, api_key = DIGSBY_ACHIEVEMENTS_API_KEY, **FB_CONNECT_OPTS)
  47.  
  48. def print_fn(func):
  49.     
  50.     def wrapper(*a, **k):
  51.         print clock(), func.__name__
  52.         return func(*a, **k)
  53.  
  54.     return wrapper
  55.  
  56.  
  57. class MethodPrinter(type):
  58.     
  59.     def __init__(cls, name, bases, dict):
  60.         for k, v in list(dict.items()):
  61.             if isinstance(v, types.FunctionType):
  62.                 setattr(cls, k, print_fn(v))
  63.                 continue
  64.         
  65.         super(MethodPrinter, cls).__init__(name, bases, dict)
  66.  
  67.  
  68.  
  69. class FBProto(object):
  70.     
  71.     def __init__(self):
  72.         self.lock = RLock()
  73.         self._init_apis()
  74.  
  75.     
  76.     def _init_apis(self):
  77.         self._init_digsby()
  78.         self._init_digsby_ach()
  79.  
  80.     
  81.     def _init_digsby(self, session_key = '', secret = ''):
  82.         self.digsby = facebookapi.FacebookAPI(DIGSBY_API_KEY, session_key, secret, name = 'digsby')
  83.  
  84.     
  85.     def _init_digsby_ach(self, session_key = '', secret = ''):
  86.         self.digsby_ach = facebookapi.FacebookAPI(DIGSBY_ACHIEVEMENTS_API_KEY, session_key, secret, name = 'digsby_ach')
  87.  
  88.  
  89.  
  90. class LoginCheck(FBProto):
  91.     
  92.     def __init__(self, digsby_api = None, digsby_ach_api = None, login_success = None, login_error = None, username = None, do_ach = True, acct = None, ask_ach = None, *a, **k):
  93.         FBProto.__init__(self)
  94.         if digsby_api is not None:
  95.             self.digsby = digsby_api
  96.         
  97.         if digsby_ach_api is not None:
  98.             self.digsby_ach = digsby_ach_api
  99.         
  100.         self.ask_ach = None if ask_ach is not None else do_ach
  101.         self.login_success = login_success
  102.         self.login_error = login_error
  103.         self.dead = False
  104.         self.do_ach = do_ach
  105.         self.username = username
  106.         self.acct = acct
  107.         self.try_login = False
  108.         self.waiting_d_init = False
  109.         self.d_init_succ = False
  110.         self.waiting_da_init = False
  111.         self.da_init_succ = False
  112.         self.lock = threading.RLock()
  113.  
  114.     
  115.     def initiatiate_check(self, try_login = False):
  116.         self.try_login = try_login
  117.         if not self.digsby.logged_in:
  118.             log.info('digsby api not logged in')
  119.             log.info_s('not logged in: api: %r, session: %r', self.digsby.name, self.digsby.session_key)
  120.             return self.do_not_logged_in()
  121.         if self.do_ach and not (self.digsby_ach.logged_in):
  122.             log.info('ach api not logged in')
  123.             log.info_s('not logged in: api: %r, session: %r', self.digsby_ach.name, self.digsby_ach.session_key)
  124.             return self.do_not_logged_in()
  125.         d_p = PermCheck(self.digsby, perms = DPERMS_REQUIRED)
  126.         self.waiting_d_init = True
  127.         d_p.check(success = self.d_init_check_succ, error = self.d_init_check_fail)
  128.  
  129.     
  130.     def d_init_check_succ(self, answer):
  131.         do_success = False
  132.         self.lock.__enter__()
  133.         
  134.         try:
  135.             self.d_init_succ = True
  136.             self.waiting_d_init = False
  137.             log.info('d_succ do_ach %r, waiting_da_init %r, da_init_succ %r', self.do_ach, self.waiting_da_init, self.da_init_succ)
  138.             if not self.do_ach:
  139.                 do_success = True
  140.             elif not (self.waiting_da_init) and self.da_init_succ:
  141.                 do_success = True
  142.         finally:
  143.             pass
  144.  
  145.  
  146.     
  147.     def d_init_check_fail(self, answer):
  148.         do_fail = False
  149.         self.lock.__enter__()
  150.         
  151.         try:
  152.             self.d_init_succ = False
  153.             self.waiting_d_init = False
  154.             log.info('d_fail do_ach %r, waiting_da_init %r, da_init_succ %r', self.do_ach, self.waiting_da_init, self.da_init_succ)
  155.             if not self.do_ach:
  156.                 do_fail = True
  157.             elif (self.waiting_da_init or not (self.waiting_da_init)) and self.da_init_succ:
  158.                 do_fail = True
  159.         finally:
  160.             pass
  161.  
  162.  
  163.     
  164.     def da_init_check_succ(self, answer):
  165.         do_success = False
  166.         self.lock.__enter__()
  167.         
  168.         try:
  169.             self.da_init_succ = True
  170.             self.waiting_da_init = False
  171.             log.info('da_succ waiting_d_init %r, d_init_succ %r', self.waiting_d_init, self.d_init_succ)
  172.             if not (self.waiting_d_init) and self.d_init_succ:
  173.                 do_success = True
  174.         finally:
  175.             pass
  176.  
  177.  
  178.     
  179.     def da_init_check_fail(self, answer):
  180.         do_fail = False
  181.         self.lock.__enter__()
  182.         
  183.         try:
  184.             self.da_init_succ = False
  185.             self.waiting_da_init = False
  186.             log.info('waiting_d_init %r, d_init_succ, %r', self.waiting_d_init, self.d_init_succ)
  187.             if self.waiting_d_init:
  188.                 do_fail = True
  189.         finally:
  190.             pass
  191.  
  192.  
  193.     
  194.     def do_not_logged_in(self, answer = None):
  195.         if self.try_login:
  196.             wx.CallAfter(self.do_initial_login)
  197.         else:
  198.             self.dead = True
  199.             self.login_error(self, answer)
  200.  
  201.     
  202.     def do_initial_login(self):
  203.         self.continue_login2(INVITE_URL)
  204.  
  205.     
  206.     def continue_login2(self, forward_to):
  207.         pref = pref
  208.         import common
  209.         next = forward_to
  210.         ach_next = ''
  211.         if self.ask_ach:
  212.             ach_next = next
  213.             next = ach_url = UrlQuery(DIGSBY_ACHIEVEMENTS_LOGIN_PERMS, next = ach_next, req_perms = ','.join(APERMS))
  214.             if not pref('facebook.webkitbrowser', default = False, type = bool):
  215.                 next = UrlQuery(LOGIN_SUCCESS_PAGE, next = next)
  216.             
  217.         
  218.         digsby_next = next
  219.         if self.ask_ach:
  220.             d_req_perms = DPERMS_DA
  221.         else:
  222.             d_req_perms = DPERMS_D
  223.         if self.ask_ach:
  224.             url = UrlQuery(DIGSBY_LOGIN_PERMS, next = next, skipcookie = 'true', req_perms = ','.join(d_req_perms), cancel_url = ach_url)
  225.         else:
  226.             url = UrlQuery(DIGSBY_LOGIN_PERMS, next = next, skipcookie = 'true', req_perms = ','.join(d_req_perms))
  227.         window = FBLoginWindow(self.username, self.acct)
  228.         if ach_next == INVITE_URL:
  229.             ach_next = INVITE_URL_FAKE
  230.         
  231.         if digsby_next == INVITE_URL:
  232.             digsby_next = INVITE_URL_FAKE
  233.         
  234.         
  235.         def on_nav(e = None, b = None, url = (None, None, None, None, None), *a, **k):
  236.             if not window.ie:
  237.                 e.Skip()
  238.                 url = e.URL
  239.             
  240.             
  241.             try:
  242.                 parsed = UrlQuery.parse(url)
  243.             except Exception:
  244.                 traceback.print_exc()
  245.  
  246.             log.info('url: %r', url)
  247.             log.info('in: %r', 'session' in parsed['query'])
  248.             if 'session' in parsed['query']:
  249.                 session = parsed['query'].get('session')
  250.                 log.info('parsed: %r', parsed)
  251.                 parsed_base = dict(parsed)
  252.                 parsed_base.pop('query')
  253.                 parsed_base.pop('scheme')
  254.                 digsby_next_parsed = UrlQuery.parse(digsby_next)
  255.                 log.info('digsby_next_parsed: %r', digsby_next_parsed)
  256.                 digsby_next_parsed['query'].pop('', None)
  257.                 digsby_next_parsed.pop('scheme')
  258.                 digsby_next_parsed_base = dict(digsby_next_parsed)
  259.                 digsby_next_parsed_base.pop('query')
  260.                 ach_next_parsed = UrlQuery.parse(ach_next)
  261.                 log.info('ach_next_parsed: %r', ach_next_parsed)
  262.                 ach_next_parsed['query'].pop('', None)
  263.                 ach_next_parsed.pop('scheme')
  264.                 ach_next_parsed_base = dict(ach_next_parsed)
  265.                 ach_next_parsed_base.pop('query')
  266.                 if parsed_base == digsby_next_parsed_base and not dictdiff(parsed['query'], digsby_next_parsed['query']):
  267.                     log.info('got digsby session')
  268.                     log.info_s('got digsby session %r', session)
  269.                     self.digsby.set_session(parsed['query']['session'])
  270.                     self.digsby.logged_in = True
  271.                     if not (self.ask_ach) and not (self.dead):
  272.                         self.dead = True
  273.                         self.login_success(self, did_login = True)
  274.                     
  275.                     if not self.dead:
  276.                         if not pref('facebook.webkitbrowser', default = False, type = bool):
  277.                             b.Stop()
  278.                             b.LoadUrl(digsby_next_parsed['query']['next'])
  279.                         
  280.                     
  281.                 elif parsed_base == ach_next_parsed_base and not dictdiff(parsed['query'], ach_next_parsed['query']):
  282.                     log.info('got ach session')
  283.                     log.info_s('got ach session %r', session)
  284.                     self.digsby_ach.set_session(parsed['query']['session'])
  285.                     self.digsby_ach.logged_in = True
  286.                     if not self.digsby.logged_in and self.digsby.uid == self.digsby_ach.uid:
  287.                         self.dead = True
  288.                         return self.login_error(self)
  289.                     if not self.dead:
  290.                         self.dead = True
  291.                         return self.login_success(self, did_login = True)
  292.                 
  293.             
  294.  
  295.         
  296.         def on_close(*a, **k):
  297.             if not self.dead:
  298.                 self.dead = True
  299.                 return self.login_success(self, did_login = True)
  300.  
  301.         if window.webkit:
  302.             
  303.             def on_load(e, browser):
  304.                 if e.URL == url:
  305.                     browser.RunScript('document.getElementById("offline_access").checked=true;')
  306.                     browser.RunScript('document.getElementById("email").value = %s;' % simplejson.dumps(self.username))
  307.                 
  308.  
  309.         else:
  310.             
  311.             def on_load(*a, **k):
  312.                 pass
  313.  
  314.         window.set_callbacks(on_nav, on_load, on_close)
  315.         window.clear_cookies()
  316.         window.LoadURL(url)
  317.  
  318.  
  319.  
  320. class FBLoginWindow(object):
  321.     
  322.     def clear_cookies(self):
  323.         if hasattr(self.browser, 'ClearCookies'):
  324.             self.browser.ClearCookies()
  325.         
  326.  
  327.     
  328.     def __init__(self, account_name = '', acct = None):
  329.         fbSize = (720, 640)
  330.         if account_name:
  331.             account_name = ' (' + account_name + ')'
  332.         
  333.         self._browser_frame = frame = wx.Frame(None, size = fbSize, title = 'Facebook Login' + account_name, name = 'Facebook Login' + account_name)
  334.         self.acct = acct
  335.         if acct is not None:
  336.             bmp = getattr(acct, 'icon', None)
  337.             if bmp is not None:
  338.                 frame.SetIcon(wx.IconFromBitmap(bmp.Resized(32)))
  339.             
  340.         
  341.         pref = pref
  342.         import common
  343.         if pref('facebook.webkitbrowser', default = True, type = bool) or sys.platform.startswith('darwin'):
  344.             self.webkit = True
  345.             self.ie = False
  346.             Browser = WebKitWindow
  347.             import gui.browser.webkit.webkitwindow
  348.         else:
  349.             self.webkit = False
  350.             self.ie = True
  351.             Browser = Browser
  352.             import gui.browser
  353.         frame.CenterOnScreen()
  354.         frame.fblogin = self
  355.         self.browser = b = Browser(frame)
  356.         if self.webkit:
  357.             b.Bind(wx.webview.EVT_WEBVIEW_BEFORE_LOAD, self.on_nav)
  358.             b.Bind(wx.webview.EVT_WEBVIEW_LOAD, self._on_load)
  359.         elif self.ie:
  360.             b.OnNav += self.on_nav
  361.             b.OnBeforeNav += self.on_nav
  362.             b.OnDoc += self.on_nav
  363.             b.OnDoc += self.on_loaded
  364.         
  365.         frame.Bind(wx.EVT_CLOSE, self.on_close)
  366.         self._browser = b
  367.         self.closed = False
  368.         self.have_loaded = False
  369.  
  370.     
  371.     def set_callbacks(self, nav = None, load = None, close = None):
  372.         self.nav = nav
  373.         self.load = load
  374.         self.close = close
  375.  
  376.     
  377.     def LoadURL(self, url):
  378.         if self.webkit:
  379.             self._browser.LoadURL(url)
  380.         elif self.ie:
  381.             self._browser.LoadUrl(url)
  382.         
  383.  
  384.     
  385.     def on_close(self, e):
  386.         self.closed = True
  387.         e.Skip()
  388.         if self.close:
  389.             self.close(e, self._browser)
  390.         
  391.  
  392.     
  393.     def on_nav(self, e):
  394.         if self.ie:
  395.             if self.nav:
  396.                 self.nav(e, self._browser, url = e)
  397.             
  398.             return None
  399.         e.Skip()
  400.         if self.nav:
  401.             self.nav(e, self._browser)
  402.         
  403.  
  404.     
  405.     def _on_load(self, e):
  406.         e.Skip()
  407.         if e.State == wx.webview.WEBVIEW_LOAD_DOC_COMPLETED:
  408.             self.have_loaded = True
  409.             return self.on_loaded(e)
  410.         if e.State == wx.webview.WEBVIEW_LOAD_FAILED:
  411.             if self.have_loaded == False and not wx.IsDestroyed(self._browser_frame) and not (self._browser_frame.Shown):
  412.                 self._browser_frame.Close()
  413.             
  414.         
  415.  
  416.     
  417.     def on_loaded(self, e):
  418.         if not self._browser_frame.Shown:
  419.             self._browser_frame.Show()
  420.         
  421.         if self.load:
  422.             if self.webkit:
  423.                 ret = self.load(e, self._browser)
  424.             elif self.ie:
  425.                 ret = self.load(e, self._browser, url = e)
  426.             
  427.         
  428.  
  429.  
  430.  
  431. class LoginCheck2(LoginCheck):
  432.     
  433.     def __init__(self, digsby_api = None, digsby_ach_api = None, username = None, do_ach = True, acct = None, callback = None, *a, **k):
  434.         pass
  435.  
  436.     __init__ = named_callbacks([
  437.         'login_error'])(__init__)
  438.     
  439.     def initiatiate_check(self, try_login = False):
  440.         self.try_login = try_login
  441.         d_p = PermCheck(self.digsby, perms = DPERMS_REQUIRED)
  442.         self.waiting_d_init = True
  443.         ('success', 'error', 'not_logged_in', 'not_all_perms')
  444.         d_p.check(success = self.d_init_check_succ, error = self.d_init_check_fail)
  445.  
  446.  
  447.