示例#1
0
def links(request, listname):

    try:
        user = request.user.get_profile()
        mlist = mailinglist.find_list(listname)
        _authorize_or_raise(user, mlist)
        alllinks = mlist.link_set.all().order_by('-created_on')
        paginator = Paginator(alllinks, 15) #sets links per page

        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1

        try:
            links = paginator.page(page)
        except (EmptyPage, InvalidPage):
            links = paginator.page(paginator.num_pages)
            
    except ValueError:
        raise Http404()

    return render_to_response('postosaurus/links.html', {
            'mlist': mlist, 
            'links': links,
            'linktab' : True,
            }, context_instance = RequestContext(request))
示例#2
0
def test_gzip_attachment():

    clear_queue()
    
    # set up list and message
    name, address = parseaddr(gzip_msg['from'])
    client = RouterConversation(address, 'Files Tests')
    client.begin()
    mlist = mailinglist.find_list(list_name)
    test_subscribe_user(sender=address, client=client, mlist=mlist)
    
    test_subscribe_user(sender=parseaddr(sender)[1], client=sender_client, mlist=mlist)
    gzip_msg['to'] = list_addr

    # deliver the message.
    clear_queue()
    Router.deliver(gzip_msg)

    assert len(mlist.message_set.all()) == 1
    msg = mlist.message_set.all()[0]
    
    assert len(msg.file_set.all()) == 1
    attached = msg.file_set.all()[0]
    path = os.path.join(attached.pathprefix, attached.sha)
    assert attached.name in os.listdir(path)
    assert queue().count() == 1, "Should be 1 message in queue, but there are %s" % queue().count()
示例#3
0
def POSTING(message, list_name=None, id_number=None, host=None):

    """
    Takes a message and posts it to the rest of the group. If there
    are multiple email addresses in the To or CC field, those emails
    will be added to the list.

    We also ensure that they don't receive a duplicate of the
    email they were just sent.
    """

    #an existing user is adding themselves to another group.
    if id_number:
        START(message, list_name=list_name, id_number=id_number, host=None)

    else:

        list_addr = "%s@%s" % (list_name, host)
        if mailinglist.is_subscribed(message['from'], list_name) and mailinglist.is_active(list_name):
            mlist = mailinglist.find_list(list_name)

        #send a request for confirmation to anyone cc'd on this list so they can
        #join the group if they want.    
            allrecpts = mailinglist.all_recpts(message)
            for address in [to for to in allrecpts if not to.endswith(host)]:
                CONFIRM.send_if_not_subscriber(relay, mlist, 'confirm', address, 'postosaurus/join-confirmation.msg', host)

            delivery = mailinglist.craft_response(message, list_name, list_addr)
            mailinglist.post_message(relay, message, delivery, list_name, host, message['from'])

            q = queue.Queue("run/work")
            q.push(message)

    return POSTING
示例#4
0
def START(message, list_name=None, host=None, bad_list=None):
    if bad_list:
        if '-' in bad_list:
            # probably put a '.' in it, try to find a similar list
            similar_lists = mailinglist.similar_named_lists(bad_list.replace('-','.'))
        else:
            similar_lists = mailinglist.similar_named_lists(bad_list)

        help = view.respond(locals(), "mail/bad_list_name.msg",
                            From="noreply@%(host)s",
                            To=message['from'],
                            Subject="That's not a valid list name.")
        relay.deliver(help)

        return START

    elif list_name in INVALID_LISTS or message['from'].endswith(host):
        logging.debug("LOOP MESSAGE to %r from %r.", message['to'],
                     message['from'])
        return START

    elif mailinglist.find_list(list_name):
        action = "subscribe to"
        CONFIRM.send(relay, list_name, message, 'mail/confirmation.msg',
                          locals())
        return CONFIRMING_SUBSCRIBE

    else:
        similar_lists = mailinglist.similar_named_lists(list_name)
        CONFIRM.send(relay, list_name, message, 'mail/create_confirmation.msg',
                          locals())

        return CONFIRMING_SUBSCRIBE
示例#5
0
文件: admin.py 项目: wRAR/lamson
def START(message, list_name=None, host=None, bad_list=None):
    if bad_list:
        if '-' in bad_list:
            # probably put a '.' in it, try to find a similar list
            similar_lists = mailinglist.similar_named_lists(
                bad_list.replace('-', '.'))
        else:
            similar_lists = mailinglist.similar_named_lists(bad_list)

        help = view.respond(locals(),
                            "mail/bad_list_name.msg",
                            From="noreply@%(host)s",
                            To=message['from'],
                            Subject="That's not a valid list name.")
        relay.deliver(help)

        return START

    elif list_name in INVALID_LISTS or message['from'].endswith(host):
        logging.debug("LOOP MESSAGE to %r from %r.", message['to'],
                      message['from'])
        return START

    elif mailinglist.find_list(list_name):
        action = "subscribe to"
        CONFIRM.send(relay, list_name, message, 'mail/confirmation.msg',
                     locals())
        return CONFIRMING_SUBSCRIBE

    else:
        similar_lists = mailinglist.similar_named_lists(list_name)
        CONFIRM.send(relay, list_name, message, 'mail/create_confirmation.msg',
                     locals())

        return CONFIRMING_SUBSCRIBE
示例#6
0
def test_link_not_added():
    """
    Check query for determining whether a link has already been added
    or not.
    """

    mlist = mailinglist.find_list(list_name)
    assert links.not_added(mlist, url)
    test_add_link()
    assert links.not_added(mlist, url) == False
def test_get_messages():

    test_add_message()
    mlist = mailinglist.find_list(list_name)
    dbmessage = Message.objects.filter(mlist=mlist).all()[0]
    key = str(dbmessage.id)
    messages = archive.get_messages([key])
    assert len(messages) == 1
    rmessage = archive.get_message(key)
    assert json.dumps(rmessage, sort_keys=True) == archive.to_json(message.base)
示例#8
0
def test_add_link():

    """
    Add a link.
    """
    
    mlist = mailinglist.find_list(list_name)
    message = Message.objects.filter(mlist=mlist).all()[0]
    links.add_link(list_name, url, message)
    assert len(Link.objects.all()) == 1
示例#9
0
def test_add_existing_link():
    
    """
    Try to add a link that has already been added.
    """

    test_add_link()
    assert len(Link.objects.all()) == 1
    mlist = mailinglist.find_list(list_name)
    message = Message.objects.filter(mlist=mlist).all()[0]
    links.add_link(list_name, url, message)
    assert len(Link.objects.all()) == 1
示例#10
0
def store_file(list_name, message, filename, dbmessage):

    """
    Given a list name and a filename store the attached file
    in the filesystem and create a db record to track relationships,
    (like this file was sent with this message, etc).
    """

    mlist = mailinglist.find_list(list_name)
    name, addr = parseaddr(message['from'])
    user = mailinglist.find_user(addr)
    sha = create_hash_from_msg(message, filename)
    dbfile = File(mlist = mlist,
                  message = dbmessage,
                  user = user,
                  sha = sha,
                  name = filename,
                  ext = os.path.splitext(filename)[1])
    dbfile.save()

    try:
        # try to just open the file and create it, assumes the directories already
        # exist, which they may not.
        pfile = open(dbfile.local_path(), 'w')
        pfile.write(file_text(message, filename))
        pfile.close()
    except IOError:
        # The directories haven't been created yet, go
        # through and make 'em.

        path = ""
        for part in dbfile.local_path().split("/")[:-1]:
            path = os.path.join(path, part)
            if not os.path.isdir(path):
                os.mkdir(path)

        #try again. Code duplicated to avoid performance hit checking to see if directory exists every
        #single time. This only needs to be done once per group.
        pfile = open(dbfile.local_path(), 'w')
        pfile.write(file_text(message, filename))
        pfile.close()
        
        if os.path.islink(dbfile.recent_local_path()):
            os.remove(dbfile.recent_local_path())
        os.symlink(os.path.join(dbfile.sha, dbfile.name), dbfile.recent_local_path())

    return dbfile
示例#11
0
def test_two_attachments():
    name, address = parseaddr(deneen_msg['from'])
    client = RouterConversation(address, 'Files Tests')
    test_subscribe_user(sender=address, client=client)
    Router.deliver(two_msg)
    mlist = mailinglist.find_list(list_name)
    assert len(mlist.message_set.all()) == 1

    msg = mlist.message_set.all()[0]
    assert len(msg.file_set.all()) == 2
    attached = msg.file_set.all()[0]
    path = os.path.join(attached.pathprefix, attached.sha)
    assert os.listdir(path)[0] == attached.name

    attached = msg.file_set.all()[1]
    path = os.path.join(attached.pathprefix, attached.sha)
    assert os.listdir(path)[0] == attached.name
示例#12
0
def archive_by_day(request, listname, month, day, year):
    month = int(month)
    day = int(day)
    year = int(year)
    mlist = mailinglist.find_list(listname)
    user = request.user.get_profile()
    _authorize_or_raise(user, mlist)

    messages = [CleanMessage(msg) for msg in \
                    archive.messages_by_day(listname, year, month, day)]

    return render_to_response('postosaurus/archivebyday.html', {
            'mlist': mlist,
            'messages': messages,
            'date' : datetime(year, month, day),
            'archivetab' : True,
            }, context_instance = RequestContext(request))
示例#13
0
def store_message(list_name, message):

    """
    Creates entries in key value store so a message can be
    retrieved and days that have messages for a list and messages
    for each of those days. 

    Messages should never be stored directly in the archive or they
    will never be able to be retrieved. Messages should only
    be archived using this function.
    """

    mlist = mailinglist.find_list(list_name)
    dbmessage = Message(mlist=mlist, subject=message['Subject'])
    dbmessage.save()
    mjson = to_json(message.base)
    archive[str(dbmessage.id)] = mjson
    return dbmessage
示例#14
0
    def clean(self, list_name):

        """
        Postosaurus only accepts list names that have alphanumeric
        characters and a period.
        """

        if not list_name:
            raise forms.ValidationError('You must provide a name for your list.')
        
        if not mailinglist.valid_name(list_name):
            raise forms.ValidationError('List names are valid email addresses that contain letters, numbers and periods. e.g. awesome.list3')

        mlist = mailinglist.find_list(list_name)
        if mlist:
            raise forms.ValidationError('That list name has already been taken.')

        return list_name
示例#15
0
def tasks(request, listname):

    try:
        profile = request.user.get_profile()
        mlist = mailinglist.find_list(listname)
        _authorize_or_raise(profile, mlist)
            
    except ValueError:
        raise Http404()

    if request.method == 'POST':
        feature = Request(email=profile.email,
                          links=False,
                          files=False,
                          tasks=True)
        feature.save()

    return render_to_response('postosaurus/tasks.html', {
            'profile' : profile,
            'mlist': mlist, 
            'taskstab' : True,
            }, context_instance = RequestContext(request))
示例#16
0
def archive_overview(request, listname):

    try:
        mlist = mailinglist.find_list(listname)
        user = request.user.get_profile()
        _authorize_or_raise(user, mlist)
        dbmessages = mlist.message_set.all().order_by('-created_on')
    except ValueError:
        raise Http404()
    
    messages = []
    for msg in dbmessages:
        month = msg.created_on.month
        day = msg.created_on.day
        year = msg.created_on.year
        url = reverse(archive_by_day, args=[listname, month, day, year])
        messages.append((msg, url))
    return render_to_response('postosaurus/archive.html', {
            'mlist': mlist,
            'messages': messages,
            'archivetab' : True,
            }, context_instance = RequestContext(request))
示例#17
0
def members(request, listname):

    user = request.user.get_profile()
    mlist = mailinglist.find_list(listname)
    _authorize_or_raise(user, mlist)

    subscriptions = mlist.subscription_set.all()
    
    if request.method == 'POST':

        if not request.POST.has_key('confirmed'):
            # not confirmed yet.
            toremove = []
            for email in request.POST.keys():
                if request.POST[email]:
                    toremove.append(email)
            if len(toremove) > 0:
                return render_to_response('postosaurus/members-confirm.html', locals(), context_instance = RequestContext(request))

        # they confirmed, now remove the members.
        toremove = [key for key in request.POST.keys() if key != 'confirmed']
        for email in toremove:
            sub = mailinglist.find_subscription(email, listname)
            if sub:
                sub.delete()

        return render_to_response('postosaurus/members.html', {
                'mlist' : mlist,
                'subscriptions' : subscriptions,
                'membertab' : True,
                }, context_instance = RequestContext(request))

    else:
        return render_to_response('postosaurus/members.html', {
                'mlist' : mlist,
                'subscriptions' : subscriptions,
                'membertab' : True,
                }, context_instance = RequestContext(request))
示例#18
0
def messages_by_day(listname, year, month, day):

    """
    Returns all messages for the given list and day.
    """

    mlist = mailinglist.find_list(listname)
    start = datetime.datetime(year, month, day)
    end = start + datetime.timedelta(hours=23, minutes=59, seconds=59)
    dbmessages = Message.objects\
        .filter(created_on__range=(start, end))\
        .filter(mlist = mlist)\
        .all()
    ids = [str(message.id) for message in dbmessages]
    messages = []
    for msg in archive.multi_get(ids):
        #The archiver sometimes screws up and misses an
        #email. multi_get returns null in that case. Do
        #this check so the json loader doesn't blow up.
        if msg:
            messages.append(json.loads(msg))
            
    return messages
示例#19
0
def START(message, list_name=None, id_number=None, host=None):
    
    """
    The start state looks for confirmation emails and move users with valid
    confirmation emails into a posting state. We also create the user's
    postosaurus account (if needed) and subscription here.

    This prevents users from being added to a list if they
    don't want to be.
    """

    mlist = mailinglist.find_list(list_name)
    if mlist:
        if CONFIRM.verify(mlist, 'confirm', message['from'], id_number):
            # Let them know they've been added.
            name, address = parseaddr(message['from'])
            CONFIRM.notify(relay, mlist, 'confirm', address)
            user = mailinglist.find_user(address)
            if not user:
                user = mailinglist.create_user(address)
            mailinglist.add_if_not_subscriber(address, list_name)
            return POSTING
    return START
示例#20
0
def test_one_attachment():

    #subscribe the original sender
    name, address = parseaddr(deneen_msg['from'])
    client = RouterConversation(address, 'Files Tests')
    test_subscribe_user(sender=address, client=client)

    # add someone else to the list
    test_subscribe_user(sender=sender, client=sender_client)

    # update the message to send to the list we just created.
    deneen_msg['to'] = list_addr

    Router.deliver(deneen_msg)
    mlist = mailinglist.find_list(list_name)
    assert len(mlist.message_set.all()) == 1
    msg = mlist.message_set.all()[0]
    
    assert len(msg.file_set.all()) == 1
    attached = msg.file_set.all()[0]
    path = os.path.join(attached.pathprefix, attached.sha)
    assert attached.name in os.listdir(path)
    assert_in_state('app.handlers.admin', deneen_msg['to'], address, 'POSTING')
示例#21
0
def add_link(list_name, url, message):
    mlist = mailinglist.find_list(list_name)
    if not_added(mlist, url):
        link = Link(mlist=mlist, url=url, message=message)
        link.save()