00001
00002
00006 #ifndef __CONTACT_FUNCTIONS_H__
00007 #define __CONTACT_FUNCTIONS_H__
00008 #include "SheppCommandFunctions.H"
00009
00010 #include "ContactCheck.H"
00011 #include "ContactInfo.H"
00012 #include "ContactCreate.H"
00013 #include "ContactUpdate.H"
00014
00016
00021 int cmd_contact_help(string error_msg, string specific = "")
00022 {
00023 if (error_msg != "") {
00024 printf("error: %s\n", error_msg.c_str());
00025 }
00026
00027 printf("contact command syntax help:\n");
00028 printf("\n");
00029
00030 if (specific == "" || specific == "check") {
00031 printf(" check <contact1> [contact2 ... contactN]\n");
00032 printf("\n");
00033 }
00034 if (specific == "" || specific == "info") {
00035 printf(" info <contact> [-auth authInfoPw[:authInfoRoid]]\n");
00036 printf("\n");
00037 }
00038 if (specific == "" || specific == "transfer") {
00039 printf(" transfer: not implemented\n");
00040 printf("\n");
00041 }
00042 if (specific == "" || specific == "create") {
00043 printf(" create <contact>\n");
00044 printf(" [-postalInfo <type> <-name \"Contact Name\"> "
00045 "[-org \"Org Name\"]\n");
00046 printf(" <-street1 \"address line 1\"> "
00047 "[-street2 \"address line 2\"]\n");
00048 printf(" [-street3 \"address line 3\"] <-city \"City Name\"> "
00049 "[-state \"State or Province\"]\n");
00050 printf(" [-pc \"postal code\"] <-cc country-code> ...]\n");
00051 printf(" [-voice number:ext] [-fax number:ext]\n");
00052 printf(" <-email emailAddr> <-auth authInfoPw[:authInfoRoid]> "
00053 "[-disclose opt,opt,...]\n");
00054 printf("\n");
00055 printf(" available values for opt:\n");
00056 printf(" {name_int name_loc org_int org_loc addr_int addr_loc "
00057 "voice fax email}\n");
00058 printf("\n");
00059 }
00060 if (specific == "" || specific == "delete") {
00061 printf(" delete: not implemented\n");
00062 printf("\n");
00063 }
00064 if (specific == "" || specific == "renew") {
00065 printf(" renew: not implemented\n");
00066 printf("\n");
00067 }
00068 if (specific == "" || specific == "update") {
00069 printf(" update <contact> [-add-status st1,...,stN] "
00070 "[-rem-status st1,...,stN]\n");
00071 printf(" [-postalInfo <type> [-name \"Contact Name\"] "
00072 "[-org \"Org Name\"]\n");
00073 printf(" [-street1 \"address line 1\"] "
00074 "[-street2 \"address line 2\"]\n");
00075 printf(" [-street3 \"address line 3\"] [-city \"City Name\"] "
00076 "[-state \"State or Province\"]\n");
00077 printf(" [-pc \"postal code\"] [-cc country-code] ...]\n");
00078 printf(" [-voice number:ext] [-fax number:ext]\n");
00079 printf(" [-email emailAddr] [-auth authInfoPw[:authInfoRoid]] "
00080 "[-disclose opt,opt,...]\n");
00081 printf("\n");
00082 printf(" available values for opt:\n");
00083 printf(" {name_int name_loc org_int org_loc addr_int addr_loc "
00084 "voice fax email}\n");
00085 printf("\n");
00086 }
00087
00088 if (error_msg != "") {
00089 return -1;
00090 }
00091
00092 return 0;
00093 }
00094
00096
00100 int cmd_contact_check(vector<string> words)
00101 {
00102 ContactCheck act;
00103 ContactCheckCmd *cmd = act.get_command();
00104
00105 string cmd_name = "check";
00106
00107 while (!words.empty()) {
00108 cmd->insert_id(words[0]);
00109 words.erase(words.begin());
00110 }
00111
00112 if (cmd->get_id_list().empty()) {
00113 return cmd_contact_help("no contact id specified", cmd_name);
00114 }
00115
00116 if (_debug) {
00117 set<string> contacts = cmd->get_id_list();
00118 set<string>::const_iterator it;
00119 printf("contacts to be checked:\n");
00120 for (it = contacts.begin(); it != contacts.end(); it++) {
00121 printf(" [%s]\n", (*it).c_str());
00122 }
00123 }
00124
00125 if (process_action(act) != 0) {
00126 return -1;
00127 }
00128
00129 return 0;
00130 }
00131
00133
00137 int cmd_contact_info(vector<string> words)
00138 {
00139 ContactInfo act;
00140 ContactInfoCmd *cmd = act.get_command();
00141 string cmd_name = "info";
00142
00143 if (words.empty()) {
00144 return cmd_contact_help("no contact id specified", cmd_name);
00145 }
00146
00147 cmd->set_id(words[0]);
00148 words.erase(words.begin());
00149
00150 while (!words.empty()) {
00151 if (words[0] == "-auth") {
00152
00153 AuthInfo auth;
00154 if (SheppObjSet::authInfo(auth, words) != 0) {
00155 return cmd_contact_help("invalid auth", cmd_name);
00156 }
00157 cmd->set_authInfo(auth);
00158 } else {
00159 return cmd_contact_help("invalid syntax near \"" + words[0] + "\"",
00160 cmd_name);
00161 }
00162 }
00163
00164 if (_debug) {
00165 printf("id to get info: [%s]\n", cmd->get_id().c_str());
00166 SheppPrint::authInfo(cmd->get_authInfo());
00167 }
00168
00169 if (process_action(act) != 0) {
00170 return -1;
00171 }
00172
00173 return 0;
00174 }
00175
00177
00181 int cmd_contact_transfer(vector<string> words)
00182 {
00183 string cmd_name = "transfer";
00184 return cmd_contact_help("", cmd_name);
00185 }
00186
00188
00192 int cmd_contact_create(vector<string> words)
00193 {
00194 ContactCreate act;
00195 ContactCreateCmd *cmd = act.get_command();
00196 string cmd_name = "create";
00197
00198 if (words.empty()) {
00199 return cmd_contact_help("no contact id specified", cmd_name);
00200 }
00201
00202 CommonData common_data;
00203
00204 common_data.set_id(words[0]);
00205 words.erase(words.begin());
00206
00207 while (!words.empty()) {
00208 if (words[0] == "-postalInfo") {
00209
00210 words.erase(words.begin());
00211 if (words.empty()) {
00212 return cmd_contact_help("missing postal info arguments", cmd_name);
00213 }
00214 CommonData::PostalInfo postal;
00215 string error_msg;
00216 if (SheppObjSet::postalInfo(postal, words, error_msg, false) != 0) {
00217 return cmd_contact_help(error_msg, cmd_name);
00218 }
00219 vector<CommonData::PostalInfo> postal_list
00220 = common_data.get_postal_info();
00221 vector<CommonData::PostalInfo>::const_iterator it;
00222 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00223 if ((*it).type == postal.type) {
00224 return cmd_contact_help("duplicated postal info type", cmd_name);
00225 }
00226 }
00227 common_data.insert_postal_info(postal);
00228 } else if (words[0] == "-voice") {
00229
00230 words.erase(words.begin());
00231 CommonData::Phone phone;
00232 if (SheppObjSet::phone(phone, words) != 0) {
00233 return cmd_contact_help("error setting voice telephone number",
00234 cmd_name);
00235 }
00236 common_data.set_voice(phone);
00237 } else if (words[0] == "-fax") {
00238
00239 words.erase(words.begin());
00240 CommonData::Phone phone;
00241 if (SheppObjSet::phone(phone, words) != 0) {
00242 return cmd_contact_help("error setting fax telephone number",
00243 cmd_name);
00244 }
00245 common_data.set_fax(phone);
00246 } else if (words[0] == "-email") {
00247
00248 words.erase(words.begin());
00249 if (words.empty()) {
00250 return cmd_contact_help("no e-mail address specified", cmd_name);
00251 }
00252 common_data.set_email(words[0]);
00253 words.erase(words.begin());
00254 } else if (words[0] == "-auth") {
00255
00256 AuthInfo auth;
00257 if (SheppObjSet::authInfo(auth, words) != 0) {
00258 return cmd_contact_help("invalid auth", cmd_name);
00259 }
00260 cmd->set_authInfo(auth);
00261 } else if (words[0] == "-disclose") {
00262
00263 words.erase(words.begin());
00264 CommonData::Disclose disclose;
00265 if (SheppObjSet::disclose(disclose, words[0]) != 0) {
00266 return cmd_contact_help("invalid diclose information", cmd_name);
00267 }
00268 words.erase(words.begin());
00269 common_data.set_disclose(disclose);
00270 } else {
00271 return cmd_contact_help("invalid syntax near \"" + words[0] + "\"",
00272 cmd_name);
00273 }
00274 }
00275
00276 if (common_data.get_postal_info().empty()) {
00277 return cmd_contact_help("at least one postal info must be entered",
00278 cmd_name);
00279 }
00280
00281 if (common_data.get_email() == "") {
00282 return cmd_contact_help("e-mail is mandatory", cmd_name);
00283 }
00284
00285 if (cmd->get_authInfo().get_pw() == "") {
00286 return cmd_contact_help("auth info is mandatory", cmd_name);
00287 }
00288
00289 cmd->set_common_data(common_data);
00290
00291 if (_debug) {
00292 printf(" contact id: [%s]\n", cmd->get_common_data().get_id().c_str());
00293
00294 vector<CommonData::PostalInfo>::const_iterator it;
00295 vector<CommonData::PostalInfo> postal_list;
00296 postal_list = cmd->get_common_data().get_postal_info();
00297 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00298 SheppPrint::postal_info((*it));
00299 }
00300
00301 if (cmd->get_common_data().get_voice().number != "") {
00302 printf(" voice:");
00303 SheppPrint::phone(cmd->get_common_data().get_voice());
00304 }
00305
00306 if (cmd->get_common_data().get_fax().number != "") {
00307 printf(" fax :");
00308 SheppPrint::phone(cmd->get_common_data().get_fax());
00309 }
00310
00311 printf(" email: [%s]\n", cmd->get_common_data().get_email().c_str());
00312
00313 SheppPrint::authInfo(cmd->get_authInfo());
00314
00315 if (cmd->get_common_data().get_disclose().is_set()) {
00316 SheppPrint::disclose(cmd->get_common_data().get_disclose());
00317 }
00318 }
00319
00320 if (process_action(act) != 0) {
00321 return -1;
00322 }
00323
00324 return 0;
00325 }
00326
00328
00332 int cmd_contact_delete(vector<string> words)
00333 {
00334 string cmd_name = "delete";
00335 return cmd_contact_help("", cmd_name);
00336 }
00337
00339
00343 int cmd_contact_renew(vector<string> words)
00344 {
00345 string cmd_name = "renew";
00346 return cmd_contact_help("", cmd_name);
00347 }
00348
00350
00354 int cmd_contact_update(vector<string> words)
00355 {
00356 ContactUpdate act;
00357 ContactUpdateCmd *cmd = act.get_command();
00358 string cmd_name = "update";
00359
00360 if (words.empty()) {
00361 return cmd_contact_help("no contact id specified", cmd_name);
00362 }
00363
00364 CommonData common_data;
00365
00366 common_data.set_id(words[0]);
00367 words.erase(words.begin());
00368
00369 while (!words.empty()) {
00370 if (words[0] == "-add-status") {
00371
00372 words.erase(words.begin());
00373 if (words.empty()) {
00374 return cmd_contact_help("error setting add-status", cmd_name);
00375 }
00376 string tmp1 = words[0];
00377 string tmp2;
00378 while (SheppStrUtil::split(tmp1, tmp1, tmp2, ",", true) == 0) {
00379 cmd->insert_status_list_add(tmp1);
00380 if (tmp2 == "") {
00381 break;
00382 }
00383 tmp1 = tmp2;
00384 }
00385 cmd->set_chg(true);
00386 words.erase(words.begin());
00387 } else if (words[0] == "-rem-status") {
00388
00389 words.erase(words.begin());
00390 if (words.empty()) {
00391 return cmd_contact_help("error setting rem-status", cmd_name);
00392 }
00393 string tmp1 = words[0];
00394 string tmp2;
00395 while (SheppStrUtil::split(tmp1, tmp1, tmp2, ",", true) == 0) {
00396 cmd->insert_status_list_rem(tmp1);
00397 if (tmp2 == "") {
00398 break;
00399 }
00400 tmp1 = tmp2;
00401 }
00402 cmd->set_chg(true);
00403 words.erase(words.begin());
00404 } else if (words[0] == "-postalInfo") {
00405
00406 words.erase(words.begin());
00407 if (words.empty()) {
00408 return cmd_contact_help("missing postal info arguments", cmd_name);
00409 }
00410 CommonData::PostalInfo postal;
00411 string error_msg;
00412 if (SheppObjSet::postalInfo(postal, words, error_msg, true) != 0) {
00413 return cmd_contact_help(error_msg, cmd_name);
00414 }
00415 vector<CommonData::PostalInfo> postal_list
00416 = common_data.get_postal_info();
00417 vector<CommonData::PostalInfo>::const_iterator it;
00418 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00419 if ((*it).type == postal.type) {
00420 return cmd_contact_help("duplicated postal info type", cmd_name);
00421 }
00422 }
00423 common_data.insert_postal_info(postal);
00424 } else if (words[0] == "-voice") {
00425
00426 words.erase(words.begin());
00427 CommonData::Phone phone;
00428 if (SheppObjSet::phone(phone, words) != 0) {
00429 return cmd_contact_help("error setting voice telephone number",
00430 cmd_name);
00431 }
00432 common_data.set_voice(phone);
00433 } else if (words[0] == "-fax") {
00434
00435 words.erase(words.begin());
00436 CommonData::Phone phone;
00437 if (SheppObjSet::phone(phone, words) != 0) {
00438 return cmd_contact_help("error setting fax telephone number",
00439 cmd_name);
00440 }
00441 common_data.set_fax(phone);
00442 } else if (words[0] == "-email") {
00443
00444 words.erase(words.begin());
00445 if (words.empty()) {
00446 return cmd_contact_help("no e-mail address specified", cmd_name);
00447 }
00448 common_data.set_email(words[0]);
00449 words.erase(words.begin());
00450 } else if (words[0] == "-auth") {
00451
00452 AuthInfo auth;
00453 if (SheppObjSet::authInfo(auth, words) != 0) {
00454 return cmd_contact_help("invalid auth", cmd_name);
00455 }
00456 cmd->set_authInfo(auth);
00457 } else if (words[0] == "-disclose") {
00458
00459 words.erase(words.begin());
00460 CommonData::Disclose disclose;
00461 if (SheppObjSet::disclose(disclose, words[0]) != 0) {
00462 return cmd_contact_help("invalid diclose information", cmd_name);
00463 }
00464 words.erase(words.begin());
00465 common_data.set_disclose(disclose);
00466 } else {
00467 return cmd_contact_help("invalid syntax near \"" + words[0] + "\"",
00468 cmd_name);
00469 }
00470 }
00471
00472 if (cmd->get_status_list_add().empty() &&
00473 cmd->get_status_list_rem().empty() &&
00474 common_data.get_postal_info().empty() &&
00475 common_data.get_voice().number == "" &&
00476 common_data.get_fax().number == "" &&
00477 common_data.get_email() == "" &&
00478 cmd->get_authInfo().get_pw() == "" &&
00479 !common_data.get_disclose().is_set()) {
00480 return cmd_contact_help("you didn't set a thing", cmd_name);
00481 }
00482
00483 cmd->set_common_data(common_data);
00484
00485 if (_debug) {
00486 printf(" contact id: [%s]\n", cmd->get_common_data().get_id().c_str());
00487
00488 set<string> status = cmd->get_status_list_add();
00489 set<string>::const_iterator st_it;
00490 if (!status.empty()) {
00491 printf(" status to add: [ ");
00492 for (st_it = status.begin(); st_it != status.end(); st_it++) {
00493 printf("%s ", (*st_it).c_str());
00494 }
00495 printf("]\n");
00496 }
00497
00498 status = cmd->get_status_list_rem();
00499 if (!status.empty()) {
00500 printf(" status to rem: [ ");
00501 for (st_it = status.begin(); st_it != status.end(); st_it++) {
00502 printf("%s ", (*st_it).c_str());
00503 }
00504 printf("]\n");
00505 }
00506
00507 vector<CommonData::PostalInfo>::const_iterator it;
00508 vector<CommonData::PostalInfo> postal_list;
00509 postal_list = cmd->get_common_data().get_postal_info();
00510 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00511 SheppPrint::postal_info((*it));
00512 }
00513
00514 if (cmd->get_common_data().get_voice().number != "") {
00515 printf(" voice:");
00516 SheppPrint::phone(cmd->get_common_data().get_voice());
00517 }
00518
00519 if (cmd->get_common_data().get_fax().number != "") {
00520 printf(" fax :");
00521 SheppPrint::phone(cmd->get_common_data().get_fax());
00522 }
00523
00524 if (cmd->get_common_data().get_email() != "") {
00525 printf(" email: [%s]\n", cmd->get_common_data().get_email().c_str());
00526 }
00527
00528 if (cmd->get_authInfo().get_pw() != "") {
00529 SheppPrint::authInfo(cmd->get_authInfo());
00530 }
00531
00532 if (cmd->get_common_data().get_disclose().is_set()) {
00533 SheppPrint::disclose(cmd->get_common_data().get_disclose());
00534 }
00535 }
00536
00537 if (process_action(act) != 0) {
00538 return -1;
00539 }
00540
00541 return 0;
00542 }
00543
00545
00549 int cmd_contact(char *arg)
00550 {
00551 if (strlen(arg) > 0) {
00552 vector<string> words = SheppStrUtil::parse_line(arg);
00553
00554
00555 if (!words.empty() && !(words[0] == "help")) {
00556 if (words[0] == "check") {
00557 words.erase(words.begin());
00558 return cmd_contact_check(words);
00559 } else if (words[0] == "info") {
00560 words.erase(words.begin());
00561 return cmd_contact_info(words);
00562 } else if (words[0] == "transfer") {
00563 words.erase(words.begin());
00564 return cmd_contact_transfer(words);
00565 } else if (words[0] == "create") {
00566 words.erase(words.begin());
00567 return cmd_contact_create(words);
00568 } else if (words[0] == "delete") {
00569 words.erase(words.begin());
00570 return cmd_contact_delete(words);
00571 } else if (words[0] == "renew") {
00572 words.erase(words.begin());
00573 return cmd_contact_renew(words);
00574 } else if (words[0] == "update") {
00575 words.erase(words.begin());
00576 return cmd_contact_update(words);
00577 } else {
00578 return cmd_contact_help("invalid command: contact " + words[0]);
00579 }
00580 }
00581 }
00582
00583 return cmd_contact_help("");
00584 }
00585
00586 #endif //__CONTACT_FUNCTIONS_H__