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