You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by pa...@apache.org on 2005/05/07 18:08:32 UTC
svn commit: r169089 - in /spamassassin/trunk: MANIFEST
lib/Mail/SpamAssassin/Client.pm lib/Mail/SpamAssassin/Reporter.pm
spamc/libspamc.c spamc/libspamc.h spamc/spamc.c spamc/spamc.pod
spamd/spamd.raw t/data/reporterplugin.pm t/spamc_optC.t
Author: parker
Date: Sat May 7 09:08:32 2005
New Revision: 169089
URL: http://svn.apache.org/viewcvs?rev=169089&view=rev
Log:
Bug 4163: add reporting support to spamd/spamc
* MANIFEST
Add new files
* lib/Mail/SpamAssassin/Client.pm
Added report and revoke methods
* lib/Mail/SpamAssassin/Reporter.pm
Default to failure for report plugin call
* t/spamc_optC.t
Added test to test the spamc -C functionality
* t/data/reporterplugin.pm
Helper plugin to test the spamC -C functionality
* spamc/libspamc.h
Added bitmask for Collaborative Reporting option
Added prototype for message_collabreport method
* spamc/spamc.c
Added new -C command line
Made the learntype/reporttype params generic (ie extratype)
Added code necessary to call collaborative reporting option
* spamc/spamc.pod
Document -C option
* spamc/libspamc.c
Added method message_collabreport to report mails to the
colaborative reporting databases
Updated header parsing code to recognize Reported:
Fixed unreachable compiler warning
* spamd/spamd.raw
Added COLLABREPORT spamd protocol option
Added:
spamassassin/trunk/t/data/reporterplugin.pm
spamassassin/trunk/t/spamc_optC.t (with props)
Modified:
spamassassin/trunk/MANIFEST
spamassassin/trunk/lib/Mail/SpamAssassin/Client.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Reporter.pm
spamassassin/trunk/spamc/libspamc.c
spamassassin/trunk/spamc/libspamc.h
spamassassin/trunk/spamc/spamc.c
spamassassin/trunk/spamc/spamc.pod
spamassassin/trunk/spamd/spamd.raw
Modified: spamassassin/trunk/MANIFEST
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/MANIFEST?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/MANIFEST (original)
+++ spamassassin/trunk/MANIFEST Sat May 7 09:08:32 2005
@@ -323,6 +323,7 @@
t/data/spam/spf1
t/data/spam/spf2
t/data/spam/spf3
+t/data/reporterplugin.pm
t/data/testplugin.pm
t/data/validuserplugin.pm
t/data/whitelists/action.eff.org
@@ -387,6 +388,7 @@
t/spamc_c.t
t/spamc_c_stdout_closed.t
t/spamc_l.t
+t/spamc_optC.t
t/spamc_optL.t
t/spamd.t
t/spamd_allow_user_rules.t
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Client.pm
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/lib/Mail/SpamAssassin/Client.pm?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Client.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Client.pm Sat May 7 09:08:32 2005
@@ -118,7 +118,7 @@
my $command = $is_check_p ? 'CHECK' : 'PROCESS';
- $self->clear_errors();
+ $self->_clear_errors();
my $remote = $self->_create_connection();
@@ -211,7 +211,7 @@
sub learn {
my ($self, $msg, $learntype) = @_;
- $self->clear_errors();
+ $self->_clear_errors();
my $remote = $self->_create_connection();
@@ -258,6 +258,37 @@
return $learned_p;
}
+=head2 report
+
+public instance (Boolean) report (String $msg)
+
+Description:
+This method provides the report interface to spamd.
+
+=cut
+
+sub report {
+ my ($self, $msg) = @_;
+
+ return $self->_report_or_revoke($msg, 0);
+}
+
+=head2 revoke
+
+public instance (Boolean) revoke (String $msg)
+
+Description:
+This method provides the revoke interface to spamd.
+
+=cut
+
+sub revoke {
+ my ($self, $msg) = @_;
+
+ return $self->_report_or_revoke($msg, 1);
+}
+
+
=head2 ping
public instance (Boolean) ping ()
@@ -346,11 +377,88 @@
return split(/\s+/, $line);
}
-sub clear_errors {
+=head2 _clear_errors
+
+private instance () _clear_errors ()
+
+Description:
+This method clears out any current errors.
+
+=cut
+
+sub _clear_errors {
my ($self) = @_;
$self->{resp_code} = undef;
$self->{resp_msg} = undef;
}
+
+
+=head2 _report_or_revoke
+
+public instance (Boolean) report_or_revoke (String $msg, Integer $reporttype)
+
+Description:
+This method implements the report or revoke call. C<$learntype> should
+be an integer, 0 for report or 1 for revoke. The return value is a
+boolean indicating if the message was learned or not.
+
+An undef return value indicates that there was an error and you
+should check the resp_code/resp_error values to determine what
+the error was.
+
+=cut
+
+sub _report_or_revoke {
+ my ($self, $msg, $reporttype) = @_;
+
+ $self->_clear_errors();
+
+ my $remote = $self->_create_connection();
+
+ return undef unless ($remote);
+
+ my $msgsize = length($msg.$EOL);
+
+ print $remote "COLLABREPORT $PROTOVERSION$EOL";
+ print $remote "Content-length: $msgsize$EOL";
+ print $remote "User: $self->{username}$EOL" if ($self->{username});
+ print $remote "CollabReport-type: $reporttype$EOL";
+ print $remote "$EOL";
+ print $remote $msg;
+ print $remote "$EOL";
+
+ my $line = <$remote>;
+
+ my ($version, $resp_code, $resp_msg) = $self->_parse_response_line($line);
+
+ $self->{resp_code} = $resp_code;
+ $self->{resp_msg} = $resp_msg;
+
+ return undef unless ($resp_code == 0);
+
+ my $reported_p = 0;
+ my $found_blank_line_p = 0;
+
+ while (!$found_blank_line_p) {
+ $line = <$remote>;
+
+ if ($line =~ /Reported: yes/i) {
+ $reported_p = 1;
+ }
+ elsif ($line =~ /Reported: no/i) {
+ $reported_p = 0;
+ }
+ elsif ($line =~ /$EOL/) {
+ $found_blank_line_p = 1;
+ }
+ }
+
+ close $remote;
+
+ return $reported_p;
+}
+
+
1;
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Reporter.pm
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/lib/Mail/SpamAssassin/Reporter.pm?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Reporter.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Reporter.pm Sat May 7 09:08:32 2005
@@ -54,7 +54,7 @@
sub report {
my ($self) = @_;
- $self->{report_return} = 1;
+ $self->{report_return} = 0;
$self->{report_available} = 0;
my $text = $self->{main}->remove_spamassassin_markup($self->{msg});
Modified: spamassassin/trunk/spamc/libspamc.c
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/spamc/libspamc.c?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/spamc/libspamc.c (original)
+++ spamassassin/trunk/spamc/libspamc.c Sat May 7 09:08:32 2005
@@ -766,11 +766,12 @@
static int
_handle_spamd_header(struct message *m, int flags, char *buf, int len,
- int *islearned)
+ int *actionok)
{
char is_spam[6];
char s_str[21], t_str[21];
char is_learned[4];
+ char is_reported[4];
UNUSED_VARIABLE(len);
@@ -819,13 +820,27 @@
}
else if (sscanf(buf, "Learned: %3s", is_learned) == 1) {
if(strcmp(is_learned, "yes") == 0 || strcmp(is_learned, "Yes") == 0) {
- *islearned = 1;
+ *actionok = 1;
}
else if(strcmp(is_learned, "no") == 0 || strcmp(is_learned, "No") == 0) {
- *islearned = 0;
+ *actionok = 0;
}
else {
- libspamc_log(flags, LOG_ERR, "spamd responded with bad Learned-state '%s'",
+ libspamc_log(flags, LOG_ERR, "spamd responded with bad Learned state '%s'",
+ buf);
+ return EX_PROTOCOL;
+ }
+ return EX_OK;
+ }
+ else if (sscanf(buf, "Reported: %3s", is_reported) == 1) {
+ if(strcmp(is_reported, "yes") == 0 || strcmp(is_reported, "Yes") == 0) {
+ *actionok = 1;
+ }
+ else if(strcmp(is_reported, "no") == 0 || strcmp(is_reported, "No") == 0) {
+ *actionok = 0;
+ }
+ else {
+ libspamc_log(flags, LOG_ERR, "spamd responded with bad Reported state '%s'",
buf);
return EX_PROTOCOL;
}
@@ -1269,25 +1284,201 @@
shutdown(sock, SHUT_RD);
closesocket(sock);
sock = -1;
+
+ libspamc_timeout = 0;
+
return EX_OK;
+ failure:
+ _use_msg_for_out(m);
+ if (sock != -1) {
+ closesocket(sock);
+ }
libspamc_timeout = 0;
- if (m->out_len != m->content_length) {
- libspamc_log(flags, LOG_ERR,
- "failed sanity check, %d bytes claimed, %d bytes seen",
- m->content_length, m->out_len);
+ if (flags & SPAMC_USE_SSL) {
+#ifdef SPAMC_SSL
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
+#endif
+ }
+ return failureval;
+}
+
+
+int message_collabreport(struct transport *tp, const char *username, int flags,
+ struct message *m, int reporttype, int *isreported)
+{
+ char buf[8192];
+ size_t bufsiz = (sizeof(buf) / sizeof(*buf)) - 4; /* bit of breathing room */
+ size_t len;
+ int sock = -1;
+ int rc;
+ char versbuf[20];
+ char strreporttype[1];
+ float version;
+ int response;
+ int failureval;
+ SSL_CTX *ctx = NULL;
+ SSL *ssl = NULL;
+ SSL_METHOD *meth;
+
+ if (flags & SPAMC_USE_SSL) {
+#ifdef SPAMC_SSL
+ SSLeay_add_ssl_algorithms();
+ meth = SSLv2_client_method();
+ SSL_load_error_strings();
+ ctx = SSL_CTX_new(meth);
+#else
+ UNUSED_VARIABLE(ssl);
+ UNUSED_VARIABLE(meth);
+ UNUSED_VARIABLE(ctx);
+ libspamc_log(flags, LOG_ERR, "spamc not built with SSL support");
+ return EX_SOFTWARE;
+#endif
+ }
+
+ m->is_spam = EX_TOOBIG;
+ if ((m->outbuf = malloc(m->max_len + EXPANSION_ALLOWANCE + 1)) == NULL) {
+ failureval = EX_OSERR;
+ goto failure;
+ }
+ m->out = m->outbuf;
+ m->out_len = 0;
+
+ /* Build spamd protocol header */
+ strcpy(buf, "COLLABREPORT ");
+
+ len = strlen(buf);
+ if (len + strlen(PROTOCOL_VERSION) + 2 >= bufsiz) {
+ _use_msg_for_out(m);
+ return EX_OSERR;
+ }
+
+ strcat(buf, PROTOCOL_VERSION);
+ strcat(buf, "\r\n");
+ len = strlen(buf);
+
+ if ((reporttype > 2) | (reporttype < 0 )) {
+ free(m->out);
+ m->out = m->msg;
+ m->out_len = m->msg_len;
+ return EX_OSERR;
+ }
+ sprintf(strreporttype,"%d",reporttype);
+ strcpy(buf + len, "CollabReport-type: ");
+ strcat(buf + len, strreporttype);
+ strcat(buf + len, "\r\n");
+ len += strlen(buf + len);
+
+ if (username != NULL) {
+ if (strlen(username) + 8 >= (bufsiz - len)) {
+ _use_msg_for_out(m);
+ return EX_OSERR;
+ }
+ strcpy(buf + len, "User: ");
+ strcat(buf + len, username);
+ strcat(buf + len, "\r\n");
+ len += strlen(buf + len);
+ }
+ if ((m->msg_len > 9999999) || ((len + 27) >= (bufsiz - len))) {
+ _use_msg_for_out(m);
+ return EX_OSERR;
+ }
+ len += sprintf(buf + len, "Content-length: %d\r\n\r\n", m->msg_len);
+
+ libspamc_timeout = m->timeout;
+
+ if (tp->socketpath)
+ rc = _try_to_connect_unix(tp, &sock);
+ else
+ rc = _try_to_connect_tcp(tp, &sock);
+
+ if (rc != EX_OK) {
+ _use_msg_for_out(m);
+ return rc; /* use the error code try_to_connect_*() gave us. */
+ }
+
+ if (flags & SPAMC_USE_SSL) {
+#ifdef SPAMC_SSL
+ ssl = SSL_new(ctx);
+ SSL_set_fd(ssl, sock);
+ SSL_connect(ssl);
+#endif
+ }
+
+ /* Send to spamd */
+ if (flags & SPAMC_USE_SSL) {
+#ifdef SPAMC_SSL
+ SSL_write(ssl, buf, len);
+ SSL_write(ssl, m->msg, m->msg_len);
+#endif
+ }
+ else {
+ full_write(sock, 0, buf, len);
+ full_write(sock, 0, m->msg, m->msg_len);
+ shutdown(sock, SHUT_WR);
+ }
+
+ /* ok, now read and parse it. SPAMD/1.2 line first... */
+ failureval =
+ _spamc_read_full_line(m, flags, ssl, sock, buf, &len, bufsiz);
+ if (failureval != EX_OK) {
+ goto failure;
+ }
+
+ if (sscanf(buf, "SPAMD/%18s %d %*s", versbuf, &response) != 2) {
+ libspamc_log(flags, LOG_ERR, "spamd responded with bad string '%s'", buf);
+ failureval = EX_PROTOCOL;
+ goto failure;
+ }
+
+ versbuf[19] = '\0';
+ version = _locale_safe_string_to_float(versbuf, 20);
+ if (version < 1.0) {
+ libspamc_log(flags, LOG_ERR, "spamd responded with bad version string '%s'",
+ versbuf);
failureval = EX_PROTOCOL;
goto failure;
}
+ m->score = 0;
+ m->threshold = 0;
+ m->is_spam = EX_TOOBIG;
+ *isreported = 0;
+ while (1) {
+ failureval =
+ _spamc_read_full_line(m, flags, ssl, sock, buf, &len, bufsiz);
+ if (failureval != EX_OK) {
+ goto failure;
+ }
+
+ if (len == 0 && buf[0] == '\0') {
+ break; /* end of headers */
+ }
+
+ if (_handle_spamd_header(m, flags, buf, len, isreported) < 0) {
+ failureval = EX_PROTOCOL;
+ goto failure;
+ }
+ }
+
+ len = 0; /* overwrite those headers */
+
+ shutdown(sock, SHUT_RD);
+ closesocket(sock);
+ sock = -1;
+
+ libspamc_timeout = 0;
+
return EX_OK;
failure:
- _use_msg_for_out(m);
+ _use_msg_for_out(m);
if (sock != -1) {
- closesocket(sock);
+ closesocket(sock);
}
+
libspamc_timeout = 0;
if (flags & SPAMC_USE_SSL) {
@@ -1298,6 +1489,8 @@
}
return failureval;
}
+
+
void message_cleanup(struct message *m)
{
Modified: spamassassin/trunk/spamc/libspamc.h
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/spamc/libspamc.h?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/spamc/libspamc.h (original)
+++ spamassassin/trunk/spamc/libspamc.h Sat May 7 09:08:32 2005
@@ -100,6 +100,9 @@
/* Nov 24, 2004 NP: added learning support */
#define SPAMC_LEARN (1<<21)
+/* May 5, 2005 NP: added list reporting support */
+#define SPAMC_COLLABREPORT (1<<20)
+
/* Aug 14, 2002 bj: A struct for storing a message-in-progress */
typedef enum
{
@@ -218,6 +221,14 @@
*/
int message_learn(struct transport *tp, const char *username, int flags,
struct message *m, int learntype, int *islearned);
+
+/* Process the message through the spamd collandreport, making as many
+ * connection attempts as are implied by the transport structure. To make
+ * this do failover, more than one host is defined, but if there is only
+ * one there, no failover is done.
+ */
+int message_collabreport(struct transport *tp, const char *username, int flags,
+ struct message *m, int reporttype, int *isreported);
/* Dump the message. If there is any data in the message (typically, m->type
* will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped
Modified: spamassassin/trunk/spamc/spamc.c
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/spamc/spamc.c?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/spamc/spamc.c (original)
+++ spamassassin/trunk/spamc/spamc.c Sat May 7 09:08:32 2005
@@ -143,6 +143,10 @@
usg(" -L learntype Learn message as spam, ham or forget to\n"
" forget or unlearn the message.\n");
+
+ usg(" -C reporttype Report message to collaborative filtering\n"
+ " databases. Report type should be report for\n"
+ " spam or revoke for ham.\n");
usg(" -B Assume input is a single BSMTP-formatted\n"
" message.\n");
@@ -176,13 +180,13 @@
*/
int
read_args(int argc, char **argv,
- int *max_size, char **username, int *learntype,
+ int *max_size, char **username, int *extratype,
struct transport *ptrn)
{
#ifndef _WIN32
- const char *opts = "-BcrRd:e:fyp:t:s:u:L:xSHU:ElhV";
+ const char *opts = "-BcrRd:e:fyp:t:s:u:L:C:xSHU:ElhV";
#else
- const char *opts = "-BcrRd:fyp:t:s:u:L:xSHElhV";
+ const char *opts = "-BcrRd:fyp:t:s:u:L:C:xSHElhV";
#endif
int opt;
int ret = EX_OK;
@@ -287,13 +291,13 @@
{
flags |= SPAMC_LEARN;
if (strcmp(optarg,"spam") == 0) {
- *learntype = 0;
+ *extratype = 0;
}
else if (strcmp(optarg,"ham") == 0) {
- *learntype = 1;
+ *extratype = 1;
}
else if (strcmp(optarg,"forget") == 0) {
- *learntype = 2;
+ *extratype = 2;
}
else {
libspamc_log(flags, LOG_ERR, "Please specifiy a legal learn type");
@@ -301,6 +305,21 @@
}
break;
}
+ case 'C':
+ {
+ flags |= SPAMC_COLLABREPORT;
+ if (strcmp(optarg,"report") == 0) {
+ *extratype = 0;
+ }
+ else if (strcmp(optarg,"revoke") == 0) {
+ *extratype = 1;
+ }
+ else {
+ libspamc_log(flags, LOG_ERR, "Please specifiy a legal report type");
+ ret = EX_USAGE;
+ }
+ break;
+ }
#ifndef _WIN32
case 'U':
{
@@ -360,6 +379,10 @@
libspamc_log(flags, LOG_ERR, "Learning excludes symbols");
ret = EX_USAGE;
}
+ if (flags & SPAMC_COLLABREPORT) {
+ libspamc_log(flags, LOG_ERR, "Learning excludes reporting to collaborative filtering databases");
+ ret = EX_USAGE;
+ }
}
return ret;
}
@@ -500,8 +523,9 @@
int out_fd = -1;
int result;
int ret;
- int learntype = 0;
+ int extratype = 0;
int islearned = 0;
+ int isreported = 0;
transport_init(&trans);
@@ -518,7 +542,7 @@
/* Now parse the command line arguments. First, set the defaults. */
max_size = 250 * 1024;
username = NULL;
- if ((ret = read_args(argc, argv, &max_size, &username, &learntype, &trans)) != EX_OK) {
+ if ((ret = read_args(argc, argv, &max_size, &username, &extratype, &trans)) != EX_OK) {
if (ret == EX_TEMPFAIL )
ret = EX_OK;
goto finish;
@@ -562,7 +586,10 @@
if (ret == EX_OK) {
if (flags & SPAMC_LEARN) {
- ret = message_learn(&trans, username, flags, &m, learntype, &islearned);
+ ret = message_learn(&trans, username, flags, &m, extratype, &islearned);
+ }
+ else if (flags & SPAMC_COLLABREPORT) {
+ ret = message_collabreport(&trans, username, flags, &m, extratype, &isreported);
}
else {
ret = message_filter(&trans, username, flags, &m);
@@ -580,6 +607,16 @@
}
else {
printf("Message was already un/learned\n");
+ }
+ message_cleanup(&m);
+ goto finish;
+ }
+ else if (flags & SPAMC_COLLABREPORT) {
+ if (isreported == 1) {
+ printf("Message successfully reported/revoked\n");
+ }
+ else {
+ printf("Unable to report/revoke message\n");
}
message_cleanup(&m);
goto finish;
Modified: spamassassin/trunk/spamc/spamc.pod
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/spamc/spamc.pod?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/spamc/spamc.pod (original)
+++ spamassassin/trunk/spamc/spamc.pod Sat May 7 09:08:32 2005
@@ -101,6 +101,11 @@
ham or forget. The exitcode for spamc will be set to 5 if the message
was learned, or 6 if it was already learned.
+=item B<-C> I<report type>
+
+Report or revoke a message to one of the configured collaborative filtering
+databases. The C<report type> can be either report or revoke.
+
=item B<-p> I<port>
In TCP/IP mode, connect to spamd server listening on given port
Modified: spamassassin/trunk/spamd/spamd.raw
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/spamd/spamd.raw?rev=169089&r1=169088&r2=169089&view=diff
==============================================================================
--- spamassassin/trunk/spamd/spamd.raw (original)
+++ spamassassin/trunk/spamd/spamd.raw Sat May 7 09:08:32 2005
@@ -1049,6 +1049,30 @@
info(sprintf("spamd: skipped large message in %3d seconds", time - $start));
}
+ # COLLABREPORT must come before REPORT, since the regex is overgenerous
+ elsif (/(COLLABREPORT) SPAMC\/(.*)/) {
+ my $method = $1;
+ my $version = $2;
+ eval {
+ Mail::SpamAssassin::Util::trap_sigalrm_fully(sub {
+ die "child processing timeout";
+ });
+ alarm $timeout_child if ($timeout_child);
+ report($method, $version, $start, $remote_hostname, $remote_hostaddr);
+ };
+ alarm 0;
+
+ if ($@) {
+ if ($@ =~ /child processing timeout/) {
+ service_timeout("($timeout_child second timeout while trying to $method)");
+ } else {
+ warn "spamd: $@";
+ }
+ $client->close();
+ return 0;
+ }
+ }
+
# It might be a CHECK message, meaning that we should just check
# if it's spam or not, then return the appropriate response.
# If we get the PROCESS command, the client is going to send a
@@ -1444,6 +1468,102 @@
return 1;
}
+sub report {
+ my ($method, $version, $start_time, $remote_hostname, $remote_hostaddr) = @_;
+ local ($_);
+ my $expected_length;
+ my $report_type;
+
+ my $hdrs = {};
+
+ return 0 unless (parse_headers($hdrs, $client));
+
+ $expected_length = $hdrs->{expected_length};
+ $report_type = $hdrs->{collabreport_type};
+
+ &handle_setuid_to_user if ($setuid_to_user && $> == 0);
+
+ if ($opt{'sql-config'} && !defined($current_user)) {
+ unless (handle_user_sql('nobody')) {
+ service_unavailable_error("Error fetching user preferences via SQL");
+ return 0;
+ }
+ }
+
+ if ($opt{'ldap-config'} && !defined($current_user)) {
+ handle_user_ldap('nobody');
+ }
+
+ my $resp = "EX_OK";
+
+ # generate mail object from input
+ my ($mail, $actual_length) = parse_body($client, $expected_length);
+
+ # Check length if we're supposed to.
+ if (defined $expected_length && $actual_length != $expected_length) {
+ protocol_error("(Content-Length mismatch: Expected $expected_length bytes, got $actual_length bytes)");
+ $mail->finish();
+ return 0;
+ }
+
+ if ( $mail->get_header("X-Spam-Checker-Version") ) {
+ my $new_mail = $spamtest->parse($spamtest->remove_spamassassin_markup($mail), 1);
+ $mail->finish();
+ $mail = $new_mail;
+ }
+
+ # attempt to fetch the message ids
+ my ($msgid, $rmsgid) = parse_msgids($mail);
+
+ $msgid ||= "(unknown)";
+ $current_user ||= "(unknown)";
+
+ my $report_type_desc;
+ my $report_type_desc_past;
+
+ if ($report_type == 0) {
+ $report_type_desc = "reporting spam";
+ $report_type_desc_past = "reported spam";
+ }
+ elsif ($report_type == 1) {
+ $report_type_desc = "revoking ham";
+ $report_type_desc_past = "revoked ham";
+ }
+
+ info("spamd: $report_type_desc"
+ . " message $msgid"
+ . ( $rmsgid ? " aka $rmsgid" : "" )
+ . " for ${current_user}:$>");
+
+ my $hdr;
+ my $status = 1;
+
+ if ($report_type) {
+ $status = $spamtest->revoke_as_spam($mail);
+ }
+ else {
+ $status = $spamtest->report_as_spam($mail);
+ }
+
+ if ($status) {
+ $hdr .= "Reported: Yes";
+ }
+ else {
+ $hdr .= "Reported: No";
+ }
+
+ print $client "SPAMD/1.1 $resphash{$resp} $resp\r\n",
+ $hdr . "\r\n\r\n";
+
+ my $scantime = sprintf( "%.1f", time - $start_time );
+
+ info("spamd: $report_type_desc_past message for $current_user:$> in"
+ . " $scantime seconds, $actual_length bytes");
+
+ $mail->finish();
+ return 1;
+}
+
###########################################################################
# generalised header parser.
@@ -1475,6 +1595,9 @@
elsif ($header eq 'Learn-type') {
return 0 unless &got_learn_type_header($hdrs, $header, $value);
}
+ elsif ($header eq 'CollabReport-type') {
+ return 0 unless &got_collabreport_type_header($hdrs, $header, $value);
+ }
}
# avoid too-many-headers DOS attack
@@ -1563,6 +1686,21 @@
return 0;
}
$hdrs->{learn_type} = $type;
+ return 1;
+}
+
+sub got_collabreport_type_header {
+ my ($hdrs, $header, $value) = @_;
+ if ($value !~ /^(\d*)$/) {
+ protocol_error("(CollabReport-type contains non-numeric bytes)");
+ return 0;
+ }
+ my $type = $1;
+ if ($type != 0 && $type != 1) {
+ protocol_error("(CollabReport-type contains invalid type)");
+ return 0;
+ }
+ $hdrs->{collabreport_type} = $type;
return 1;
}
Added: spamassassin/trunk/t/data/reporterplugin.pm
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/t/data/reporterplugin.pm?rev=169089&view=auto
==============================================================================
--- spamassassin/trunk/t/data/reporterplugin.pm (added)
+++ spamassassin/trunk/t/data/reporterplugin.pm Sat May 7 09:08:32 2005
@@ -0,0 +1,60 @@
+=head1 WARNING
+
+This is a sample plugin, it may not work at all, so buyer beware.
+
+=cut
+
+package reporterplugin;
+
+use strict;
+use bytes;
+use Cwd;
+
+use Mail::SpamAssassin::Plugin;
+
+use vars qw(@ISA);
+@ISA = qw(Mail::SpamAssassin::Plugin);
+
+sub new {
+ my $class = shift;
+ my $mailsaobject = shift;
+
+ # some boilerplate...
+ $class = ref($class) || $class;
+ my $self = $class->SUPER::new($mailsaobject);
+ bless ($self, $class);
+
+ return $self;
+}
+
+sub plugin_report {
+ my ($self, $options) = @_;
+
+ if (-e 'log/rptfail') {
+ $options->{report}->{report_available} = 0;
+ $options->{report}->{report_return} = 0;
+ }
+ else {
+ $options->{report}->{report_available} = 1;
+ $options->{report}->{report_return} = 1;
+ }
+
+ return;
+}
+
+sub plugin_revoke {
+ my ($self, $options) = @_;
+
+ if (-e 'log/rptfail') {
+ $options->{revoke}->{revoke_available} = 0;
+ $options->{revoke}->{revoke_return} = 0;
+ }
+ else {
+ $options->{revoke}->{revoke_available} = 1;
+ $options->{revoke}->{revoke_return} = 1;
+ }
+
+ return;
+}
+
+1;
Added: spamassassin/trunk/t/spamc_optC.t
URL: http://svn.apache.org/viewcvs/spamassassin/trunk/t/spamc_optC.t?rev=169089&view=auto
==============================================================================
--- spamassassin/trunk/t/spamc_optC.t (added)
+++ spamassassin/trunk/t/spamc_optC.t Sat May 7 09:08:32 2005
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use lib '.'; use lib 't';
+use SATest; sa_t_init("spamc_optC");
+
+use Test; plan tests => $SKIP_SPAMC_TESTS ? 0 : 9;
+
+exit if $SKIP_SPAMC_TESTS;
+
+# ---------------------------------------------------------------------------
+
+tstlocalrules ("
+ loadplugin reporterplugin data/reporterplugin.pm
+");
+
+start_spamd("-L");
+
+%patterns = ( 'Message successfully reported/revoked' => 'reported spam' );
+
+ok (spamcrun ("-C report < data/spam/001", \&patterns_run_cb));
+ok_all_patterns();
+
+%patterns = ( 'Message successfully reported/revoked' => 'revoked ham' );
+
+ok (spamcrun ("-C revoke < data/nice/001", \&patterns_run_cb));
+ok_all_patterns();
+
+open (OUT, ">log/rptfail");
+print OUT "file created to trigger a reporterplugin failure";
+close OUT;
+
+%patterns = ( 'Unable to report/revoke message' => 'failed to report spam' );
+
+ok (spamcrun ("-C report < data/spam/001", \&patterns_run_cb));
+ok_all_patterns();
+
+%patterns = ( 'Unable to report/revoke message' => 'failed to revoke ham' );
+
+ok (spamcrun ("-C revoke < data/nice/001", \&patterns_run_cb));
+ok_all_patterns();
+
+stop_spamd();
+
+ok(unlink 'log/rptfail'); # need a little cleanup
Propchange: spamassassin/trunk/t/spamc_optC.t
------------------------------------------------------------------------------
svn:executable = *