aboutsummaryrefslogblamecommitdiffstats
path: root/plugin.c
blob: 3f7eb97ae47c27d7c9c2a7fc9e0a44d4c8b12b8f (plain) (tree)
1
  























                                                                                      
   
 





                     
                 



                   










                                     
 

                            
 
                                                                      
 
                            
 


                                                     
 
                 

 
 
                                                                  
 
                            
 








                                                                              
         

 


                                                                

                                               
                                                      


                                         
                                    

                
                                                                  










                                                                             

                                                                


                                                                       
                                                                  

                                                               
                                                      






                                                    

                                                          



                   
 
                                                                 




                                              
                                           
                                         
                                               
                







                                                                    















                                                                    
                                                              









                                                                         
                                                    

















                                                                          
                                                                              
                

























                                                                       
                                                  
                                                                                
                 

                                    

         
                               

 
 




                                       

                                                                             
                                                                            




                                                                  




                       
/*
 * antispam plugin for dovecot
 *
 * Copyright (C) 2004-2007  Johannes Berg <johannes@sipsolutions.net>
 *                    2006  Frank Cusack
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * 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
 *
 * based on the original framework http://www.dovecot.org/patches/1.0/copy_plugin.c
 *
 * Please see http://johannes.sipsolutions.net/wiki/Projects/dovecot-dspam-integration
 * for more information on this code.
 *
 * Install the plugin in the usual dovecot module location.
 */

#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#ifdef DEBUG
#include <syslog.h>
#endif

/* dovecot headers we need */
#include "lib.h"
#include "mempool.h"
#include "str.h"
#include "strfuncs.h"
#include "commands.h"
#include "imap-search.h"
#include "lib-storage/mail-storage.h"
#include "mail-storage.h"
#include "client.h"
#include "ostream.h"

/* internal stuff we need */
#include "plugin.h"

static struct signature *list_add(pool_t pool, struct signature *list)
{
	struct signature *n;

	n = p_malloc(pool, sizeof(struct signature));
	n->next = list;
	n->sig = NULL;

	return n;
}


static void client_send_sendalive_if_needed(struct client *client)
{
	time_t now, last_io;

	if (o_stream_get_buffer_used_size(client->output) != 0)
		return;

	now = time(NULL);
	last_io = I_MAX(client->last_input, client->last_output);
	if (now - last_io > MAIL_STORAGE_STAYALIVE_SECS) {
		o_stream_send_str(client->output, "* OK Hang in there..\r\n");
		o_stream_flush(client->output);
		client->last_output = now;
	}
}

static int fetch_and_copy(struct client *client,
			  struct mailbox_transaction_context *t,
			  struct mail_search_arg *search_args)
{
	struct mail_search_context *search_ctx;
        struct mailbox_transaction_context *src_trans;
	struct mail_keywords *keywords;
	const char *const *keywords_list;
	struct mail *mail;
	unsigned int copy_count = 0;
	int ret;

	src_trans = mailbox_transaction_begin(client->mailbox, 0);
	search_ctx = mailbox_search_init(src_trans, NULL, search_args, NULL);

	mail = mail_alloc(src_trans, MAIL_FETCH_STREAM_HEADER |
			  MAIL_FETCH_STREAM_BODY, NULL);
	ret = 1;
	while (mailbox_search_next(search_ctx, mail) > 0 && ret > 0) {
		if (mail->expunged) {
			ret = 0;
			break;
		}

		if ((++copy_count % COPY_CHECK_INTERVAL) == 0)
			client_send_sendalive_if_needed(client);

		keywords_list = mail_get_keywords(mail);
		keywords = strarray_length(keywords_list) == 0 ? NULL :
			mailbox_keywords_create(t, keywords_list);
		if (mailbox_copy(t, mail, mail_get_flags(mail),
				 keywords, NULL) < 0)
			ret = mail->expunged ? 0 : -1;
		mailbox_keywords_free(t, &keywords);
	}
	mail_free(&mail);

	if (mailbox_search_deinit(&search_ctx) < 0)
		ret = -1;

	if (mailbox_transaction_commit(&src_trans, 0) < 0)
		ret = -1;

	return ret;
}


static bool cmd_copy_antispam(struct client_command_context *cmd)
{
	struct client *client = cmd->client;
	struct mail_storage *storage;
	struct mailbox *destbox;
	struct mailbox_transaction_context *t;
        struct mail_search_arg *search_arg;
	const char *messageset, *mailbox;
        enum mailbox_sync_flags sync_flags = 0;
	int ret;

	/* <message set> <mailbox> */
	if (!client_read_string_args(cmd, 2, &messageset, &mailbox))
		return FALSE;

	if (!client_verify_open_mailbox(cmd))
		return TRUE;

	/* open the destination mailbox */
	if (!client_verify_mailbox_name(cmd, mailbox, TRUE, FALSE))
		return TRUE;

	search_arg = imap_search_get_arg(cmd, messageset, cmd->uid);
	if (search_arg == NULL)
		return TRUE;

	storage = client_find_storage(cmd, &mailbox);
	if (storage == NULL)
		return TRUE;

	if (mailbox_equals(client->mailbox, storage, mailbox))
		destbox = client->mailbox;
	else {
		destbox = mailbox_open(storage, mailbox, NULL,
				       MAILBOX_OPEN_SAVEONLY |
				       MAILBOX_OPEN_FAST |
				       MAILBOX_OPEN_KEEP_RECENT);
		if (destbox == NULL) {
			client_send_storage_error(cmd, storage);
			return TRUE;
		}
	}

	t = mailbox_transaction_begin(destbox,
				      MAILBOX_TRANSACTION_FLAG_EXTERNAL);
	ret = fetch_and_copy(client, t, search_arg);

	if (ret <= 0)
		mailbox_transaction_rollback(&t);
	else {
		if (mailbox_transaction_commit(&t, 0) < 0)
			ret = -1;
	}

	if (destbox != client->mailbox) {
		sync_flags |= MAILBOX_SYNC_FLAG_FAST;
		mailbox_close(&destbox);
	}

	if (ret > 0)
		return cmd_sync(cmd, sync_flags, 0, "OK Copy completed.");
	else if (ret == 0) {
		/* some messages were expunged, sync them */
		return cmd_sync(cmd, 0, 0,
			"NO Some of the requested messages no longer exist.");
	} else {
		client_send_storage_error(cmd, storage);
		return TRUE;
	}
}


static bool cmd_append_antispam(struct client_command_context *cmd)
{
	const char *mailbox;
	struct mail_storage *storage;
	struct mailbox *box;

	/* <mailbox> */
	if (!client_read_string_args(cmd, 1, &mailbox))
		return FALSE;

	storage = client_find_storage(cmd, &mailbox);
	if (storage == NULL)
		return FALSE;
	box =
	    mailbox_open(storage, mailbox, NULL,
			 MAILBOX_OPEN_FAST | MAILBOX_OPEN_KEEP_RECENT);
	if (box != NULL) {

		if (mailbox_equals(box, storage, "SPAM")) {
			mailbox_close(&box);
			return cmd_sync(cmd, 0, 0,
					"NO Cannot APPEND to SPAM box, sorry.");
		}

		mailbox_close(&box);
	}

	return cmd_append(cmd);
}


void dspam_init(void)
{
	command_unregister("COPY");
	command_unregister("APPEND");
	command_unregister("UID COPY");
	/*
	 * i_strdup() here is a kludge to avoid crashing in commands_deinit()
	 * since modules are unloaded before it's called, this "COPY" string
	 * would otherwise point to nonexisting memory.
	 */
	command_register(i_strdup("COPY"), cmd_copy_antispam);
	command_register(i_strdup("UID COPY"), cmd_copy_antispam);
	command_register(i_strdup("APPEND"), cmd_append_antispam);
}

void dspam_deinit(void)
{
}