Example #1
0
    def pgp_validation_check(self, store):
        node_desc = db_admin_serialize_node(store, 'en')

        expired_or_expiring = []

        rcvrs = store.find(Receiver)

        for rcvr in rcvrs:

            if rcvr.pgp_key_public and rcvr.pgp_key_expiration != datetime_null(
            ):
                if rcvr.pgp_key_expiration < datetime_now():
                    expired_or_expiring.append(
                        admin_serialize_receiver(
                            rcvr, GLSettings.memory_copy.default_language))
                    if node_desc['allow_unencrypted']:
                        # The PGP key status should be downgraded only if the node
                        # accept non PGP mails/files to be sent/stored.
                        # If the node wont accept this the pgp key status
                        # will remain enabled and mail won't be sent by regular flow.
                        rcvr.pgp_key_status = u'disabled'
                elif rcvr.pgp_key_expiration < datetime_now() - timedelta(
                        days=15):
                    expired_or_expiring.append(
                        admin_serialize_receiver(
                            rcvr, GLSettings.memory_copy.default_language))

        return expired_or_expiring
Example #2
0
def load_complete_events(store, event_number=GLSetting.notification_limit):
    """
    _complete_ is explicit because do not serialize, but make an OD() of the description.

    event_number represent the amount of event that can be returned by the function,
    event to be notified are taken in account later.
    """

    node_desc = db_admin_serialize_node(store, GLSetting.defaults.language)

    event_list = []
    storedevnts = store.find(EventLogs, EventLogs.mail_sent == False)
    storedevnts.order_by(Desc(EventLogs.creation_date))

    for i, stev in enumerate(storedevnts):

        if len(event_list) == event_number:
            log.debug(
                "Maximum number of notification event reach (Mailflush) %d, after %d"
                % (event_number, i))
            break

        if not stev.description['receiver_info']['file_notification'] and \
                        stev.event_reference['kind'] == 'File':
            continue
        if not stev.description['receiver_info']['message_notification'] and \
                        stev.event_reference['kind'] == 'Message':
            continue
        if not stev.description['receiver_info']['comment_notification'] and \
                        stev.event_reference['kind'] == 'Comment':
            continue
        if not stev.description['receiver_info']['tip_notification'] and \
                        stev.event_reference['kind'] == 'Tip':
            continue

        eventcomplete = OD()

        # node level information are not stored in the node, but fetch now
        eventcomplete.notification_settings = admin_serialize_notification(
            store.find(Notification).one(),
            stev.description['receiver_info']['language'])

        eventcomplete.node_info = node_desc

        # event level information are decoded form DB in the old 'Event'|nametuple format:
        eventcomplete.receiver_info = stev.description['receiver_info']
        eventcomplete.tip_info = stev.description['tip_info']
        eventcomplete.subevent_info = stev.description['subevent_info']
        eventcomplete.context_info = stev.description['context_info']
        eventcomplete.steps_info = stev.description['steps_info']

        eventcomplete.type = stev.description['type']  # 'Tip', 'Comment'
        eventcomplete.trigger = stev.event_reference[
            'kind']  # 'plaintext_blah' ...

        eventcomplete.storm_id = stev.id

        event_list.append(eventcomplete)

    return event_list
Example #3
0
def load_complete_events(store, events_limit=GLSettings.notification_limit):
    """
    This function do not serialize, but make an OD() of the description.
    events_limit represent the amount of event that can be returned by the function,
    events to be notified are taken in account later.
    """
    node_desc = db_admin_serialize_node(store, GLSettings.defaults.language)

    event_list = []
    totaleventinqueue = store.find(EventLogs, EventLogs.mail_sent == False).count()
    storedevnts = store.find(EventLogs, EventLogs.mail_sent == False)[:events_limit * 3]

    debug_event_counter = {}
    for i, stev in enumerate(storedevnts):
        if len(event_list) == events_limit:
            log.debug("Maximum number of notification event reach (Mailflush) %d, after %d" %
                      (events_limit, i))
            break

        debug_event_counter.setdefault(stev.event_reference['kind'], 0)
        debug_event_counter[stev.event_reference['kind']] += 1

        if not stev.description['receiver_info']['tip_notification']:
            continue

        eventcomplete = OD()

        # node level information are not stored in the node, but fetch now
        eventcomplete.notification_settings = admin_serialize_notification(
            store.find(Notification).one(), stev.description['receiver_info']['language']
        )

        eventcomplete.node_info = node_desc

        # event level information are decoded form DB in the old 'Event'|nametuple format:
        eventcomplete.receiver_info = stev.description['receiver_info']
        eventcomplete.tip_info = stev.description['tip_info']
        eventcomplete.subevent_info = stev.description['subevent_info']
        eventcomplete.context_info = stev.description['context_info']

        eventcomplete.type = stev.description['type'] # 'Tip', 'Comment'
        eventcomplete.trigger = stev.event_reference['kind'] # 'blah' ...

        eventcomplete.orm_id = stev.id

        event_list.append(eventcomplete)

    if debug_event_counter:
        if totaleventinqueue > (events_limit * 3):
            log.debug("load_complete_events: %s from %d Events" %
                      (debug_event_counter, totaleventinqueue ))
        else:
            log.debug("load_complete_events: %s from %d Events, with a protection limit of %d" %
                      (debug_event_counter, totaleventinqueue, events_limit * 3 ))

    return event_list
Example #4
0
def load_complete_events(store, event_number=GLSetting.notification_limit):
    """
    _complete_ is explicit because do not serialize, but make an OD() of the description.

    event_number represent the amount of event that can be returned by the function,
    event to be notified are taken in account later.
    """

    node_desc = db_admin_serialize_node(store, GLSetting.defaults.language)

    event_list = []
    storedevnts = store.find(EventLogs, EventLogs.mail_sent == False)
    storedevnts.order_by(Asc(EventLogs.creation_date))

    debug_event_counter = {}
    for i, stev in enumerate(storedevnts):
        if len(event_list) == event_number:
            log.debug("Maximum number of notification event reach (Mailflush) %d, after %d" %
                      (event_number, i))
            break

        debug_event_counter.setdefault(stev.event_reference['kind'], 0)
        debug_event_counter[stev.event_reference['kind']] += 1

        if not stev.description['receiver_info']['tip_notification']:
            continue

        eventcomplete = OD()

        # node level information are not stored in the node, but fetch now
        eventcomplete.notification_settings = admin_serialize_notification(
            store.find(Notification).one(), stev.description['receiver_info']['language']
        )

        eventcomplete.node_info = node_desc

        # event level information are decoded form DB in the old 'Event'|nametuple format:
        eventcomplete.receiver_info = stev.description['receiver_info']
        eventcomplete.tip_info = stev.description['tip_info']
        eventcomplete.subevent_info = stev.description['subevent_info']
        eventcomplete.context_info = stev.description['context_info']
        eventcomplete.steps_info = stev.description['steps_info']

        eventcomplete.type = stev.description['type'] # 'Tip', 'Comment'
        eventcomplete.trigger = stev.event_reference['kind'] # 'plaintext_blah' ...

        eventcomplete.storm_id = stev.id

        event_list.append(eventcomplete)

    if debug_event_counter:
        log.debug("load_complete_events: %s" % debug_event_counter)

    return event_list
Example #5
0
    def pgp_validation_check(self, store):
        node_desc = db_admin_serialize_node(store, 'en')

        expired_or_expiring = []

        rcvrs = store.find(Receiver)

        for rcvr in rcvrs:

            if rcvr.pgp_key_public and rcvr.pgp_key_expiration != datetime_null():
               if rcvr.pgp_key_expiration < datetime_now():
                   expired_or_expiring.append(admin_serialize_receiver(rcvr, GLSetting.memory_copy.language))
                   if node_desc['allow_unencrypted']:
                       # The PGP key status should be downgraded only if the node
                       # accept non PGP mails/files to be sent/stored.
                       # If the node wont accept this the pgp key status
                       # will remain enabled and mail won't be sent by regular flow.
                       rcvr.pgp_key_status = u'disabled'
               elif rcvr.pgp_key_expiration < datetime_now() - timedelta(days=15):
                   expired_or_expiring.append(admin_serialize_receiver(rcvr, GLSetting.memory_copy.language))

        return expired_or_expiring
    def create_tip_notification_events(self, store, notification_counter):
        """
        This transaction will return all a list of tuples containing the tips
        for which the notification event has not been run.

        Returns:

            events: a list of tuples containing (tip_id, an instance of
                :class:`globaleaks.plugins.base.Event`).

        """
        events = []

        # settings.notification_plugins contain a list of supported plugin
        # at the moment only 1. so [0] is used. but different context/receiver
        # may use different code-plugin:
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_tips = store.find(
            models.ReceiverTip,
            models.ReceiverTip.mark == models.ReceiverTip._marker[0])

        node_desc = admin.db_admin_serialize_node(
            store, GLSetting.memory_copy.default_language)

        if not_notified_tips.count():
            log.debug("Receiver Tips found to be notified: %d" %
                      not_notified_tips.count())

        for receiver_tip in not_notified_tips:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug(
                    "Notification counter has reached the suggested limit: %d (tip)"
                    % notification_counter)
                break

            if not receiver_tip.internaltip or not receiver_tip.internaltip.context:
                log.err(
                    "(tip_notification) Integrity failure: missing InternalTip|Context"
                )
                continue

            context_desc = admin.admin_serialize_context(
                receiver_tip.internaltip.context,
                GLSetting.memory_copy.default_language)

            receiver_desc = admin.admin_serialize_receiver(
                receiver_tip.receiver, GLSetting.memory_copy.default_language)
            if not receiver_desc.has_key('mail_address'):
                log.err("Receiver %s lack of email address!" %
                        receiver_tip.receiver.name)
                continue

            # check if the receiver has the Tip notification enabled or not
            if not receiver_desc['tip_notification']:
                log.debug("Receiver %s has tip notification disabled" %
                          receiver_tip.receiver.user.username)
                receiver_tip.mark = models.ReceiverTip._marker[3]  # 'disabled'
                store.commit()
                continue

            tip_desc = serialize_receivertip(receiver_tip)

            if receiver_desc[
                    'gpg_key_status'] == u'Enabled':  # Receiver._gpg_types[1]
                template_type = u'encrypted_tip'
            else:
                template_type = u'plaintext_tip'

            event = Event(type=template_type,
                          trigger='Tip',
                          notification_settings=self.notification_settings,
                          trigger_info=tip_desc,
                          trigger_parent=None,
                          node_info=node_desc,
                          receiver_info=receiver_desc,
                          context_info=context_desc,
                          plugin=plugin)
            events.append((unicode(receiver_tip.id), event))

        return events, notification_counter
    def create_file_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added files..

        Returns:
            events: a list of tuples containing ((receiverfile_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).

        """
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_rfiles = store.find(
            models.ReceiverFile,
            models.ReceiverFile.mark == models.ReceiverFile._marker[0])

        node_desc = admin.db_admin_serialize_node(
            store, GLSetting.memory_copy.default_language)

        if not_notified_rfiles.count():
            log.debug("Receiverfiles found to be notified: %d" %
                      not_notified_rfiles.count())

        for rfile in not_notified_rfiles:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug(
                    "Notification counter has reached the suggested limit: %d (files)"
                    % notification_counter)
                break

            if not rfile.internalfile:
                log.err(
                    "(file_notification) Integrity check failure (InternalFile)"
                )
                continue

            file_desc = serialize_internalfile(rfile.internalfile)

            if  not rfile.internalfile or \
                not rfile.internalfile.internaltip or \
                not rfile.internalfile.internaltip.context:
                log.err(
                    "(file_notification) Integrity check failure (File+Tip)")
                continue

            context_desc = admin.admin_serialize_context(
                rfile.internalfile.internaltip.context,
                GLSetting.memory_copy.default_language)

            receiver_desc = admin.admin_serialize_receiver(
                rfile.receiver, GLSetting.memory_copy.default_language)
            if not receiver_desc.has_key('mail_address'):
                log.err("Receiver %s lack of email address!" %
                        rfile.receiver.user.name)
                continue

            # check if the receiver has the File notification enabled or not
            if not rfile.receiver.file_notification:
                log.debug(
                    "Receiver %s has file notification disabled: %s skipped" %
                    (rfile.receiver.user.username, rfile.internalfile.name))
                rfile.mark = models.ReceiverFile._marker[3]  # 'disabled'
                store.commit()
                continue

            # by ticket https://github.com/globaleaks/GlobaLeaks/issues/444
            # send notification of file only if notification of tip is already on send status
            if rfile.receiver_tip.mark == models.ReceiverTip._marker[
                    0]:  # 'not notified'
                rfile.mark = models.ReceiverFile._marker[4]  # 'skipped'
                log.debug(
                    "Skipped notification of %s (for %s) because Tip not yet notified"
                    % (rfile.internalfile.name, rfile.receiver.name))
                store.commit()
                continue

            tip_desc = serialize_receivertip(rfile.receiver_tip)

            if receiver_desc[
                    'gpg_key_status'] == u'Enabled':  # Receiver._gpg_types[1]
                template_type = u'encrypted_file'
            else:
                template_type = u'plaintext_file'

            event = Event(type=template_type,
                          trigger='File',
                          notification_settings=self.notification_settings,
                          trigger_info=file_desc,
                          trigger_parent=tip_desc,
                          node_info=node_desc,
                          receiver_info=receiver_desc,
                          context_info=context_desc,
                          plugin=plugin)

            events.append(
                ((unicode(rfile.id), unicode(rfile.receiver.id)), event))

        return events, notification_counter
    def create_comment_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added comments.

        Returns:
            events: a list of tuples containing ((comment_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_comments = store.find(
            models.Comment, models.Comment.mark == models.Comment._marker[0])

        node_desc = admin.db_admin_serialize_node(
            store, GLSetting.memory_copy.default_language)

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" %
                      not_notified_comments.count())

        for comment in not_notified_comments:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug(
                    "Notification counter has reached the suggested limit: %d (comment)"
                    % notification_counter)
                break

            if comment.internaltip is None or comment.internaltip.receivers is None:
                log.err(
                    "Comment %s has internaltip or receivers broken reference"
                    % comment.id)
                comment.mark = models.Comment._marker[2]  # 'unable to notify'
                continue

            # for every comment, iter on the associated receiver
            log.debug("Comments receiver: %d" %
                      comment.internaltip.receivers.count())

            comment_desc = rtip.receiver_serialize_comment(comment)

            if not comment.internaltip.context:
                log.err(
                    "(comment_notification) Integrity check failure Context")
                continue

            context_desc = admin.admin_serialize_context(
                comment.internaltip.context,
                GLSetting.memory_copy.default_language)

            # XXX BUG! All notification is marked as correctly send,
            # This can't be managed by callback, and can't be managed by actual DB design
            comment.mark = models.Comment._marker[1]  # 'notified'

            for receiver in comment.internaltip.receivers:

                receiver_desc = admin.admin_serialize_receiver(
                    receiver, GLSetting.memory_copy.default_language)
                if not receiver_desc.has_key('mail_address'):
                    log.err("Receiver %s lack of email address!" %
                            receiver.name)
                    continue

                # if the comment author is the one to be notified: skip the notification
                # ----- BUG, remind,
                # if two receiver has the same name, and one has notification disabled
                # also the homonymous would get the notification dropped.
                if comment.type == models.Comment._types[
                        0] and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" %
                              receiver.user.username)
                    continue

                # check if the receiver has the Comment notification enabled or not
                if not receiver.comment_notification:
                    log.debug(
                        "Receiver %s has comment notification disabled: skipped [source: %s]"
                        % (receiver.user.username, comment.author))
                    continue

                receivertip = store.find(
                    models.ReceiverTip,
                    (models.ReceiverTip.internaltip_id
                     == comment.internaltip_id, models.ReceiverTip.receiver_id
                     == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                if receiver_desc[
                        'gpg_key_status'] == u'Enabled':  # Receiver._gpg_types[1]
                    template_type = u'encrypted_comment'
                else:
                    template_type = u'plaintext_comment'

                event = Event(type=template_type,
                              trigger='Comment',
                              notification_settings=self.notification_settings,
                              trigger_info=comment_desc,
                              trigger_parent=tip_desc,
                              node_info=node_desc,
                              receiver_info=receiver_desc,
                              context_info=context_desc,
                              plugin=plugin)

                events.append(
                    ((unicode(comment.id), unicode(receiver.id)), event))

        return events, notification_counter
    def create_message_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added messages.

        Returns:
            events: a list of tuples containing ((message_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_messages = store.find(
            models.Message, models.Message.mark == models.Message._marker[0])

        node_desc = admin.db_admin_serialize_node(
            store, GLSetting.memory_copy.default_language)

        if not_notified_messages.count():
            log.debug("Messages found to be notified: %d" %
                      not_notified_messages.count())

        for message in not_notified_messages:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug(
                    "Notification counter has reached the suggested limit: %d (messages)"
                    % notification_counter)
                break

            if message.receivertip is None:
                log.err("Message %s has ReceiverTip broken reference" %
                        message.id)
                message.mark = models.Message._marker[2]  # 'unable to notify'
                continue

            tip_desc = serialize_receivertip(message.receivertip)

            receiver = store.find(
                Receiver,
                Receiver.id == message.receivertip.receiver_id).one()
            if not receiver:
                log.err("Message %s do not find receiver!?" % message.id)

            if not receiver.mail_address:
                log.err("Receiver %s lack of email address!" % receiver.name)
                continue

            receiver_desc = admin.admin_serialize_receiver(
                receiver, GLSetting.memory_copy.default_language)
            log.debug("Messages receiver: %s" %
                      message.receivertip.receiver.name)

            context = message.receivertip.internaltip.context
            if not context:
                log.err("Reference chain fail!")
                continue

            context_desc = admin.admin_serialize_context(
                context, GLSetting.memory_copy.default_language)

            message_desc = rtip.receiver_serialize_message(message)
            message.mark = u'notified'  # models.Message._marker[1]

            if message.type == u"receiver":
                log.debug("Receiver is the Author (%s): skipped" %
                          receiver.user.username)
                continue

            # check if the receiver has the Message notification enabled or not
            if not receiver.message_notification:
                log.debug(
                    "Receiver %s has message notification disabled: skipped [source: %s]"
                    % (receiver.user.username, message.author))
                continue

            if receiver_desc[
                    'gpg_key_status'] == u'Enabled':  # Receiver._gpg_types[1]
                template_type = u'encrypted_message'
            else:
                template_type = u'plaintext_message'

            event = Event(type=template_type,
                          trigger='Message',
                          notification_settings=self.notification_settings,
                          trigger_info=message_desc,
                          trigger_parent=tip_desc,
                          node_info=node_desc,
                          receiver_info=receiver_desc,
                          context_info=context_desc,
                          plugin=plugin)

            events.append(((unicode(message.id), unicode(receiver.id)), event))

        return events, notification_counter
Example #10
0
    def create_tip_notification_events(self, store, notification_counter):
        """
        This transaction will return all a list of tuples containing the tips
        for which the notification event has not been run.

        Returns:

            events: a list of tuples containing (tip_id, an instance of
                :class:`globaleaks.plugins.base.Event`).

        """
        language = GLSetting.memory_copy.default_language
        events = []

        # settings.notification_plugins contain a list of supported plugin
        # at the moment only 1. so [0] is used. but different context/receiver
        # may use different code-plugin:
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_tips = store.find(models.ReceiverTip,
            models.ReceiverTip.mark == models.ReceiverTip._marker[0]
        )

        node_desc = admin.db_admin_serialize_node(store, language)

        if not_notified_tips.count():
            log.debug("Receiver Tips found to be notified: %d" % not_notified_tips.count() )

        for receiver_tip in not_notified_tips:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug("Notification counter has reached the suggested limit: %d (tip)" %
                          notification_counter)
                break

            if not receiver_tip.internaltip or not receiver_tip.internaltip.context:
                log.err("(tip_notification) Integrity failure: missing (InternalTip/Context)")
                store.remove(receiver_tip)
                continue

            context_desc = admin.admin_serialize_context(store, receiver_tip.internaltip.context, language)

            receiver_desc = admin.admin_serialize_receiver(receiver_tip.receiver, language)
            if not receiver_desc.has_key('mail_address'):
                log.err("Receiver %s lack of email address!" % receiver_tip.receiver.name)
                continue

            # check if the receiver has the Tip notification enabled or not
            if not receiver_desc['tip_notification']:
                log.debug("Receiver %s has tip notification disabled" % receiver_tip.receiver.user.username)
                receiver_tip.mark = models.ReceiverTip._marker[3] # 'disabled'
                store.commit()
                continue

            tip_desc = serialize_receivertip(receiver_tip)

            if  receiver_desc['gpg_key_status'] == u'Enabled': # Receiver._gpg_types[1]
                template_type = u'encrypted_tip'
            else:
                template_type = u'plaintext_tip'

            notification_settings = self._get_notification_settings(store, receiver_desc['language'])

            event = Event(type=template_type, trigger='Tip',
                            notification_settings=notification_settings,
                            trigger_info=tip_desc,
                            trigger_parent=None,
                            node_info=node_desc,
                            receiver_info=receiver_desc,
                            context_info=context_desc,
                            steps_info = admin.db_get_context_steps(store,
                                                                    context_desc['id'],
                                                                    language),
                            plugin=plugin)

            events.append((unicode(receiver_tip.id), event))

        return events, notification_counter
Example #11
0
    def create_file_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added files..

        Returns:
            events: a list of tuples containing ((receiverfile_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).

        """
        language = GLSetting.memory_copy.default_language
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_rfiles = store.find(models.ReceiverFile,
            models.ReceiverFile.mark == models.ReceiverFile._marker[0]
        )

        node_desc = admin.db_admin_serialize_node(store, language)

        if not_notified_rfiles.count():
            log.debug("Receiverfiles found to be notified: %d" % not_notified_rfiles.count() )

        for rfile in not_notified_rfiles:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug("Notification counter has reached the suggested limit: %d (files)" %
                          notification_counter)
                store.remove(rfile)
                break

            if not rfile.internalfile:
                log.err("(file_notification) Integrity check failure (InternalFile)")
                store.remove(rfile)
                continue

            file_desc = serialize_internalfile(rfile.internalfile)

            if not rfile.internalfile.internaltip or \
                not rfile.internalfile.internaltip.context:
                log.err("(file_notification) Integrity check failure (InternalTip/Context)")
                store.remove(rfile)
                continue

            context_desc = admin.admin_serialize_context(store,
                                                         rfile.internalfile.internaltip.context,
                                                         language)

            receiver_desc = admin.admin_serialize_receiver(rfile.receiver, language)
            if not receiver_desc.has_key('mail_address'):
                log.err("Receiver %s lack of email address!" % rfile.receiver.user.name)
                continue

            # check if the receiver has the File notification enabled or not
            if not rfile.receiver.file_notification:
                log.debug("Receiver %s has file notification disabled: %s skipped" % (
                    rfile.receiver.user.username, rfile.internalfile.name ))
                rfile.mark = models.ReceiverFile._marker[3] # 'disabled'
                store.commit()
                continue

            # by ticket https://github.com/globaleaks/GlobaLeaks/issues/444
            # send notification of file only if notification of tip is already on send status
            if rfile.receiver_tip.mark == models.ReceiverTip._marker[0]: # 'not notified'
                rfile.mark = models.ReceiverFile._marker[4] # 'skipped'
                log.debug("Skipped notification of %s (for %s) because Tip not yet notified" %
                          (rfile.internalfile.name, rfile.receiver.name) )
                store.commit()
                continue

            tip_desc = serialize_receivertip(rfile.receiver_tip)

            if  receiver_desc['gpg_key_status'] == u'Enabled': # Receiver._gpg_types[1]
                template_type = u'encrypted_file'
            else:
                template_type = u'plaintext_file'

            notification_settings = self._get_notification_settings(store, receiver_desc['language'])

            event = Event(type=template_type, trigger='File',
                notification_settings=notification_settings,
                trigger_info=file_desc,
                trigger_parent=tip_desc,
                node_info=node_desc,
                receiver_info=receiver_desc,
                context_info=context_desc,
                steps_info = admin.db_get_context_steps(store,
                                                        context_desc['id'],
                                                        language),
                plugin=plugin)

            events.append(((unicode(rfile.id), unicode(rfile.receiver.id)), event))

        return events, notification_counter
Example #12
0
    def create_comment_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added comments.

        Returns:
            events: a list of tuples containing ((comment_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        language = GLSetting.memory_copy.default_language
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_comments = store.find(models.Comment,
            models.Comment.mark == models.Comment._marker[0]
        )

        node_desc = admin.db_admin_serialize_node(store, language)

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" % not_notified_comments.count() )

        for comment in not_notified_comments:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug("Notification counter has reached the suggested limit: %d (comment)" %
                          notification_counter)
                store.remove(comment)
                break

            if comment.internaltip is None or comment.internaltip.receivers is None:
                log.err("Comment %s has internaltip or receivers broken reference" % comment.id)
                comment.mark = models.Comment._marker[2] # 'unable to notify'
                continue

            # for every comment, iter on the associated receiver
            log.debug("Comments receiver: %d" % comment.internaltip.receivers.count())

            comment_desc = rtip.receiver_serialize_comment(comment)

            if not comment.internaltip.context:
                log.err("(comment_notification) Integrity check failure Context")
                store.remove(comment)
                continue

            context_desc = admin.admin_serialize_context(store, comment.internaltip.context, language)

            # XXX BUG! All notification is marked as correctly send,
            # This can't be managed by callback, and can't be managed by actual DB design
            comment.mark = models.Comment._marker[1] # 'notified'

            for receiver in comment.internaltip.receivers:

                receiver_desc = admin.admin_serialize_receiver(receiver, language)
                if not receiver_desc.has_key('mail_address'):
                    log.err("Receiver %s lack of email address!" % receiver.name)
                    continue

                # if the comment author is the one to be notified: skip the notification
                # ----- BUG, remind,
                # if two receiver has the same name, and one has notification disabled
                # also the homonymous would get the notification dropped.
                if comment.type == models.Comment._types[0] and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                    continue

                # check if the receiver has the Comment notification enabled or not
                if not receiver.comment_notification:
                    log.debug("Receiver %s has comment notification disabled: skipped [source: %s]" % (
                        receiver.user.username, comment.author))
                    continue

                receivertip = store.find(models.ReceiverTip,
                    (models.ReceiverTip.internaltip_id == comment.internaltip_id,
                     models.ReceiverTip.receiver_id == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                if  receiver_desc['gpg_key_status'] == u'Enabled': # Receiver._gpg_types[1]
                    template_type = u'encrypted_comment'
                else:
                    template_type = u'plaintext_comment'

                notification_settings = self._get_notification_settings(store, receiver_desc['language'])

                event = Event(type=template_type, trigger='Comment',
                    notification_settings=notification_settings,
                    trigger_info=comment_desc,
                    trigger_parent=tip_desc,
                    node_info=node_desc,
                    receiver_info=receiver_desc,
                    context_info=context_desc,
                    steps_info = admin.db_get_context_steps(store,
                                                            context_desc['id'],
                                                            language),
                    plugin=plugin)

                events.append(((unicode(comment.id), unicode(receiver.id)), event))

        return events, notification_counter
Example #13
0
    def create_message_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added messages.

        Returns:
            events: a list of tuples containing ((message_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        language = GLSetting.memory_copy.default_language
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_messages = store.find(models.Message,
                                           models.Message.mark == models.Message._marker[0]
        )

        node_desc = admin.db_admin_serialize_node(store, language)

        if not_notified_messages.count():
            log.debug("Messages found to be notified: %d" % not_notified_messages.count() )

        for message in not_notified_messages:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug("Notification counter has reached the suggested limit: %d (messages)" %
                          notification_counter)
                store.remove(message)
                break

            if message.receivertip is None:
                log.err("Message %s has ReceiverTip broken reference" % message.id)
                message.mark = models.Message._marker[2] # 'unable to notify'
                continue

            tip_desc = serialize_receivertip(message.receivertip)

            receiver = models.Receiver.get(store, message.receivertip.receiver_id)
            if not receiver:
                log.err("Message %s do not find receiver!?" % message.id)

            if not receiver.mail_address:
                log.err("Receiver %s lack of email address!" % receiver.name)
                continue

            receiver_desc = admin.admin_serialize_receiver(receiver, language)
            log.debug("Messages receiver: %s" % message.receivertip.receiver.name)

            context = message.receivertip.internaltip.context
            if not context:
                log.err("Reference chain fail!")
                continue

            context_desc = admin.admin_serialize_context(store, context, language)

            message_desc = rtip.receiver_serialize_message(message)
            message.mark = u'notified' # models.Message._marker[1]

            if message.type == u"receiver":
                log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                continue

            # check if the receiver has the Message notification enabled or not
            if not receiver.message_notification:
                log.debug("Receiver %s has message notification disabled: skipped [source: %s]" % (
                    receiver.user.username, message.author))
                continue

            if  receiver_desc['gpg_key_status'] == u'Enabled': # Receiver._gpg_types[1]
                template_type = u'encrypted_message'
            else:
                template_type = u'plaintext_message'

            notification_settings = self._get_notification_settings(store, receiver_desc['language'])

            event = Event(type=template_type, trigger='Message',
                          notification_settings=notification_settings,
                          trigger_info=message_desc,
                          trigger_parent=tip_desc,
                          node_info=node_desc,
                          receiver_info=receiver_desc,
                          context_info=context_desc,
                          steps_info = admin.db_get_context_steps(store,
                                                                  context_desc['id'],
                                                                  language),
                          plugin=plugin)

            events.append(((unicode(message.id), unicode(receiver.id)), event))

        return events, notification_counter