src/shepp/AsnFunctions.H

Go to the documentation of this file.
00001 /* ${copyright}$ */
00002 /* $Id: AsnFunctions.H 991 2009-01-06 16:29:22Z rafael $ */
00006 #ifndef __ASN_FUNCTIONS_H__
00007 #define __ASN_FUNCTIONS_H__
00008 #include "SheppCommandFunctions.H"
00009 
00010 #include "AsnCheck.H"
00011 #include "AsnCreate.H"
00012 #include "AsnDelete.H"
00013 #include "AsnInfo.H"
00014 #include "AsnRenew.H"
00015 #include "AsnTransfer.H"
00016 #include "AsnUpdate.H"
00017 
00019 
00024 int cmd_asn_help(string error_msg, string specific = "")
00025 {
00026   if (error_msg != "") {
00027     printf("error: %s\n", error_msg.c_str());
00028   }
00029 
00030   printf("asn command syntax help:\n");
00031   printf("\n");
00032 
00033   if (specific == "" || specific == "check") {
00034     printf("  check <asn> [asn ...]\n");
00035     printf("\n");
00036   }
00037   if (specific == "" || specific == "create") {
00038     printf("  create <asn> <-o organization> <-contact type=value> "
00039            "[-contact type=value ...]\n");
00040     printf("\n");
00041   }
00042   if (specific == "" || specific == "delete") {
00043     printf("  delete <asn>\n");
00044     printf("\n");
00045   }
00046   if (specific == "" || specific == "info") {
00047     printf("  info <asn>\n");
00048     printf("\n");
00049   }
00050   if (specific == "" || specific == "renew") {
00051     printf("  renew <asn> <-expdate expDate> [-period time:unit]\n");
00052     printf("\n");
00053   }
00054   if (specific == "" || specific == "transfer") {
00055     printf("  transfer <asn> <-op [query|request]>\n");
00056     printf("\n");
00057   }
00058   if (specific == "" || specific == "update") {
00059     printf("  update <asn> [-add-contact type=value ...]\n"
00060            "    [-rem-contact type=value ...] [-o organization]\n");
00061     printf("\n");
00062   }
00063 
00064   if (error_msg != "") {
00065     return -1;
00066   }
00067 
00068   return 0;
00069 }
00070 
00072 
00076 int cmd_asn_check(vector<string> &args)
00077 {
00078   string cmd_name = "check";
00079 
00080   AsnCheck act;
00081   AsnCheckCmd *cmd = act.get_command();
00082 
00083   if (args.empty()) {
00084     return cmd_asn_help("no ASNs specified", cmd_name);
00085   }
00086 
00087   while (!args.empty()) {
00088     int asn = atoi(args[0].c_str());
00089     if (asn > 0) {
00090       cmd->add_asn(asn);
00091       args.erase(args.begin());
00092     } else {
00093       return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00094     }
00095   }
00096 
00097   if (_debug) {
00098     list<int> asns = cmd->get_asn_list();
00099     printf("ASNs to be checked:\n");
00100     list<int>::const_iterator asn;
00101     for (asn = asns.begin(); asn != asns.end(); asn++) {
00102       printf("  [%d]\n", (*asn));
00103     }
00104   } // _debug
00105 
00106   if (process_action(act) != 0) {
00107     return -1;
00108   }
00109 
00110   return 0;
00111 }
00112 
00114 
00118 int cmd_asn_create(vector<string> &args)
00119 {
00120   string cmd_name = "create";
00121 
00122   AsnCreate act;
00123   AsnCreateCmd *cmd = act.get_command();
00124 
00125   if (args.empty()) {
00126     return cmd_asn_help("no ASN specified", cmd_name);
00127   }
00128 
00129   int asn = atoi(args[0].c_str());
00130   if (asn <= 0) {
00131     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00132   }
00133   cmd->set_asn(asn);
00134   args.erase(args.begin());
00135 
00136   bool hasOrganization = false;
00137   bool hasContacts = false;
00138 
00139   while (!args.empty()) {
00140     if (args[0] == "-contact") {
00141       //contact
00142       hasContacts = true;
00143       args.erase(args.begin());
00144       if (args.empty()) {
00145         return cmd_asn_help("contact parameter missing", cmd_name);
00146       }
00147 
00148       string type;
00149       string value;
00150       if (SheppStrUtil::split(args[0], type, value, "=", false) != 0) {
00151         return cmd_asn_help("invalid contact", cmd_name);
00152       }
00153 
00154       cmd->insert_contact(type, value);
00155       args.erase(args.begin());      
00156     } else if (args[0] == "-o") {
00157       //set organization
00158       if (hasOrganization) {
00159         return cmd_asn_help("only one organization allowed per command",
00160                             cmd_name);
00161       } else {
00162         hasOrganization = true;
00163       }
00164       args.erase(args.begin());
00165       if (args.empty()) {
00166         return cmd_asn_help("organization parameter missing", cmd_name);
00167       }
00168       cmd->set_organization(args[0]);
00169       args.erase(args.begin());
00170     } else {
00171       return cmd_asn_help("invalid syntax near \"" + args[0] + "\"",
00172                           cmd_name);
00173     }
00174   }
00175 
00176   if (!hasOrganization) {
00177     return cmd_asn_help("no organization specified", cmd_name);
00178   }
00179 
00180   if (!hasContacts) {
00181     return cmd_asn_help("no contact specified", cmd_name);
00182   }
00183 
00184   if (_debug) {
00185     printf("ASN to be created: [%d]\n", cmd->get_asn());
00186     printf("organization: [%s]\n", cmd->get_organization().c_str());
00187 
00188     map< string, string, less<string> > contacts = cmd->get_contacts();
00189     map< string, string, less<string> >::const_iterator cit;
00190     for (cit = contacts.begin(); cit != contacts.end(); cit++) {
00191       printf("contact %s: %s\n", (*cit).first.c_str(), (*cit).second.c_str());
00192     }
00193   } // _debug
00194 
00195   if (process_action(act) != 0) {
00196     return -1;
00197   }
00198 
00199   return 0;
00200 }
00201 
00203 
00207 int cmd_asn_delete(vector<string> &args)
00208 {
00209   string cmd_name = "delete";
00210 
00211   AsnDelete act;
00212   AsnDeleteCmd *cmd = act.get_command();
00213 
00214   if (args.size() != 1) {
00215     return cmd_asn_help("exactly one ASN must be specified", cmd_name);
00216   }
00217 
00218   int asn = atoi(args[0].c_str());
00219   if (asn <= 0) {
00220     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00221   }
00222 
00223   cmd->set_asn(asn);
00224 
00225   if (_debug) {
00226     printf("ASN to be deleted: [%d]\n", cmd->get_asn());
00227   } // _debug
00228 
00229   if (process_action(act) != 0) {
00230     return -1;
00231   }
00232 
00233   return 0;
00234 }
00235 
00237 
00241 int cmd_asn_info(vector<string> &args)
00242 {
00243   string cmd_name = "info";
00244 
00245   AsnInfo act;
00246   AsnInfoCmd *cmd = act.get_command();
00247 
00248   if (args.size() != 1) {
00249     return cmd_asn_help("exactly one ASN must be specified", cmd_name);
00250   }
00251 
00252   int asn = atoi(args[0].c_str());
00253   if (asn <= 0) {
00254     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00255   }
00256 
00257   cmd->set_asn(asn);
00258 
00259   if (_debug) {
00260     printf("ASN to get info: [%d]\n", cmd->get_asn());
00261   } // _debug
00262 
00263   if (process_action(act) != 0) {
00264     return -1;
00265   }
00266 
00267   return 0;
00268 }
00269 
00271 
00275 int cmd_asn_renew(vector<string> &args)
00276 {
00277   string cmd_name = "renew";
00278 
00279   AsnRenew act;
00280   AsnRenewCmd *cmd = act.get_command();
00281 
00282   if (args.empty()) {
00283     return cmd_asn_help("no ASN specified", cmd_name);
00284   }
00285 
00286   int asn = atoi(args[0].c_str());
00287   if (asn <= 0) {
00288     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00289   }
00290   cmd->set_asn(asn);
00291   args.erase(args.begin());
00292 
00293   // mandatory field
00294   bool hasExpDate = false;
00295 
00296   while (!args.empty()) {
00297     if (args[0] == "-expdate") {
00298       //expdate
00299       if (hasExpDate) {
00300         return cmd_asn_help("only one expDate allowed per command",
00301                             cmd_name);
00302       } else {
00303         hasExpDate = true;
00304       }
00305       args.erase(args.begin());
00306       if (args.empty()) {
00307         return cmd_asn_help("expDate missing", cmd_name);
00308       }
00309       cmd->set_expDate(args[0]);
00310       args.erase(args.begin());      
00311     } else if (args[0] == "-period") {
00312       //period
00313       args.erase(args.begin());
00314       if (args.empty()) {
00315         return cmd_asn_help("period parameter missing", cmd_name);
00316       }
00317 
00318       string time;
00319       string unit;
00320       if (SheppStrUtil::split(args[0], time, unit, ":", false) != 0) {
00321         return cmd_asn_help("invalid period", cmd_name);
00322       }
00323 
00324       cmd->set_period(atoi(time.c_str()), unit);
00325       args.erase(args.begin());
00326     } else {
00327       return cmd_asn_help("invalid syntax near \"" + args[0] + "\"",
00328                           cmd_name);
00329     }
00330   }
00331 
00332   // check mandatory fields
00333   if (!hasExpDate) {
00334     return cmd_asn_help("no expDate specified", cmd_name);
00335   }
00336 
00337   if (_debug) {
00338     printf("asn    : [%d]\n", cmd->get_asn());
00339     printf("expDate: [%s]\n", cmd->get_expDate().c_str());
00340     if (cmd->get_period().time != 0 || cmd->get_period().unit != "") {
00341       printf("period : [%d %s]\n", cmd->get_period().time,
00342              cmd->get_period().unit.c_str());
00343     }
00344   } // _debug
00345 
00346   if (process_action(act) != 0) {
00347     return -1;
00348   }
00349 
00350   return 0;
00351 }
00352 
00354 
00358 int cmd_asn_transfer(vector<string> &args)
00359 {
00360   string cmd_name = "transfer";
00361 
00362   AsnTransfer act;
00363   AsnTransferCmd *cmd = act.get_command();
00364 
00365   // mandatory field
00366   bool hasOp = false;
00367 
00368   if (args.empty()) {
00369     return cmd_asn_help("no ASN specified", cmd_name);
00370   }
00371 
00372   int asn = atoi(args[0].c_str());
00373   if (asn <= 0) {
00374     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00375   }
00376   cmd->set_asn(asn);
00377   args.erase(args.begin());
00378 
00379   while (!args.empty()) {
00380     if (args[0] == "-op") {
00381       //op
00382       if (hasOp) {
00383         return cmd_asn_help("only one operation allowed per command",
00384                             cmd_name);
00385       } else {
00386         hasOp = true;
00387       }
00388       args.erase(args.begin());
00389       if (args.empty()) {
00390         return cmd_asn_help("operation parameter missing", cmd_name);
00391       }
00392 
00393       if (args[0] != "query" && args[0] != "request") {
00394         return cmd_asn_help("invalid operation '" + args[0] + "'",
00395                             cmd_name);
00396       }
00397 
00398       cmd->set_operation(args[0]);
00399       args.erase(args.begin());
00400     } else {
00401       return cmd_asn_help("invalid syntax near \"" + args[0] + "\"",
00402                           cmd_name);
00403     }
00404   }
00405 
00406   if (!hasOp) {
00407     return cmd_asn_help("no operation specified", cmd_name);
00408   }
00409 
00410   if (_debug) {
00411     printf("asn : [%d]\n", cmd->get_asn());
00412     printf("op  : [%s]\n", cmd->get_operation().c_str());
00413   } // _debug
00414 
00415   if (process_action(act) != 0) {
00416     return -1;
00417   }
00418 
00419   return 0;
00420 }
00421 
00423 
00427 int cmd_asn_update(vector<string> &args)
00428 {
00429   string cmd_name = "update";
00430 
00431   AsnUpdate act;
00432   AsnUpdateCmd *cmd = act.get_command();
00433 
00434   if (args.empty()) {
00435     return cmd_asn_help("no ASN specified", cmd_name);
00436   }
00437 
00438   int asn = atoi(args[0].c_str());
00439   if (asn <= 0) {
00440     return cmd_asn_help("invalid ASN: '" + args[0] + "'", cmd_name);
00441   }
00442   cmd->set_asn(asn);
00443   args.erase(args.begin());
00444 
00445   bool hasOrganization = false;
00446   bool hasContactsAdd = false;
00447   bool hasContactsRem = false;
00448 
00449   while (!args.empty()) {
00450     if (args[0] == "-add-contact") {
00451       //contact(s) to add
00452       hasContactsAdd = true;
00453       args.erase(args.begin());
00454       if (args.empty()) {
00455         return cmd_asn_help("add-contact parameter missing", cmd_name);
00456       }
00457 
00458       string type;
00459       string value;
00460       if (SheppStrUtil::split(args[0], type, value, "=", false) != 0) {
00461         return cmd_asn_help("invalid contact", cmd_name);
00462       }
00463 
00464       cmd->insert_contact_add(type, value);
00465       args.erase(args.begin());      
00466     } else if (args[0] == "-rem-contact") {
00467       //contact(s) to remove
00468       hasContactsRem = true;
00469       args.erase(args.begin());
00470       if (args.empty()) {
00471         return cmd_asn_help("rem-contact parameter missing", cmd_name);
00472       }
00473 
00474       string type;
00475       string value;
00476       if (SheppStrUtil::split(args[0], type, value, "=", false) != 0) {
00477         return cmd_asn_help("invalid contact", cmd_name);
00478       }
00479 
00480       cmd->insert_contact_rem(type, value);
00481       args.erase(args.begin());      
00482     } else if (args[0] == "-o") {
00483       //set organization
00484       if (hasOrganization) {
00485         return cmd_asn_help("only one organization allowed per command",
00486                             cmd_name);
00487       } else {
00488         hasOrganization = true;
00489       }
00490       args.erase(args.begin());
00491       if (args.empty()) {
00492         return cmd_asn_help("organization parameter missing", cmd_name);
00493       }
00494       cmd->set_organization(args[0]);
00495       args.erase(args.begin());
00496     } else {
00497       return cmd_asn_help("invalid syntax near \"" + args[0] + "\"",
00498                           cmd_name);
00499     }
00500   }
00501 
00502   if (!hasOrganization && !hasContactsAdd && !hasContactsRem) {
00503     return cmd_asn_help("nothing to update", cmd_name);
00504   }
00505 
00506   if (_debug) {
00507     printf("ASN to be updated: [%d]\n", cmd->get_asn());
00508     printf("organization: [%s]\n", cmd->get_organization().c_str());
00509 
00510     map< string, string, less<string> >::const_iterator cit;
00511     map< string, string, less<string> > contacts;
00512 
00513     contacts = cmd->get_contacts_add();
00514     for (cit = contacts.begin(); cit != contacts.end(); cit++) {
00515       printf("contact to add %s: %s\n",
00516              (*cit).first.c_str(), (*cit).second.c_str());
00517     }
00518 
00519     contacts = cmd->get_contacts_rem();
00520     for (cit = contacts.begin(); cit != contacts.end(); cit++) {
00521       printf("contact to remove %s: %s\n",
00522              (*cit).first.c_str(), (*cit).second.c_str());
00523     }
00524   } // _debug
00525 
00526   if (process_action(act) != 0) {
00527     return -1;
00528   }
00529 
00530   return 0;
00531 }
00532 
00534 
00538 int cmd_asn(vector<string> &args)
00539 {
00540   // asn command processing
00541   if (!args.empty() && !(args[0] == "help")) {
00542     if (args[0] == "check") {
00543       args.erase(args.begin());
00544       return cmd_asn_check(args);
00545     } else if (args[0] == "create") {
00546       args.erase(args.begin());
00547       return cmd_asn_create(args);
00548     } else if (args[0] == "delete") {
00549       args.erase(args.begin());
00550       return cmd_asn_delete(args);
00551     } else if (args[0] == "info") {
00552       args.erase(args.begin());
00553       return cmd_asn_info(args);
00554     } else if (args[0] == "renew") {
00555       args.erase(args.begin());
00556       return cmd_asn_renew(args);
00557     } else if (args[0] == "transfer") {
00558       args.erase(args.begin());
00559       return cmd_asn_transfer(args);
00560     } else if (args[0] == "update") {
00561       args.erase(args.begin());
00562       return cmd_asn_update(args);
00563     } else {
00564       return cmd_asn_help("invalid command: asn " + args[0]);
00565     }
00566   }
00567   
00568   return cmd_asn_help("");
00569 }
00570 
00571 #endif //__ASN_FUNCTIONS_H__

Generated on Tue Mar 17 16:03:07 2009 for libepp_nicbr by  doxygen 1.4.7