예제 #1
0
파일: gnupg.cpp 프로젝트: psi-plus/plugins
void GnuPG::sendPublicKey()
{
    QAction *action = qobject_cast<QAction*>(sender());
    QString fingerprint = "0x" + action->data().toString();

    GpgProcess gpg;
    QStringList arguments;
    arguments << "--armor"
              << "--export"
              << fingerprint;

    gpg.start(arguments);
    gpg.waitForFinished();

    // do nothing if error is occurred
    if (gpg.exitCode()) {
        return;
    }

    QString key = QString::fromUtf8(gpg.readAllStandardOutput());

    QString jid = _activeTab->getYourJid();
    QString jidToSend = _activeTab->getJid();
    int account = 0;
    QString tmpJid;
    while (jid != (tmpJid = _accountInfo->getJid(account))) {
        ++account;
        if (tmpJid == "-1") {
            return;
        }
    }

    _stanzaSending->sendMessage(account, jidToSend, key, "", "chat");
    _accountHost->appendSysMsg(account, jidToSend, _stanzaSending->escape(QString(tr("Public key %1 sent")).arg(action->text())));
}
예제 #2
0
파일: gnupg.cpp 프로젝트: psi-plus/plugins
bool GnuPG::incomingStanza(int account, const QDomElement& stanza)
{
    if (!_enabled) {
        return false;
    }

    if (!_optionHost->getPluginOption("auto-import", true).toBool()) {
        return false;
    }

    if (stanza.tagName() != "message" && stanza.attribute("type") != "chat") {
        return false;
    }

    QString body = stanza.firstChildElement("body").text();

    int start = body.indexOf("-----BEGIN PGP PUBLIC KEY BLOCK-----");
    if (start == -1) {
        return false;
    }

    int end = body.indexOf("-----END PGP PUBLIC KEY BLOCK-----", start);
    if (end == -1) {
        return false;
    }

    QString key = body.mid(start, end - start);

    GpgProcess gpg;
    QStringList arguments;
    arguments << "--batch"
              << "--import";
    gpg.start(arguments);
    gpg.waitForStarted();
    gpg.write(key.toUtf8());
    gpg.closeWriteChannel();
    gpg.waitForFinished();

    QString from = stanza.attribute("from");
    // Cut trash from gpg command output
    QString res = QString::fromUtf8(gpg.readAllStandardError());
    res = _stanzaSending->escape(res.mid(0, res.indexOf('\n')));
    _accountHost->appendSysMsg(account, from, res);

    // Don't hide message if an error occurred
    if (gpg.exitCode()) {
        return false;
    }

    if (!_optionHost->getPluginOption("hide-key-message", true).toBool()) {
        return false;
    }
    else {
        return true;
    }
}
예제 #3
0
void Options::removeKey()
{
	QItemSelectionModel *selModel = ui->keys->selectionModel();

	if (!selModel->hasSelection()) {
		return;
	}

	QModelIndexList indexes = selModel->selectedIndexes();
	QModelIndexList pkeys; // Key IDs
	foreach (QModelIndex index, indexes) {
		// Every selection contains all columns. Need to work only with first
		if (index.column() > 0) {
			continue;
		}

		// Choose only primary keys
		QModelIndex pIndex = index;
		if (index.parent().isValid()) {
			pIndex = index.parent();
		}

		if (pkeys.indexOf(pIndex) < 0) {
			pkeys << pIndex;
		}
	}

	if (!pkeys.isEmpty()) {
		if (QMessageBox::question(this, tr("Delete"), tr("Do you want to delete the selected keys?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) {
			return;
		}
	}

	// Remove primary keys
	foreach (QModelIndex key, pkeys) {
		GpgProcess gpg;
		QStringList arguments;
		arguments << "--yes"
				  << "--batch"
				  << "--delete-secret-and-public-key"
				  << "0x" + key.sibling(key.row(), Model::Fingerprint).data().toString();

		gpg.start(arguments);
		gpg.waitForFinished();
	}
예제 #4
0
void Options::addKey()
{
	AddKeyDlg dlg(this);
	if (dlg.exec() == QDialog::Rejected) {
		return;
	}

	QString key;
	QString type, stype, length, name, comment, email, expiration, pass;
	switch (dlg.type()) {
	case 0: type = stype = "RSA"; break;
	case 1: type = "DSA"; stype = "ELG-E"; break;
	case 2: type = "DSA"; break;
	case 3: type = "RSA"; break;
	}

	length = QString::number(dlg.length());
	name = dlg.name();
	comment = dlg.comment();
	email = dlg.email();
	expiration = dlg.expiration().isValid() ? dlg.expiration().toString(Qt::ISODate) : "0";
	pass = dlg.pass();

	key += QString("Key-Type: %1\n").arg(type);
	key += QString("Key-Length: %2\n").arg(length);
	if (!stype.isEmpty()) {
		key += QString("Subkey-Type: %1\n").arg(stype);
		key += QString("Subkey-Length: %2\n").arg(length);
	}

	if (!name.isEmpty()) {
		key += QString("Name-Real: %1\n").arg(name);
	}

	if (!comment.isEmpty()) {
		key += QString("Name-Comment: %1\n").arg(comment);
	}

	if (!email.isEmpty()) {
		key += QString("Name-Email: %1\n").arg(email);
	}

	key += QString("Expire-Date: %1\n").arg(expiration);

	if (!pass.isEmpty()) {
		key += QString("Passphrase: %1\n").arg(pass);
	}

	key += "%commit\n";

	QProgressDialog waitingDlg("", trUtf8("Cancel"), 0, 0, this);

	QLabel progressTextLabel(trUtf8(
"<b>Please wait!</b><br/>"
"We need to generate a lot of random bytes. It is a good idea to perform "
"some other action (type on the keyboard, move the mouse, utilize the "
"disks) during the prime generation; this gives the random number "
"generator a better chance to gain enough entropy."), &waitingDlg);
	progressTextLabel.setAlignment(Qt::AlignHCenter);
	progressTextLabel.setWordWrap(true);

	waitingDlg.setLabel(&progressTextLabel);

	QProgressBar progressBar(&waitingDlg);
	progressBar.setAlignment(Qt::AlignHCenter);
	progressBar.setMinimum(0);
	progressBar.setMaximum(0);

	waitingDlg.setBar(&progressBar);

	waitingDlg.setWindowModality(Qt::WindowModal);
	waitingDlg.setWindowTitle(trUtf8("Key pair generating"));
	waitingDlg.show();

	GpgProcess gpg;
	QStringList arguments;
	arguments << "--batch"
			  << "--gen-key";

	gpg.start(arguments);
	gpg.waitForStarted();
	gpg.write(key.toUtf8());
	gpg.closeWriteChannel();
	while (gpg.state() == QProcess::Running) {
		gpg.waitForFinished(1);
		if (waitingDlg.wasCanceled()) {
			gpg.terminate();
			break;
		}
		qApp->processEvents();
	}

	updateKeys();
}