commands/listvdomain.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 "config/configrc.h"
       
    19 #include "vpwentry/vpwentry.h"
       
    20 #include "cli/cli.h"
       
    21 #include "fdbuf/fdbuf.h"
       
    22 #include "vcommand.h"
       
    23 
       
    24 const char* cli_program = "listvdomain";
       
    25 const char* cli_help_prefix = "Lists the members of a virtual domain\n";
       
    26 const char* cli_help_suffix = "";
       
    27 const char* cli_args_usage = "[user [user ...]]";
       
    28 const int cli_args_min = 0;
       
    29 const int cli_args_max = -1;
       
    30 static int o_noaliases = false;
       
    31 static int o_nousers = false;
       
    32 static int o_quiet = false;
       
    33 
       
    34 // This program lists all the users in a domain.
       
    35 // The listing consists of one user per line,
       
    36 // and each line has three columns:
       
    37 // the virtual user name, the mailbox directory (or C<-> if none is set),
       
    38 // and an optional list of forwarding addresses, all seperated by a space.
       
    39 
       
    40 cli_option cli_options[] = {
       
    41   { 'a', "aliases", cli_option::flag, true, &o_nousers,
       
    42     "Show only accounts without a mailbox", 0 },
       
    43   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    44     "Suppress all status messages", 0 },
       
    45   { 'u', "users", cli_option::flag, true, &o_noaliases,
       
    46     "Show only accounts with a mailbox", 0 },
       
    47   {0}  
       
    48 };
       
    49 
       
    50 void show_user(const vpwentry& vpw)
       
    51 {
       
    52   if(o_noaliases && !vpw.mailbox)
       
    53     return;
       
    54   if(o_nousers && !!vpw.mailbox)
       
    55     return;
       
    56   fout << vpw.name;
       
    57   if(!vpw.mailbox)
       
    58     fout << " -";
       
    59   else {
       
    60     fout << ' ' << vpw.mailbox;
       
    61     if(!vpw.is_mailbox_enabled)
       
    62       fout << "(disabled)";
       
    63   }
       
    64   for(mystring_iter iter(vpw.forwards, '\0'); iter; ++iter)
       
    65     fout << ' ' << *iter;
       
    66   fout << '\n';
       
    67 }
       
    68 
       
    69 int cli_main(int argc, char* argv[])
       
    70 {
       
    71   if(!go_home())
       
    72     return 1;
       
    73 
       
    74   int errors = 0;
       
    75   
       
    76   if(o_noaliases && o_nousers) {
       
    77     if(!o_quiet)
       
    78       ferr << "listvdomain: specify only one of -a and -u" << endl;
       
    79     return 1;
       
    80   }
       
    81   
       
    82   vpwtable* table = domain.table();
       
    83 
       
    84   fout << "User Mailbox Aliases\n";
       
    85   
       
    86   if(argc) {
       
    87     vpwentry* vpw;
       
    88     for(int i = 0; i < argc; i++) {
       
    89       vpw = table->getbyname(argv[i]);
       
    90       if(!vpw) {
       
    91 	if(!o_quiet)
       
    92 	  ferr << "listvdomain: unknown user '" << argv[i] << "'" << endl;
       
    93 	errors = 1;
       
    94       }
       
    95       else {
       
    96 	show_user(*vpw);
       
    97 	delete vpw;
       
    98       }
       
    99     }
       
   100   }
       
   101   else {
       
   102     vpwtable_reader* r = table->start_read();
       
   103     if(!*r) {
       
   104       if(!o_quiet)
       
   105 	ferr << "listvdomain: Can't open password table" << endl;
       
   106       return 1;
       
   107     }
       
   108     vpwentry vpw;
       
   109     while(r->get(vpw))
       
   110       show_user(vpw);
       
   111     delete r;
       
   112   }
       
   113   return errors;
       
   114 }