daemon/dispatch.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 "daemon.h"
       
    19 #include "misc/lookup.h"
       
    20 
       
    21 // declare the commands
       
    22 extern CMD(adduser2);
       
    23 extern CMD(autoresponse);
       
    24 extern CMD(chattr);
       
    25 extern CMD(check);
       
    26 extern CMD(deluser);
       
    27 extern CMD(listdomain);
       
    28 extern CMD(lookup);
       
    29 
       
    30 #ifdef TEST_DAEMON
       
    31 CMD(echo)
       
    32 {
       
    33   logcommand(args);
       
    34   mystring msg;
       
    35   for(unsigned i = 0; i < args.count(); i++) {
       
    36     if(i > 0) msg += " ";
       
    37     msg += args[i];
       
    38   }
       
    39   RETURN(ok, msg);
       
    40 }
       
    41 CMD(fecho)
       
    42 {
       
    43   logcommand(args);
       
    44   mystring msg;
       
    45   for(unsigned i = 0; i < args.count(); i++) {
       
    46     if(i > 0) msg += " ";
       
    47     msg += args[i];
       
    48   }
       
    49   RETURN(ok, msg);
       
    50 }
       
    51 #endif // TEST_DAEMON
       
    52 
       
    53 //CMD(stat);
       
    54 //CMD(statall);
       
    55 
       
    56 struct dispatch 
       
    57 {
       
    58   const char* name;
       
    59   response (*function)(command&, int);
       
    60   unsigned arg_min;
       
    61   unsigned arg_max;
       
    62   bool decode_virtual;
       
    63   //unsigned count;
       
    64 };
       
    65 
       
    66 #define ENTRY(NAME,MIN,MAX,DV) { #NAME , NAME##_cmd , unsigned(MIN), unsigned(MAX), DV }
       
    67 dispatch dispatch_table[] = {
       
    68   ENTRY(lookup,       3,  3, true),
       
    69   ENTRY(check,        3,  3, true),
       
    70   ENTRY(chattr,       5, -1, true),
       
    71   ENTRY(adduser2,     5, -1, true),
       
    72   ENTRY(autoresponse, 4,  5, true),
       
    73   ENTRY(deluser,      3,  3, true),
       
    74   ENTRY(listdomain,   2,  2, false),
       
    75 #ifdef TEST_DAEMON
       
    76   ENTRY(echo,         0, -1, false), // For testing purposes only
       
    77   ENTRY(fecho,        0, -1, false), // For testing purposes only
       
    78 #endif // TEST_DAEMON
       
    79   { "", 0, 0, false, 0 }
       
    80 };
       
    81 //ENTRY(stat, 1, , false),
       
    82 //ENTRY(statall, 0, 0, false),
       
    83 
       
    84 static dispatch* find_dispatch(mystring name)
       
    85 {
       
    86   for(dispatch* ptr = dispatch_table; ptr->function != 0; ptr++) {
       
    87     if(name == ptr->name)
       
    88       return ptr;
       
    89   }
       
    90   return 0;
       
    91 }
       
    92 
       
    93 #if 0
       
    94 CMD(stat)
       
    95   // Usage: stat function
       
    96   // Returns: count
       
    97 {
       
    98   logcommand(args);
       
    99   const dispatch* ptr = find_dispatch(args[0]);
       
   100   if(!ptr)
       
   101     RETURN(err, "Unknown operation to stat");
       
   102   RETURN(ok, itoa(ptr->count));
       
   103 }
       
   104 
       
   105 CMD(statall)
       
   106   // Usage: statall
       
   107   // Returns: name:count;name:count...
       
   108 {
       
   109   logcommand(args);
       
   110   mystring r;
       
   111   for(const dispatch* ptr = dispatch_table; ptr->function != 0; ptr++) {
       
   112     if(!r.empty())
       
   113       r += ";";
       
   114     r += ptr->name;
       
   115     r += ":";
       
   116     r += itoa(ptr->count);
       
   117   }
       
   118   RETURN(ok, r);
       
   119 }
       
   120 #endif
       
   121 
       
   122 response dispatch_cmd(command& args, int fd)
       
   123 {
       
   124   dispatch* ptr = find_dispatch(args.name());
       
   125   if(ptr) {
       
   126     if(args.count() < ptr->arg_min ||
       
   127        (ptr->arg_max != unsigned(-1) && args.count() > ptr->arg_max))
       
   128       RETURN(bad, "Incorrect number of parameters to command " + args.name());
       
   129     //++ptr->count;
       
   130     if(ptr->decode_virtual) {
       
   131       if(is_local(args[0]))
       
   132 	args.replace_first_two(args[1]);
       
   133       else {
       
   134 	mystring baseuser(find_virtual(args[0]));
       
   135 	if(baseuser.empty())
       
   136 	  RETURN(err, "Invalid or unknown domain name: " + args[0]);
       
   137 	else if(!args[1])
       
   138 	  args.replace_first_two(baseuser);
       
   139 	else
       
   140 	  args.replace_first_two(baseuser + "-" + args[1]);
       
   141       }
       
   142     }
       
   143     return ptr->function(args, fd);
       
   144   }
       
   145   else
       
   146     RETURN(bad, "Invalid operation");
       
   147 }