Exemple #1
0
    def load(self, filename=None, data=None, config=None):
        """
        Load VCard lines from a file on disk or data in memory.
        """
        if data:
            pass
        elif filename:
            from mailpile.crypto.streamer import DecryptingStreamer
            self.filename = filename or self.filename
            data = DecryptingStreamer(config.prefs.obfuscate_index,
                                      open(self.filename, 'rb')
                                      ).read().decode('utf-8')
        else:
            raise ValueError('Need data or a filename!')

        def unwrap(text):
            # This undoes the VCard standard line wrapping
            return text.replace('\n ', '').replace('\n\t', '')

        lines = [l.strip() for l in unwrap(data.strip()).splitlines()]
        if (not len(lines) >= 2 or
                not lines.pop(0).upper() == 'BEGIN:VCARD' or
                not lines.pop(-1).upper() == 'END:VCARD'):
            raise ValueError('Not a valid VCard: %s' % '\n'.join(lines))

        for line in lines:
            self.add(VCardLine(line))

        return self
Exemple #2
0
    def load(self, filename=None, data=None, config=None):
        """
        Load VCard lines from a file on disk or data in memory.
        """
        if data:
            pass
        elif filename:
            from mailpile.crypto.streamer import DecryptingStreamer
            self.filename = filename or self.filename
            data = DecryptingStreamer(config.prefs.obfuscate_index,
                                      open(self.filename,
                                           'rb')).read().decode('utf-8')
        else:
            raise ValueError('Need data or a filename!')

        def unwrap(text):
            # This undoes the VCard standard line wrapping
            return text.replace('\n ', '').replace('\n\t', '')

        lines = [l.strip() for l in unwrap(data.strip()).splitlines()]
        if (not len(lines) >= 2 or not lines.pop(0).upper() == 'BEGIN:VCARD'
                or not lines.pop(-1).upper() == 'END:VCARD'):
            raise ValueError('Not a valid VCard: %s' % '\n'.join(lines))

        for line in lines:
            self.add(VCardLine(line))

        return self
Exemple #3
0
 def load_pickle(self, pfn):
     fd = None
     try:
         fd = open(os.path.join(self.workdir, pfn), 'rb')
         if self.prefs.obfuscate_index:
             from mailpile.crypto.streamer import DecryptingStreamer
             fd = DecryptingStreamer(self.prefs.obfuscate_index, fd)
         return cPickle.loads(fd.read())
     finally:
         if fd:
             fd.close()
Exemple #4
0
 def load_pickle(self, pfn):
     fd = None
     try:
         fd = open(os.path.join(self.workdir, pfn), 'rb')
         if self.prefs.obfuscate_index:
             from mailpile.crypto.streamer import DecryptingStreamer
             fd = DecryptingStreamer(self.prefs.obfuscate_index, fd)
         return cPickle.loads(fd.read())
     finally:
         if fd:
             fd.close()
Exemple #5
0
    def load(self, filename=None, data=None, config=None):
        """
        Load VCard lines from a file on disk or data in memory.
        """
        if data:
            pass
        elif filename:
            from mailpile.crypto.streamer import DecryptingStreamer
            self.filename = filename or self.filename
            with open(self.filename, 'rb') as fd:
                with DecryptingStreamer(fd,
                                        mep_key=self.decryption_key_func(),
                                        name='VCard/load') as streamer:
                    data = streamer.read().decode('utf-8')
                    streamer.verify(_raise=IOError)
        else:
            raise ValueError('Need data or a filename!')

        def unwrap(text):
            # This undoes the VCard standard line wrapping
            return text.replace('\n ', '').replace('\n\t', '')

        lines = [l.strip() for l in unwrap(data.strip()).splitlines()]
        if (not len(lines) >= 2 or
                not lines.pop(0).upper() == 'BEGIN:VCARD' or
                not lines.pop(-1).upper() == 'END:VCARD'):
            raise ValueError('Not a valid VCard: %s' % '\n'.join(lines))

        with self._lock:
            for line in lines:
                self.add(VCardLine(line))

        return self
Exemple #6
0
 def load_pickle(self, pfn):
     with open(os.path.join(self.workdir, pfn), 'rb') as fd:
         if self.prefs.obfuscate_index:
             from mailpile.crypto.streamer import DecryptingStreamer
             with DecryptingStreamer(self.prefs.obfuscate_index,
                                     fd) as streamer:
                 return cPickle.loads(streamer.read())
         else:
             return cPickle.loads(fd.read())
Exemple #7
0
 def _load_logfile(self, lfn):
     enc_key = self.encryption_key_func()
     with open(os.path.join(self.logdir, lfn)) as fd:
         if enc_key:
             lines = fd.read()
         else:
             with DecryptingStreamer(enc_key, fd) as streamer:
                 lines = streamer.read()
         if lines:
             for line in lines.splitlines():
                 event = Event.Parse(line)
                 self._events[event.event_id] = event
Exemple #8
0
 def _load_logfile(self, lfn):
     enc_key = self.decryption_key_func()
     with open(os.path.join(self.logdir, lfn)) as fd:
         if enc_key:
             with DecryptingStreamer(fd, mep_key=enc_key,
                                     name='EventLog/DS') as streamer:
                 lines = streamer.read()
                 streamer.verify(_raise=IOError)
         else:
             lines = fd.read()
         if lines:
             for line in lines.splitlines():
                 event = Event.Parse(line)
                 self._events[event.event_id] = event
Exemple #9
0
 def _load_logfile(self, lfn):
     enc_key = self.encryption_key_func()
     with open(os.path.join(self.logdir, lfn)) as fd:
         if enc_key:
             lines = fd.read()
         else:
             with DecryptingStreamer(enc_key, fd) as streamer:
                 lines = streamer.read()
         if lines:
             for line in lines.splitlines():
                 event = Event.Parse(line)
                 if Event.COMPLETE in event.flags:
                     if event.event_id in self._events:
                         del self._events[event.event_id]
                 else:
                     self._events[event.event_id] = event
     self._save_events(self._events.values())
Exemple #10
0
def _decrypt(data, config):
    with DecryptingStreamer(cStringIO.StringIO(data),
                            mep_key=config.get_master_key()) as fd:
        data = fd.read()
        fd.verify(_raise=IOError)
    return data