faq, man, manual moved to doc/; fix automake warnings
[claws.git] / src / privacy.c
blob76a3cd1628dea01153e3f965d85f3afdbd2274a2
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <glib.h>
22 #include "intl.h"
23 #include "privacy.h"
24 #include "procmime.h"
26 static GSList *systems = NULL;
28 /**
29 * Register a new Privacy System
31 * \param system The Privacy System that should be registered
33 void privacy_register_system(PrivacySystem *system)
35 systems = g_slist_append(systems, system);
38 /**
39 * Unregister a new Privacy System. The system must not be in
40 * use anymore when it is unregistered.
42 * \param system The Privacy System that should be unregistered
44 void privacy_unregister_system(PrivacySystem *system)
46 systems = g_slist_remove(systems, system);
49 /**
50 * Free a PrivacyData of a PrivacySystem
52 * \param privacydata The data to free
54 void privacy_free_privacydata(PrivacyData *privacydata)
56 g_return_if_fail(privacydata != NULL);
58 privacydata->system->free_privacydata(privacydata);
61 /**
62 * Check if a MimeInfo is signed with one of the available
63 * privacy system. If a privacydata is set in the MimeInfo
64 * it will directory return the return value by the system
65 * set in the privacy data or check all available privacy
66 * systems otherwise.
68 * \return True if the MimeInfo has a signature
70 gboolean privacy_mimeinfo_is_signed(MimeInfo *mimeinfo)
72 GSList *cur;
73 g_return_val_if_fail(mimeinfo != NULL, FALSE);
75 if (mimeinfo->privacy != NULL) {
76 PrivacySystem *system = mimeinfo->privacy->system;
78 if (system->is_signed != NULL)
79 return system->is_signed(mimeinfo);
80 else
81 return FALSE;
84 for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
85 PrivacySystem *system = (PrivacySystem *) cur->data;
87 if(system->is_signed != NULL && system->is_signed(mimeinfo))
88 return TRUE;
91 return FALSE;
94 /**
95 * Check the signature of a MimeInfo. privacy_mimeinfo_is_signed
96 * should be called before otherwise it is done by this function.
97 * If the MimeInfo is not signed an error code will be returned.
99 * \return Error code indicating the result of the check,
100 * < 0 if an error occured
102 gint privacy_mimeinfo_check_signature(MimeInfo *mimeinfo)
104 PrivacySystem *system;
106 g_return_val_if_fail(mimeinfo != NULL, -1);
108 if (mimeinfo->privacy == NULL)
109 privacy_mimeinfo_is_signed(mimeinfo);
111 if (mimeinfo->privacy == NULL)
112 return -1;
114 system = mimeinfo->privacy->system;
115 if (system->check_signature == NULL)
116 return -1;
118 return system->check_signature(mimeinfo);
121 SignatureStatus privacy_mimeinfo_get_sig_status(MimeInfo *mimeinfo)
123 PrivacySystem *system;
125 g_return_val_if_fail(mimeinfo != NULL, -1);
127 if (mimeinfo->privacy == NULL)
128 privacy_mimeinfo_is_signed(mimeinfo);
130 if (mimeinfo->privacy == NULL)
131 return SIGNATURE_UNCHECKED;
133 system = mimeinfo->privacy->system;
134 if (system->get_sig_status == NULL)
135 return SIGNATURE_UNCHECKED;
137 return system->get_sig_status(mimeinfo);
140 gchar *privacy_mimeinfo_sig_info_short(MimeInfo *mimeinfo)
142 PrivacySystem *system;
144 g_return_val_if_fail(mimeinfo != NULL, NULL);
146 if (mimeinfo->privacy == NULL)
147 privacy_mimeinfo_is_signed(mimeinfo);
149 if (mimeinfo->privacy == NULL)
150 return g_strdup(_("No signature found"));
152 system = mimeinfo->privacy->system;
153 if (system->get_sig_info_short == NULL)
154 return g_strdup(_("No information available"));
156 return system->get_sig_info_short(mimeinfo);
159 gchar *privacy_mimeinfo_sig_info_full(MimeInfo *mimeinfo)
161 PrivacySystem *system;
163 g_return_val_if_fail(mimeinfo != NULL, NULL);
165 if (mimeinfo->privacy == NULL)
166 privacy_mimeinfo_is_signed(mimeinfo);
168 if (mimeinfo->privacy == NULL)
169 return g_strdup(_("No signature found"));
171 system = mimeinfo->privacy->system;
172 if (system->get_sig_info_full == NULL)
173 return g_strdup(_("No information available"));
175 return system->get_sig_info_full(mimeinfo);
178 gboolean privacy_mimeinfo_is_encrypted(MimeInfo *mimeinfo)
180 GSList *cur;
181 g_return_val_if_fail(mimeinfo != NULL, FALSE);
183 for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
184 PrivacySystem *system = (PrivacySystem *) cur->data;
186 if(system->is_encrypted != NULL && system->is_encrypted(mimeinfo))
187 return TRUE;
190 return FALSE;
193 static gint decrypt(MimeInfo *mimeinfo, PrivacySystem *system)
195 MimeInfo *decryptedinfo, *parentinfo;
196 gint childnumber;
198 g_return_val_if_fail(system->decrypt != NULL, -1);
200 decryptedinfo = system->decrypt(mimeinfo);
201 if (decryptedinfo == NULL)
202 return -1;
204 parentinfo = procmime_mimeinfo_parent(mimeinfo);
205 childnumber = g_node_child_index(parentinfo->node, mimeinfo);
207 procmime_mimeinfo_free_all(mimeinfo);
209 g_node_insert(parentinfo->node, childnumber, decryptedinfo->node);
211 return 0;
214 gint privacy_mimeinfo_decrypt(MimeInfo *mimeinfo)
216 GSList *cur;
217 g_return_val_if_fail(mimeinfo != NULL, FALSE);
219 for(cur = systems; cur != NULL; cur = g_slist_next(cur)) {
220 PrivacySystem *system = (PrivacySystem *) cur->data;
222 if(system->is_encrypted != NULL && system->is_encrypted(mimeinfo))
223 return decrypt(mimeinfo, system);
226 return -1;