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 street/line 1\"> "
00047 "[-street2 \"address number/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]]\n");
00053 printf(" [-disclose <flag> 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 street/line 1\"] "
00074 "[-street2 \"address number/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]]\n");
00080 printf(" [-disclose <flag> 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
00208 AuthInfo auth;
00209 cmd->set_authInfo(auth);
00210
00211 while (!words.empty()) {
00212 if (words[0] == "-postalInfo") {
00213
00214 words.erase(words.begin());
00215 if (words.empty()) {
00216 return cmd_contact_help("missing postal info arguments", cmd_name);
00217 }
00218 PostalInfo postal;
00219 string error_msg;
00220 if (SheppObjSet::postalInfo(postal, words, error_msg, false) != 0) {
00221 return cmd_contact_help(error_msg, cmd_name);
00222 }
00223 vector<PostalInfo> postal_list = common_data.get_postal_info();
00224 for (int i = 0; i < (int) postal_list.size(); i++) {
00225 if (postal_list[i].get_type() == postal.get_type()) {
00226 return cmd_contact_help("duplicated postal info type", cmd_name);
00227 }
00228 }
00229 common_data.insert_postal_info(postal);
00230 } else if (words[0] == "-voice") {
00231
00232 words.erase(words.begin());
00233 CommonData::Phone phone;
00234 if (SheppObjSet::phone(phone, words) != 0) {
00235 return cmd_contact_help("error setting voice telephone number",
00236 cmd_name);
00237 }
00238 common_data.set_voice(phone);
00239 } else if (words[0] == "-fax") {
00240
00241 words.erase(words.begin());
00242 CommonData::Phone phone;
00243 if (SheppObjSet::phone(phone, words) != 0) {
00244 return cmd_contact_help("error setting fax telephone number",
00245 cmd_name);
00246 }
00247 common_data.set_fax(phone);
00248 } else if (words[0] == "-email") {
00249
00250 words.erase(words.begin());
00251 if (words.empty()) {
00252 return cmd_contact_help("no e-mail address specified", cmd_name);
00253 }
00254 common_data.set_email(words[0]);
00255 words.erase(words.begin());
00256 } else if (words[0] == "-auth") {
00257
00258 AuthInfo auth;
00259 if (SheppObjSet::authInfo(auth, words) != 0) {
00260 return cmd_contact_help("invalid auth", cmd_name);
00261 }
00262 cmd->set_authInfo(auth);
00263 } else if (words[0] == "-disclose") {
00264
00265 words.erase(words.begin());
00266 CommonData::Disclose disclose;
00267
00268 if (words[0] != "0" && words[0] != "1") {
00269 return cmd_contact_help("disclose flag must be '0' or '1'", cmd_name);
00270 }
00271 disclose.flag = atoi(words[0].c_str());
00272 words.erase(words.begin());
00273
00274 if (SheppObjSet::disclose(disclose, words[0]) != 0) {
00275 return cmd_contact_help("invalid diclose information", cmd_name);
00276 }
00277 words.erase(words.begin());
00278 common_data.set_disclose(disclose);
00279 } else {
00280 return cmd_contact_help("invalid syntax near \"" + words[0] + "\"",
00281 cmd_name);
00282 }
00283 }
00284
00285 if (common_data.get_postal_info().empty()) {
00286 return cmd_contact_help("at least one postal info must be entered",
00287 cmd_name);
00288 }
00289
00290 if (common_data.get_email() == "") {
00291 return cmd_contact_help("e-mail is mandatory", cmd_name);
00292 }
00293
00294 cmd->set_common_data(common_data);
00295
00296 if (_debug) {
00297 printf(" contact id: [%s]\n", cmd->get_common_data().get_id().c_str());
00298
00299 vector<PostalInfo>::const_iterator it;
00300 vector<PostalInfo> postal_list;
00301 postal_list = cmd->get_common_data().get_postal_info();
00302 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00303 SheppPrint::postal_info((*it));
00304 }
00305
00306 if (cmd->get_common_data().get_voice().number != "") {
00307 printf(" voice:");
00308 SheppPrint::phone(cmd->get_common_data().get_voice());
00309 }
00310
00311 if (cmd->get_common_data().get_fax().number != "") {
00312 printf(" fax :");
00313 SheppPrint::phone(cmd->get_common_data().get_fax());
00314 }
00315
00316 printf(" email: [%s]\n", cmd->get_common_data().get_email().c_str());
00317
00318 SheppPrint::authInfo(cmd->get_authInfo());
00319
00320 if (cmd->get_common_data().get_disclose().is_set()) {
00321 SheppPrint::disclose(cmd->get_common_data().get_disclose());
00322 }
00323 }
00324
00325 if (process_action(act) != 0) {
00326 return -1;
00327 }
00328
00329 return 0;
00330 }
00331
00333
00337 int cmd_contact_delete(vector<string> words)
00338 {
00339 string cmd_name = "delete";
00340 return cmd_contact_help("", cmd_name);
00341 }
00342
00344
00348 int cmd_contact_renew(vector<string> words)
00349 {
00350 string cmd_name = "renew";
00351 return cmd_contact_help("", cmd_name);
00352 }
00353
00355
00359 int cmd_contact_update(vector<string> words)
00360 {
00361 ContactUpdate act;
00362 ContactUpdateCmd *cmd = act.get_command();
00363 string cmd_name = "update";
00364
00365 if (words.empty()) {
00366 return cmd_contact_help("no contact id specified", cmd_name);
00367 }
00368
00369 CommonData common_data;
00370
00371 common_data.set_id(words[0]);
00372 words.erase(words.begin());
00373
00374 while (!words.empty()) {
00375 if (words[0] == "-add-status") {
00376
00377 words.erase(words.begin());
00378 if (words.empty()) {
00379 return cmd_contact_help("error setting add-status", cmd_name);
00380 }
00381 string tmp1 = words[0];
00382 string tmp2;
00383 while (SheppStrUtil::split(tmp1, tmp1, tmp2, ",", true) == 0) {
00384 cmd->insert_status_list_add(tmp1);
00385 if (tmp2 == "") {
00386 break;
00387 }
00388 tmp1 = tmp2;
00389 }
00390 words.erase(words.begin());
00391 } else if (words[0] == "-rem-status") {
00392
00393 words.erase(words.begin());
00394 if (words.empty()) {
00395 return cmd_contact_help("error setting rem-status", cmd_name);
00396 }
00397 string tmp1 = words[0];
00398 string tmp2;
00399 while (SheppStrUtil::split(tmp1, tmp1, tmp2, ",", true) == 0) {
00400 cmd->insert_status_list_rem(tmp1);
00401 if (tmp2 == "") {
00402 break;
00403 }
00404 tmp1 = tmp2;
00405 }
00406 words.erase(words.begin());
00407 } else if (words[0] == "-postalInfo") {
00408
00409 words.erase(words.begin());
00410 if (words.empty()) {
00411 return cmd_contact_help("missing postal info arguments", cmd_name);
00412 }
00413 PostalInfo postal;
00414 string error_msg;
00415 if (SheppObjSet::postalInfo(postal, words, error_msg, true) != 0) {
00416 return cmd_contact_help(error_msg, cmd_name);
00417 }
00418 vector<PostalInfo> postal_list = common_data.get_postal_info();
00419 for (int i = 0; i < (int) postal_list.size(); i++) {
00420 if (postal_list[i].get_type() == postal.get_type()) {
00421 return cmd_contact_help("duplicated postal info type", cmd_name);
00422 }
00423 }
00424 common_data.insert_postal_info(postal);
00425 } else if (words[0] == "-voice") {
00426
00427 words.erase(words.begin());
00428 CommonData::Phone phone;
00429 if (SheppObjSet::phone(phone, words) != 0) {
00430 return cmd_contact_help("error setting voice telephone number",
00431 cmd_name);
00432 }
00433 common_data.set_voice(phone);
00434 } else if (words[0] == "-fax") {
00435
00436 words.erase(words.begin());
00437 CommonData::Phone phone;
00438 if (SheppObjSet::phone(phone, words) != 0) {
00439 return cmd_contact_help("error setting fax telephone number",
00440 cmd_name);
00441 }
00442 common_data.set_fax(phone);
00443 } else if (words[0] == "-email") {
00444
00445 words.erase(words.begin());
00446 if (words.empty()) {
00447 return cmd_contact_help("no e-mail address specified", cmd_name);
00448 }
00449 common_data.set_email(words[0]);
00450 words.erase(words.begin());
00451 } else if (words[0] == "-auth") {
00452
00453 AuthInfo auth;
00454 if (SheppObjSet::authInfo(auth, words) != 0) {
00455 return cmd_contact_help("invalid auth", cmd_name);
00456 }
00457 cmd->set_authInfo(auth);
00458 } else if (words[0] == "-disclose") {
00459
00460 words.erase(words.begin());
00461 CommonData::Disclose disclose;
00462
00463 if (words[0] != "0" && words[0] != "1") {
00464 return cmd_contact_help("disclose flag must be '0' or '1'", cmd_name);
00465 }
00466 disclose.flag = atoi(words[0].c_str());
00467 words.erase(words.begin());
00468
00469 if (SheppObjSet::disclose(disclose, words[0]) != 0) {
00470 return cmd_contact_help("invalid diclose information", cmd_name);
00471 }
00472 words.erase(words.begin());
00473 common_data.set_disclose(disclose);
00474 } else {
00475 return cmd_contact_help("invalid syntax near \"" + words[0] + "\"",
00476 cmd_name);
00477 }
00478 }
00479
00480 if (cmd->get_status_list_add().empty() &&
00481 cmd->get_status_list_rem().empty() &&
00482 common_data.get_postal_info().empty() &&
00483 common_data.get_voice().number == "" &&
00484 common_data.get_fax().number == "" &&
00485 common_data.get_email() == "" &&
00486 cmd->get_authInfo().get_pw() == "" &&
00487 !common_data.get_disclose().is_set()) {
00488 return cmd_contact_help("you didn't set a thing", cmd_name);
00489 }
00490
00491 cmd->set_common_data(common_data);
00492
00493 if (_debug) {
00494 printf(" contact id: [%s]\n", cmd->get_common_data().get_id().c_str());
00495
00496 set<string> status = cmd->get_status_list_add();
00497 set<string>::const_iterator st_it;
00498 if (!status.empty()) {
00499 printf(" status to add: [ ");
00500 for (st_it = status.begin(); st_it != status.end(); st_it++) {
00501 printf("%s ", (*st_it).c_str());
00502 }
00503 printf("]\n");
00504 }
00505
00506 status = cmd->get_status_list_rem();
00507 if (!status.empty()) {
00508 printf(" status to rem: [ ");
00509 for (st_it = status.begin(); st_it != status.end(); st_it++) {
00510 printf("%s ", (*st_it).c_str());
00511 }
00512 printf("]\n");
00513 }
00514
00515 vector<PostalInfo>::const_iterator it;
00516 vector<PostalInfo> postal_list;
00517 postal_list = cmd->get_common_data().get_postal_info();
00518 for (it = postal_list.begin(); it != postal_list.end(); it++) {
00519 SheppPrint::postal_info((*it));
00520 }
00521
00522 if (cmd->get_common_data().get_voice_f()) {
00523 printf(" voice:");
00524 SheppPrint::phone(cmd->get_common_data().get_voice());
00525 }
00526
00527 if (cmd->get_common_data().get_fax_f()) {
00528 printf(" fax :");
00529 SheppPrint::phone(cmd->get_common_data().get_fax());
00530 }
00531
00532 if (cmd->get_common_data().get_email_f()) {
00533 printf(" email: [%s]\n", cmd->get_common_data().get_email().c_str());
00534 }
00535
00536 if (cmd->get_authInfo().get_pw() != "") {
00537 SheppPrint::authInfo(cmd->get_authInfo());
00538 }
00539
00540 if (cmd->get_common_data().get_disclose().is_set()) {
00541 SheppPrint::disclose(cmd->get_common_data().get_disclose());
00542 }
00543 }
00544
00545 if (process_action(act) != 0) {
00546 return -1;
00547 }
00548
00549 return 0;
00550 }
00551
00553
00557 int cmd_contact(char *arg)
00558 {
00559 if (strlen(arg) > 0) {
00560 vector<string> words = SheppStrUtil::parse_line(arg);
00561
00562
00563 if (!words.empty() && !(words[0] == "help")) {
00564 if (words[0] == "check") {
00565 words.erase(words.begin());
00566 return cmd_contact_check(words);
00567 } else if (words[0] == "info") {
00568 words.erase(words.begin());
00569 return cmd_contact_info(words);
00570 } else if (words[0] == "transfer") {
00571 words.erase(words.begin());
00572 return cmd_contact_transfer(words);
00573 } else if (words[0] == "create") {
00574 words.erase(words.begin());
00575 return cmd_contact_create(words);
00576 } else if (words[0] == "delete") {
00577 words.erase(words.begin());
00578 return cmd_contact_delete(words);
00579 } else if (words[0] == "renew") {
00580 words.erase(words.begin());
00581 return cmd_contact_renew(words);
00582 } else if (words[0] == "update") {
00583 words.erase(words.begin());
00584 return cmd_contact_update(words);
00585 } else {
00586 return cmd_contact_help("invalid command: contact " + words[0]);
00587 }
00588 }
00589 }
00590
00591 return cmd_contact_help("");
00592 }
00593
00594 #endif //__CONTACT_FUNCTIONS_H__