src/shepp/SheppObjSet.H

Go to the documentation of this file.
00001 /* ${copyright}$ */
00002 /* $Id: SheppObjSet.H 915 2007-05-31 18:19:51Z eduardo $ */
00007 #ifndef __SHEPP_OBJ_SET_H__
00008 #define __SHEPP_OBJ_SET_H__
00009 
00010 #include "CommonData.H"
00011 #include "DomainUpdateCmd.H"
00012 
00014 class SheppObjSet {
00015 public:
00016 
00017   //used by both contact and domain
00018 
00020 
00025   static int authInfo(AuthInfo &auth, vector<string> &words)
00026   {
00027     words.erase(words.begin());
00028     
00029     if (words.size() <= 0) {
00030       return -1;
00031     }
00032     
00033     string pw;
00034     string roid;
00035     if (SheppStrUtil::split(words[0], pw, roid, ":", true) != 0) {
00036       return -1;
00037     }
00038     
00039     auth.set_pw(pw);
00040     auth.set_roid(roid);
00041     
00042     words.erase(words.begin()); 
00043     
00044     return 0;
00045   }
00046 
00047   //domain specific
00048 
00050 
00055   static int nameServer(NameServer &ns, vector<string> &words)
00056   {
00057     words.erase(words.begin());
00058     if (words.empty()) {
00059       return -1;
00060     }
00061 
00062     string ips;
00063     SheppStrUtil::split(words[0], ns.name, ips, ",", true);
00064 
00065     if (ips != "") {
00066       string this_ip;
00067       do {
00068         if (SheppStrUtil::split(ips, this_ip, ips, ",", true) != 0) {
00069           return -1;
00070         }
00071       
00072         NSIPAddr ip;
00073         if (SheppStrUtil::split(this_ip, ip.version, ip.addr, ":", false)
00074             != 0) {
00075           return -1;
00076         }
00077 
00078         ns.ips.insert(ip);
00079       } while (ips != "");
00080     }
00081 
00082     words.erase(words.begin());
00083   
00084     return 0;
00085   }
00086 
00088 
00093   static int status(DomainUpdateCmd::Status &st, vector<string> &words)
00094   {
00095     string status;
00096     string lang;
00097     string description;
00098 
00099     if (SheppStrUtil::split(words[0], status, lang, ":", false) != 0) {
00100       return -1;
00101     }
00102     words.erase(words.begin());
00103 
00104     if (SheppStrUtil::split(lang, lang, description, ":", false) != 0) {
00105       return -1;
00106     }
00107 
00108     SheppStrUtil::quote_gathering(words, description);
00109 
00110     st.s = status;
00111     st.lang = lang;
00112     st.msg = description;
00113 
00114     return 0;
00115   }
00116 
00118 
00123   static int dsInfo(DSInfo &ds, vector<string> &words, string &error_msg)
00124   {
00125     words.erase(words.begin()); // -ds
00126 
00127     // must have at least 4 elements: keytag, algorithm, digtype and digest
00128     if (words.size() < 4) {
00129       error_msg = "not enough arguments for DS";
00130       return -1;
00131     }
00132 
00133     // parse the key tag: check if it has only numbers
00134     string c;
00135     for (unsigned int i = 0; i < words[0].length(); i++) {
00136       c = words[0][i];
00137       if (c != "0" && atoi(c.c_str()) == 0) {
00138         error_msg = "keyTag must be a number";
00139         return -1;
00140       }
00141     }
00142 
00143     int key_tag = atoi(words[0].c_str());
00144     if (key_tag < 0) {
00145       error_msg = "keyTag cannot be a negative number";
00146       return -1;
00147     }
00148     ds.set_key_tag(key_tag);
00149 
00150     int alg = atoi(words[1].c_str());
00151     if (alg < 0) {
00152       error_msg = "alg cannot be a negative number";
00153       return -1;
00154     }
00155     ds.set_algo(alg);
00156 
00157     int dig_type = atoi(words[2].c_str());
00158     if (dig_type < 0) {
00159       error_msg = "digestType cannot be a negative number";
00160       return -1;
00161     }
00162     ds.set_digest_type(dig_type);
00163     ds.set_digest(words[3]);
00164 
00165     words.erase(words.begin(), words.begin() + 4);
00166 
00167     //optional parameters
00168     while (!words.empty()) {
00169       if (words[0] == "-maxlife") {
00170         if (words.size() < 2) {
00171           error_msg = "maxSigLife value missing";
00172           return -1;
00173         }
00174         words.erase(words.begin()); // -maxlife
00175         int maxlife = atoi(words[0].c_str());
00176         if (maxlife < 0) {
00177           error_msg = "maxSigLife cannot be a negative number";
00178           return -1;
00179         }
00180         ds.set_max_sig_life(maxlife);
00181         words.erase(words.begin()); // maxlife value
00182       } else if (words[0] == "-keyData") {
00183         if (words.size() < 5) {
00184           error_msg = "not enough arguments for keyData";
00185           return -1;
00186         }
00187         DSInfo::KeyData kd;
00188         kd._flags = atoi(words[1].c_str());
00189         kd._protocol = atoi(words[2].c_str());
00190         kd._algorithm = atoi(words[3].c_str());
00191         kd._pub_key = words[4];
00192 
00193         if (kd._flags < 0 || kd._protocol < 0 || kd._algorithm < 0) {
00194           error_msg =
00195             "keyData flags, protocol and alg cannot be negative numbers";
00196           return -1;
00197         }
00198         words.erase(words.begin(), words.begin() + 5); 
00199         ds.set_key_data(kd);
00200       } else {
00201         break;
00202       }
00203     }
00204 
00205     return 0;
00206   }
00207 
00208   //contact specific
00209 
00211 
00218   static int postalInfo(PostalInfo &postal, vector<string> &words,
00219                         string &error_msg, bool relaxed)
00220   {
00221     //type
00222     string postal_type = words[0];
00223     words.erase(words.begin());
00224     if (SheppStrUtil::quote_gathering(words, postal_type) != 0) {
00225       error_msg = "error setting postalInfo type";
00226       return -1;
00227     }
00228     postal.set_type(postal_type);
00229 
00230     while (!words.empty()) {
00231       if (words[0] == "-name") {
00232         //name
00233         words.erase(words.begin());
00234         if (words.empty()) {
00235           error_msg = "unspecified contact name";
00236           return -1;
00237         }
00238         string postal_name = words[0];
00239         words.erase(words.begin());
00240         if (SheppStrUtil::quote_gathering(words, postal_name) != 0) {
00241           error_msg = "error setting contact name";
00242           return -1;
00243         }
00244         postal.set_name(postal_name);
00245       } else if (words[0] == "-org") {
00246         //org
00247         words.erase(words.begin());
00248         if (words.empty()) {
00249           error_msg = "unspecified organization";
00250           return -1;
00251         }
00252         string postal_org = words[0];
00253         words.erase(words.begin());
00254         if (SheppStrUtil::quote_gathering(words, postal_org) != 0) {
00255           error_msg = "error setting organization";
00256           return -1;
00257         }
00258         postal.set_org(postal_org);
00259       } else if (words[0] == "-street1") {
00260         //street1
00261         words.erase(words.begin());
00262         if (words.empty()) {
00263           error_msg = "unspecified address street/line 1";
00264           return -1;
00265         }
00266         string postal_str1 = words[0];
00267         words.erase(words.begin());
00268         if (SheppStrUtil::quote_gathering(words, postal_str1) != 0) {
00269           error_msg = "error setting address street/line 1";
00270           return -1;
00271         }
00272         postal.set_str1(postal_str1);
00273       } else if (words[0] == "-street2") {
00274         //street2
00275         words.erase(words.begin());
00276         if (words.empty()) {
00277           error_msg = "unspecified address number/line 2";
00278           return -1;
00279         }
00280         string postal_str2 = words[0];
00281         words.erase(words.begin());
00282         if (SheppStrUtil::quote_gathering(words, postal_str2) != 0) {
00283           error_msg = "error setting address number/line 2";
00284           return -1;
00285         }
00286         postal.set_str2(postal_str2);
00287       } else if (words[0] == "-street3") {
00288         //street3
00289         words.erase(words.begin());
00290         if (words.empty()) {
00291           error_msg = "unspecified street3";
00292           return -1;
00293         }
00294         string postal_str3 = words[0];
00295         words.erase(words.begin());
00296         if (SheppStrUtil::quote_gathering(words, postal_str3) != 0) {
00297           error_msg = "error setting address line 3";
00298           return -1;
00299         }
00300         postal.set_str3(postal_str3);
00301       } else if (words[0] == "-city") {
00302         //city
00303         words.erase(words.begin());
00304         if (words.empty()) {
00305           error_msg = "unspecified city";
00306           return -1;
00307         }
00308         string postal_city = words[0];
00309         words.erase(words.begin());
00310         if (SheppStrUtil::quote_gathering(words, postal_city) != 0) {
00311           error_msg = "error setting city";
00312           return -1;
00313         }
00314         postal.set_city(postal_city);
00315       } else if (words[0] == "-state") {
00316         //state
00317         words.erase(words.begin());
00318         if (words.empty()) {
00319           error_msg = "unspecified state";
00320           return -1;
00321         }
00322         string postal_sp = words[0];
00323         words.erase(words.begin());
00324         if (SheppStrUtil::quote_gathering(words, postal_sp) != 0) {
00325           error_msg = "error setting state/province";
00326           return -1;
00327         }
00328         postal.set_sp(postal_sp);
00329       } else if (words[0] == "-pc") {
00330         //pc
00331         words.erase(words.begin());
00332         if (words.empty()) {
00333           error_msg = "unspecified postal code";
00334           return -1;
00335         }
00336         string postal_pc = words[0];
00337         words.erase(words.begin());
00338         if (SheppStrUtil::quote_gathering(words, postal_pc) != 0) {
00339           error_msg = "error setting postal code";
00340           return -1;
00341         }
00342         postal.set_pc(postal_pc);
00343       } else if (words[0] == "-cc") {
00344         //cc
00345         words.erase(words.begin());
00346         if (words.empty()) {
00347           error_msg = "unspecified country code";
00348           return -1;
00349         }
00350         string postal_cc = words[0];
00351         words.erase(words.begin());
00352         if (SheppStrUtil::quote_gathering(words, postal_cc) != 0) {
00353           error_msg = "error setting country code";
00354           return -1;
00355         }
00356         postal.set_cc(postal_cc);
00357       } else {
00358         break;
00359       }
00360     }
00361 
00362     //relaxed: update; !relaxed: create
00363     if (!relaxed) {
00364       if (postal.get_name() == "") {
00365         error_msg = "name is mandatory";
00366         return -1;
00367       }
00368 
00369       if (postal.get_str1() == "") {
00370         error_msg = "address street/line 1 is mandatory";
00371         return -1;
00372       }
00373     
00374       if (postal.get_city() == "") {
00375         error_msg = "city is mandatory";
00376         return -1;
00377       }
00378     
00379       if (postal.get_cc() == "") {
00380         error_msg = "country code is mandatory";
00381         return -1;
00382       }
00383     } else {
00384       if (postal.get_name() == "" &&
00385           postal.get_org() == "" &&
00386           postal.get_str1() == "" &&
00387           postal.get_str2() == "" &&
00388           postal.get_str3() == "" &&
00389           postal.get_city() == "" &&
00390           postal.get_sp() == "" &&
00391           postal.get_pc() == "" &&
00392           postal.get_cc() == "") {
00393         error_msg = "empty postal info";
00394         return -1;
00395       }
00396     }   
00397 
00398     return 0;
00399   }
00400 
00402 
00407   static int phone(CommonData::Phone &phone, vector<string> &words)
00408   {
00409     if (words.empty() ||
00410         SheppStrUtil::split(words[0], phone.number, phone.ext, ":", true)
00411         != 0) {
00412       return -1;
00413     }
00414     words.erase(words.begin());
00415     return 0;
00416   }
00417 
00419 
00424   static int disclose(CommonData::Disclose &disclose, string word)
00425   {
00426     string opt1 = word;
00427     string opt2;
00428     while (opt1 != "" &&
00429            SheppStrUtil::split(opt1, opt1, opt2, ",", true) == 0) {
00430       if (opt1 == "name_int") {
00431         disclose.name_int = true;
00432       } else if (opt1 == "name_loc") {
00433         disclose.name_loc = true;
00434       } else if (opt1 == "org_int") {
00435         disclose.org_int = true;
00436       } else if (opt1 == "org_loc") {
00437         disclose.org_loc = true;
00438       } else if (opt1 == "addr_int") {
00439         disclose.addr_int = true;
00440       } else if (opt1 == "addr_loc") {
00441         disclose.addr_loc = true;
00442       } else if (opt1 == "voice") {
00443         disclose.voice = true;
00444       } else if (opt1 == "fax") {
00445         disclose.fax = true;
00446       } else if (opt1 == "email") {
00447         disclose.email = true;
00448       } else {
00449         return -1;
00450       }
00451       opt1 = opt2;
00452     }
00453     return 0;
00454   }
00455 
00456   // brorg specific
00457 
00459 
00465   static int contacts(map<string, string, less<string> > &contacts,
00466                       string args, string &error_msg)
00467   {
00468     string one = args;
00469     string rest;
00470 
00471     do {
00472       if (SheppStrUtil::split(one, one, rest, ",", true) != 0) {
00473         error_msg = "invalid contact";
00474         return -1;
00475       }
00476 
00477       string key;
00478       string value;
00479       if (SheppStrUtil::split(one, key, value, "=", false) != 0) {
00480         error_msg = "invalid contact";
00481         return -1;
00482       }
00483 
00484       contacts[key] = value;
00485 
00486       one = rest;
00487     } while (one != "");
00488 
00489     return 0;
00490   }
00491 };
00492 
00493 #endif //__SHEPP_OBJ_SET_H__

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