bincimap

Log | Files | Refs | LICENSE

operator-rename.cc (4254B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    operator-rename.cc
      5  *  
      6  *  Description:
      7  *    Implementation of the RENAME command.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #include <string>
     39 #include <iostream>
     40 
     41 #include <dirent.h>
     42 #include <sys/stat.h>
     43 #include <sys/types.h>
     44 
     45 #include "mailbox.h"
     46 #include "io.h"
     47 #include "storage.h"
     48 #include "convert.h"
     49 
     50 #include "recursivedescent.h"
     51 
     52 #include "session.h"
     53 #include "depot.h"
     54 #include "operators.h"
     55 
     56 using namespace ::std;
     57 using namespace Binc;
     58 
     59 //----------------------------------------------------------------------
     60 RenameOperator::RenameOperator(void)
     61 {
     62 }
     63 
     64 //----------------------------------------------------------------------
     65 RenameOperator::~RenameOperator(void)
     66 {
     67 }
     68 
     69 //----------------------------------------------------------------------
     70 const string RenameOperator::getName(void) const
     71 {
     72   return "RENAME";
     73 }
     74 
     75 //----------------------------------------------------------------------
     76 int RenameOperator::getState(void) const
     77 {
     78   return Session::AUTHENTICATED | Session::SELECTED;
     79 }
     80 
     81 //------------------------------------------------------------------------
     82 Operator::ProcessResult RenameOperator::process(Depot &depot,
     83 						Request &command)
     84 {
     85   Session &session = Session::getInstance();
     86 
     87   const string &srcmailbox = command.getMailbox();
     88   const string &canonmailbox = toCanonMailbox(srcmailbox);
     89   const string &canondestmailbox = toCanonMailbox(command.getNewMailbox());
     90 
     91   // renaming INBOX should actually create the destination mailbox,
     92   // move over all the messages and then leave INBOX empty.
     93   if (canonmailbox == "INBOX") {
     94     session.setLastError("Sorry, renaming INBOX is not yet supported"
     95 			 " by this IMAP server. Try copying the messages"
     96 			 " instead");
     97     return NO;
     98   }
     99 
    100   if (canondestmailbox == "INBOX") {
    101     session.setLastError("It is not allowed to rename a mailbox to INBOX");
    102     return NO;
    103   }
    104 
    105   if (depot.renameMailbox(canonmailbox, canondestmailbox))
    106     return OK;
    107   else
    108     return NO;
    109 }
    110 
    111 //----------------------------------------------------------------------
    112 Operator::ParseResult RenameOperator::parse(Request &c_in) const
    113 {
    114   Session &session = Session::getInstance();
    115  
    116   if (c_in.getUidMode())
    117     return REJECT;
    118 
    119   Operator::ParseResult res;
    120   if ((res = expectSPACE()) != ACCEPT) {
    121     session.setLastError("Expected SPACE after RENAME");
    122     return res;
    123   }
    124 
    125   string mailbox;
    126   if ((res = expectMailbox(mailbox)) != ACCEPT) {
    127     session.setLastError("Expected mailbox after RENAME SPACE");
    128     return res;
    129   }
    130 
    131   if ((res = expectSPACE()) != ACCEPT) {
    132     session.setLastError("Expected SPACE after RENAME SPACE mailbox");
    133     return res;
    134   }
    135 
    136   string newmailbox;
    137   if ((res = expectMailbox(newmailbox)) != ACCEPT) {
    138     session.setLastError("Expected mailbox after RENAME SPACE"
    139 			 " mailbox SPACE");
    140     return res;
    141   }
    142 
    143   if ((res = expectCRLF()) != ACCEPT) {
    144     session.setLastError("Expected CRLF after RENAME SPACE"
    145 			 " mailbox SPACE mailbox");
    146     return res;
    147   }
    148 
    149   session.mailboxchanges = true;
    150 
    151   c_in.setName("RENAME");
    152   c_in.setMailbox(mailbox);
    153   c_in.setNewMailbox(newmailbox);
    154   return ACCEPT;
    155 }