commands/vchforwards.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include "fdbuf/fdbuf.h"
       
    19 #include "mystring/mystring.h"
       
    20 #include "config/configrc.h"
       
    21 #include "cli/cli.h"
       
    22 #include "vcommand.h"
       
    23 
       
    24 const char* cli_program = "vchforwards";
       
    25 const char* cli_help_prefix =
       
    26 "Change virtual user forwarding addresses.\n";
       
    27 const char* cli_help_suffix =
       
    28 "If no forwarding addresses are given, forwarding is disabled.
       
    29 ";
       
    30 const char* cli_args_usage = "USERNAME [DESTINATION1 ...]";
       
    31 const int cli_args_min = 1;
       
    32 const int cli_args_max = -1;
       
    33 
       
    34 static int o_quiet = false;
       
    35 
       
    36 // This program replaces the virtual user's list of forwarding addresses
       
    37 // with the given list.
       
    38 
       
    39 cli_option cli_options[] = {
       
    40   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    41     "Suppress all status messages", 0 },
       
    42   {0}
       
    43 };
       
    44 
       
    45 // SEE ALSO
       
    46 //
       
    47 // vmailmgr(7)
       
    48 
       
    49 int cli_main(int argc, char* argv[])
       
    50 {
       
    51   if(!go_home())
       
    52     return 1;
       
    53 
       
    54   mystring username = argv[0];
       
    55   username = username.lower();
       
    56   
       
    57   vpwentry* vpw = domain.lookup(username, false);
       
    58   if(!vpw) {
       
    59     if(!o_quiet)
       
    60       ferr << "vchforwards: User '" << username << "' does not exist." << endl;
       
    61     return 1;
       
    62   }
       
    63 
       
    64   for(int i = 1; i < argc; i++) {
       
    65     response resp1 = domain.validate_forward(argv[i]);
       
    66     if(!resp1) {
       
    67       if(!o_quiet)
       
    68 	ferr << "vchforwards: error with forwarding address '" << argv[i]
       
    69 	     << "':\n  " << resp1.msg << endl;
       
    70       return 1;
       
    71     }
       
    72   }
       
    73   
       
    74   if(argc > 1) {
       
    75     mystring dest = argv[1];
       
    76     for(int i = 2; i < argc; i++)
       
    77       dest = dest + mystring::NUL + argv[i];
       
    78     vpw->forwards = dest;
       
    79   }
       
    80   else
       
    81     vpw->forwards = 0;
       
    82 
       
    83   response resp2 = domain.set(vpw, false);
       
    84   if(!resp2) {
       
    85     if(!o_quiet)
       
    86       ferr << "vchforwards: Could not change user '" << username << "':\n"
       
    87 	"  " << resp2.msg << endl;
       
    88     return 1;
       
    89   }
       
    90   if(!o_quiet)
       
    91     fout << "vchforwards: User '" << username << "' successfully changed."
       
    92 	 << endl;
       
    93   return 0;
       
    94 }