challenger-send-post.sh (12613B)
1 #!/bin/bash 2 # This file is in the public domain. 3 set -eux 4 5 # You need to provide these (set in environment). 6 # PINGEN_CLIENT_ID= 7 # PINGEN_CLIENT_SECRET= 8 # PINGEN_ORG_ID= 9 if [ -x "$PINGEN_CLIENT_ID" ] 10 then 11 echo "PINGEN_CLIENT_ID not sent in environment" 12 exit 1 13 fi 14 if [ -x "$PINGEN_CLIENT_SECRET" ] 15 then 16 echo "PINGEN_CLIENT_SECRET not sent in environment" 17 exit 1 18 fi 19 if [ -x "$PINGEN_ORG_ID" ] 20 then 21 echo "PINGEN_ORG_ID not sent in environment" 22 exit 1 23 fi 24 25 ENDPOINT="https://api.pingen.com" 26 LOGS="$HOME/authorization-post.log" 27 ERRORS="$HOME/authorization-post.err" 28 29 MESSAGE=$(cat -) 30 ADDR="$1" 31 # We'll use "tr" to filter out anything that could be a problem in LaTeX. 32 # Name of business or individual, possibly with "Attn. $INDIVIDUAL_NAME" 33 CONTACT_NAME=$(echo "$ADDR" | jq -r .CONTACT_NAME | tr '|`~!"<>[]%_^#$&@\\{}' . ) 34 # Multi-line address 35 ADDRESS_LINES=$(echo "$ADDR" | jq -r .ADDRESS_LINES) 36 # This will just be the 2-letter country code 37 ADDRESS_COUNTRY=$(echo "$ADDR" | jq -r .ADDRESS_COUNTRY | tr '|`~!"<>[]%_^#$&@\\{}' . ) 38 39 case "$ADDRESS_COUNTRY" 40 in 41 AF) 42 COUNTRY='Afghanistan' 43 ;; 44 AX) 45 COUNTRY='Åland Islands' 46 ;; 47 AL) 48 COUNTRY='Albania' 49 ;; 50 DZ) 51 COUNTRY='Algeria' 52 ;; 53 AS) 54 COUNTRY='American Samoa' 55 ;; 56 AD) 57 COUNTRY='Andorra' 58 ;; 59 AO) 60 COUNTRY='Angola' 61 ;; 62 AI) 63 COUNTRY='Anguilla' 64 ;; 65 AQ) 66 COUNTRY='Antarctica' 67 ;; 68 AG) 69 COUNTRY='Antigua \\\& Barbuda' 70 ;; 71 AR) 72 COUNTRY='Argentina' 73 ;; 74 AM) 75 COUNTRY='Armenia' 76 ;; 77 AW) 78 COUNTRY='Aruba' 79 ;; 80 AU) 81 COUNTRY='Australia' 82 ;; 83 AT) 84 COUNTRY='Austria' 85 ;; 86 AZ) 87 COUNTRY='Azerbaijan' 88 ;; 89 BS) 90 COUNTRY='Bahamas' 91 ;; 92 BH) 93 COUNTRY='Bahrain' 94 ;; 95 BD) 96 COUNTRY='Bangladesh' 97 ;; 98 BB) 99 COUNTRY='Barbados' 100 ;; 101 BY) 102 COUNTRY='Belarus' 103 ;; 104 BE) 105 COUNTRY='Belgium' 106 ;; 107 BZ) 108 COUNTRY='Belize' 109 ;; 110 BJ) 111 COUNTRY='Benin' 112 ;; 113 BM) 114 COUNTRY='Bermuda' 115 ;; 116 BT) 117 COUNTRY='Bhutan' 118 ;; 119 BO) 120 COUNTRY='Bolivia' 121 ;; 122 BA) 123 COUNTRY='Bosnia \\\& Herzegovina' 124 ;; 125 BW) 126 COUNTRY='Botswana' 127 ;; 128 BV) 129 COUNTRY='Bouvet Island' 130 ;; 131 BR) 132 COUNTRY='Brazil' 133 ;; 134 IO) 135 COUNTRY='British Indian Ocean Territory' 136 ;; 137 VG) 138 COUNTRY='British Virgin Islands' 139 ;; 140 BN) 141 COUNTRY='Brunei' 142 ;; 143 BG) 144 COUNTRY='Bulgaria' 145 ;; 146 BF) 147 COUNTRY='Burkina Faso' 148 ;; 149 BI) 150 COUNTRY='Burundi' 151 ;; 152 KH) 153 COUNTRY='Cambodia' 154 ;; 155 CM) 156 COUNTRY='Cameroon' 157 ;; 158 CA) 159 COUNTRY='Canada' 160 ;; 161 CV) 162 COUNTRY='Cape Verde' 163 ;; 164 BQ) 165 COUNTRY='Caribbean Netherlands' 166 ;; 167 KY) 168 COUNTRY='Cayman Islands' 169 ;; 170 CF) 171 COUNTRY='Central African Republic' 172 ;; 173 TD) 174 COUNTRY='Chad' 175 ;; 176 CL) 177 COUNTRY='Chile' 178 ;; 179 CN) 180 COUNTRY='China' 181 ;; 182 CX) 183 COUNTRY='Christmas Island' 184 ;; 185 CC) 186 COUNTRY='Cocos' 187 ;; 188 CO) 189 COUNTRY='Colombia' 190 ;; 191 KM) 192 COUNTRY='Comoros' 193 ;; 194 CG) 195 COUNTRY='Congo - Brazzaville' 196 ;; 197 CD) 198 COUNTRY='Congo - Kinshasa' 199 ;; 200 CK) 201 COUNTRY='Cook Islands' 202 ;; 203 CR) 204 COUNTRY='Costa Rica' 205 ;; 206 CI) 207 #shellcheck disable=SC1112 208 COUNTRY='Côte d’Ivoire' 209 ;; 210 HR) 211 COUNTRY='Croatia' 212 ;; 213 CU) 214 COUNTRY='Cuba' 215 ;; 216 CW) 217 COUNTRY='Curaçao' 218 ;; 219 CY) 220 COUNTRY='Cyprus' 221 ;; 222 CZ) 223 COUNTRY='Czechia' 224 ;; 225 DK) 226 COUNTRY='Denmark' 227 ;; 228 DJ) 229 COUNTRY='Djibouti' 230 ;; 231 DM) 232 COUNTRY='Dominica' 233 ;; 234 DO) 235 COUNTRY='Dominican Republic' 236 ;; 237 EC) 238 COUNTRY='Ecuador' 239 ;; 240 EG) 241 COUNTRY='Egypt' 242 ;; 243 SV) 244 COUNTRY='El Salvador' 245 ;; 246 GQ) 247 COUNTRY='Equatorial Guinea' 248 ;; 249 ER) 250 COUNTRY='Eritrea' 251 ;; 252 EE) 253 COUNTRY='Estonia' 254 ;; 255 SZ) 256 COUNTRY='Eswatini' 257 ;; 258 ET) 259 COUNTRY='Ethiopia' 260 ;; 261 FK) 262 COUNTRY='Falkland Islands' 263 ;; 264 FO) 265 COUNTRY='Faroe Islands' 266 ;; 267 FJ) 268 COUNTRY='Fiji' 269 ;; 270 FI) 271 COUNTRY='Finland' 272 ;; 273 FR) 274 COUNTRY='France' 275 ;; 276 GF) 277 COUNTRY='French Guiana' 278 ;; 279 PF) 280 COUNTRY='French Polynesia' 281 ;; 282 TF) 283 COUNTRY='French Southern Territories' 284 ;; 285 GA) 286 COUNTRY='Gabon' 287 ;; 288 GM) 289 COUNTRY='Gambia' 290 ;; 291 GE) 292 COUNTRY='Georgia' 293 ;; 294 DE) 295 COUNTRY='Germany' 296 ;; 297 GH) 298 COUNTRY='Ghana' 299 ;; 300 GI) 301 COUNTRY='Gibraltar' 302 ;; 303 GR) 304 COUNTRY='Greece' 305 ;; 306 GL) 307 COUNTRY='Greenland' 308 ;; 309 GD) 310 COUNTRY='Grenada' 311 ;; 312 GP) 313 COUNTRY='Guadeloupe' 314 ;; 315 GU) 316 COUNTRY='Guam' 317 ;; 318 GT) 319 COUNTRY='Guatemala' 320 ;; 321 GG) 322 COUNTRY='Guernsey' 323 ;; 324 GN) 325 COUNTRY='Guinea' 326 ;; 327 GW) 328 COUNTRY='Guinea-Bissau' 329 ;; 330 GY) 331 COUNTRY='Guyana' 332 ;; 333 HT) 334 COUNTRY='Haiti' 335 ;; 336 HM) 337 COUNTRY='Heard \\\& McDonald Islands' 338 ;; 339 HN) 340 COUNTRY='Honduras' 341 ;; 342 HK) 343 COUNTRY='Hong Kong SAR China' 344 ;; 345 HU) 346 COUNTRY='Hungary' 347 ;; 348 IS) 349 COUNTRY='Iceland' 350 ;; 351 IN) 352 COUNTRY='India' 353 ;; 354 ID) 355 COUNTRY='Indonesia' 356 ;; 357 IR) 358 COUNTRY='Iran' 359 ;; 360 IQ) 361 COUNTRY='Iraq' 362 ;; 363 IE) 364 COUNTRY='Ireland' 365 ;; 366 IM) 367 COUNTRY='Isle of Man' 368 ;; 369 IL) 370 COUNTRY='Israel' 371 ;; 372 IT) 373 COUNTRY='Italy' 374 ;; 375 JM) 376 COUNTRY='Jamaica' 377 ;; 378 JP) 379 COUNTRY='Japan' 380 ;; 381 JE) 382 COUNTRY='Jersey' 383 ;; 384 JO) 385 COUNTRY='Jordan' 386 ;; 387 KZ) 388 COUNTRY='Kazakhstan' 389 ;; 390 KE) 391 COUNTRY='Kenya' 392 ;; 393 KI) 394 COUNTRY='Kiribati' 395 ;; 396 KW) 397 COUNTRY='Kuwait' 398 ;; 399 KG) 400 COUNTRY='Kyrgyzstan' 401 ;; 402 LA) 403 COUNTRY='Laos' 404 ;; 405 LV) 406 COUNTRY='Latvia' 407 ;; 408 LB) 409 COUNTRY='Lebanon' 410 ;; 411 LS) 412 COUNTRY='Lesotho' 413 ;; 414 LR) 415 COUNTRY='Liberia' 416 ;; 417 LY) 418 COUNTRY='Libya' 419 ;; 420 LI) 421 COUNTRY='Liechtenstein' 422 ;; 423 LT) 424 COUNTRY='Lithuania' 425 ;; 426 LU) 427 COUNTRY='Luxembourg' 428 ;; 429 MO) 430 COUNTRY='Macao SAR China' 431 ;; 432 MG) 433 COUNTRY='Madagascar' 434 ;; 435 MW) 436 COUNTRY='Malawi' 437 ;; 438 MY) 439 COUNTRY='Malaysia' 440 ;; 441 MV) 442 COUNTRY='Maldives' 443 ;; 444 ML) 445 COUNTRY='Mali' 446 ;; 447 MT) 448 COUNTRY='Malta' 449 ;; 450 MH) 451 COUNTRY='Marshall Islands' 452 ;; 453 MQ) 454 COUNTRY='Martinique' 455 ;; 456 MR) 457 COUNTRY='Mauritania' 458 ;; 459 MU) 460 COUNTRY='Mauritius' 461 ;; 462 YT) 463 COUNTRY='Mayotte' 464 ;; 465 MX) 466 COUNTRY='Mexico' 467 ;; 468 FM) 469 COUNTRY='Micronesia' 470 ;; 471 MD) 472 COUNTRY='Moldova' 473 ;; 474 MC) 475 COUNTRY='Monaco' 476 ;; 477 MN) 478 COUNTRY='Mongolia' 479 ;; 480 ME) 481 COUNTRY='Montenegro' 482 ;; 483 MS) 484 COUNTRY='Montserrat' 485 ;; 486 MA) 487 COUNTRY='Morocco' 488 ;; 489 MZ) 490 COUNTRY='Mozambique' 491 ;; 492 MM) 493 COUNTRY='Myanmar' 494 ;; 495 NA) 496 COUNTRY='Namibia' 497 ;; 498 NR) 499 COUNTRY='Nauru' 500 ;; 501 NP) 502 COUNTRY='Nepal' 503 ;; 504 NL) 505 COUNTRY='Netherlands' 506 ;; 507 NC) 508 COUNTRY='New Caledonia' 509 ;; 510 NZ) 511 COUNTRY='New Zealand' 512 ;; 513 NI) 514 COUNTRY='Nicaragua' 515 ;; 516 NE) 517 COUNTRY='Niger' 518 ;; 519 NG) 520 COUNTRY='Nigeria' 521 ;; 522 NU) 523 COUNTRY='Niue' 524 ;; 525 NF) 526 COUNTRY='Norfolk Island' 527 ;; 528 KP) 529 COUNTRY='North Korea' 530 ;; 531 MK) 532 COUNTRY='North Macedonia' 533 ;; 534 MP) 535 COUNTRY='Northern Mariana Islands' 536 ;; 537 NO) 538 COUNTRY='Norway' 539 ;; 540 OM) 541 COUNTRY='Oman' 542 ;; 543 PK) 544 COUNTRY='Pakistan' 545 ;; 546 PW) 547 COUNTRY='Palau' 548 ;; 549 PS) 550 COUNTRY='Palestinian Territories' 551 ;; 552 PA) 553 COUNTRY='Panama' 554 ;; 555 PG) 556 COUNTRY='Papua New Guinea' 557 ;; 558 PY) 559 COUNTRY='Paraguay' 560 ;; 561 PE) 562 COUNTRY='Peru' 563 ;; 564 PH) 565 COUNTRY='Philippines' 566 ;; 567 PN) 568 COUNTRY='Pitcairn Islands' 569 ;; 570 PL) 571 COUNTRY='Poland' 572 ;; 573 PT) 574 COUNTRY='Portugal' 575 ;; 576 PR) 577 COUNTRY='Puerto Rico' 578 ;; 579 QA) 580 COUNTRY='Qatar' 581 ;; 582 RE) 583 COUNTRY='Réunion' 584 ;; 585 RO) 586 COUNTRY='Romania' 587 ;; 588 RU) 589 COUNTRY='Russia' 590 ;; 591 RW) 592 COUNTRY='Rwanda' 593 ;; 594 WS) 595 COUNTRY='Samoa' 596 ;; 597 SM) 598 COUNTRY='San Marino' 599 ;; 600 ST) 601 COUNTRY='São Tomé \\\& Príncipe' 602 ;; 603 SA) 604 COUNTRY='Saudi Arabia' 605 ;; 606 SN) 607 COUNTRY='Senegal' 608 ;; 609 RS) 610 COUNTRY='Serbia' 611 ;; 612 SC) 613 COUNTRY='Seychelles' 614 ;; 615 SL) 616 COUNTRY='Sierra Leone' 617 ;; 618 SG) 619 COUNTRY='Singapore' 620 ;; 621 SX) 622 COUNTRY='Sint Maarten' 623 ;; 624 SK) 625 COUNTRY='Slovakia' 626 ;; 627 SI) 628 COUNTRY='Slovenia' 629 ;; 630 SB) 631 COUNTRY='Solomon Islands' 632 ;; 633 SO) 634 COUNTRY='Somalia' 635 ;; 636 ZA) 637 COUNTRY='South Africa' 638 ;; 639 GS) 640 COUNTRY='South Georgia \\\& South Sandwich Islands' 641 ;; 642 KR) 643 COUNTRY='South Korea' 644 ;; 645 SS) 646 COUNTRY='South Sudan' 647 ;; 648 ES) 649 COUNTRY='Spain' 650 ;; 651 LK) 652 COUNTRY='Sri Lanka' 653 ;; 654 BL) 655 COUNTRY='St. Barthélemy' 656 ;; 657 SH) 658 COUNTRY='St. Helena' 659 ;; 660 KN) 661 COUNTRY='St. Kitts \\\& Nevis' 662 ;; 663 LC) 664 COUNTRY='St. Lucia' 665 ;; 666 MF) 667 COUNTRY='St. Martin' 668 ;; 669 PM) 670 COUNTRY='St. Pierre \\\& Miquelon' 671 ;; 672 VC) 673 COUNTRY='St. Vincent \\\& Grenadines' 674 ;; 675 SD) 676 COUNTRY='Sudan' 677 ;; 678 SR) 679 COUNTRY='Suriname' 680 ;; 681 SJ) 682 COUNTRY='Svalbard \\\& Jan Mayen' 683 ;; 684 SE) 685 COUNTRY='Sweden' 686 ;; 687 CH) 688 COUNTRY='Switzerland' 689 ;; 690 SY) 691 COUNTRY='Syria' 692 ;; 693 TW) 694 COUNTRY='Taiwan' 695 ;; 696 TJ) 697 COUNTRY='Tajikistan' 698 ;; 699 TZ) 700 COUNTRY='Tanzania' 701 ;; 702 TH) 703 COUNTRY='Thailand' 704 ;; 705 TL) 706 COUNTRY='Timor-Leste' 707 ;; 708 TG) 709 COUNTRY='Togo' 710 ;; 711 TK) 712 COUNTRY='Tokelau' 713 ;; 714 TO) 715 COUNTRY='Tonga' 716 ;; 717 TT) 718 COUNTRY='Trinidad \\\& Tobago' 719 ;; 720 TN) 721 COUNTRY='Tunisia' 722 ;; 723 TR) 724 COUNTRY='Turkey' 725 ;; 726 TM) 727 COUNTRY='Turkmenistan' 728 ;; 729 TC) 730 COUNTRY='Turks \\\& Caicos Islands' 731 ;; 732 TV) 733 COUNTRY='Tuvalu' 734 ;; 735 UM) 736 COUNTRY='U.S. Outlying Islands' 737 ;; 738 VI) 739 COUNTRY='U.S. Virgin Islands' 740 ;; 741 UG) 742 COUNTRY='Uganda' 743 ;; 744 UA) 745 COUNTRY='Ukraine' 746 ;; 747 AE) 748 COUNTRY='United Arab Emirates' 749 ;; 750 GB) 751 COUNTRY='United Kingdom' 752 ;; 753 US) 754 COUNTRY='United States' 755 ;; 756 UY) 757 COUNTRY='Uruguay' 758 ;; 759 UZ) 760 COUNTRY='Uzbekistan' 761 ;; 762 VU) 763 COUNTRY='Vanuatu' 764 ;; 765 VA) 766 COUNTRY='Vatican City' 767 ;; 768 VE) 769 COUNTRY='Venezuela' 770 ;; 771 VN) 772 COUNTRY='Vietnam' 773 ;; 774 WF) 775 COUNTRY='Wallis \\\& Futuna' 776 ;; 777 EH) 778 COUNTRY='Western Sahara' 779 ;; 780 YE) 781 COUNTRY='Yemen' 782 ;; 783 ZM) 784 COUNTRY='Zambia' 785 ;; 786 ZW) 787 COUNTRY='Zimbabwe' 788 ;; 789 *) 790 echo "ERROR: Country code '${ADDRESS_COUNTRY}' not recognized" 1>&2 791 COUNTRY="${ADDRESS_COUNTRY}" 792 ;; 793 esac 794 795 # Convert new lines to " " for LaTeX. excluding trailing new-line 796 # at the end if present: 797 CONTACT_TEX=$(echo -n "${CONTACT_NAME%$'\n'}" | awk '{printf "%s ", $0}') 798 # Convert new lines to "\\" for LaTeX, and escape problematic characters, 799 # but leave "\\" intact. 800 ADDRESS_TEX=$(echo -n "${ADDRESS_LINES%$'\n'}" | awk '{printf "%s\\\\\\\\", $0}' | tr '|`~!"<>[]%_^#$&@{}' . | sed 's/\\\\/@@ESCAPED@@/g; s/\\./\./g; s/@@ESCAPED@@/\\\\/g') 801 802 803 MYDIR=$(mktemp -d /tmp/authorization-post-XXXXXX) 804 cd "$MYDIR" 805 sed -e "s|%NAME%|$CONTACT_TEX|g" \ 806 -e "s|%ADDRESS_TEX%|$ADDRESS_TEX|g" \ 807 -e "s/%COUNTRY%/$COUNTRY/g" > input.tex <<EOF 808 \NeedsTeXFormat{LaTeX2e} 809 \documentclass[fontsize=11pt,a4paper]{scrlttr2} 810 \makeatletter 811 \KOMAoptions{foldmarks=off} 812 \makeatother 813 \setplength{toaddrhpos}{3cm} 814 \setkomavar{subject}{Address validation} 815 \setkomavar{signature}{Taler Operations AG, c/o Berner Fachhochschule, H\"oheweg 80, 2502 Biel/Bienne} 816 \date{\today} 817 \begin{document} 818 \begin{letter}{%NAME%\\\\ %ADDRESS_TEX% %COUNTRY%} 819 \opening{To whom it may concern,} 820 EOF 821 echo "$MESSAGE" >> input.tex 822 cat - >> input.tex <<EOF 823 \closing{Best regards} 824 \end{letter} 825 \end{document} 826 EOF 827 pdflatex input.tex > "$MYDIR/pdflatex.out" 2> "$MYDIR/pdflatex.err" 828 829 REPLY=$(curl \ 830 -s \ 831 -X POST \ 832 -H "Content-Type: application/x-www-form-urlencoded" \ 833 --data-urlencode "grant_type=client_credentials" \ 834 --data-urlencode "client_id=$PINGEN_CLIENT_ID" \ 835 --data-urlencode "client_secret=$PINGEN_CLIENT_SECRET" \ 836 --data-urlencode "scope=letter" \ 837 https://identity.pingen.com/auth/access-tokens) 838 839 ACCESS_TOKEN=$(echo "$REPLY" | jq -r .access_token) 840 841 REPLY=$(curl -s \ 842 -X GET "$ENDPOINT/file-upload" \ 843 -H "Authorization: Bearer $ACCESS_TOKEN") 844 ATTRS=$(echo "$REPLY" | jq .data.attributes) 845 UPLOAD_URL=$(echo "$ATTRS" | jq -r .url) 846 URL_SIG=$(echo "$ATTRS" | jq -r .url_signature) 847 848 STATUS=$(curl -s \ 849 -X PUT \ 850 -T input.pdf \ 851 "$UPLOAD_URL" \ 852 -o "$MYDIR/upload-status.txt" \ 853 -w "%{http_code}" -s) 854 855 case "$STATUS" in 856 200) 857 ;; 858 201) 859 ;; 860 204) 861 ;; 862 *) 863 echo "Failed to upload letter: $STATUS" >> "$ERRORS" 864 cat "$MYDIR/upload-status.txt" >> "$ERRORS" 865 exit 1; 866 ;; 867 esac 868 869 870 REQUEST="$(jq -n ' 871 { data: { 872 type: "letters", 873 attributes: { 874 file_original_name: "input.pdf", 875 file_url: $UPLOAD_URL, 876 file_url_signature: $URL_SIG, 877 address_position: "left", 878 delivery_product: "cheap", 879 print_mode: "duplex", 880 auto_send: true, 881 print_spectrum: "grayscale" 882 } } 883 }' \ 884 --arg UPLOAD_URL "$UPLOAD_URL" \ 885 --arg URL_SIG "$URL_SIG" \ 886 )" 887 888 STATUS=$(curl -s \ 889 --request POST \ 890 --url "$ENDPOINT/organisations/${PINGEN_ORG_ID}/letters" \ 891 --header "Content-Type: application/vnd.api+json" \ 892 --header "Authorization: Bearer $ACCESS_TOKEN" \ 893 -d "$REQUEST" \ 894 -o "$MYDIR/final-reply.txt" \ 895 -w "%{http_code}" -s) 896 case "$STATUS" in 897 201) 898 cat "$MYDIR/final-reply.txt" >> "$LOGS" 899 ;; 900 *) 901 echo "Failed to add letter: $STATUS" >> "$ERRORS" 902 cat "$MYDIR/final-reply.txt" >> "$ERRORS" 903 exit 1; 904 ;; 905 esac 906 907 rm -r "$MYDIR" 908 exit 0