summaryrefslogtreecommitdiffstats
path: root/src/otrlconfinterface.cpp
blob: 75bcb229364f95ac3d6c8686ddd9ba1d703fa19f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/***************************************************************************
 *   Copyright (C) 2007 by Michael Zanetti
 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/


/**
  * @author Michael Zanetti
  */

#include <tqapplication.h>
#include <tqeventloop.h>

#include <kopetechatsession.h>
#include <kopeteaccount.h>

#include <kdebug.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <kanimwidget.h>


#include "otrlconfinterface.h"
#include "otrlchatinterface.h"
#include "otrplugin.h"
#include "privkeypopup.h"



/*********************** Konstruktor/Destruktor **********************/

OtrlConfInterface::OtrlConfInterface( TQWidget *preferencesDialog ){

	this->preferencesDialog = preferencesDialog;

	OTRL_INIT;
	
	userstate = OtrlChatInterface::self()->getUserstate();

	kdDebug() << "OtrlConfInterface created" << endl;
}

OtrlConfInterface::~ OtrlConfInterface(){
	otrl_userstate_free(userstate);
}

/*********************** Functions for kcm module ************************/

TQString OtrlConfInterface::getPrivFingerprint( TQString accountId, TQString protocol){
//	if (otrl_privkey_read(userstate, TQString(TDEGlobal::dirs()->saveLocation("data", "kopete_otr/", true )) + "privkey" ) == 0){
		char fingerprint[45];
		if( otrl_privkey_fingerprint( userstate, fingerprint, accountId.latin1(), protocol.latin1()) != 0 ){
			return fingerprint;
		}
//	}
	return i18n("No fingerprint present.");
}


bool OtrlConfInterface::hasPrivFingerprint( TQString accountId, TQString protocol ){
	char fingerprint[45];
	if( otrl_privkey_fingerprint( userstate, fingerprint, accountId.latin1(), protocol.latin1() ) != 0 ){
		return true;
	}
	return false;
}


void OtrlConfInterface::generateNewPrivKey( TQString accountId, TQString protocol ){
	PrivKeyPopup *popup = new PrivKeyPopup( preferencesDialog, i18n("Generating private key"),  TQt::WStyle_Dialog | TQt::WStyle_StaysOnTop );
	KAnimWidget *anim = new KAnimWidget( "kde", 72, popup->animFrame, "kopete" );
	anim->start();
	anim->show();

	popup->setCloseLock( true );
	popup->show();
	KeyGenThread *keyGenThread = new KeyGenThread ( accountId, protocol );
	keyGenThread->start();
	while( !keyGenThread->wait(100) ){
		tqApp->eventLoop()->processEvents(TQEventLoop::ExcludeUserInput | TQEventLoop::ExcludeSocketNotifiers, 100);
	}
	popup->setCloseLock( false );
	popup->close();
}

TQValueList<TQString[5]> OtrlConfInterface::readAllFingerprints(){
	ConnContext *context;
	Fingerprint *fingerprint;
	TQString entry[5];
	char hash[45];
	TQValueList<TQString[5]> list;

	for( context = userstate->context_root; context != NULL; context = context->next ){
		fingerprint = context->fingerprint_root.next;
		while( fingerprint ){
			entry[0] = context->username;

			if( ( context->msgstate == OTRL_MSGSTATE_ENCRYPTED ) && ( context->active_fingerprint != fingerprint ) ){
				entry[1] = i18n("Unused");
			} else {
				if (context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
					if (context->active_fingerprint->trust && context->active_fingerprint->trust[0] != NULL) {
						entry[1] = i18n("Private");
					} else {
						entry[1] = i18n("Unverified");
					}
				} else if (context && context->msgstate == OTRL_MSGSTATE_FINISHED) {
					entry[1] = i18n("Finished");
				} else {
					entry[1] = i18n("Not Private");
				}
			}
			entry[2] = ( fingerprint->trust && fingerprint->trust[0] ) ? i18n("Yes") : i18n("No") ;
			otrl_privkey_hash_to_human( hash, fingerprint->fingerprint );
			entry[3] = hash;
			entry[4] = context->protocol;
			list << entry;
			fingerprint = fingerprint->next;
		}
	}
	return list;
}

void OtrlConfInterface::verifyFingerprint( TQString strFingerprint, bool trust ){
	Fingerprint *fingerprint;

	fingerprint = findFingerprint( strFingerprint );

	if( fingerprint != 0 ){
		if( trust ){
			otrl_context_set_trust( fingerprint, "verified" );
		} else {
			otrl_context_set_trust( fingerprint, NULL );
		}
		kdDebug() << "Writing fingerprints" << endl;
		otrl_privkey_write_fingerprints( userstate, TQString(TDEGlobal::dirs()->saveLocation("data", "kopete_otr/", true )) + "fingerprints" );
	} else {
		kdDebug() << "could not find fingerprint" << endl;
	}
}

bool OtrlConfInterface::isVerified( TQString strFingerprint ){
	Fingerprint *fingerprint;	

	fingerprint = findFingerprint( strFingerprint );

	if( fingerprint->trust && fingerprint->trust[0] ){
		kdDebug() << "found trust" << endl;
		return true;
	} else {
		kdDebug() << "not trusted" << endl;
		return false;
	}
}


void OtrlConfInterface::forgetFingerprint( TQString strFingerprint ){
	Fingerprint *fingerprint;
	
	fingerprint = findFingerprint( strFingerprint );
	otrl_context_forget_fingerprint( fingerprint, 1 );
	otrl_privkey_write_fingerprints( userstate, TQString(TDEGlobal::dirs()->saveLocation("data", "kopete_otr/", true )) + "fingerprints" );
}

Fingerprint *OtrlConfInterface::findFingerprint( TQString strFingerprint ){
//	const char *cFingerprint = ;
//	Fingerprint *fingerprintRoot = &userstate->context_root->fingerprint_root;
	ConnContext *context;
	Fingerprint *fingerprint;
	Fingerprint *foundFingerprint = NULL;
	char hash[45];

	for( context = userstate->context_root; context != NULL; context = context->next ){
		fingerprint = context->fingerprint_root.next;
		while( fingerprint ){
			otrl_privkey_hash_to_human(hash, fingerprint->fingerprint);
			if( strcmp( hash, strFingerprint.latin1()) == 0 ){
				foundFingerprint = fingerprint;
			}
			fingerprint = fingerprint->next;
		}
	}	
	return foundFingerprint;
}

bool OtrlConfInterface::isEncrypted( TQString strFingerprint ){
	Fingerprint *fingerprint;
	Fingerprint *tmpFingerprint;
	Fingerprint *foundFingerprint;
	ConnContext *context;
	ConnContext *foundContext = NULL;

	context = userstate->context_root;

	fingerprint = findFingerprint( strFingerprint );
	for( context = userstate->context_root; context != NULL; context = context->next ){
		tmpFingerprint = context->fingerprint_root.next;
		while( tmpFingerprint ){
			if( tmpFingerprint == fingerprint ){
				kdDebug() << "Found context" << endl;
				foundContext = context;
				foundFingerprint = tmpFingerprint;
			}
			tmpFingerprint = tmpFingerprint->next;
		}
	}

	if( foundContext && foundContext->msgstate != OTRL_MSGSTATE_ENCRYPTED ){
		return false;
	} else if( foundContext && foundFingerprint && foundContext->active_fingerprint == foundFingerprint ){
		return true;
	} else {
		return false;
	}
}