src/shepp/SheppCommandFunctions.H

Go to the documentation of this file.
00001 /* ${copyright}$ */
00002 /* $Id: SheppCommandFunctions.H 626 2006-03-22 17:15:31Z eduardo $ */
00006 #ifndef __SHEPP_COMMAND_FUNCTIONS_H__
00007 #define __SHEPP_COMMAND_FUNCTIONS_H__
00008 
00009 #include "SheppGlobal.H"
00010 #include "SheppStrUtil.H"
00011 #include "SheppObjSet.H"
00012 #include "SheppPrint.H"
00013 
00014 #include "IoException.H"
00015 #include "TransportException.H"
00016 #include "EppException.H"
00017 #include "XmlException.H"
00018 
00019 #include "Login.H"
00020 
00021 // Functions for eppsh commands
00022 int cmd_login __P((char *));
00023 int cmd_hello __P((char *));
00024 int cmd_poll __P((char *));
00025 int cmd_domain __P((char *));
00026 int cmd_contact __P((char *));
00027 int cmd_brorg __P((char *));
00028 
00029 int cmd_xmlcmd __P((char *));
00030 int cmd_xmlrsp __P((char *));
00031 
00032 int cmd_server __P((char *));
00033 int cmd_port __P((char *));
00034 int cmd_client_pem __P((char *));
00035 int cmd_root_pem __P((char *));
00036 int cmd_pass __P((char *));
00037 int cmd_user __P((char *));
00038 int cmd_login_pw __P((char *));
00039 int cmd_login_new_pw __P((char *));
00040 int cmd_connect __P((char *));
00041 
00042 int cmd_help __P((char *));
00043 int cmd_about __P((char *));
00044 int cmd_quit __P((char *));
00045 
00046 int cmd_beauty __P((char *));
00047 int cmd_debug __P((char *));
00048 
00050 void init_commands()
00051 {
00052   SheppCommand cmd;
00053 
00054   //non-EPP commands
00055   cmd.reset("?", cmd_help, "This help screen");
00056   _commands.insert(_commands.end(), cmd);
00057   cmd.reset("help", cmd_help, "This help screen");
00058   _commands.insert(_commands.end(), cmd);
00059   cmd.reset("about", cmd_about, "shepp version information");
00060   _commands.insert(_commands.end(), cmd);
00061   cmd.reset("exit", cmd_quit, "Exit shepp\n");
00062   _commands.insert(_commands.end(), cmd);
00063 
00064   //runtime flags
00065   cmd.reset("beauty", cmd_beauty, "Turn beautifier on/off");
00066   _commands.insert(_commands.end(), cmd);
00067   cmd.reset("debug", cmd_debug, "Turn debug messages on/off\n");
00068   _commands.insert(_commands.end(), cmd);
00069 
00070   //connection related commands
00071   cmd.reset("server", cmd_server, "Sets server address");
00072   _commands.insert(_commands.end(), cmd);
00073   cmd.reset("port", cmd_port, "Sets server port");
00074   _commands.insert(_commands.end(), cmd);
00075   cmd.reset("client-pem", cmd_client_pem,
00076             "Sets client.pem certificate file location");
00077   _commands.insert(_commands.end(), cmd);
00078   cmd.reset("root-pem", cmd_root_pem,
00079             "Sets root.pem certificate file location");
00080   _commands.insert(_commands.end(), cmd);
00081   cmd.reset("pass", cmd_pass, "Sets SSL certificate passphrase");
00082   _commands.insert(_commands.end(), cmd);  
00083   cmd.reset("user", cmd_user, "Sets EPP Login username");
00084   _commands.insert(_commands.end(), cmd);
00085   cmd.reset("pw", cmd_login_pw, "Sets EPP Login password");
00086   _commands.insert(_commands.end(), cmd);
00087   cmd.reset("newpw", cmd_login_new_pw, "Sets new EPP Login password");
00088   _commands.insert(_commands.end(), cmd);
00089   cmd.reset("connect", cmd_connect, "Establishes connection\n");
00090   _commands.insert(_commands.end(), cmd);
00091 
00092   //EPP commands
00093   cmd.reset("login", cmd_login, "EPP Login");
00094   _commands.insert(_commands.end(), cmd);
00095   cmd.reset("hello", cmd_hello, "EPP Hello");
00096   _commands.insert(_commands.end(), cmd);
00097   cmd.reset("poll", cmd_poll, "EPP Poll related actions");
00098   _commands.insert(_commands.end(), cmd);
00099   cmd.reset("domain", cmd_domain, "EPP Domain related actions");
00100   _commands.insert(_commands.end(), cmd);
00101   cmd.reset("contact", cmd_contact, "EPP Contact related actions");
00102   _commands.insert(_commands.end(), cmd);
00103   cmd.reset("brorg", cmd_brorg, "EPP BrOrg related actions\n");
00104   _commands.insert(_commands.end(), cmd);
00105 
00106   //XML related commands
00107   cmd.reset("xmlcmd", cmd_xmlcmd, "Show last command XML");
00108   _commands.insert(_commands.end(), cmd);
00109   cmd.reset("xmlrsp", cmd_xmlrsp, "Show last response XML");
00110   _commands.insert(_commands.end(), cmd);
00111 }
00112 
00114 
00118 const SheppCommand* find_command(string name)
00119 {
00120   CMD_CONTAINER<SheppCommand>::const_iterator it;
00121   for (it = _commands.begin(); it != _commands.end(); it++) {
00122     if ((*it).name == name) {
00123       return &(*it);
00124     }
00125   }
00126 
00127   return ((const SheppCommand *) NULL);
00128 }
00129 
00131 
00135 int cmd_server(char *arg)
00136 {
00137   vector<string> words = SheppStrUtil::parse_line(arg);
00138 
00139   if (words.size() != 0) {
00140     string new_port;
00141     string new_server;
00142     SheppStrUtil::split(words[0], new_server, new_port, ":", true);
00143     if (new_server != _server) {
00144       _server = new_server;
00145       _session->set_server(_server);
00146     }
00147     if (new_port != "" && _port != atoi(new_port.c_str())) {
00148       _port = atoi(new_port.c_str());
00149       _session->set_port(_port);
00150     }
00151   }
00152 
00153   if (_server != "") {
00154     printf("server %s:%d\n", _server.c_str(), _port);
00155   } else {
00156     printf("no server address given.\n");
00157   }
00158 
00159   return 0;
00160 }
00161 
00163 
00167 int cmd_port(char *arg)
00168 {
00169   vector<string> words = SheppStrUtil::parse_line(arg);
00170 
00171   if (words.size() == 1) {
00172     if (_port != atoi(words[0].c_str())) {
00173       _port = atoi(words[0].c_str());
00174       _session->set_port(_port);
00175     }
00176   }
00177 
00178   printf("port %d\n", _port);
00179 
00180   return 0;
00181 }
00182 
00184 
00188 int cmd_client_pem(char *arg)
00189 {
00190   vector<string> words = SheppStrUtil::parse_line(arg);
00191 
00192   if (words.empty()) {
00193     printf("Current client.pem is %s\n", _client_pem.c_str());
00194     return 0;
00195   } else if (words.size() > 1) {
00196     printf("usage: client-pem <file>\n");
00197     return -1;
00198   }
00199 
00200   _client_pem = words[0];
00201 
00202   return 0;
00203 }
00204 
00206 
00210 int cmd_root_pem(char *arg)
00211 {
00212   vector<string> words = SheppStrUtil::parse_line(arg);
00213 
00214   if (words.empty()) {
00215     printf("Current root.pem is %s\n", _root_pem.c_str());
00216     return 0;
00217   } else if (words.size() > 1) {
00218     printf("usage: root-pem <file>\n");
00219     return -1;
00220   }
00221 
00222   _root_pem = words[0];
00223 
00224   return 0;
00225 }
00226 
00228 
00232 int cmd_pass(char *arg)
00233 {
00234   _passphrase = (string) arg;
00235   return 0;
00236 }
00237 
00239 
00243 int cmd_user(char *arg)
00244 {
00245   vector<string> words = SheppStrUtil::parse_line(arg);
00246 
00247   if (words.size() == 0) {
00248     if (_user != "") {
00249       printf("username: %s\n", _user.c_str());
00250       return 0;
00251     } else {
00252       printf("no username given.\n");
00253       return -1;
00254     }
00255   }
00256 
00257   _user = words[0];
00258 
00259   return 0;
00260 }
00261 
00263 
00267 int cmd_login_pw(char *arg)
00268 {
00269   _login_pw = (string) arg;
00270   return 0;
00271 }
00272 
00274 
00278 int cmd_login_new_pw(char *arg)
00279 {
00280   _login_new_pw = (string) arg;
00281   _new_pw = true;
00282   return 0;
00283 }
00284 
00286 
00290 int cmd_connect(char *arg)
00291 {
00292   if (cmd_server(arg) != 0) {
00293     return -1;
00294   }
00295 
00296   bool exception = false;
00297 
00298   // Session code goes here
00299   try {
00300     _session = auto_ptr<Session>(new Session(_server, _port));
00301     _session->connect(_client_pem, _root_pem, _passphrase);
00302     printf("Connected to %s\n", _server.c_str());
00303     
00304     Greeting *greeting = _session->get_greeting();  
00305     if (_debug) {
00306       if (greeting) {
00307         printf("Greeting received\n");
00308       }
00309     } //_debug
00310 
00311     _connected = true;
00312 
00313   } catch (const IoException &e) {
00314     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00315            e.get_code(), e.get_msg().c_str());
00316     exception = true;
00317   } catch (const TransportException &e) {
00318     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00319            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00320     exception = true;
00321   } catch (const GeneralException &e) {
00322     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00323            e.get_code(), e.get_msg().c_str());
00324     exception = true;
00325   }
00326 
00327   if (exception) {
00328 #if 0
00329     printf("error connecting to %s:%d\n", _server.c_str(), _port);
00330 #endif
00331     return -1;
00332   }
00333 
00334   return 0;
00335 }
00336 
00338 
00342 int cmd_beauty(char *arg)
00343 {
00344   vector<string> words = SheppStrUtil::parse_line(arg);
00345 
00346   if (words.empty()) {
00347     if (_beauty) {
00348       printf("Beautifier is ON\n");
00349     } else {
00350       printf("Beautifier is OFF\n");
00351     }
00352     return 0;
00353   } else if (words.size() > 1) {
00354     printf("usage: beauty [on|off]\n");
00355     return -1;
00356   }
00357 
00358   if (words[0] == "on") {
00359     _beauty = true;
00360     printf("Beautifier now ON\n");
00361   } else if (words[0] == "off") {
00362     _beauty = false;
00363     printf("Beautifier now OFF\n");
00364   } else {
00365     printf("usage: beauty [on|off]\n");
00366     return -1;
00367   }
00368 
00369   return 0;
00370 }
00371 
00373 
00377 int cmd_debug(char *arg)
00378 {
00379   vector<string> words = SheppStrUtil::parse_line(arg);
00380 
00381   if (words.empty()) {
00382     if (_debug) {
00383       printf("Debug is ON\n");
00384     } else {
00385       printf("Debug is OFF\n");
00386     }
00387     return 0;
00388   } else if (words.size() > 1) {
00389     printf("usage: beauty [on|off]\n");
00390     return -1;
00391   }
00392 
00393   if (words[0] == "on") {
00394     _debug = true;
00395     printf("Debug now ON\n");
00396   } else if (words[0] == "off") {
00397     _debug = false;
00398     printf("Debug now OFF\n");
00399   } else {
00400     printf("usage: beauty [on|off]\n");
00401     return -1;
00402   }
00403 
00404   return 0;
00405 }
00406 
00408 
00412 int cmd_xmlcmd(char *arg)
00413 {
00414   if (_beauty) {
00415     if (_session->get_last_command() == "") {
00416       printf("\n");
00417       return 0;
00418     }
00419     try {
00420       StrUtil str_util;
00421       printf("%s\n",
00422              str_util.xml_beautifier(_session->get_last_command()).c_str());
00423     } catch (const XmlException &e) {
00424       printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00425              e.get_msg().c_str());
00426     }
00427   } else {
00428     printf("%s\n", _session->get_last_command().c_str());
00429   }
00430   return 0;
00431 }
00432 
00434 
00438 int cmd_xmlrsp(char *arg)
00439 {
00440   if (_beauty) {
00441     if (_session->get_last_response() == "") {
00442       printf("\n");
00443       return 0;
00444     }
00445     try {
00446       StrUtil str_util;
00447       printf("%s\n",
00448              str_util.xml_beautifier(_session->get_last_response()).c_str());
00449     } catch (const XmlException &e) {
00450       printf("ERROR! XML Exception [%d]:\n[%s]\n", e.get_code(),
00451              e.get_msg().c_str());
00452     }
00453   } else {
00454     printf("%s\n", _session->get_last_response().c_str());
00455   }
00456   return 0;
00457 }
00458 
00459 // EPP action processing functions
00460 
00462 void print_cmd_sent_ok()
00463 {
00464   printf("Ok! Use 'xmlcmd' and 'xmlrsp' to view command/response "
00465          "XML code.\n");
00466 }
00467 
00469 
00472 int process_action(Action &act)
00473 {
00474   if (!_connected) {
00475     printf("not connected\n");
00476     return -1;
00477   }
00478 
00479   bool exception = false;
00480 
00481   try {
00482     _session->process_action(&act);
00483   } catch (const EppException &e) {
00484     printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00485            e.get_msg().c_str());  
00486     exception = true;
00487   } catch (const IoException &e) {
00488     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00489            e.get_code(), e.get_msg().c_str());
00490     exception = true;
00491     _connected = false;
00492   } catch (const TransportException &e) {
00493     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00494            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00495     exception = true;
00496     _connected = false;
00497   } catch (const GeneralException &e) {
00498     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00499            e.get_code(), e.get_msg().c_str());
00500     exception = true;
00501   }
00502 
00503   if (exception) {
00504     return -1;
00505   }
00506 
00507   print_cmd_sent_ok();
00508   return 0;
00509 }
00510 
00512 
00516 int cmd_login(char *arg)
00517 {
00518   if (!_connected) {
00519     printf("not connected\n");
00520     return -1;
00521   }
00522 
00523   if (_user == "") {
00524     printf("no username given.\n");
00525     return -1;
00526   }
00527 
00528   Login act;
00529   LoginCmd *cmd = act.get_command();
00530 
00531   cmd->set_clID(_user);
00532   cmd->set_pw(_login_pw);
00533 
00534   if (_new_pw) {
00535     _new_pw = false;
00536     cmd->set_new_pw(_login_new_pw);
00537   }
00538 
00539   if (process_action(act) != 0) {
00540     return -1;
00541   }  
00542 
00543   return 0;
00544 }
00545 
00547 
00551 int cmd_hello(char *arg)
00552 {
00553   if (!_connected) {
00554     printf("not connected\n");
00555     return -1;
00556   }
00557 
00558   bool exception = false;
00559 
00560   try {
00561     if (_debug) {
00562       printf("Sending EPP Hello\n");
00563     }
00564     _session->send_hello();
00565   } catch (const EppException &e) {
00566     printf("ERROR! EPP Exception [%d]:\n[%s]\n", e.get_code(),
00567            e.get_msg().c_str());  
00568     exception = true;
00569   } catch (const IoException &e) {
00570     printf("ERROR! IO Exception [%d]:\n[%s]\n", 
00571            e.get_code(), e.get_msg().c_str());
00572     exception = true;
00573     _connected = false;
00574   } catch (const TransportException &e) {
00575     printf("ERROR! Transport Exception [%d]:\n[%s]\n[%s]\n",
00576            e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
00577     exception = true;
00578     _connected = false;
00579   } catch (const GeneralException &e) {
00580     printf("ERROR! General Exception [%d]:\n[%s]\n", 
00581            e.get_code(), e.get_msg().c_str());
00582     exception = true;
00583   }
00584 
00585   if (exception) {
00586     return -1;
00587   }
00588 
00589   print_cmd_sent_ok();
00590   return 0;
00591 }
00592 
00593 // non-EPP commands
00594 
00596 
00600 int cmd_quit(char *arg)
00601 {
00602   printf("Bye\n");
00603   exit(0);
00604 }
00605 
00607 
00611 int cmd_help(char *arg)
00612 {
00613   CMD_CONTAINER<SheppCommand>::const_iterator it;
00614   for (it = _commands.begin(); it != _commands.end(); it++) {
00615     printf("%-16s %s\n", (*it).name.c_str(), (*it).brief.c_str());
00616   }
00617 
00618   return 0;
00619 }
00620 
00622 
00626 int cmd_about(char *arg)
00627 {
00628   printf("shepp version " SHEPP_VERSION ", an EPP client shell!\n");
00629   printf("Copyright 2006 Registro.br <libepp@registro.br>\n");
00630   printf("shepp is distributed with libepp-nicbr: "
00631          "http://registro.br/epp/index-EN.html\n");
00632 #if USE_BR_DOMAINS
00633   printf("Built with BrDomain EPP extension support.\n");
00634 #endif //USE_BR_DOMAINS
00635 
00636   return 0;
00637 }
00638 
00639 // -------------------------------------
00640 // Buggy Auto-complete related functions
00641 // -------------------------------------
00642 
00643 // char** shepp_completion __P((const char *, int, int));
00644 
00645 // // Attempts to auto-complete "text" with known commands
00646 // char** cpp_completion(string txt)
00647 // {
00648 //   set<string> matches_set;
00649 //   int len = strlen(txt.c_str());
00650 
00651 //   set<SheppCommand>::const_iterator it;
00652 //   for (it = commands.begin(); it != commands.end(); it++) {
00653 //     if (strncmp((*it).name.c_str(), txt.c_str(), len) == 0) {
00654 //       matches_set.insert((*it).name);
00655 //     }
00656 //   }
00657 
00658 //   char **matches = (char **) NULL;
00659 
00660 //   if (matches_set.size() > 0) {
00661 //     const int _DIMENSION = 128;
00662 //     matches = new char*[_DIMENSION];
00663 //     int j;
00664 //       for (j = 0; j < _DIMENSION; j++) {
00665 //         matches[j] = new char[_DIMENSION];
00666 //      bzero(matches[j], _DIMENSION);
00667 //       }
00668     
00669 //     set<string>::const_iterator it_str;
00670 //     for (it_str = matches_set.begin(), j = 0;
00671 //       it_str != matches_set.end();
00672 //       it_str++, j++) {
00673 //       matches[j] = new char[_DIMENSION];
00674 //       strncpy(matches[j], (*it_str).c_str(), strlen((*it_str).c_str()) + 1);
00675 // #if 0
00676 //       printf("found: [%s]\n", matches[j]);
00677 // #endif
00678 //     }
00679 //   }
00680 
00681 //   return matches;
00682 // }
00683 
00684 // char** shepp_completion (const char *text, int start, int end)
00685 // {
00686 //   char **matches = (char **)NULL;;
00687 
00688 //   // if 'text' starts at position '0' then it should be checked
00689 //   // against the command list
00690 //   if (start == 0) {
00691 //     //  matches = rl_completion_matches(text, command_generator);
00692 //     matches = cpp_completion(text);
00693 //   }
00694 
00695 //   return (matches);
00696 // }
00697 
00698 #endif //__SHEPP_COMMAND_FUNCTIONS_H__

Generated on Wed Mar 22 14:18:27 2006 for libepp_nicbr by  doxygen 1.4.6