home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from time import clock
- from threading import RLock
- import wx.webview as wx
- from util.primitives.mapping import dictdiff
- from permchecks import PermCheck
- import sys
- import threading
- import types
- import facebookapi
- import traceback
- from util.net import UrlQuery
- from logging import getLogger
- from util.callbacks import named_callbacks
- import simplejson
- log = getLogger('fb20.connect')
- HTTP_FBLOGIN = 'http://www.facebook.com/login.php'
- INVITE_URL = 'http://www.digsby.com/fbinvite'
- INVITE_URL_FAKE = 'http://apps.facebook.com/digsbyim/invite.php'
- DIGSBY_LOGIN_URL = 'https://accounts.digsby.com/login.php'
- FACEBOOK_URL = 'http://www.facebook.com/'
- ADD_APP_URL = FACEBOOK_URL + 'add.php'
- LOGIN_SUCCESS_PAGE = 'http://apps.facebook.com/digsbyim/wait.php'
- DIGSBY_API_KEY = 'da7f445feeec8e704ddc36a517a650e8'
- WIDGET_API_KEY = '6dd2ef04610590229f5b036f08c35097'
- DIGSBY_ACHIEVEMENTS_API_KEY = '86656aa55878222a1c29bf7263c85550'
- FB_CONNECT_OPTS = dict(fbconnect = 'true', v = '1.0', connect_display = 'popup', return_session = 'true')
- DPERMS_DA = [
- 'read_stream',
- 'offline_access'] + [
- 'publish_stream'] + [
- 'user_events'] + [
- 'xmpp_login']
- DPERMS_D = DPERMS_DA
- DPERMS_REQUIRED = [
- 'read_stream'] + [
- 'user_events'] + [
- 'xmpp_login']
- APERMS = [
- 'publish_stream',
- 'offline_access']
- APERMS_REQUIRED = APERMS[:]
- DIGSBY_LOGIN_PERMS = UrlQuery(HTTP_FBLOGIN, api_key = DIGSBY_API_KEY, **FB_CONNECT_OPTS)
- DIGSBY_ACHIEVEMENTS_LOGIN_PERMS = UrlQuery(HTTP_FBLOGIN, api_key = DIGSBY_ACHIEVEMENTS_API_KEY, **FB_CONNECT_OPTS)
-
- def print_fn(func):
-
- def wrapper(*a, **k):
- print clock(), func.__name__
- return func(*a, **k)
-
- return wrapper
-
-
- class MethodPrinter(type):
-
- def __init__(cls, name, bases, dict):
- for k, v in list(dict.items()):
- if isinstance(v, types.FunctionType):
- setattr(cls, k, print_fn(v))
- continue
-
- super(MethodPrinter, cls).__init__(name, bases, dict)
-
-
-
- class FBProto(object):
-
- def __init__(self):
- self.lock = RLock()
- self._init_apis()
-
-
- def _init_apis(self):
- self._init_digsby()
- self._init_digsby_ach()
-
-
- def _init_digsby(self, session_key = '', secret = ''):
- self.digsby = facebookapi.FacebookAPI(DIGSBY_API_KEY, session_key, secret, name = 'digsby')
-
-
- def _init_digsby_ach(self, session_key = '', secret = ''):
- self.digsby_ach = facebookapi.FacebookAPI(DIGSBY_ACHIEVEMENTS_API_KEY, session_key, secret, name = 'digsby_ach')
-
-
-
- class LoginCheck(FBProto):
-
- 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):
- FBProto.__init__(self)
- if digsby_api is not None:
- self.digsby = digsby_api
-
- if digsby_ach_api is not None:
- self.digsby_ach = digsby_ach_api
-
- self.ask_ach = None if ask_ach is not None else do_ach
- self.login_success = login_success
- self.login_error = login_error
- self.dead = False
- self.do_ach = do_ach
- self.username = username
- self.acct = acct
- self.try_login = False
- self.waiting_d_init = False
- self.d_init_succ = False
- self.waiting_da_init = False
- self.da_init_succ = False
- self.lock = threading.RLock()
-
-
- def initiatiate_check(self, try_login = False):
- self.try_login = try_login
- if not self.digsby.logged_in:
- log.info('digsby api not logged in')
- log.info_s('not logged in: api: %r, session: %r', self.digsby.name, self.digsby.session_key)
- return self.do_not_logged_in()
- if self.do_ach and not (self.digsby_ach.logged_in):
- log.info('ach api not logged in')
- log.info_s('not logged in: api: %r, session: %r', self.digsby_ach.name, self.digsby_ach.session_key)
- return self.do_not_logged_in()
- d_p = PermCheck(self.digsby, perms = DPERMS_REQUIRED)
- self.waiting_d_init = True
- d_p.check(success = self.d_init_check_succ, error = self.d_init_check_fail)
-
-
- def d_init_check_succ(self, answer):
- do_success = False
- self.lock.__enter__()
-
- try:
- self.d_init_succ = True
- self.waiting_d_init = False
- 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)
- if not self.do_ach:
- do_success = True
- elif not (self.waiting_da_init) and self.da_init_succ:
- do_success = True
- finally:
- pass
-
-
-
- def d_init_check_fail(self, answer):
- do_fail = False
- self.lock.__enter__()
-
- try:
- self.d_init_succ = False
- self.waiting_d_init = False
- 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)
- if not self.do_ach:
- do_fail = True
- elif (self.waiting_da_init or not (self.waiting_da_init)) and self.da_init_succ:
- do_fail = True
- finally:
- pass
-
-
-
- def da_init_check_succ(self, answer):
- do_success = False
- self.lock.__enter__()
-
- try:
- self.da_init_succ = True
- self.waiting_da_init = False
- log.info('da_succ waiting_d_init %r, d_init_succ %r', self.waiting_d_init, self.d_init_succ)
- if not (self.waiting_d_init) and self.d_init_succ:
- do_success = True
- finally:
- pass
-
-
-
- def da_init_check_fail(self, answer):
- do_fail = False
- self.lock.__enter__()
-
- try:
- self.da_init_succ = False
- self.waiting_da_init = False
- log.info('waiting_d_init %r, d_init_succ, %r', self.waiting_d_init, self.d_init_succ)
- if self.waiting_d_init:
- do_fail = True
- finally:
- pass
-
-
-
- def do_not_logged_in(self, answer = None):
- if self.try_login:
- wx.CallAfter(self.do_initial_login)
- else:
- self.dead = True
- self.login_error(self, answer)
-
-
- def do_initial_login(self):
- self.continue_login2(INVITE_URL)
-
-
- def continue_login2(self, forward_to):
- pref = pref
- import common
- next = forward_to
- ach_next = ''
- if self.ask_ach:
- ach_next = next
- next = ach_url = UrlQuery(DIGSBY_ACHIEVEMENTS_LOGIN_PERMS, next = ach_next, req_perms = ','.join(APERMS))
- if not pref('facebook.webkitbrowser', default = False, type = bool):
- next = UrlQuery(LOGIN_SUCCESS_PAGE, next = next)
-
-
- digsby_next = next
- if self.ask_ach:
- d_req_perms = DPERMS_DA
- else:
- d_req_perms = DPERMS_D
- if self.ask_ach:
- url = UrlQuery(DIGSBY_LOGIN_PERMS, next = next, skipcookie = 'true', req_perms = ','.join(d_req_perms), cancel_url = ach_url)
- else:
- url = UrlQuery(DIGSBY_LOGIN_PERMS, next = next, skipcookie = 'true', req_perms = ','.join(d_req_perms))
- window = FBLoginWindow(self.username, self.acct)
- if ach_next == INVITE_URL:
- ach_next = INVITE_URL_FAKE
-
- if digsby_next == INVITE_URL:
- digsby_next = INVITE_URL_FAKE
-
-
- def on_nav(e = None, b = None, url = (None, None, None, None, None), *a, **k):
- if not window.ie:
- e.Skip()
- url = e.URL
-
-
- try:
- parsed = UrlQuery.parse(url)
- except Exception:
- traceback.print_exc()
-
- log.info('url: %r', url)
- log.info('in: %r', 'session' in parsed['query'])
- if 'session' in parsed['query']:
- session = parsed['query'].get('session')
- log.info('parsed: %r', parsed)
- parsed_base = dict(parsed)
- parsed_base.pop('query')
- parsed_base.pop('scheme')
- digsby_next_parsed = UrlQuery.parse(digsby_next)
- log.info('digsby_next_parsed: %r', digsby_next_parsed)
- digsby_next_parsed['query'].pop('', None)
- digsby_next_parsed.pop('scheme')
- digsby_next_parsed_base = dict(digsby_next_parsed)
- digsby_next_parsed_base.pop('query')
- ach_next_parsed = UrlQuery.parse(ach_next)
- log.info('ach_next_parsed: %r', ach_next_parsed)
- ach_next_parsed['query'].pop('', None)
- ach_next_parsed.pop('scheme')
- ach_next_parsed_base = dict(ach_next_parsed)
- ach_next_parsed_base.pop('query')
- if parsed_base == digsby_next_parsed_base and not dictdiff(parsed['query'], digsby_next_parsed['query']):
- log.info('got digsby session')
- log.info_s('got digsby session %r', session)
- self.digsby.set_session(parsed['query']['session'])
- self.digsby.logged_in = True
- if not (self.ask_ach) and not (self.dead):
- self.dead = True
- self.login_success(self, did_login = True)
-
- if not self.dead:
- if not pref('facebook.webkitbrowser', default = False, type = bool):
- b.Stop()
- b.LoadUrl(digsby_next_parsed['query']['next'])
-
-
- elif parsed_base == ach_next_parsed_base and not dictdiff(parsed['query'], ach_next_parsed['query']):
- log.info('got ach session')
- log.info_s('got ach session %r', session)
- self.digsby_ach.set_session(parsed['query']['session'])
- self.digsby_ach.logged_in = True
- if not self.digsby.logged_in and self.digsby.uid == self.digsby_ach.uid:
- self.dead = True
- return self.login_error(self)
- if not self.dead:
- self.dead = True
- return self.login_success(self, did_login = True)
-
-
-
-
- def on_close(*a, **k):
- if not self.dead:
- self.dead = True
- return self.login_success(self, did_login = True)
-
- if window.webkit:
-
- def on_load(e, browser):
- if e.URL == url:
- browser.RunScript('document.getElementById("offline_access").checked=true;')
- browser.RunScript('document.getElementById("email").value = %s;' % simplejson.dumps(self.username))
-
-
- else:
-
- def on_load(*a, **k):
- pass
-
- window.set_callbacks(on_nav, on_load, on_close)
- window.clear_cookies()
- window.LoadURL(url)
-
-
-
- class FBLoginWindow(object):
-
- def clear_cookies(self):
- if hasattr(self.browser, 'ClearCookies'):
- self.browser.ClearCookies()
-
-
-
- def __init__(self, account_name = '', acct = None):
- fbSize = (720, 640)
- if account_name:
- account_name = ' (' + account_name + ')'
-
- self._browser_frame = frame = wx.Frame(None, size = fbSize, title = 'Facebook Login' + account_name, name = 'Facebook Login' + account_name)
- self.acct = acct
- if acct is not None:
- bmp = getattr(acct, 'icon', None)
- if bmp is not None:
- frame.SetIcon(wx.IconFromBitmap(bmp.Resized(32)))
-
-
- pref = pref
- import common
- if pref('facebook.webkitbrowser', default = True, type = bool) or sys.platform.startswith('darwin'):
- self.webkit = True
- self.ie = False
- Browser = WebKitWindow
- import gui.browser.webkit.webkitwindow
- else:
- self.webkit = False
- self.ie = True
- Browser = Browser
- import gui.browser
- frame.CenterOnScreen()
- frame.fblogin = self
- self.browser = b = Browser(frame)
- if self.webkit:
- b.Bind(wx.webview.EVT_WEBVIEW_BEFORE_LOAD, self.on_nav)
- b.Bind(wx.webview.EVT_WEBVIEW_LOAD, self._on_load)
- elif self.ie:
- b.OnNav += self.on_nav
- b.OnBeforeNav += self.on_nav
- b.OnDoc += self.on_nav
- b.OnDoc += self.on_loaded
-
- frame.Bind(wx.EVT_CLOSE, self.on_close)
- self._browser = b
- self.closed = False
- self.have_loaded = False
-
-
- def set_callbacks(self, nav = None, load = None, close = None):
- self.nav = nav
- self.load = load
- self.close = close
-
-
- def LoadURL(self, url):
- if self.webkit:
- self._browser.LoadURL(url)
- elif self.ie:
- self._browser.LoadUrl(url)
-
-
-
- def on_close(self, e):
- self.closed = True
- e.Skip()
- if self.close:
- self.close(e, self._browser)
-
-
-
- def on_nav(self, e):
- if self.ie:
- if self.nav:
- self.nav(e, self._browser, url = e)
-
- return None
- e.Skip()
- if self.nav:
- self.nav(e, self._browser)
-
-
-
- def _on_load(self, e):
- e.Skip()
- if e.State == wx.webview.WEBVIEW_LOAD_DOC_COMPLETED:
- self.have_loaded = True
- return self.on_loaded(e)
- if e.State == wx.webview.WEBVIEW_LOAD_FAILED:
- if self.have_loaded == False and not wx.IsDestroyed(self._browser_frame) and not (self._browser_frame.Shown):
- self._browser_frame.Close()
-
-
-
-
- def on_loaded(self, e):
- if not self._browser_frame.Shown:
- self._browser_frame.Show()
-
- if self.load:
- if self.webkit:
- ret = self.load(e, self._browser)
- elif self.ie:
- ret = self.load(e, self._browser, url = e)
-
-
-
-
-
- class LoginCheck2(LoginCheck):
-
- def __init__(self, digsby_api = None, digsby_ach_api = None, username = None, do_ach = True, acct = None, callback = None, *a, **k):
- pass
-
- __init__ = named_callbacks([
- 'login_error'])(__init__)
-
- def initiatiate_check(self, try_login = False):
- self.try_login = try_login
- d_p = PermCheck(self.digsby, perms = DPERMS_REQUIRED)
- self.waiting_d_init = True
- ('success', 'error', 'not_logged_in', 'not_all_perms')
- d_p.check(success = self.d_init_check_succ, error = self.d_init_check_fail)
-
-
-