Пример #1
0
    def read(self, path, size, offset, fh=None):
        """
        Is used to get contents of posts, comments, etc from reddit to the end
        user.
        """
        path_split = path.split('/')
        path_len = len(path_split)

        if path_split[1] == 'r' and path_len == 5:
            # Get the post
            post_id = path_split[3].split(' ')[-1]
            post = reddit.get_submission(submission_id=post_id)

            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_sub_content(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_submission(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif (path_split[-1] == 'thumbnail' and post.thumbnail != ''
                  and post.thumbnail != 'self'
                  and post.thumbnail != 'default'):
                f = urllib2.urlopen(post.thumbnail)
                if f.getcode() == 200:
                    formatted = f.read()
            elif path_split[-1] == 'raw_content' and post.selftext:
                formatted = post.selftext.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content' and post.url:
                formatted = post.url.encode('ascii', 'ignore')
            elif path_split[-1] == 'link_content' and post.url:
                f = urllib2.urlopen(post.url)
                if f.getcode() == 200:
                    formatted = f.read()
            return formatted[offset:offset + size]
        elif path_split[1] == 'r' and path_len > 5:
            # Get the comment
            post = get_comment_obj(path)
            if path_split[-1] == 'content':
                formatted = format.format_comment(post, recursive=False)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_comment(post, recursive=True)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content':
                formatted = post.body.encode('ascii', 'ignore')
            return formatted[offset:offset + size]

        return -errno.ENOSYS
Пример #2
0
    def read(self, path, size, offset, fh=None):
        """
        Is used to get contents of posts, comments, etc from reddit to the end
        user.
        """
        path_split = path.split('/')
        path_len = len(path_split)

        if path_split[1] == 'r' and path_len == 5:
            # Get the post
            post_id = path_split[3].split(' ')[-1]
            post = reddit.get_submission(submission_id=post_id)

            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_sub_content(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_submission(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif (path_split[-1] == 'thumbnail' and post.thumbnail != '' and
                    post.thumbnail != 'self' and post.thumbnail != 'default'):
                f = urllib2.urlopen(post.thumbnail)
                if f.getcode() == 200:
                    formatted = f.read()
            elif path_split[-1] == 'raw_content' and post.selftext:
                formatted = post.selftext.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content' and post.url:
                formatted = post.url.encode('ascii', 'ignore')
            elif path_split[-1] == 'link_content' and post.url:
                f = urllib2.urlopen(post.url)
                if f.getcode() == 200:
                    formatted = f.read()
            return formatted[offset:offset+size]
        elif path_split[1] == 'r' and path_len > 5:
            # Get the comment
            post = get_comment_obj(path)
            if path_split[-1] == 'content':
                formatted = format.format_comment(post, recursive=False)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_comment(post, recursive=True)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content':
                formatted = post.body.encode('ascii', 'ignore')
            return formatted[offset:offset+size]

        return -errno.ENOSYS
Пример #3
0
def review_comment(id):
    if not re.match(r'^[\da-f]+$', id):
        return flask.make_response('Invalid ID', 500)

    path = os.path.join(get_queue_dir(), id)
    with open(path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    if flask.request.method == 'GET':
        if data.get('type') == 'mention':
            try:
                validate_mention(data)
                with open(path, 'w', encoding='utf-8') as file:
                    json.dump(data, file, ensure_ascii=False)
            except:
                data['error'] = traceback.format_exc()

        return flask.render_template('review_comment.html',
                                     baseurl=config.get('site', 'baseurl'),
                                     **data)

    if 'approve' in flask.request.form or 'reject' in flask.request.form:
        approved = 'approve' in flask.request.form
        reply = flask.request.form.get('reply', '').strip()
        reply_html = format_comment(reply) if reply else ''

        if approved:
            data['comment_id'] = save_comment(data, reply_html)

        os.unlink(path)

        if reply_html and 'email_reply' in flask.request.form and data['email']:
            sender = config.get('mail', 'sender')
            send_mail('comment_reply.mail',
                      sender,
                      data['email'],
                      reply=reply_html,
                      approved=approved,
                      **data)
        if approved:
            return flask.make_response('Comment has been approved.', 200)
        else:
            return flask.make_response('Comment has been rejected.', 200)
    else:
        return flask.make_response('Invalid request', 500)
Пример #4
0
def submit_comment():
    if flask.request.method == 'OPTIONS':
        return flask.make_response('', 200)

    if 'X-XMLHttpRequest' not in flask.request.headers:
        return flask.jsonify({
            'error':
            True,
            'message':
            'X-XMLHttpRequest header missing from request.'
        })

    name = flask.request.form.get('name', '').strip()
    if not name:
        return flask.jsonify({'error': True, 'message': 'Name is mandatory.'})

    email = flask.request.form.get('email', '').strip()
    if email and (not '@' in email or re.search(r'\s', email)):
        return flask.jsonify({'error': True, 'message': 'Invalid email.'})

    web = flask.request.form.get('web', '').strip()
    if web and not re.match(r'^https?://\S+$', web):
        return flask.jsonify({'error': True, 'message': 'Invalid website.'})

    message = flask.request.form.get('message', '').strip()
    if not message:
        return flask.jsonify({
            'error': True,
            'message': 'Comment message is mandatory.'
        })
    message_html = format_comment(message)

    uri = flask.request.form.get('uri', '').strip()
    if not uri or not uri.startswith('/') or re.search(r'\s', uri):
        return flask.jsonify({
            'error': True,
            'message': 'Article URI not specified or invalid.'
        })

    article, title = get_article_path(uri)
    if not article:
        return flask.jsonify({
            'error': True,
            'message': 'Could not find article path.'
        })

    id = secrets.token_hex(32)
    data = {
        'id': id,
        'date': datetime.datetime.utcnow().isoformat(' ', 'seconds'),
        'name': name,
        'email': email,
        'web': web,
        'message': message_html,
        'uri': uri,
        'article': article,
        'title': title,
    }

    with open(os.path.join(get_queue_dir(), id), 'w',
              encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False)

    sender = config.get('mail', 'sender')
    send_mail('new_comment.mail', sender, sender, **data)
    return flask.jsonify({
        'error':
        False,
        'message':
        'Your comment has been submitted and awaits moderation.'
    })
Пример #5
0
    def getattr(self, path):
        """
        Returns stat info for file, such as permissions and access times.
        """

        # default nlink and time info
        st = fuse.Stat()
        st.st_nlink = 2
        st.st_atime = int(time.time())
        st.st_mtime = st.st_atime
        st.st_ctime = st.st_atime

        # everything defaults to being a normal file unless explicitly set
        # otherwise
        st.st_mode = stat.S_IFREG | 0444

        # useful information
        path_split = path.split('/')
        path_len = len(path_split)

        # "." and ".."
        if path_split[-1] == '.' or path_split[-1] == '..':
            # . and ..
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # top-level directories
        if path in ['/', '/u', '/r']:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/ - subreddits
        if path_split[1] == 'r' and path_len == 3:
            # check for .sub directories for subscribing
            if reddit.is_logged_in():
                if path.split('/')[-1:][0][-4:] == '.sub':
                    my_subs = [
                        sub.display_name.lower()
                        for sub in reddit.get_my_subreddits()
                    ]
                    if (path.split('/')[-1:][0][:-4]).lower() not in my_subs:
                        st = -2
                    else:
                        st.st_mode = stat.S_IFDIR | 0555
                else:
                    st.st_mode = stat.S_IFDIR | 0555
            else:
                # normal subreddit
                st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/* - submissions
        if path_split[1] == 'r' and path_len == 4:
            if path_split[-1] == 'post':
                # file to post a submission
                st.st_mode = stat.S_IFREG | 0666
            else:
                # submission
                st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/*/[vote, etc] - content stuff in submission
        if (path_split[1] == 'r' and path_len == 5
                and path_split[-1] in content_stuff):
            st.st_mode = stat.S_IFREG | 0444
            post_id = path_split[3].split(' ')[-1]
            post = reddit.get_submission(submission_id=post_id)
            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_sub_content(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_submission(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif (path_split[-1] == 'thumbnail' and 'thumbnail' in dir(post)
                  and post.thumbnail != '' and post.thumbnail != 'self'
                  and post.thumbnail != 'default'):
                f = urllib2.urlopen(post.thumbnail)
                if f.getcode() == 200:
                    formatted = f.read()
            elif path_split[-1] == 'reply':
                st.st_mode = stat.S_IFREG | 0666
            elif path_split[-1] == 'raw_content' and post.selftext:
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.selftext.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content' and post.url:
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.url.encode('ascii', 'ignore')
            elif path_split[-1] == 'link_content' and post.url:
                f = urllib2.urlopen(post.url)
                if f.getcode() == 200:
                    formatted = f.read()
            st.st_size = len(formatted)
            return st

        # r/*/*/** - comment post
        if (path_split[1] == 'r' and path_len > 4
                and path_split[-1] not in content_stuff
                and path.split('/')[-1:][0][-1:] != '_'):
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # user link
        if (path_split[1] == 'r' and path_len > 4
                and path_split[-1] not in content_stuff
                and path.split('/')[-1:][0][-1:] == '_'):
            st.st_mode = stat.S_IFLNK | 0777
            return st

        # r/*/*/** - comment directory
        if (path_split[1] == 'r' and path_len > 5
                and path_split[-1] not in content_stuff):
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/*/** - comment stuff
        if (path_split[1] == 'r' and path_len > 5
                and path_split[-1] in content_stuff):
            st.st_mode = stat.S_IFREG | 0444
            post = get_comment_obj(path)
            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_comment(post, recursive=False)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_comment(post, recursive=True)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'reply':
                st.st_mode = stat.S_IFREG | 0666
            elif path_split[-1] == 'raw_content':
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.body.encode('ascii', 'ignore')
            st.st_size = len(formatted)
            return st

        # u/* - user
        if path_split[1] == 'u' and path_len == 3:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # u/*/* - user stuff (comments, submitted, etc)
        if path_split[1] == 'u' and path_len == 4:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # u/*/*/* - links (comment, submitted, etc)
        elif (path_split[1] == 'u' and path_len == 5):
            st.st_mode = stat.S_IFLNK | 0777
            return st
Пример #6
0
    def getattr(self, path):
        """
        Returns stat info for file, such as permissions and access times.
        """

        # default nlink and time info
        st = fuse.Stat()
        st.st_nlink = 2
        st.st_atime = int(time.time())
        st.st_mtime = st.st_atime
        st.st_ctime = st.st_atime

        # everything defaults to being a normal file unless explicitly set
        # otherwise
        st.st_mode = stat.S_IFREG | 0444

        # useful information
        path_split = path.split('/')
        path_len = len(path_split)

        # "." and ".."
        if path_split[-1] == '.' or path_split[-1] == '..':
            # . and ..
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # top-level directories
        if path in ['/', '/u', '/r']:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/ - subreddits
        if path_split[1] == 'r' and path_len == 3:
            # check for .sub directories for subscribing
            if reddit.is_logged_in():
                if path.split('/')[-1:][0][-4:] == '.sub':
                    my_subs = [sub.display_name.lower() for sub in
                               reddit.get_my_subreddits()]
                    if (path.split('/')[-1:][0][:-4]).lower() not in my_subs:
                        st = -2
                    else:
                        st.st_mode = stat.S_IFDIR | 0555
                else:
                    st.st_mode = stat.S_IFDIR | 0555
            else:
                # normal subreddit
                st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/* - submissions
        if path_split[1] == 'r' and path_len == 4:
            if path_split[-1] == 'post':
                # file to post a submission
                st.st_mode = stat.S_IFREG | 0666
            else:
                # submission
                st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/*/[vote, etc] - content stuff in submission
        if (path_split[1] == 'r' and path_len == 5 and path_split[-1] in
                content_stuff):
            st.st_mode = stat.S_IFREG | 0444
            post_id = path_split[3].split(' ')[-1]
            post = reddit.get_submission(submission_id=post_id)
            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_sub_content(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_submission(post)
                formatted = formatted.encode('ascii', 'ignore')
            elif (path_split[-1] == 'thumbnail' and 'thumbnail' in dir(post)
                    and post.thumbnail != '' and post.thumbnail != 'self'
                    and post.thumbnail != 'default'):
                f = urllib2.urlopen(post.thumbnail)
                if f.getcode() == 200:
                    formatted = f.read()
            elif path_split[-1] == 'reply':
                st.st_mode = stat.S_IFREG | 0666
            elif path_split[-1] == 'raw_content' and post.selftext:
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.selftext.encode('ascii', 'ignore')
            elif path_split[-1] == 'raw_content' and post.url:
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.url.encode('ascii', 'ignore')
            elif path_split[-1] == 'link_content' and post.url:
                f = urllib2.urlopen(post.url)
                if f.getcode() == 200:
                    formatted = f.read()
            st.st_size = len(formatted)
            return st

        # r/*/*/** - comment post
        if (path_split[1] == 'r' and path_len > 4 and path_split[-1] not in
                content_stuff and path.split('/')[-1:][0][-1:] != '_'):
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # user link
        if (path_split[1] == 'r' and path_len > 4 and path_split[-1] not in
                content_stuff and path.split('/')[-1:][0][-1:] == '_'):
            st.st_mode = stat.S_IFLNK | 0777
            return st

        # r/*/*/** - comment directory
        if (path_split[1] == 'r' and path_len > 5 and path_split[-1] not in
                content_stuff):
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # r/*/*/** - comment stuff
        if (path_split[1] == 'r' and path_len > 5 and path_split[-1] in
                content_stuff):
            st.st_mode = stat.S_IFREG | 0444
            post = get_comment_obj(path)
            formatted = ''
            if path_split[-1] == 'content':
                formatted = format.format_comment(post, recursive=False)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'votes':
                formatted = str(post.score) + '\n'
            elif path_split[-1] == 'flat':
                formatted = format.format_comment(post, recursive=True)
                formatted = formatted.encode('ascii', 'ignore')
            elif path_split[-1] == 'reply':
                st.st_mode = stat.S_IFREG | 0666
            elif path_split[-1] == 'raw_content':
                st.st_mode = stat.S_IFREG | 0666
                formatted = post.body.encode('ascii', 'ignore')
            st.st_size = len(formatted)
            return st

        # u/* - user
        if path_split[1] == 'u' and path_len == 3:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # u/*/* - user stuff (comments, submitted, etc)
        if path_split[1] == 'u' and path_len == 4:
            st.st_mode = stat.S_IFDIR | 0555
            return st

        # u/*/*/* - links (comment, submitted, etc)
        elif (path_split[1] == 'u' and path_len == 5):
            st.st_mode = stat.S_IFLNK | 0777
            return st