Removed the logging facility from citserver, use syslog instead
[citadel.git] / citadel / modules / spam / serv_spam.c
index b8ccfb8d69517b2acd76564acddb602c0e1145fa..df65ba6ef2a3e89449b5f24880c3d92a64a60030 100644 (file)
@@ -1,10 +1,24 @@
 /*
- * $Id$
- *
  * This module allows Citadel to use SpamAssassin to filter incoming messages
  * arriving via SMTP.  For more information on SpamAssassin, visit
  * http://www.spamassassin.org (the SpamAssassin project is not in any way
  * affiliated with the Citadel project).
+ *
+ * Copyright (c) 1998-2009 by the citadel.org team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #define SPAMASSASSIN_PORT       "783"
@@ -41,9 +55,7 @@
 #include "support.h"
 #include "config.h"
 #include "control.h"
-#include "room_ops.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -65,8 +77,8 @@ int spam_assassin(struct CtdlMessage *msg) {
        char buf[SIZ];
        int is_spam = 0;
        int sa;
-       char *msgtext;
-       size_t msglen;
+       StrBuf *msgtext;
+       CitContext *CCC=CC;
 
        /* For users who have authenticated to this server we never want to
         * apply spam filtering, because presumably they're trustworthy.
@@ -80,9 +92,9 @@ int spam_assassin(struct CtdlMessage *msg) {
        /* Try them one by one until we get a working one */
         for (sa=0; sa<num_sahosts; ++sa) {
                 extract_token(buf, sahosts, sa, '|', sizeof buf);
-                CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
-                sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
-                if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
+                syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
+                sock = sock_connect(buf, SPAMASSASSIN_PORT);
+                if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
         }
 
        if (sock < 0) {
@@ -92,46 +104,46 @@ int spam_assassin(struct CtdlMessage *msg) {
                return(0);
        }
 
+       CCC->SBuf.Buf = NewStrBuf();
+       CCC->sMigrateBuf = NewStrBuf();
+       CCC->SBuf.ReadWritePointer = NULL;
+
        /* Command */
-       CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
+       syslog(LOG_DEBUG, "Transmitting command\n");
        sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
 
        /* Message */
-       CC->redirect_buffer = malloc(SIZ);
-       CC->redirect_len = 0;
-       CC->redirect_alloc = SIZ;
+       CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
        CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
        msgtext = CC->redirect_buffer;
-       msglen = CC->redirect_len;
        CC->redirect_buffer = NULL;
-       CC->redirect_len = 0;
-       CC->redirect_alloc = 0;
 
-       sock_write(sock, msgtext, msglen);
-       free(msgtext);
+       sock_write(&sock, SKEY(msgtext));
+       FreeStrBuf(&msgtext);
 
        /* Close one end of the socket connection; this tells SpamAssassin
         * that we're done.
         */
-       sock_shutdown(sock, SHUT_WR);
+       if (sock != -1)
+               sock_shutdown(sock, SHUT_WR);
        
        /* Response */
-       CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
-        if (sock_getln(sock, buf, sizeof buf) < 0) {
+       syslog(LOG_DEBUG, "Awaiting response\n");
+        if (sock_getln(&sock, buf, sizeof buf) < 0) {
                 goto bail;
         }
-        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
+        syslog(LOG_DEBUG, "<%s\n", buf);
        if (strncasecmp(buf, "SPAMD", 5)) {
                goto bail;
        }
-        if (sock_getln(sock, buf, sizeof buf) < 0) {
+        if (sock_getln(&sock, buf, sizeof buf) < 0) {
                 goto bail;
         }
-        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-        CtdlLogPrintf(CTDL_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
+        syslog(LOG_DEBUG, "<%s\n", buf);
+        syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
         if (config.c_spam_flag_only) {
-                CtdlLogPrintf(CTDL_DEBUG, "flag spam code used");
+                syslog(LOG_DEBUG, "flag spam code used");
                int headerlen;
                int newmsgsize;
                int oldmsgsize;
@@ -162,7 +174,7 @@ int spam_assassin(struct CtdlMessage *msg) {
                memcpy(msg->cm_fields['M'],buf,headerlen);
 
        } else {
-                CtdlLogPrintf(CTDL_DEBUG, "reject spam code used");
+                syslog(LOG_DEBUG, "reject spam code used");
                if (!strncasecmp(buf, "Spam: True", 10)) {
                        is_spam = 1;
                }
@@ -176,6 +188,8 @@ int spam_assassin(struct CtdlMessage *msg) {
        }
 
 bail:  close(sock);
+       FreeStrBuf(&CCC->SBuf.Buf);
+       FreeStrBuf(&CCC->sMigrateBuf);
        return(is_spam);
 }
 
@@ -189,5 +203,5 @@ CTDL_MODULE_INIT(spam)
        }
        
        /* return our Subversion id for the Log */
-        return "$Id$";
+        return "spam";
 }