libkpgp

kpgpblock.cpp
1/*
2 kpgpblock.cpp
3
4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
6
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
8
9 KPGP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19#include "kpgpblock.h"
20#include "kpgp.h"
21
22#include <string.h>
23
24namespace Kpgp {
25
26Block::Block( const TQCString& str )
27 : mText(str), mProcessedText(), mError(),
28 mSignatureUserId(), mSignatureKeyId(), mSignatureDate(),
29 mRequiredKey(), mEncryptedFor(),
30 mStatus(0), mHasBeenProcessed(false), mType(NoPgpBlock)
31{
32 mEncryptedFor.setAutoDelete( true );
33}
34
35Block::~Block()
36{
37}
38
39void
40Block::reset()
41{
42 mProcessedText = TQCString();
43 mError = TQCString();
44 mSignatureUserId = TQString();
45 mSignatureKeyId = TQCString();
46 mSignatureDate = TQCString();
47 mRequiredKey = TQCString();
48 mEncryptedFor.clear();
49 mStatus = 0;
50 mHasBeenProcessed = false;
51}
52
53void
54Block::clear()
55{
56 reset();
57 mText = TQCString();
58 mType = NoPgpBlock;
59}
60
61BlockType
62Block::determineType() const
63{
64 if( !strncmp( mText.data(), "-----BEGIN PGP ", 15 ) )
65 {
66 if( !strncmp( mText.data() + 15, "SIGNED", 6 ) )
67 return ClearsignedBlock;
68 else if( !strncmp( mText.data() + 15, "SIGNATURE", 9 ) )
69 return SignatureBlock;
70 else if( !strncmp( mText.data() + 15, "PUBLIC", 6 ) )
71 return PublicKeyBlock;
72 else if( !strncmp( mText.data() + 15, "PRIVATE", 7 ) ||
73 !strncmp( mText.data() + 15, "SECRET", 6 ) )
74 return PrivateKeyBlock;
75 else if( !strncmp( mText.data() + 15, "MESSAGE", 7 ) )
76 {
77 if( !strncmp( mText.data() + 22, ", PART", 6 ) )
78 return MultiPgpMessageBlock;
79 else
80 return PgpMessageBlock;
81 }
82 else if( !strncmp( mText.data() + 15, "ARMORED FILE", 12 ) )
83 return PgpMessageBlock;
84 else
85 return UnknownBlock;
86 }
87 else
88 return NoPgpBlock;
89}
90
91bool
92Block::decrypt()
93{
94 Kpgp::Module *pgp = Kpgp::Module::getKpgp();
95
96 if( pgp == 0 )
97 return false;
98
99 return pgp->decrypt( *this );
100}
101
102bool
103Block::verify()
104{
105 Kpgp::Module *pgp = Kpgp::Module::getKpgp();
106
107 if( pgp == 0 )
108 return false;
109
110 return pgp->verify( *this );
111}
112
113Kpgp::Result
114Block::clearsign( const TQCString& keyId, const TQCString& charset )
115{
116 Kpgp::Module *pgp = Kpgp::Module::getKpgp();
117
118 if( pgp == 0 )
119 return Kpgp::Failure;
120
121 return pgp->clearsign( *this, keyId, charset );
122}
123
124Kpgp::Result
125Block::encrypt( const TQStringList& receivers, const TQCString& keyId,
126 const bool sign, const TQCString& charset )
127{
128 Kpgp::Module *pgp = Kpgp::Module::getKpgp();
129
130 if( pgp == 0 )
131 return Kpgp::Failure;
132
133 return pgp->encrypt( *this, receivers, keyId, sign, charset );
134}
135
136} // namespace Kpgp