def func_bind(self, args, oauth_token=None): if len(args) == 1 and args[0].isdigit(): twitter_user = None if oauth_token is None: twitter_user = TwitterUser.get_by_twitter_name(None, self._google_user.jid) if twitter_user is None: return _('INVALID_PIN_CODE') % '' oauth_token = twitter_user.access_token_key token = oauth.Token(oauth_token) if type(args[0]) != unicode: args[0] = args[0].encode('UTF8') token.set_verifier(args[0]) consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET) client = oauth.Client(consumer, token) resp = client.request(twitter.ACCESS_TOKEN_URL, "POST") if not resp: return _('NETWORK_ERROR') access_token = dict(cgi.parse_qsl(resp)) if 'oauth_token' not in access_token: return _('INVALID_PIN_CODE') % args[0] if twitter_user is not None: twitter_user.delete() TwitterUser.add(self._google_user.jid, access_token['oauth_token'], access_token['oauth_token_secret'], access_token['screen_name']) if self._google_user.enabled_user == '': self._google_user.enabled_user = access_token['screen_name'] if IdList.get_by_jid(self._google_user.jid, self._google_user.shard) is None: IdList.add(self._google_user.jid, self._google_user.shard) return _('SUCCESSFULLY_BIND') % access_token['screen_name'] else: raise NotImplementedError
def func_bind(self, args, oauth_token=None): if len(args) == 1 and args[0].isalnum(): jid = self._google_user.jid if oauth_token is None: twitter_user = TwitterUser.get_by_twitter_name(None, jid) if twitter_user is None: return _('INVALID_PIN_CODE') % '' oauth_token = twitter_user.access_token_key token = oauth.Token(oauth_token) token.set_verifier(args[0].encode('UTF8')) consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET) client = oauth.Client(consumer, token) resp = client.request(twitter.ACCESS_TOKEN_URL, "POST") if not resp: return _('NETWORK_ERROR') access_token = dict(cgi.parse_qsl(resp)) if 'oauth_token' not in access_token: return _('INVALID_PIN_CODE') % args[0] if oauth_token is None: twitter_user.delete() TwitterUser.add(jid, access_token['oauth_token'], access_token['oauth_token_secret'], access_token['screen_name']) if self._google_user.enabled_user == '': self._google_user.enabled_user = access_token['screen_name'] if IdList.get_by_jid(jid, self._google_user.shard) is None: IdList.add(jid, self._google_user.shard) return _('SUCCESSFULLY_BIND') % access_token['screen_name'] else: raise NotImplementedError
def func_bind(self, args, oauth_token=None): if len(args) == 1 and args[0].isalnum(): jid = self._google_user.jid if oauth_token is None: twitter_user = TwitterUser.get_by_twitter_name(None, jid) if twitter_user is None: return _("INVALID_PIN_CODE") % "" oauth_token = twitter_user.access_token_key token = oauth.Token(oauth_token) token.set_verifier(args[0].encode("UTF8")) consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET) client = oauth.Client(consumer, token) resp = client.request(twitter.ACCESS_TOKEN_URL, "POST") if not resp: return _("NETWORK_ERROR") access_token = dict(cgi.parse_qsl(resp)) if "oauth_token" not in access_token: return _("INVALID_PIN_CODE") % args[0] if oauth_token is None: twitter_user.delete() TwitterUser.add( jid, access_token["oauth_token"], access_token["oauth_token_secret"], access_token["screen_name"] ) if self._google_user.enabled_user == "": self._google_user.enabled_user = access_token["screen_name"] if IdList.get_by_jid(jid, self._google_user.shard) is None: IdList.add(jid, self._google_user.shard) return _("SUCCESSFULLY_BIND") % access_token["screen_name"] else: raise NotImplementedError
def generate_short_id(self, id): if not db.WRITE_CAPABILITY: return None, None id = str(id) id_list = IdList.get_by_jid(self._jid, self._user.shard) if id in id_list.short_id_list: if not self.allow_duplicate: return None, None short_id = id_list.short_id_list.index(id) else: id_list.list_pointer += 1 id_list.list_pointer %= MAX_SHORT_ID_LIST_NUM id_list.short_id_list[id_list.list_pointer] = id IdList.set(self._jid, id_list) short_id = id_list.list_pointer short_id_str = '' t = short_id + 1 while t > 0: if t % 26: short_id_str = chr(t % 26 + 64) + short_id_str t //= 26 else: short_id_str = 'Z' + short_id_str t = t // 26 - 1 return short_id, short_id_str
def process(self): global _locale try: message = xmpp.Message(self.request.POST) except xmpp.InvalidMessageError: return jid = message.sender.split("/")[0] self._google_user = GoogleUser.get_by_jid(jid) if self._google_user is None: self._google_user = GoogleUser.add(jid) _locale = self._google_user.locale if self._google_user.enabled_user: self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid) self._api = Dummy() if self._twitter_user is None: self._google_user.enabled_user = "" else: self._api = twitter.Api( consumer_key=config.OAUTH_CONSUMER_KEY, consumer_secret=config.OAUTH_CONSUMER_SECRET, access_token_key=self._twitter_user.access_token_key, access_token_secret=self._twitter_user.access_token_secret, ) try: self._user = self._api.verify_credentials() if not self._user: raise twitter.TwitterAuthenticationError except twitter.TwitterAuthenticationError: self._google_user.retry += 1 if self._google_user.retry >= config.MAX_RETRY: GoogleUser.disable(self._google_user.jid) xmpp.send_message(self._google_user.jid, _("NO_AUTHENTICATION")) else: Db.set_datastore(self._google_user) return else: if self._google_user.retry > 0: self._google_user.retry = 0 if self._twitter_user.twitter_name != self._user["screen_name"]: self._twitter_user.twitter_name = self._user["screen_name"] self._google_user.enabled_user = self._user["screen_name"] else: self._twitter_user = Dummy() self._api = Dummy() self._user = Dummy() utils.set_jid(self._google_user.jid) result = self.parse_command(message.body) if result is None: return if result: while CapabilitySet("xmpp").is_enabled(): try: message.reply(result) except xmpp.Error: pass else: break IdList.flush(self._google_user.jid) Db.set_datastore(self._google_user)
def post(self): global _locale try: message = xmpp.Message(self.request.POST) except xmpp.InvalidMessageError: return jid = message.sender.split('/')[0] self._google_user = GoogleUser.get_by_jid(jid) if self._google_user is None: self._google_user = GoogleUser.add(jid) _locale = self._google_user.locale if self._google_user.enabled_user: self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid) self._api = Dummy() if self._twitter_user is None: self._google_user.enabled_user = '' else: self._api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY, consumer_secret=config.OAUTH_CONSUMER_SECRET, access_token_key=self._twitter_user.access_token_key, access_token_secret=self._twitter_user.access_token_secret) try: self._user = self._api.verify_credentials() except twitter.TwitterAuthenticationError: self._google_user.retry += 1 if self._google_user.retry >= config.MAX_RETRY: GoogleUser.disable(self._google_user.jid) xmpp.send_message(self._google_user.jid, _('NO_AUTHENTICATION')) else: Db.set_datastore(self._google_user) return else: if self._google_user.retry > 0: self._google_user.retry = 0 try: if self._twitter_user.twitter_name != self._user['screen_name']: self._twitter_user.twitter_name = self._user['screen_name'] self._google_user.enabled_user = self._user['screen_name'] except TypeError: logging.error('%s: %s' % (jid, unicode(self._user))) return else: self._twitter_user = Dummy() self._api = Dummy() self._user = Dummy() utils.set_jid(self._google_user.jid) result = self.parse_command(message.body) if result is None: return if result: message.reply(result) IdList.flush(self._google_user.jid) Db.set_datastore(self._google_user)
def generate_short_id(id): if not db.WRITE_CAPABILITY: return None id = str(id) id_list = IdList.get_by_jid(_jid, _user.shard) if id in id_list.short_id_list: return id_list.short_id_list.index(id) else: id_list.list_pointer += 1 id_list.list_pointer %= MAX_SHORT_ID_LIST_NUM id_list.short_id_list[id_list.list_pointer] = id IdList.set(_jid, id_list) return id_list.list_pointer
def post(self): try: message = xmpp.Message(self.request.POST) except xmpp.InvalidMessageError: return else: try: result = self.process(message) except (CapabilityDisabledError, DeadlineExceededError): pass else: if result: send_message(message.sender, result) IdList.flush(self._google_user.jid) Db.set_datastore(self._google_user)
def restore_short_id(short_id, jid): if short_id < MAX_SHORT_ID_LIST_NUM: id_list = IdList.get_by_jid(jid, _user.shard) id = id_list.short_id_list[short_id] if id: return int(id) return None
def restore_short_id(self, short_id): short_id_regex = r'^(?:#)?([a-zA-Z]+|\d+)$' m = re.match(short_id_regex, str(short_id)) if m is None: raise ValueError g = m.group(1) try: short_id = int(g) except ValueError: short_id = 0 for x in g.lower(): short_id = short_id * 26 + ord(x) - 96 short_id -= 1 if short_id < MAX_SHORT_ID_LIST_NUM: id_list = IdList.get_by_jid(self._jid, self._user.shard) id = id_list.short_id_list[short_id] if id: return int(id) else: return short_id return None
class cron_handler(webapp.RequestHandler): def get(self, cron_id): cron_id = int(cron_id) data = Session.get_all(shard=cron_id) for u in data: jid = u.key().name() try: self.process(u) except CapabilityDisabledError: try: xmpp.send_presence(jid, presence_show=xmpp.PRESENCE_SHOW_AWAY) except xmpp.Error: pass else: try: xmpp.send_presence(jid) except xmpp.Error: pass def process(self, u): jid = u.key().name() try: flag = xmpp.get_presence(jid) except (xmpp.Error, DeadlineExceededError): flag = True if not flag: u.delete() return google_user = GoogleUser.get_by_jid(jid) if google_user is None: u.delete() return time_delta = int(time()) - google_user.last_update if time_delta < google_user.interval * 60 - 30: return _ = lambda x: gettext(x, locale=google_user.locale) twitter_user = TwitterUser.get_by_twitter_name( google_user.enabled_user, google_user.jid) if twitter_user is None: google_user.enabled_user = '' Db.set_datastore(google_user) return api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY, consumer_secret=config.OAUTH_CONSUMER_SECRET, access_token_key=twitter_user.access_token_key, access_token_secret=twitter_user.access_token_secret) try: self._user = api.verify_credentials() if not self._user or 'screen_name' not in self._user: raise twitter.TwitterError except twitter.TwitterError: google_user.retry += 1 if google_user.retry >= config.MAX_RETRY: GoogleUser.disable(jid=google_user.jid) xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION')) else: Db.set_cache(google_user) return finally: if google_user.retry > 0: google_user.retry = 0 Db.set_cache(google_user) if twitter_user.twitter_name != self._user['screen_name']: twitter_user.twitter_name = self._user['screen_name'] Db.set_cache(twitter_user) google_user.enabled_user = self._user['screen_name'] Db.set_cache(google_user) utils.set_jid(google_user.jid) home_statuses = [] home_mention_statuses = [] all_statuses = [] at_username = '******' + google_user.enabled_user if google_user.display_timeline & MODE_HOME or google_user.display_timeline & MODE_MENTION: home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id, async=True) else: home_rpc = None if google_user.display_timeline & MODE_LIST: list_rpc = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id, since_id=google_user.last_list_id, async=True) else: list_rpc = None if google_user.display_timeline & MODE_MENTION: mention_rpc = api.get_mentions( since_id=google_user.last_mention_id, async=True) else: mention_rpc = None if google_user.display_timeline & MODE_DM: dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id, async=True) else: dm_rpc = None if google_user.display_timeline & MODE_HOME: try: home_statuses = api._process_result(home_rpc) if home_statuses: all_statuses.extend(home_statuses) if home_statuses[0]['id'] > google_user.last_msg_id: google_user.last_msg_id = home_statuses[0]['id'] except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Home:\n' + err.getvalue()) if google_user.display_timeline & MODE_MENTION: try: statuses = api._process_result(mention_rpc) if statuses: all_statuses.extend(statuses) if statuses[0]['id'] > google_user.last_mention_id: google_user.last_mention_id = statuses[0]['id'] if not google_user.display_timeline & MODE_HOME: try: home_statuses = api._process_result(home_rpc) except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Home:\n' + err.getvalue()) else: if home_statuses: if home_statuses[0]['id'] > google_user.last_msg_id: google_user.last_msg_id = home_statuses[0][ 'id'] home_mention_statuses = [ x for x in home_statuses if at_username in x['text'] and x['id'] > google_user.last_mention_id ] if home_mention_statuses: all_statuses.extend(home_mention_statuses) except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Mention:\n' + err.getvalue()) if google_user.display_timeline & MODE_LIST: try: statuses = api._process_result(list_rpc) if statuses: if statuses[0]['id'] > google_user.last_list_id: google_user.last_list_id = statuses[0]['id'] for i in range(len(statuses) - 1, -1, -1): if at_username in statuses[i]['text'] and statuses[i][ 'id'] <= google_user.last_mention_id: del statuses[i] all_statuses.extend(statuses) except twitter.TwitterInternalServerError: pass except BaseException, e: if 'Not found' not in e.message: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' List:\n' + err.getvalue()) if all_statuses: all_statuses.sort(cmp=lambda x, y: cmp(x['id'], y['id'])) last = all_statuses[-1]['id'] for i in range(len(all_statuses) - 2, -1, -1): if last == all_statuses[i]['id']: del all_statuses[i] else: last = all_statuses[i]['id'] content = utils.parse_statuses(all_statuses, filter_self=True, reverse=False) if content.strip(): IdList.flush(google_user.jid) while CapabilitySet('xmpp').is_enabled(): try: xmpp.send_message(google_user.jid, content) except xmpp.Error: pass else: break if google_user.display_timeline & MODE_DM: try: statuses = api._process_result(dm_rpc) content = utils.parse_statuses(statuses) if content.strip(): while CapabilitySet('xmpp').is_enabled(): try: xmpp.send_message( google_user.jid, _('DIRECT_MESSAGES') + '\n\n' + content) except xmpp.Error: pass else: break if statuses[-1]['id'] > google_user.last_dm_id: google_user.last_dm_id = statuses[-1]['id'] except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' DM:\n' + err.getvalue()) google_user.last_update = int(time()) Db.set_datastore(google_user)
def process(self): global _locale try: message = xmpp.Message(self.request.POST) except xmpp.InvalidMessageError: return jid = message.sender.split('/')[0] self._google_user = GoogleUser.get_by_jid(jid) if self._google_user is None: self._google_user = GoogleUser.add(jid) _locale = self._google_user.locale if self._google_user.enabled_user: self._twitter_user = TwitterUser.get_by_twitter_name( self._google_user.enabled_user, self._google_user.jid) self._api = Dummy() if self._twitter_user is None: self._google_user.enabled_user = '' else: self._api = twitter.Api( consumer_key=config.OAUTH_CONSUMER_KEY, consumer_secret=config.OAUTH_CONSUMER_SECRET, access_token_key=self._twitter_user.access_token_key, access_token_secret=self._twitter_user.access_token_secret) try: self._user = self._api.verify_credentials() if not self._user: raise twitter.TwitterAuthenticationError except twitter.TwitterAuthenticationError: self._google_user.retry += 1 if self._google_user.retry >= config.MAX_RETRY: GoogleUser.disable(self._google_user.jid) xmpp.send_message(self._google_user.jid, _('NO_AUTHENTICATION')) else: Db.set_datastore(self._google_user) return else: if self._google_user.retry > 0: self._google_user.retry = 0 if self._twitter_user.twitter_name != self._user[ 'screen_name']: self._twitter_user.twitter_name = self._user[ 'screen_name'] self._google_user.enabled_user = self._user[ 'screen_name'] else: self._twitter_user = Dummy() self._api = Dummy() self._user = Dummy() utils.set_jid(self._google_user.jid) result = self.parse_command(message.body) if result is None: return if result: while CapabilitySet('xmpp').is_enabled(): try: message.reply(result) except xmpp.Error: pass else: break IdList.flush(self._google_user.jid) Db.set_datastore(self._google_user)
def post(self): if db.WRITE_CAPABILITY: jids = self.request.get_all('jid') for jid in jids: try: google_user = GoogleUser.get_by_jid(jid) except db.Error: continue _ = lambda x: gettext(x, locale=google_user.locale) try: twitter_user = TwitterUser.get_by_twitter_name(google_user.enabled_user, google_user.jid) except db.Error: continue if twitter_user is None: google_user.enabled_user = '' Db.set_datastore(google_user) continue api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY, consumer_secret=config.OAUTH_CONSUMER_SECRET, access_token_key=twitter_user.access_token_key, access_token_secret=twitter_user.access_token_secret) try: self._user = api.verify_credentials() if 'screen_name' not in self._user: raise twitter.TwitterError except twitter.TwitterError: google_user.retry += 1 if google_user.retry >= config.MAX_RETRY: GoogleUser.disable(jid=google_user.jid) xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION')) else: Db.set_cache(google_user) return finally: if google_user.retry > 0: google_user.retry = 0 Db.set_cache(google_user) if twitter_user.twitter_name != self._user['screen_name']: twitter_user.twitter_name = self._user['screen_name'] Db.set_cache(twitter_user) google_user.enabled_user = self._user['screen_name'] Db.set_cache(google_user) utils.set_jid(google_user.jid) home_statuses = [] home_mention_statuses = [] all_statuses = [] if google_user.display_timeline & MODE_HOME: home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id, async=True) else: home_rpc = api.get_home_timeline(since_id=google_user.last_mention_id, async=True) if google_user.display_timeline & MODE_LIST: list_rpc = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id, since_id=google_user.last_list_id, async=True) else: list_rpc = None if google_user.display_timeline & MODE_MENTION: mention_rpc = api.get_mentions(since_id=google_user.last_mention_id, async=True) else: mention_rpc = None if google_user.display_timeline & MODE_DM: dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id, async=True) else: dm_rpc = None if google_user.display_timeline & MODE_HOME: try: home_statuses = api._process_result(home_rpc) if home_statuses: all_statuses.extend(home_statuses) if home_statuses[0]['id'] > google_user.last_msg_id: google_user.last_msg_id = home_statuses[0]['id'] except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Home:\n' + err.getvalue()) if google_user.display_timeline & MODE_LIST: try: statuses = api._process_result(list_rpc) if statuses: all_statuses.extend(statuses) if statuses[0]['id'] > google_user.last_list_id: google_user.last_list_id = statuses[0]['id'] except twitter.TwitterInternalServerError: pass except BaseException, e: if 'Not found' not in e.message: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' List:\n' + err.getvalue()) if google_user.display_timeline & MODE_MENTION: try: statuses = api._process_result(mention_rpc) all_statuses.extend(statuses) if not google_user.display_timeline & MODE_HOME: try: home_statuses = api._process_result(home_rpc) except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Home:\n' + err.getvalue()) if home_statuses: if home_statuses[0]['id'] > google_user.last_mention_id: google_user.last_mention_id = home_statuses[0]['id'] at_username = '******' + google_user.enabled_user home_mention_statuses = [x for x in home_statuses if at_username in x['text']] if home_mention_statuses: all_statuses.extend(home_mention_statuses) if home_statuses and home_statuses[0]['id'] > google_user.last_mention_id: google_user.last_mention_id = home_statuses[0]['id'] if statuses and statuses[0]['id'] > google_user.last_mention_id: google_user.last_mention_id = statuses[0]['id'] except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' Mention:\n' + err.getvalue()) if all_statuses: all_statuses.sort(cmp=lambda x, y: cmp(x['id'], y['id'])) last = all_statuses[-1]['id'] for i in range(len(all_statuses) - 2, -1, -1): if last == all_statuses[i]['id']: del all_statuses[i] else: last = all_statuses[i]['id'] content = utils.parse_statuses(all_statuses, filter_self=True, reverse=False) if content.strip(): IdList.flush(google_user.jid) xmpp.send_message(google_user.jid, content) if google_user.display_timeline & MODE_DM: try: statuses = api._process_result(dm_rpc) content = utils.parse_statuses(statuses) if content.strip(): IdList.flush(google_user.jid) xmpp.send_message(google_user.jid, _('DIRECT_MESSAGES') + '\n\n' + content) if statuses[-1]['id'] > google_user.last_dm_id: google_user.last_dm_id = statuses[-1]['id'] except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO('') traceback.print_exc(file=err) logging.error(google_user.jid + ' DM:\n' + err.getvalue()) google_user.last_update = int(time()) Db.set_datastore(google_user)
if tmp_mention_statuses[0]['id'] > google_user.last_mention_id: google_user.last_mention_id = tmp_mention_statuses[0]['id'] for x in tmp_mention_statuses: if x['id'] not in statuses_id: mention_statuses.append(x) statuses_id.append(x['id']) mention_statuses.sort(cmp=lambda x, y: cmp(x['id'], y['id'])) mention_content = self._utils.parse_statuses(mention_statuses, reverse=False, filter_self=True) except twitter.TwitterInternalServerError: pass except BaseException: err = StringIO() traceback.print_exc(file=err) logging.error(google_user.jid + ' Mention:\n' + err.getvalue()) IdList.flush(google_user.jid) google_user.last_update = int(time()) if Db.set_datastore(google_user): if dm_content: xmpp.send_message(google_user.jid, _('DIRECT_MESSAGES') + '\n\n' + dm_content) if mention_content: xmpp.send_message(google_user.jid, _('MENTIONS') + '\n\n' + mention_content) if list_content: xmpp.send_message(google_user.jid, _('LIST') % (google_user.list_user + '/' + google_user.list_name) + '\n\n' + list_content) if home_content: xmpp.send_message(google_user.jid, _('TIMELINE') + '\n\n' + home_content) if __name__ == "__main__": application = webapp.WSGIApplication([('/cron(\d+)', cron_handler)])