summaryrefslogtreecommitdiff
path: root/camel/camel-junk-filter.c
blob: 8a058a94bf34172ed52c36fd0edf3c74eb56ceb3 (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
/*
 * camel-junk-filter.c
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "camel-junk-filter.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#include "camel-operation.h"

G_DEFINE_INTERFACE (CamelJunkFilter, camel_junk_filter, G_TYPE_OBJECT)

static void
camel_junk_filter_default_init (CamelJunkFilterInterface *iface)
{
}

/**
 * camel_junk_filter_classify:
 * @junk_filter: a #CamelJunkFilter
 * @message: a #CamelMimeMessage
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Classifies @message as junk, not junk or inconclusive.
 *
 * If an error occurs, the function sets @error and returns
 * %CAMEL_JUNK_STATUS_ERROR.
 *
 * Returns: the junk status determined by @junk_filter
 *
 * Since: 3.2
 **/
CamelJunkStatus
camel_junk_filter_classify (CamelJunkFilter *junk_filter,
                            CamelMimeMessage *message,
                            GCancellable *cancellable,
                            GError **error)
{
	CamelJunkFilterInterface *iface;

	g_return_val_if_fail (CAMEL_IS_JUNK_FILTER (junk_filter), 0);
	g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), 0);

	iface = CAMEL_JUNK_FILTER_GET_INTERFACE (junk_filter);
	g_return_val_if_fail (iface->classify != NULL, 0);

	return iface->classify (
		junk_filter, message, cancellable, error);
}

/**
 * camel_junk_filter_learn_junk:
 * @junk_filter: a #CamelJunkFilter
 * @message: a #CamelMimeMessage
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Instructs @junk_filter to classify @message as junk.  If using an
 * adaptive junk filtering algorithm, explicitly marking @message as
 * junk will influence the classification of future messages.
 *
 * If an error occurs, the function sets @error and returns %FALSE.
 *
 * Returns: %TRUE if @message was successfully classified
 *
 * Since: 3.2
 **/
gboolean
camel_junk_filter_learn_junk (CamelJunkFilter *junk_filter,
                              CamelMimeMessage *message,
                              GCancellable *cancellable,
                              GError **error)
{
	CamelJunkFilterInterface *iface;

	g_return_val_if_fail (CAMEL_IS_JUNK_FILTER (junk_filter), FALSE);
	g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), FALSE);

	iface = CAMEL_JUNK_FILTER_GET_INTERFACE (junk_filter);
	g_return_val_if_fail (iface->learn_junk != NULL, FALSE);

	return iface->learn_junk (
		junk_filter, message, cancellable, error);
}

/**
 * camel_junk_filter_learn_not_junk:
 * @junk_filter: a #CamelJunkFilter
 * @message: a #CamelMimeMessage
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Instructs @junk_filter to classify @message as not junk.  If using an
 * adaptive junk filtering algorithm, explicitly marking @message as not
 * junk will influence the classification of future messages.
 *
 * If an error occurs, the function sets @error and returns %FALSE.
 *
 * Returns: %TRUE if @message was successfully classified
 *
 * Since: 3.2
 **/
gboolean
camel_junk_filter_learn_not_junk (CamelJunkFilter *junk_filter,
                                  CamelMimeMessage *message,
                                  GCancellable *cancellable,
                                  GError **error)
{
	CamelJunkFilterInterface *iface;

	g_return_val_if_fail (CAMEL_IS_JUNK_FILTER (junk_filter), FALSE);
	g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), FALSE);

	iface = CAMEL_JUNK_FILTER_GET_INTERFACE (junk_filter);
	g_return_val_if_fail (iface->learn_not_junk != NULL, FALSE);

	return iface->learn_not_junk (
		junk_filter, message, cancellable, error);
}

/**
 * camel_junk_filter_synchronize:
 * @junk_filter: a #CamelJunkFilter
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Instructs @junk_filter to flush any in-memory caches to disk, if
 * applicable.  When filtering many messages, delaying this step until
 * all messages have been classified can improve performance.
 *
 * If an error occurs, the function sets @error and returns %FALSE.
 *
 * Returns: %TRUE if @junk_filter was successfully synchronized
 *
 * Since: 3.2
 **/
gboolean
camel_junk_filter_synchronize (CamelJunkFilter *junk_filter,
                               GCancellable *cancellable,
                               GError **error)
{
	CamelJunkFilterInterface *iface;
	gboolean success = TRUE;

	g_return_val_if_fail (CAMEL_IS_JUNK_FILTER (junk_filter), FALSE);

	/* This method is optional. */
	iface = CAMEL_JUNK_FILTER_GET_INTERFACE (junk_filter);

	if (iface->synchronize != NULL) {
		camel_operation_push_message (
			cancellable, _("Synchronizing junk database"));

		success = iface->synchronize (
			junk_filter, cancellable, error);

		camel_operation_pop_message (cancellable);
	}

	return success;
}