* migrate redirect_buffer logic to StrBuf, valgrind reported some related errors.
[citadel.git] / citadel / modules / pop3 / serv_pop3.c
index aee455fed4e5d7b23b0eccd0bb1c598671e114b7..7dd317db3f7fe17f12699b34fdd83c574a6bc0bc 100644 (file)
@@ -2,8 +2,22 @@
  * $Id$ 
  *
  * POP3 service for the Citadel system
- * Copyright (C) 1998-2001 by Art Cancro and others.
- * This code is released under the terms of the GNU General Public License.
+ *
+ * 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
  *
  * Current status of standards conformance:
  *
@@ -47,7 +61,6 @@
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
 #include "database.h"
@@ -156,15 +169,10 @@ void pop3_add_message(long msgnum, void *userdata) {
         */
        GetMetaData(&smi, msgnum);
        if (smi.meta_rfc822_length <= 0L) {
-               CC->redirect_buffer = malloc(SIZ);
-               CC->redirect_len = 0;
-               CC->redirect_alloc = SIZ;
-               CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, 0);
-               smi.meta_rfc822_length = CC->redirect_len;
-               free(CC->redirect_buffer);
-               CC->redirect_buffer = NULL;
-               CC->redirect_len = 0;
-               CC->redirect_alloc = 0;
+               CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+               CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
+               smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
+               FreeStrBuf(&CC->redirect_buffer); /* TODO: WHEW, all this for just knowing the length???? */
                PutMetaData(&smi);
        }
        POP3->msgs[POP3->num_msgs-1].rfc822_length = smi.meta_rfc822_length;
@@ -181,7 +189,7 @@ int pop3_grab_mailbox(void) {
         struct visit vbuf;
        int i;
 
-       if (getroom(&CC->room, MAILROOM) != 0) return(-1);
+       if (CtdlGetRoom(&CC->room, MAILROOM) != 0) return(-1);
 
        /* Load up the messages */
        CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
@@ -252,7 +260,7 @@ void pop3_apop(char *argbuf)
        return;
    }
    
-   if (getuser(&CC->user, CC->curr_user))
+   if (CtdlGetUser(&CC->user, CC->curr_user))
    {
        cprintf("-ERR No such user.\r\n");
        return;
@@ -373,7 +381,10 @@ void pop3_retr(char *argbuf) {
        }
 
        cprintf("+OK Message %d:\r\n", which_one);
-       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, ESC_DOT);
+       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
+               MT_RFC822, HEADERS_ALL, 0, 1, NULL,
+               (ESC_DOT|SUPPRESS_ENV_TO)
+       );
        cprintf(".\r\n");
 }
 
@@ -386,8 +397,8 @@ void pop3_top(char *argbuf) {
        int lines_requested = 0;
        int lines_dumped = 0;
        char buf[1024];
-       char *msgtext;
-       char *ptr;
+       StrBuf *msgtext;
+       const char *ptr;
        int in_body = 0;
        int done = 0;
 
@@ -402,20 +413,15 @@ void pop3_top(char *argbuf) {
                return;
        }
 
-       CC->redirect_buffer = malloc(SIZ);
-       CC->redirect_len = 0;
-       CC->redirect_alloc = SIZ;
-       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
-                     MT_RFC822, HEADERS_ALL, 0, 1, NULL, 0);
+       CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
        msgtext = CC->redirect_buffer;
        CC->redirect_buffer = NULL;
-       CC->redirect_len = 0;
-       CC->redirect_alloc = 0;
 
        cprintf("+OK Message %d:\r\n", which_one);
-
-       ptr = msgtext;
-
+       
+       ptr = ChrPtr(msgtext);
+       // TODO: use buffer stuff here
        while (ptr = memreadline(ptr, buf, (sizeof buf - 2)),
              ( (*ptr != 0) && (done == 0))) {
                strcat(buf, "\r\n");
@@ -434,7 +440,7 @@ void pop3_top(char *argbuf) {
        }
 
        if (buf[strlen(buf)-1] != 10) cprintf("\n");
-       free(msgtext);
+       FreeStrBuf(&msgtext);
 
        cprintf(".\r\n");
 }
@@ -489,14 +495,14 @@ void pop3_update(void) {
 
        /* Set last read pointer */
        if (POP3->num_msgs > 0) {
-               lgetuser(&CC->user, CC->curr_user);
+               CtdlGetUserLock(&CC->user, CC->curr_user);
 
                CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
                snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld",
                        POP3->msgs[POP3->num_msgs-1].msgnum);
                CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
 
-               lputuser(&CC->user);
+               CtdlPutUserLock(&CC->user);
        }
 
 }
@@ -671,6 +677,11 @@ void pop3_command_loop(void) {
        else if (!CC->logged_in) {
                cprintf("-ERR Not logged in.\r\n");
        }
+       
+       else if (CC->nologin) {
+               cprintf("-ERR System busy, try later.\r\n");
+               CC->kill_me = 1;
+       }
 
        else if (!strncasecmp(cmdbuf, "LIST", 4)) {
                pop3_list(&cmdbuf[5]);