def reg_invite_set_login(value): sess = env.user.session() if not sess.data(): env.user.session(reg_invite_set_login, key=value) return 'Please enter your nickname' if not sess['key']: env.user.session_destroy() return 'Fail' env.user.session_destroy() if value.startswith('@'): value = value[1:] if not validate_nickname(value): return xmpp_template('reg_invalid') try: users.register(login=value, accounts=[('xmpp', env.jid)]) redis = RedisPool(settings.storage_socket) redis.delete('invite:%s' % sess['key']) return xmpp_template('reg_ok', login=value) except UserExists: return 'User @%s already exists.' % value except UserError, e: log.error('%s: %s' % (e.__class__.__name__, e.message)) return e.message
def passwd_steps(value=None): sess = env.user.session() if not value or not sess.data(): env.user.session(passwd_steps, step='passwd') return xmpp_template('passwd_enter_new') password = None if sess['step'] == 'passwd': #if env.user.check_password_set(): # env.user.session(passwd_steps, step='check', passwd=value) # return xmpp_template('passwd_enter_current') password = value env.user.session_destroy() #if sess['step'] == 'check': # if env.user.check_password_set() and not env.user.check_password(value): # env.user.session_destroy() # return xmpp_template('passwd_wrong') # password = sess['passwd'] env.user.session_destroy() if password: env.user.set_password(password) env.user.save() return xmpp_template('passwd_changed') else: return 'Unknown error'
def show_post(post_id, last=False, all=False, show=False, offset=None, limit=None): try: post = posts.show_post(post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except (SubscribeError, PostAuthorError): return xmpp_template('post_denied', post_id=post_id) subscribed = post.check_subscriber(env.user) if env.user.id else False updates = [] for i, upd in enumerate(post.updates()): updates.append({'no': i + 1, 'text': upd['text']}) comments = [] posts.clear_unread_posts(post_id) if last or all or show: if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 res = post.comments(last=last, all=all, offset=offset, limit=limit) for c in res: comments.append({ 'comment_id': c.id, 'to_comment_id': c.to_comment_id, 'comment_author': c.author.login, 'comment_text': c.text, 'is_rec': c.is_rec, 'files': c.files }) cnt = None posts.clear_unread_comments(post_id, map(lambda c: c.id, res)) else: cnt = post.comments_count() return xmpp_template('post', post_id=post_id, author=post.author.login, type=post.type, private=post.private, title=post.title, link=post.link, tags=post.tags, text=post.text, files=post.files, subscribed=subscribed, updates=updates, comments=comments, comments_count=cnt, archive=post.archive, rec_users=post.recommended_users())
def unsubscribe(post_id): try: posts.unsubscribe(post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except PostAuthorError: return xmpp_template('post_denied', post_id=post_id) return xmpp_template('post_unsub_ok', post_id=post_id)
def del_from_blacklist(taglist, login=None): taglist = parse_tags(taglist) try: tags.del_from_blacklist(taglist, login) except UserNotFound: return xmpp_template('user_not_found', login=login) return xmpp_template('blacklist_updated')
def unsubscribe(taglist, login=None): taglist = parse_tags(taglist) try: tags.unsubscribe(taglist, login) except UserNotFound: return xmpp_template('user_not_found', login=login) return xmpp_template('tags_unsub_ok', login=login, tags=taglist)
def delete_post(post_id): try: posts.delete_post(post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except PostAuthorError: return xmpp_template('post_del_denied', post_id=post_id) return xmpp_template('post_del', post_id=post_id)
def del_recipients(post_id, to): to = parse_logins(to) try: posts.del_recipients(post_id, to) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except PostAuthorError: return xmpp_template('post_denied', post_id=post_id) except UserNotFound, e: return xmpp_template('user_not_found', login=e.message)
def update_post(post_id, text): try: posts.update_post(post_id, text) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except (SubscribeError, PostAuthorError): return xmpp_template('post_denied', post_id=post_id) except PostUpdateError: return xmpp_template('post_upd_err', post_id=post_id) return xmpp_template('post_upd', post_id=post_id)
def subscribe(taglist, login=None): taglist = parse_tags(taglist) try: tags.subscribe(taglist, login) except UserNotFound: return xmpp_template('user_not_found', login=login) except SubscribeError: return xmpp_template('sub_denied', login=login) return xmpp_template('tags_sub_ok', login=login, tags=taglist)
def add_to_blacklist(taglist, login=None): taglist = parse_tags(taglist) try: tags.add_to_blacklist(taglist, login) except UserNotFound: return xmpp_template('user_not_found', login=login) except SubscribeError: return xmpp_template('bl_denied', login=login) return xmpp_template('blacklist_updated')
def subscribe(post_id): try: post = posts.subscribe(post_id) cnt = post.comments_count() except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except (PostAuthorError, SubscribeError): return xmpp_template('post_denied', post_id=post_id) except AlreadySubscribed: return xmpp_template('post_sub_already', post_id=post_id) return xmpp_template('post_sub_ok', post_id=post_id, comments=cnt)
def add_post(text, taglist=None, to=None, private=False): taglist = parse_tags(taglist) if to: text = '%s %s' % (to.strip(), text.strip()) to = parse_logins(to) try: post_id = posts.add_post(text, tags=taglist, to=to, private=private) except PostTextError: return xmpp_template('post_inadmissible') except UserNotFound, e: return xmpp_template('user_not_found', login=e.message)
def edit_post(post_id, text, taglist=None, private=None): taglist = parse_tags(taglist) try: posts.edit_post(post_id, text, taglist, private) except (SubscribeError, PostAuthorError): return xmpp_template('post_denied', post_id=post_id) except PostUpdateError: return xmpp_template('post_upd_err', post_id=post_id) except PostCommentedError: return xmpp_template('post_commented_err', post_id=post_id) except PostDiffError: return xmpp_template('post_diff_err', post_id=post_id) return xmpp_template('post_upd', post_id=post_id)
def show_post(post_id, last=False, all=False, show=False, offset=None, limit=None): try: post = posts.show_post(post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except (SubscribeError, PostAuthorError): return xmpp_template('post_denied', post_id=post_id) subscribed = post.check_subscriber(env.user) if env.user.id else False updates = [] for i, upd in enumerate(post.updates()): updates.append({'no': i+1, 'text': upd['text']}) comments = [] posts.clear_unread_posts(post_id) if last or all or show: if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 res = post.comments(last=last, all=all, offset=offset, limit=limit) for c in res: comments.append({ 'comment_id': c.id, 'to_comment_id': c.to_comment_id, 'comment_author': c.author.login, 'comment_text': c.text, 'is_rec': c.is_rec, 'files': c.files }) cnt = None posts.clear_unread_comments(post_id, map(lambda c: c.id, res)) else: cnt = post.comments_count() return xmpp_template('post', post_id=post_id, author=post.author.login, type=post.type, private=post.private, title=post.title, link=post.link, tags=post.tags, text=post.text, tune=post.tune, files=post.files, subscribed=subscribed, updates=updates, comments=comments, comments_count=cnt, archive=post.archive, rec_users=post.recommended_users())
def unsubscribe(post_id): """Unsubscribe from certain post. If user is not subscribed to this post, return "You are not subscribed to #post_id", else return "You've sucessfully unsubscribed..." """ try: if posts.check_subscribe_to(post_id): posts.unsubscribe(post_id) return xmpp_template('post_unsub_ok', post_id=post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except PostAuthorError: return xmpp_template('post_denied', post_id=post_id) return xmpp_template('post_not_subscribed', post_id=post_id)
def subscribe(login, rec): """Subscribe to user """ if rec: fn = users.subscribe_rec else: fn = users.subscribe try: if not fn(login): return xmpp_template('sub_req_sent', login=login) except UserNotFound: return xmpp_template('user_not_found', login=login) except AlreadySubscribed: if rec: return xmpp_template('sub_rec_already', login=login) else: return xmpp_template('sub_already', login=login) except AlreadyRequested: return xmpp_template('sub_req_already', login=login) except SubscribeError: return xmpp_template('sub_denied', login=login) if rec: return xmpp_template('sub_rec_ok', login=login) else: return xmpp_template('sub_ok', login=login)
def add_comment(post_id, to_comment_id, text): try: comment_id = posts.add_comment(post_id, to_comment_id, text) except PostTextError: return xmpp_template('post_inadmissible') except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except CommentNotFound: return xmpp_template('comment_not_found', post_id=post_id, comment_id=to_comment_id) except (PostAuthorError, SubscribeError): return xmpp_template('post_denied', post_id=post_id) return xmpp_template('msg_comment_sent', post_id=post_id, comment_id=comment_id)
def register(login, password=None): """Register a new user """ return 'Registration is temporarily available on the web site only. Please follow: https://point.im.register .' if env.user.id: return xmpp_template('reg_already', login=env.user.login) if not validate_nickname(login): return xmpp_template('reg_invalid') sess = env.user.session() if not sess.data(): return reg_steps(login, password)
def show_comment(post_id, comment_id): try: comment = posts.show_comment(post_id, comment_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except CommentNotFound: return xmpp_template('comment_not_found', post_id=post_id, comment_id=comment_id) except PostAuthorError: return xmpp_template('post_denied', id=post_id) return xmpp_template('comment', post_id=post_id, comment_id=comment_id, to_comment_id=comment.to_comment_id, author=comment.author.login, text=comment.text, files=comment.files)
def set_param(param, value): """Set profile/info parameter, add/remove account """ value = value.strip() try: if re.search(r'[^a-z0-9_\.]', param, re.I): raise KeyError #if param in ('passwd', 'password'): # env.user.set_password(value) elif param.startswith('info.') and param[5:] in fields['info']: env.user.set_info(param[5:], value) elif param in fields['account']: if value.startswith('-'): return del_account(param, value[1:].strip()) if value.startswith('+'): value = value[1:] return add_account(param, value.strip()) else: env.user.set_profile(param, value) env.user.save() except ValueError, e: v = e.message if e.message else value return xmpp_template('profile_value_err', value=v)
def add_to_whitelist(logins): logins = re.split(r'[\s@,]+', logins.strip(' \t@')) added = [] already = [] denied = [] not_found = [] for login in logins: try: if users.add_to_whitelist(login): added.append(login) else: already.append(login) except SubscribeError: denied.append(login) except UserNotFound: not_found.append(login) except AlreadySubscribed: already.append(login) return xmpp_template('wl_updated', added=added, already=already, denied=denied, not_found=not_found)
def unpin_post(post_id): try: post = Post(id) if env.user.id == post.author.id: if post.pinned: post.set_pinned(False) return xmpp_template('post_unpinned', post_id=post_id) else: raise PostNotPinnedError else: raise PostAuthorError except PostAuthorError: return xmpp_template('post_denied', post_id=post_id) except PostNotPinnedError: return xmpp_template('post_not_pinned', post_id=post_id) except PostNotFound: return xmpp_template('post_not_found', post_id=post_id)
def unsubscribe(login, rec): """Unsubscribe from user """ try: if login == env.user.login: raise SubscribeError if rec: if users.check_subscribe_to_user_rec(login): fn = users.unsubscribe_rec else: return xmpp_template('sub_rec_not', login=login) else: if users.check_subscribe_to_user(login): fn = users.unsubscribe else: return xmpp_template('sub_not', login=login) if not fn(login): if rec: return xmpp_template('sub_unsub_rec_ok', login=login) else: return xmpp_template('sub_unsub_ok', login=login) except UserNotFound: return xmpp_template('user_not_found', login=login) except SubscribeError: if rec: return xmpp_template('sub_unsub_rec_error', login=login) else: return xmpp_template('sub_unsub_error', login=login)
def recommend(post_id, comment_id=None, text=None): try: try: posts.recommend(post_id, comment_id, text) return xmpp_template('recommendation_sent', post_id=post_id, comment_id=comment_id) except RecommendationExists: try: posts.unrecommend(post_id, comment_id) return xmpp_template('recommendation_cancel_sent', post_id=post_id, comment_id=comment_id) except RecommendationNotFound: pass except PostNotFound: return xmpp_template('post_not_found', post_id=post_id) except CommentNotFound: return xmpp_template('comment_not_found', post_id=post_id, comment_id=comment_id) except PostReadonlyError: return xmpp_template('post_readonly', post_id=post_id) except RecommendationError: return xmpp_template('post_recommend_denied', post_id=post_id) except PostAuthorError: return xmpp_template('post_denied', post_id=post_id)
def unsubscribe(login, rec): """Unsubscribe from user """ if rec: fn = users.unsubscribe_rec else: fn = users.unsubscribe try: if not fn(login): if rec: return xmpp_template('sub_rec_not', login=login) else: return xmpp_template('sub_not', login=login) except UserNotFound: return xmpp_template('user_not_found', login=login) except SubscribeError: if rec: return xmpp_template('sub_rec_not', login=login) else: return xmpp_template('sub_not', login=login) if rec: return xmpp_template('sub_unsub_rec_ok', login=login) else: return xmpp_template('sub_unsub_ok', login=login)
def get_param(param): """Set profile/info parameter, add/remove account """ if param in ('passwd', 'password'): return xmpp_template('password') if param in ACCOUNT_TYPES: active = env.user.get_active_account(param) accounts = env.user.get_accounts(param) unconfirmed = env.user.get_unconfirmed_accounts(param) return xmpp_template('accounts', type=param, accounts=accounts, unconfirmed=unconfirmed, active=active) try: if re.search(r'[^a-z0-9_\.]', param, re.I): raise KeyError return {'body': '%s = %s' % (param, env.user.get_profile(param))} except KeyError: return xmpp_template('profile_param_err', param=param)
def subscriptions(): fn = lambda u: u.login.lower() subs = sorted(env.user.subscriptions(), key=fn) subscribers = sorted(env.user.subscribers(), key=fn) in_req = sorted(env.user.incoming_subscription_requests(), key=fn) out_req = sorted(env.user.outgoing_subscription_requests(), key=fn) tags = sorted(env.user.tag_subscriptions(), key=lambda t: t['login'].lower()) return xmpp_template('subscriptions', subscriptions=subs, subscribers=subscribers, in_req=in_req, out_req=out_req, tags=tags)
def tag_posts(tag, login=None, show=None, offset=None, limit=None): if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 try: author = User('login', login) if login else None except UserNotFound, e: return xmpp_template('user_not_found', login=e.message)
def bookmarks(show=None, offset=None, limit=None): if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 plist = posts.bookmarks(offset=offset, limit=limit) plist.reverse() return xmpp_template('posts', posts=plist)
def all_posts(show=None, offset=None, limit=None): if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 plist = posts.select_posts(offset=offset, limit=limit, private=False, blacklist=True) plist.reverse() return xmpp_template('posts', posts=plist)
def all_posts(show=None, offset=None, limit=None): if offset: offset = int(offset) if limit: limit = int(limit) else: limit = 10 plist = posts.select_posts(offset=offset, limit=limit, private=False) plist.reverse() return xmpp_template('posts', posts=plist)
def search_posts(login=None, taglist=None, text=None, offset=None, limit=None): taglist = parse_tags(taglist) if text: text = text.strip() offset = int(offset) if offset else 0 limit = int(limit) if limit else 10 results, has_next, total = search.search_posts(text, offset=offset, limit=limit) return xmpp_template('search', text=text, results=results, has_next=has_next, limit=limit, offset=offset, total=total)
def private_posts(show=False, offset=None, limit=None): if offset: offset = int(offset) if limit: limit = int(limit) else: offset = 0 limit = 10 plist = posts.private_unread(offset=offset, limit=limit) if not plist: plist = posts.private_incoming(offset=offset, limit=limit) plist.reverse() return xmpp_template('posts', posts=plist)