/usr/share/perl5/PilotMgr/SyncPlan.pm is in pilot-manager 1.107.0pre108-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 | #
# sync-plan by Kenneth Albanowski (kjahds@kjahds.com)
# Conversion to SyncPlan PilotManager conduit by Alan.Harder@Sun.COM
# Originally converted from sync-plan in pilot-link 0.8.9.
# 1/28/98
# Bugfix patch to ReadPlanReply sub by Aaron Kaplan (kaplan@cs.rochester.edu)
# 5/22/98
# Bugfix for talking to netplan with -a flag by
# Chris Keane (Chris.Keane@comlab.ox.ac.uk) and
# Jeff Dairiki (dairiki@dairiki.org)
# 1/25/99
# Handling for dates prior to 1970 (supported on pilot, not by plan) patch by
# Jeff Dairiki (dairiki@dairiki.org)
# 1/25/99
# Updated with changes in sync-plan from pilot-link up thru version 0.9.3.
# New change:
# Support for weekly appts from pilot with frequency > 1.
# Changes in sync-plan that were already made in SyncPlan:
# Updates to keep pilot connection alive during sync.
# Fix for parsing partial responses from plan.
# Alan.Harder@Sun.COM
# 11/9/99
# Research for fixing repeat end date for daily repeating plan records
# ("repeat every _1_ days; Last day: stop repeating on __")
# by Aaron Kaplan (kaplan@csw.rochester.edu), patch by Alan.Harder@Sun.COM
# 11/9/99
# Patch to support plan records with two alarms (just use earlier one for
# pilot) by Kevin Fink (kevin@n2h2.com)
# 11/9/99
# Configurable level of verbose output and configurable netplan port,
# patch by Andreas S. Oesterhelt (oes@paradis.rhein.de)
# 11/9/99
#
package SyncPlan;
use IO::Socket;
use IO::Select;
use Time::Local;
use Data::Dumper;
use Digest::MD5;
use Carp;
use strict;
my $VERSION = "0.96 BETA";
my ($gConfigDlg, $controldir, $dlp, $info, $db);
my (%control, %pilothash, %pilotID, %planID, %exceptID, %planRecord,
%dbname, %sawName);
my ($slowsync, $file, $pilotname, $maxseed, $netplanversion);
my ($PREFS); # configuration variables
my ($RCFILE); # configuration file
sub DatePlanToPerl {
my($PlanDate) = @_;
my($m,$d,$y) = split(m!/!,$PlanDate);
if ($y<40) {
$y+=100;
}
if ($y>1900) {
$y-=1900;
}
$m--;
timelocal(0,0,0,$d,$m,$y);
}
sub TimePlanToPerl {
my($PlanTime) = @_;
my($h,$m,$s) = split(m!:!,$PlanTime);
return undef if $h == 99 and $m == 99 and $s == 99;
$s + ($m*60) + ($h*60*60);
}
sub TimePerlToPlan {
my($PerlDT) = @_;
return "99:99:99" if not defined $PerlDT;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($PerlDT);
"$hour:$min:$sec";
}
sub TimeRelPerlToPlan {
my($PerlDT) = @_;
return "99:99:99" if not defined $PerlDT;
my($sec,$min,$hour);
$hour = int($PerlDT/ (60*60));
$PerlDT -= $hour*60*60;
$min = int($PerlDT/ (60));
$PerlDT -= $min*60;
$sec = int($PerlDT);
$PerlDT -= $sec;
"$hour:$min:$sec";
}
sub DatePilotToPerl {
my ($s,$m,$h, $mday,$mon,$year) = @_;
if (ref $s eq 'ARRAY') {
($s,$m,$h, $mday,$mon,$year) = @$s;
}
my ($date, $time);
if ($year >= 70 and $year <= 138) {
$date = eval { timelocal($s,$m,$h,$mday,$mon,$year) };
PilotMgr::msg("Trouble converting date: $mon/$mday/$year $h:$m$s")
if $@;
$time = $s + 60 * ($m + 60 * $h);
}
else {
PilotMgr::msg("Bad year: $year");
}
return wantarray ? ($date, $time) : $date;
}
sub DatePerlToPlan {
my($PerlDT) = @_;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($PerlDT);
$year += 1900;
$mon++;
"$mon/$mday/$year";
}
sub RecordPlanToPilot {
my($plan,$pilot) = @_;
if (not defined $pilot) {
$pilot = PDA::Pilot::AppointmentDatabase->record;
}
$pilot->{'id'} = $plan->{'pilotid'};
$pilot->{'description'} = join("\xA", @{$plan->{'note'}}) if defined $plan->{'note'};
$pilot->{'note'} = join("\xA", @{$plan->{'message'}}) if defined $plan->{'message'};
$pilot->{'description'} ||= "";
if (defined $plan->{'time'}) {
$pilot->{'begin'} = [localtime($plan->{'date'}+$plan->{'time'})];
$pilot->{'end'} = [localtime($plan->{'date'}+$plan->{'time'}+$plan->{'length'})];
$pilot->{'event'}=0;
} else {
$pilot->{'begin'} = [localtime($plan->{'date'})];
$pilot->{'event'}=1;
}
if ($plan->{'early'} and $plan->{'late'} and ($plan->{'early'} != $plan->{'late'})) {
PilotMgr::msg( "Two alarms - using earlier one." );
$plan->{'late'} = $plan->{'early'};
#return undef;
}
if ($plan->{'early'} or $plan->{'late'}) {
my($alarm) = $plan->{'early'} || $plan->{'late'};
if ($alarm > (60*60*24)) {
$pilot->{'alarm'}->{'units'} = "days";
$pilot->{'alarm'}->{'advance'} = int($alarm / (60*60*24));
} elsif ($alarm > (60*60)) {
$pilot->{'alarm'}->{'units'} = "hours";
$pilot->{'alarm'}->{'advance'} = int($alarm / (60*60));
} else {
$pilot->{'alarm'}->{'units'} = "minutes";
$pilot->{'alarm'}->{'advance'} = int($alarm / 60);
}
}
if (defined $plan->{'exceptions'}) {
foreach (@{$plan->{'exceptions'}}) {
push @{$pilot->{'exceptions'}}, [localtime($_)];
}
} else {
delete $pilot->{'exceptions'};
}
if (defined $plan->{repeat}) {
PilotMgr::msg( "Converting repetition...\n" ) if ($PREFS->{'Debug'} > 2);
delete $pilot->{repeat};
if ($plan->{repeat}->[1]) {
# It seems $plan->{repeat}->[1] contains seconds
# since 0:00 1/1/1970 *local time*, not UTC.
# (see comments in time.c in the plan source)
# So we convert here with the gmtime function
# instead of localtime so that no timezone converting
# is done, but we still end up with a local time.
$pilot->{repeat}->{end} = [gmtime($plan->{repeat}->[1])];
}
my($days,$end,$weekday,$mday,$yearly) = @{$plan->{repeat}};
PilotMgr::msg( "Days: $days, End: $end, Weekday: $weekday, Mday: $mday, Yearly: $yearly\n" ) if ($PREFS->{'Debug'} > 2);
$pilot->{repeat}->{weekstart} = 0;
$pilot->{repeat}->{frequency} = 1;
if ($days and !$weekday and !$mday and !$yearly) {
$pilot->{repeat}->{type} = "Daily";
$pilot->{repeat}->{frequency} = $days / (60*60*24);
} elsif(!$days and !$weekday and !$mday and $yearly) {
$pilot->{repeat}->{type} = "Yearly";
} elsif(!$days and !$weekday and ($mday == (1 << $pilot->{begin}[3])) and !$yearly) {
$pilot->{repeat}->{type} = "MonthlyByDate";
} elsif(!$days and $weekday and (($weekday & 0xff80) == 0) and !$mday and !$yearly) {
my $i;
$pilot->{repeat}->{type} = "Weekly";
foreach $i (0..6) {
$pilot->{repeat}->{days}[$i] = !! ($weekday & (1<<$i));
}
# If the weekday list does include the day the event is one, abort
if (!$pilot->{repeat}{days}[$pilot->{begin}[6]]) {
return undef;
}
} elsif(not $days and $weekday and not $mday and not $yearly) {
my($wday) = $pilot->{begin}[6];
my($week) = int(($pilot->{begin}[3]-1)/7);
PilotMgr::msg( "weekday = $weekday, wday = $wday, week = $week\n" ) if ($PREFS->{'Debug'} > 2);
if (($weekday & 0x7f) != (1<<$wday)) {
return undef;
}
if (($weekday & 4096) and ($weekday & 8192)) {
$weekday &= ~4096;
}
if ($week == 4) {
$week = 5;
}
if (($weekday & 0xff00) != (256<<$week)) {
return undef;
}
if ($week == 5) {
$week = 4;
}
$pilot->{repeat}->{type} = "MonthlyByDay";
$pilot->{repeat}->{day} = $week*7+$wday;
} else {
return undef;
}
} else {
delete $pilot->{repeat};
}
$pilot;
}
sub RecordPilotToPlan {
my($pilot,$plan) = @_;
$plan = {color => 0} if not defined $plan;
$plan->{pilotid} = $pilot->{id};
$plan->{id} ||= 0;
$plan->{message} = [split("\xA", $pilot->{note})] if defined $pilot->{note};
$plan->{note} = [split("\xA", $pilot->{description})] if defined $pilot->{description};
my ($date, $time) = DatePilotToPerl($pilot->{begin});
unless ($date) {
PilotMgr::msg("Begin time in Pilot record untranslatable.");
return undef;
}
$plan->{date} = $date;
if ($pilot->{event}) {
$plan->{'time'} = undef;
$plan->{'length'} = 0;
} else {
$plan->{'time'} = $time;
my $end = DatePilotToPerl($pilot->{end});
unless ($end) {
PilotMgr::msg("End time in Pilot record untranslatable.");
return undef;
}
$plan->{'length'} = $end - $date;
}
if (exists $pilot->{'alarm'}) {
my($alarm)=0;
$plan->{noalarm} = 0;
if ($pilot->{'alarm'}{units} eq "days") {
$alarm = $pilot->{'alarm'}->{advance} * (60*60*24);
} elsif ($pilot->{'alarm'}{units} eq "hours") {
$alarm = $pilot->{'alarm'}->{advance} * (60*60);
} elsif ($pilot->{'alarm'}{units} eq "minutes") {
$alarm = $pilot->{'alarm'}->{advance} * (60);
}
if ($plan->{late}) {
$plan->{late} = $alarm;
$plan->{early} = 0;
} else {
$plan->{late} = 0;
$plan->{early} = $alarm;
}
} else {
$plan->{noalarm} = 1;
$plan->{late}=0;
$plan->{early}=0;
}
if (exists $pilot->{exceptions}) {
# Plan records can only deal with four exceptions,
if (@{$pilot->{exceptions}} > 4) {
return undef;
}
foreach (@{$pilot->{exceptions}}) {
push @{$plan->{exceptions}}, timelocal(@{$_});
}
}
delete $plan->{repeat};
if (exists $pilot->{repeat}) {
$plan->{repeat} = [0,0,0,0,0];
if ($pilot->{repeat}->{type} eq "Daily") {
$plan->{repeat}->[0] = (60*60*24) * $pilot->{repeat}->{frequency};
$plan->{repeat}->[4] = 0;
} elsif ($pilot->{repeat}->{type} eq "Yearly" and ($pilot->{repeat}->{frequency}==1)) {
$plan->{repeat}->[4] = 1;
} elsif ($pilot->{repeat}->{type} eq "Weekly" and ($pilot->{repeat}->{frequency}==1)) {
my($r,$i)=(0);
foreach $i (0..6) {
if ($pilot->{repeat}->{days}[$i]) {
$r |= (1<<$i);
}
}
$plan->{repeat}->[2] = $r;
} elsif ($pilot->{repeat}->{type} eq "Weekly" and ($pilot->{repeat}->{frequency}>1)) {
$plan->{repeat}->[0] = (60*60*24) * $pilot->{repeat}->{frequency} * 7;
$plan->{repeat}->[4] = 0;
} elsif ($pilot->{repeat}->{type} eq "MonthlyByDate" and ($pilot->{repeat}->{frequency}==1)) {
$plan->{repeat}->[3] = 1 << $pilot->{begin}[3];
} elsif ($pilot->{repeat}->{type} eq "MonthlyByDay" and ($pilot->{repeat}->{frequency}==1)) {
my($day) = $pilot->{repeat}{day} % 7;
my($week) = int($pilot->{repeat}{day} / 7);
$week=5 if $week == 4;
$plan->{repeat}->[2] = (1 << $day) | (256 << $week);
} else {
return undef;
}
if (defined $pilot->{repeat}->{end}) {
$plan->{repeat}->[1] = timelocal(@{$pilot->{repeat}->{end}});
}
}
$plan;
}
sub generaterecord {
my($rec) = @_;
my(@output);
#print "Generating Plan record: ", Dumper($rec),"\n";
push(@output, DatePerlToPlan($rec->{date})." ".
TimeRelPerlToPlan($rec->{'time'})." ".
TimeRelPerlToPlan($rec->{'length'})." ".
TimeRelPerlToPlan($rec->{early})." ".
TimeRelPerlToPlan($rec->{late})." ".
($rec->{suspended} ? "S" : "-").
($rec->{private} ? "P" : "-").
($rec->{noalarm} ? "N" : "-").
($rec->{hide_month} ? "M" : "-").
($rec->{hide_year} ? "Y" : "-").
($rec->{hide_week} ? "W" : "-").
($rec->{hide_yearover} ? "O" : "-").
($rec->{d_flag} ? "D" : "-").
"-".
"-".
" ".$rec->{color});
if (defined $rec->{repeat}) {
push @output, "R\t".join(" ",@{$rec->{repeat}});
}
if (defined $rec->{exceptions}) {
foreach (@{$rec->{exceptions}}) {
push @output, "E\t".DatePerlToPlan($_);
}
}
if (defined $rec->{note}) {
push @output, map("N\t$_", @{$rec->{note}});
}
if (defined $rec->{message}) {
push @output, map("M\t$_", @{$rec->{message}});
}
if (defined $rec->{script}) {
push @output, map("S\t$_", @{$rec->{script}});
}
if (defined $rec->{other}) {
foreach (@{$rec->{other}}) {
push @output, $_;
}
}
my($hash) = new Digest::MD5;
foreach (@output) {
#print "Adding |$_| to hash\n";
$hash->add($_);
}
$rec->{pilothash} = $hash->hexdigest;
{
my($i);
for($i=0;$i<@output;$i++) {
last if $output[$i] =~ /^S/;
}
$rec->{pilotexcept} += 0;
my(@US);
@US = @{$rec->{'unhashedscript'}} if defined $rec->{'unhashedscript'};
unshift @US, "S\t#Pilot: 1 $pilotname $rec->{pilothash} $rec->{pilotexcept} $rec->{pilotid}";
splice @output, $i, 0, @US;
}
PilotMgr::msg( "Generated record |" . join("\n", @output). "|\n" ) if ($PREFS->{'Debug'} > 2);
join("\n",@output);
}
sub PrintPlanRecord {
my($rec) = @_;
my($output);
$output = DatePerlToPlan($rec->{date});
if ($rec->{'time'}) {
$output .= " ".TimePerlToPlan($rec->{'time'})."-".
TimePerlToPlan($rec->{'time'}+$rec->{'length'});
}
$output .= " '".join("\\n",@{$rec->{note}})."'" if defined $rec->{'note'};
$output .= " (".join("\\n",@{$rec->{message}}).")" if defined $rec->{message};
if ($rec->{repeat}) {
my(@r);
if ($rec->{repeat}[0]) {
push @r, "every " . ($rec->{repeat}[0] / (60*60*24)) . " days";
}
if ($rec->{repeat}[4]) {
push @r, "every year";
}
if ($rec->{repeat}[3]) {
my($i) = $rec->{repeat}[3];
if ($i & 1) {
push @r, "the last day of each month";
}
foreach (1..31) {
push @r, "the $_ of each month" if $i & (1<<$_);
}
}
if ($rec->{repeat}[2]) {
push @r, "until ".scalar(localtime($rec->{repeat}[2]));
}
if (@r) {
$output .= " repeat ".join(", ", @r);
}
}
$output .= " {ID:$rec->{pilotid}, Except:";
$output .= $rec->{pilotexcept} if (defined $rec->{pilotexcept});
$output .= ", Changed:";
$output .= $rec->{modified} if (defined $rec->{modified});
$output .= ", Deleted:";
$output .= $rec->{deleted} if (defined $rec->{deleted});
$output .= "}";
$output;
}
sub PrintPilotRecord {
my($rec) = @_;
my($output);
$output = ($rec->{begin}[5]+1900)."/".($rec->{begin}[4]+1)."/".$rec->{begin}[3];
if (!$rec->{event}) {
$output .= " ";
$output .= ($rec->{begin}[2]).":".($rec->{begin}[1]).":".$rec->{begin}[0];
$output .= "-";
$output .= ($rec->{end}[2]).":".($rec->{end}[1]).":".$rec->{end}[0];
}
$output .= " '$rec->{description}'";
$output .= " ($rec->{message})" if (defined $rec->{message});
$output .= " {ID:$rec->{id}, Except:";
$output .= $exceptID{$rec->{id}} if (defined $exceptID{$rec->{id}});
$output .= ", Changed:";
$output .= $rec->{modified} if (defined $rec->{modified});
$output .= ", Deleted:";
$output .= $rec->{deleted} if (defined $rec->{deleted});
$output .= "}";
$output =~ s/\r/\\r/g;
$output =~ s/\n/\\n/g;
$output;
}
# takes a Plan record in hash format
sub WritePlanRecord {
my($socket, $record) = @_;
my($raw) = generaterecord($record);
my($reply);
$record->{id} ||= 0;
#print "ID is $record->{id}\n";
$raw =~ s/\n/\\\n/g;
$raw = "w$file $record->{id} $raw\n";
$record->{raw} = $raw;
SendPlanCommand($socket, $raw);
$reply = ReadPlanReply($socket);
#print "Installing record $record->{id} (PilotID: $record->{pilotid}) in Plan: ", Dumper($record);
# syswrite $socket, $raw, length($raw);
# sysread $socket, $reply, 1024;
# print "Reply to installation: |$reply|\n";
if ($reply =~ /^w[tf](\d+)/) {
$record->{id} = $1;
$planRecord{$1} = $record;
# print "New record id: $1\n";
} else {
PilotMgr::msg( "Failed write: $reply\n" );
}
}
sub LoadPilotRecord {
my($db, $i) = @_;
my($record) = $db->getRecord($i);
if ($record) {
$pilotID{$record->{id}} = $record;
}
$record;
}
# takes a Plan record in hash format
sub DeletePlanRecord {
my($socket, $record) = @_;
my($raw);
$raw = "d$file $record->{id}\n";
# print "Deleting record $record->{id} (PilotID: $record->{pilotid}) in Plan\n";
# syswrite $socket, $raw, length($raw);
SendPlanCommand($socket, $raw);
}
# takes a Pilot record in hash format
sub WritePilotRecord {
my($db, $control, $record) = @_;
$record->{id} ||= 0;
#print "Installing record in Pilot: ",Dumper($record);
my($id) = $db->setRecord($record);
if ($id) {
$pilotID{$id} = $record;
my ($hash) = HashPilotRecord($record);
$pilothash{$id} = $hash;
$dbname{$id} = $control->{name};
$record->{id} = $id;
$exceptID{$id} = 0;
}
$id;
}
sub DeletePilotRecord {
my($db, $id) = @_;
my($result) = $db->deleteRecord($id);
if ($result>=0) {
delete $pilothash{$id};
delete $pilotID{$id};
delete $dbname{$id};
delete $exceptID{$id};
}
$result;
}
$maxseed = 0;
sub dorecord {
my($db,$socket,$control, $i,$r) = @_;
# print "Record: $r\n";
my(@l) = split(/\n/,$r);
my($rec) = { raw => [@l], other => [] };
my(@E,@R,@N,@M,@S,@US);
my($hash) = new Digest::MD5;
$l[0] =~ s/\s+/ /g;
$hash->add($l[0]);
my($date, $time, $length, $early, $late, $flags, $color) = split(/\s+/, shift @l);
$rec->{pilotrec} = "";
foreach (@l) {
if (/^E\t/) {
push @E, $';
} elsif (/^M\t/) {
push @M, $';
} elsif (/^N\t/) {
push @N, $';
} elsif (/^S\t/) {
my ($s) = $';
if ($s =~ /^\s*#Pilot:\s+(\d+)\s*(.*)$/) {
if ($1 == 1) { # version number
my($name,$hash,$except,$id) = split(/\s+/, $2);
#print Dumper({Name=>$name,Hash=>$hash,Except=>$except,ID=>$id});
if ($name eq $pilotname) {
$rec->{pilotid} = $id;
$rec->{pilotexcept} = $except || 0;
$rec->{pilothash} = $hash;
$planID{$id} = $rec;
next;
}
}
push @US, $_;
next; # skip hash add
} else {
push @S, $s;
}
} elsif (/^R\t/) {
my($r) = $';
$r =~ s/\s+/ /g;
$rec->{repeat} = [split(/\s+/, $r)];
} else {
push @{$rec->{other}}, $_;
}
#print "Adding |$_| to hash\n";
$hash->add($_);
}
$hash = $hash->hexdigest;
#print "Old hash: $hash, New hash: $rec->{pilothash}\n";
$rec->{modified} = ($rec->{pilothash} ne $hash);
$rec->{note} = \@N if @N;
$rec->{script} = \@S if @S;
$rec->{unhashedscript} = \@US if @US;
$rec->{message} = \@M if @M;
$rec->{date} = DatePlanToPerl($date);
$rec->{'time'} = TimePlanToPerl($time);
$rec->{'length'} = TimePlanToPerl($length);
$rec->{early} = TimePlanToPerl($early);
$rec->{late} = TimePlanToPerl($late);
$rec->{color} = $color;
$rec->{suspended} = substr($flags,0,1) ne "-";
$rec->{private} = substr($flags,1,1) ne "-";
$rec->{noalarm} = substr($flags,2,1) ne "-";
$rec->{hide_month} = substr($flags,3,1) ne "-";
$rec->{hide_year} = substr($flags,4,1) ne "-";
$rec->{hide_week} = substr($flags,5,1) ne "-";
$rec->{hide_yearover} = substr($flags,6,1) ne "-";
$rec->{d_flag} = substr($flags,7,1) ne "-";
$rec->{locked} = 1;
$rec->{id} = $i;
$rec->{exceptions} = [map(DatePlanToPerl($_), @E)] if @E;
$planRecord{$i} = $rec;
#print "Read plan record:\n";
#print Dumper($rec);
}
sub HashPilotRecord {
my($record) = @_;
my($hash) = new Digest::MD5;
$hash->add($record->{raw});
$hash->hexdigest;
}
sub doafterplan {
my($db,$socket,$control) = @_;
PilotMgr::msg( "After stuff:\n" ) if ($PREFS->{'Debug'} > 2);
# This batch of code scans for Plan records with identical PilotIDs,
# presumambly caused by duplicating a plan record. We remove the ids
# from the duplicates. The weird sort is magic to prefer keeping the id
# (and thus leaving unmodified) of an otherwise unmodified record.
my (@uniq) = sort {$a->{pilotid} <=> $b->{pilotid} or $a->{modified} <=> $b->{modified}} grep {exists $_->{pilotid}} values %planRecord;
my($i)=0;
for($i=@uniq-1;$i>=1;$i--) {
#print "Checking plan record: ", Dumper($uniq[$i]),"\n";
if ($uniq[$i]->{pilotid} == $uniq[$i-1]->{pilotid}) {
delete $uniq[$i]->{pilotid};
$planID{$uniq[$i-1]->{pilotid}} = $uniq[$i-1];
#print "... A dup, blessed be ye without id, and be ye modified.\n";
$uniq[$i]->{modified} = 1;
}
}
# Use our saved Pilot ID cache to detect deleted Plan records.
# This will not catch deleted Plan records that were never assigned
# a Pilot ID, but that is OK because such records do not have to be
# removed from the Pilot.
my ($loop_count) = (0);
my($del)=-1;
foreach (keys %pilothash) {
# Pilot records originally downloaded from a different Plan database
# are off-limits during this pass.
next if $dbname{$_} ne $control->{name};
# print "Pilot cached ID: $_\n";
if (not defined $planID{$_} and not $exceptID{$_}) {
#print "Deleted plan record, with Pilot id $_\n";
$planID{$_}->{deleted} = 1;
$planID{$_}->{pilotid} = $_;
$planID{$_}->{id} = $del;
$planRecord{$del} = $planID{$_};
$del--;
}
}
PilotMgr::msg( "Pilot loop\n" ) if ($PREFS->{'Debug'} > 2);
foreach (keys %pilotID) {
$dlp->tickle unless (++$loop_count % 50);
# Pilot records originally downloaded from a different Plan database
# are off-limits during this pass.
next if $dbname{$_} ne $control->{name};
PilotMgr::msg( "Pilot record: " . PrintPilotRecord($pilotID{$_}) . "\n" ) if ($PREFS->{'Debug'} > 1);
#print "Pilot record: ",Dumper($pilotID{$_}),"\n";
if ($pilotID{$_}->{deleted} || $pilotID{$_}->{archived}) {
#
# # At this point are seeing Pilot records marked as deleted or
# # archived. In the case of a slow sync, deleted records may not
# # be seen until a later pass.
#
# # Action: If there is an associated Plan record that has not
# # already been deleted, delete it.
#
# if (defined $planID{$_} and not $planID{$_}->{deleted}) {
# DeletePlanRecord($planID{$_});
# delete $planRecord{$planID{$_}->{id}};
# delete $planID{$_};
# }
#
# # Remove the Pilot ID from the exception cache, if present
# delete $exceptID{$_};
#
# delete $lastID{$_};
#
# delete $pilothash{$_};
} else {
my($hash) = HashPilotRecord($pilotID{$_});
# If the pilot record ID is not cached, then it is definitely
# new. If the MD5 hash of the record is different from the
# cached hash, then it is definitely different. These checks are
# only needed during a slow sync (which will have inaccurate
# flags), but are harmless during a fast sync.
#print "Old hash: $pilothash{$_}, new hash: $hash\n";
if ((not exists $pilothash{$_}) or ($hash ne $pilothash{$_})) {
$pilotID{$_}->{modified} = 1;
#print "Note: cache indicates record is changed\n";
}
$pilothash{$_} = $hash; # Record the hash and ID for the next sync
# Remove the record from the exception cache if it has been
# modified: perhaps it is not exceptional any more
delete $exceptID{$_} if $pilotID{$_}->{modified};
#print "Matching plan record: ", Dumper($planID{$_}),"\n";
if (not defined $planID{$_}) {
if (!$exceptID{$_}) {
# The Pilot record has no matching Plan record
# Action: Install the Pilot record in Plan, regardless of
# changed status
PilotMgr::msg( "Installing Pilot record in Plan: ".
PrintPilotRecord($pilotID{$_}). "\n" ) if ($PREFS->{'Debug'});
#print "Installing pilot record in plan: ",Dumper($pilotID{$_});
my($record) = RecordPilotToPlan($pilotID{$_});
if (not defined $record) {
# The record is not translatable to a Plan record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
# Code above will remove the exception flag when the
# record is changed.
$exceptID{$_} = 1;
PilotMgr::msg( "Pilot record unsyncable\n" );
} else {
WritePlanRecord($socket, $record);
}
}
} elsif ($pilotID{$_}->{modified} and $planID{$_}->{deleted}) {
# The Pilot record has a matching _deleted_ Plan record.
# This is collision, with a relatively simple solution.
# replace the Plan record with the Pilot record. As the Plan
# record has already been permanently deleted, we need only copy
# the Pilot record over.
# Action: Install the Pilot record in Plan
my($record) = RecordPilotToPlan($pilotID{$_}, $planID{$_});
if (not defined $record) {
# The record is not translatable to a Plan record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
$exceptID{$_} = 1;
PilotMgr::msg( "Pilot record modified while Plan record deleted, but new Pilot record unsyncable\n" );
} else {
WritePlanRecord($socket, $record);
PilotMgr::msg( "Pilot record modified while Plan record deleted\n" ) if ($PREFS->{'Debug'} > 1);
}
} elsif ($pilotID{$_}->{modified} and $planID{$_}->{modified}) {
# The Pilot record has a matching _modified_ Plan record.
# TODO: Use a comparator function to verify that the records
# are actually substantially different. If not, simply skip
# any action.
# This is collision with an ugly, but lossless, solution.
# Neither the Pilot or Plan record is inherantly preferable,
# so we duplicate each record on the other side, severing
# the link between the original new records, forging two new
# links and two new records, one on each side.
# Action: Install the Pilot record in Plan as a new,
# distinct, record, and install the Plan record on the Pilot
# as a new, distinct, record.
PilotMgr::msg( "Conflicting modified Plan and Pilot records\n" );
{
my($record) = RecordPlanToPilot($planID{$_});
if (not defined $record) {
# The Plan record is not translatable to a Pilot record.
# Action: Abort the install.
PilotMgr::msg( "Conflicting Plan record unsyncable.\n" );
} else {
$record->{id} = 0;
my($id) = WritePilotRecord($db, $control, $record);
#$db->setRecord($record);
#
#my ($hash) = HashPilotRecord($record);
#$pilothash{$id} = $hash;
#
#$record->{id} = $id;
#$pilotID{$id} = $record;
#$dbname{$id} = $dbname;
$planID{$_}->{pilotid} = $id;
$planID{$_}->{modified} = 0;
WritePlanRecord($socket, $planID{$_});
PilotMgr::msg( "ID of new Pilot record is $id\n" ) if ($PREFS->{'Debug'} > 2);
}
}
{
my($record) = RecordPilotToPlan($pilotID{$_});
if (not defined $record) {
# The Pilot record is not translatable to a Plan record.
# Action: Abort the install.
$exceptID{$_} = 1;
PilotMgr::msg( "Conflicting Pilot record unsyncable.\n" );
} else {
$record->{modified} = 0;
my($id) = WritePlanRecord($socket, $record);
PilotMgr::msg( "ID of new Plan record is $id\n" ) if ($PREFS->{'Debug'} > 2);
}
}
} elsif($pilotID{$_}->{modified}) {
# At this point, we have a changed Pilot record with an
# existing unmodified Plan record.
# Action: Install the Pilot record in Plan, overwriting the
# Plan record.
my($record) = RecordPilotToPlan($pilotID{$_}, $planID{$_});
if (not defined $record) {
# The record is not translatable to a Plan record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
# Code above will remove the exception flag when the
# record is changed.
$exceptID{$_} = 1;
DeletePlanRecord($socket, $planID{$_});
PilotMgr::msg( "Pilot record modified while Plan record unchanged, but new Pilot record unsyncable. Plan record has been deleted.\n" );
} else {
#print "Overwriting plan record: ",Dumper($planID{$_});
#print "With pilot record: ",Dumper($pilotID{$_});
#print "As plan record: ",Dumper($record);
WritePlanRecord($socket, $record);
PilotMgr::msg( "Updating Plan record with modified Pilot record: ".PrintPilotRecord($pilotID{$_})."\n" ) if ($PREFS->{'Debug'});
#print "New plan record state: ",Dumper($planID{$_}),"\n";
}
}
}
}
$dlp->tickle;
PilotMgr::msg( "Plan loop\n" ) if ($PREFS->{'Debug'} > 2);
foreach (keys %planRecord) {
$dlp->tickle unless (++$loop_count % 100);
PilotMgr::msg( "Plan record: " . PrintPlanRecord($planRecord{$_}),"\n" ) if ($PREFS->{'Debug'} > 1);
my($record) = $planRecord{$_};
my($pid) = $planRecord{$_}->{pilotid};
#print "Plan record: ",Dumper($record),"\n";
if ($record->{deleted}) {
#
# # At this point are seeing Pilot records marked as deleted or
# # archived. In the case of a slow sync, deleted records may not
# # be seen until a later pass.
#
# # Action: If there is an associated Plan record that has not
# # already been deleted, delete it.
#
# if (defined $planID{$_} and not $planID{$_}->{deleted}) {
# DeletePlanRecord($planID{$_});
# delete $planRecord{$planID{$_}->{id}};
# delete $planID{$_};
# }
#
# # Remove the Pilot ID from the exception cache, if present
# delete $exceptID{$_};
#
# delete $lastID{$_};
#
# delete $pilothash{$_};
} else {
# Remove the record from the exception cache if it has been
# modified: perhaps it is not exceptional any more
delete $record->{pilotexcept} if $record->{modified};
# If this is a fast sync, it's possible the record hasn't been
# fetched yet.
if (!$slowsync and defined $pid and not exists $pilotID{$pid}) {
my($precord) = LoadPilotRecord($db, $pid);
#$db->getRecord($pid);
if (defined $precord) {
if (not defined $dbname{$pid}) {
$dbname{$pid} = $control->{defaultname};
}
$pilotID{$pid} = $precord;
}
}
if (defined $pid and defined $pilotID{$pid} and ($dbname{$pid} ne $control->{name})) {
PilotMgr::msg( "Weird: Plan database $control->{name} claims to own Pilot record $pid,\n" );
PilotMgr::msg( "but my ID database says it is owned by $dbname{$pid}. I'll skip it.\n" );
next;
}
#print "Matching pilot record: ", Dumper($pilotID{$pid}),"\n";
if (not defined $pid or not defined $pilotID{$pid}) {
if (!$record->{pilotexcept}) {
# The Plan record has no matching Pilot record
# Action: Install the Plan record in Pilot, regardless of
# changed status
PilotMgr::msg( "Installing Plan record in Pilot: ".
PrintPlanRecord($record). "\n" ) if ($PREFS->{'Debug'});
#print "Installing plan record in pilot: ",Dumper($record);
#print "Trying to install Plan record: ",Dumper($record),"\n";
my($newrecord) = RecordPlanToPilot($record);
if (not defined $newrecord) {
# The record is not translatable to a Pilot record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
# Code above will remove the exception flag when the
# record is changed.
$record->{pilotexcept} = 1;
$record->{modified} = 1;
PilotMgr::msg( "Plan record unsyncable\n" );
} else {
#print "Installing Pilot record: ", Dumper($newrecord),"\n";
$newrecord->{id} = 0;
$newrecord->{secret} = 0;
my($id) = WritePilotRecord($db,$control,$newrecord);
#$db->setRecord($newrecord);
PilotMgr::msg( "ID of new Pilot record is $id\n" ) if ($PREFS->{'Debug'} > 2);
#my ($hash) = HashPilotRecord($newrecord);
#$pilothash{$id} = $hash;
#
#$newrecord->{id} = $id;
#$pilotID{$id} = $newrecord;
#$dbname{$id} = $dbname;
$record->{pilotid} = $id; # Match the Pilot record to the Plan record
$record->{modified} = 1; # and make sure it is written back out
}
}
} elsif ($record->{modified} and $pilotID{$pid}->{deleted}) {
# The Plan record has a matching _deleted_ Pilot record.
# This is collision, with a relatively simple solution.
# replace the Pilot record with the Plan record.
# Action: Install the Plan record in Pilot
my($newrecord) = RecordPlanToPilot($record, $pilotID{$pid});
if (not defined $newrecord) {
# The record is not translatable to a Pilot record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
$record->{pilotexcept} = 1;
PilotMgr::msg( "Plan record modified while Pilot record deleted, but new Plan record unsyncable\n" );
} else {
#print "Installing Pilot record: ", Dumper($newrecord),"\n";
WritePilotRecord($db,$control,$newrecord);
#$db->setRecord($newrecord);
#my ($hash) = HashPilotRecord($newrecord);
#$pilothash{$pid} = $hash;
PilotMgr::msg( "Plan record modified while Pilot record deleted\n" ) if ($PREFS->{'Debug'} > 1);
}
} elsif ($record->{modified} and $pilotID{$pid}->{modified}) {
croak("This shouldn't happen...");
} elsif ($record->{modified}) {
# At this point, we have a changed Plan record with an
# existing unmodified Pilot record.
# Action: Install the Plan record in the Pilot, overwriting the
# Pilot record.
#print "Trying to install Plan record: ",Dumper($record),"\n";
my($newrecord) = RecordPlanToPilot($record, $pilotID{$pid});
if (not defined $newrecord) {
# The record is not translatable to a Plan record.
# Action: Abort the install, and mark the record as
# uninstallable so that it will not be tried each sync.
# Code above will remove the exception flag when the
# record is changed.
$record->{pilotexcept} = 1;
DeletePilotRecord($db,$pid);
#$db->deleteRecord($record->{pilotid});
#delete $pilothash{$record->{pilotid}};
#delete $exceptID{$record->{pilotid}};
PilotMgr::msg( "Plan record modified while Pilot record unchanged, but new Plan record unsyncable. Pilot record has been deleted.\n" );
} else {
#print "Overwriting pilot record: ",Dumper($pilotID{$_});
#print "With plan record: ",Dumper($record);
#print "As pilot record: ",Dumper($newrecord);
#print "Installing Pilot record: ", Dumper($newrecord),"\n";
WritePilotRecord($db,$control,$newrecord);
#$db->setRecord($newrecord);
#my ($hash) = HashPilotRecord($newrecord);
#$pilothash{$pid} = $hash;
PilotMgr::msg( "Updating Pilot record with modified Plan record: ".PrintPlanRecord($record)."\n" ) if ($PREFS->{'Debug'});
}
}
}
if ($record->{modified}) {
WritePlanRecord($socket, $record);
}
}
PilotMgr::msg( "Pilot delete loop\n" ) if ($PREFS->{'Debug'} > 2);
foreach (keys %pilotID) {
$dlp->tickle unless (++$loop_count % 100);
# Pilot records originally downloaded from a different Plan database
# are off-limits during this pass.
next if $dbname{$_} ne $control->{name};
#print "Pilot record: ",Dumper($pilotID{$_}),"\n";
PilotMgr::msg( "Pilot record: " . PrintPilotRecord($pilotID{$_}) . "\n" ) if ($PREFS->{'Debug'} > 1);
if ($pilotID{$_}->{deleted} || $pilotID{$_}->{archived}) {
# At this point are seeing Pilot records marked as deleted or
# archived. In the case of a slow sync, deleted records may not
# be seen until a later pass.
# Action: If there is an associated Plan record that has not
# already been deleted, delete it.
PilotMgr::msg( "Deleting Pilot record.\n" ) if ($PREFS->{'Debug'} > 1);
if (defined $planID{$_} and not $planID{$_}->{deleted}) {
PilotMgr::msg( "... and associated Plan record.\n" ) if ($PREFS->{'Debug'} > 1);
PilotMgr::msg( "Deleting from Plan: ". PrintPlanRecord($planRecord{$planID{$_}->{id}}) ."\n") if ($PREFS->{'Debug'});
DeletePlanRecord($socket, $planID{$_});
delete $planRecord{$planID{$_}->{id}};
delete $planID{$_};
}
# Remove the Pilot ID from the exception cache, if present
delete $exceptID{$_};
delete $pilotID{$_};
delete $dbname{$_};
delete $pilothash{$_};
}
}
PilotMgr::msg( "Plan delete loop\n" ) if ($PREFS->{'Debug'} > 2);
foreach (keys %planRecord) {
$dlp->tickle unless (++$loop_count % 100);
my($record) = $planRecord{$_};
my($pid) = $planRecord{$_}->{pilotid};
#print "Plan record: ",Dumper($record),"\n";
PilotMgr::msg( "Plan record: " . PrintPlanRecord($planRecord{$_}) . "\n" ) if ($PREFS->{'Debug'} > 1);
# In a fast sync, we might not have loaded the record yet.
if (!$slowsync and defined $pid and not exists $pilotID{$pid}) {
my($precord) = LoadPilotRecord($db, $pid);
#$db->getRecord($pid);
if (defined $precord) {
if (not defined $dbname{$pid}) {
$dbname{$pid} = $control->{defaultname};
}
$pilotID{$pid} = $precord;
}
}
if (defined $pid and defined $pilotID{$pid} and ($dbname{$pid} ne $control->{name})) {
PilotMgr::msg( "Weird: Plan database $control->{name} claims to own Pilot record $pid,\n" );
PilotMgr::msg( "but my ID database says it is owned by $dbname{$pid}. I'll skip it.\n" );
next;
}
if ($record->{deleted}) {
# At this point are seeing Pilot records marked as deleted or
# archived. In the case of a slow sync, deleted records may not
# be seen until a later pass.
# Action: If there is an associated Plan record that has not
# already been deleted, delete it.
PilotMgr::msg( "Deleting Plan record.\n" ) if ($PREFS->{'Debug'} > 1);
if (defined $pid and defined $pilotID{$pid} and not $pilotID{$_}->{deleted}) {
PilotMgr::msg( "... and associated Pilot record.\n" ) if ($PREFS->{'Debug'} > 1);
PilotMgr::msg( "Deleting from Pilot: " . PrintPilotRecord($pilotID{$pid}) ."\n" ) if ($PREFS->{'Debug'});
DeletePilotRecord($db, $pid);
#$db->deleteRecord($pid);
#delete $pilotID{$pid};
#delete $pilothash{$pid};
#delete $exceptID{$pid};
}
# Remove the Pilot ID from the exception cache, if present
delete $planRecord{$_};
}
}
}
sub loadpilotrecords {
PilotMgr::msg( "Loading pilot records:\n" );
if ($dlp->getStatus<0) {
croak "Cancelled.\n";
}
$pilotname = $info->{name} . "_ " . $info->{userID};
$pilotname =~ s/[^A-Za-z0-9]+/_/g;
PilotMgr::msg( "Synchronizing pilot called '$pilotname'\n" ) if ($PREFS->{'Debug'} > 1);
if (not defined $control{$pilotname}) {
PilotMgr::msg( "Database access list for Pilot has not been defined!\n\n" );
PilotMgr::msg( "Pilot '$pilotname' has been added to $controldir/control.\n" );
PilotMgr::msg( "Please edit ~/.pilotmgr/$controldir/control and add the names of the Plan databases\n" );
PilotMgr::msg( "that this Pilot should synchronize with.\n" );
open (C, ">>$controldir/control");
print C "$pilotname\n";
close (C);
return 0;
}
$db = $dlp->open("DatebookDB");
my ($r, $i);
$i=0;
while(defined($r = LoadPilotRecord($db,$i++))) {
# push @pilotRecord, $r;
#print "Pilot Record: ",Dumper($r),"\n";
# $pilotID{$r->{id}} = $r;
}
PilotMgr::msg( "Done reading records\n" ) if ($PREFS->{'Debug'} > 1);
$slowsync = 1;
if ($slowsync) {
foreach (keys %pilothash) {
if (not exists $pilotID{$_}) {
$pilotID{$_}->{deleted} = 1;
}
}
}
return 1;
}
sub SendPlanCommand {
my($socket,$text) = @_;
my($len);
#print "Sending |$text|\n";
while (length($text)) {
$len = syswrite $socket, $text, length($text);
$text = substr($text,$len);
}
}
my($partialReply) = "";
sub ReadPlanReply {
my($socket) = @_;
my($reply) = "";
my($buf);
while (1) {
while ($partialReply =~ /\A(.*?)(\\)?\n/m) {
$reply .= $1."\n";
$partialReply = $';
if (not defined($2)) {
$reply =~ s/\\\n/\n/sg;
$reply =~ s/\n$//sg;
if ($reply =~ /\AR/) { # Discard
next;
} elsif ($reply =~ /\A\?/) { # Discard
PilotMgr::msg( "Plan message: $'" );
next;
} else {
#print "Reply: |$reply|\n";
return $reply;
}
$reply = "";
}
}
do {
sysread($socket,$buf,1024);
$partialReply .= $buf;
} while ($buf !~ /[^\\]\n|\A\n/);
# ^^ the regexp matches if $buf contains an unescaped
# newline, i.e. a newline that's either the first
# character, or preceded by a non-escape character.
}
}
sub SyncDB {
my($db, $control) = @_;
my $dbname = $control->{dbname};
#print "Opening database $control->{name}\@$control->{host}:$control->{port}.\n";
my $socket = IO::Socket::INET->new(PeerPort => $control->{port}, PeerAddr => $control->{host}, Proto => 'tcp');
if (not defined $socket) {
croak "Unable to open plan socket on $control->{host}:$control->{port}\n";
}
$socket->autoflush(1);
my $select = IO::Select->new();
$select->add($socket);
my $reply=ReadPlanReply($socket);
if ($reply !~ /^!/) {
croak "Unknown response from netplan: $reply\n";
}
$netplanversion = $reply;
# Send "hello" type message, required when netplan runs with -a flag.
SendPlanCommand($socket, "=SyncPlan\n");
SendPlanCommand($socket, "o$dbname\n");
$reply = ReadPlanReply($socket);
if ($reply !~ /^otw(\d+)/) {
croak "Failed to open database $control->{name}\@$control->{host}:$control->{port}.\n";
}
$file = $1;
SendPlanCommand($socket, "n$file\n");
$reply = ReadPlanReply($socket);
if ($reply !~ /^n\d+\s+(\d+)/) {
croak "Failed to get record count.\n";
}
my $records = $1;
my @id= ();
SendPlanCommand($socket, "r$file 0\n");
while ($records) {
$reply = ReadPlanReply($socket);
if ($reply =~ /\Art\d+\s+(\d+)\s+/) {
push @id, $1;
#print "Got ID $1\n";
$records--;
}
}
my ($loop_count) = (0);
foreach (@id) {
$dlp->tickle unless (++$loop_count % 50);
SendPlanCommand($socket, "l$file $_\n");
$reply = ReadPlanReply($socket);
if ($reply !~ /^lt/) {
croak "Failed to lock record $_.\n";
}
SendPlanCommand($socket, "r$file $_\n");
$reply = ReadPlanReply($socket);
if ($reply !~ /\Art\d+\s+(\d+)\s+/s) {
croak "Didn't get record I was looking for.\n";
}
dorecord($db, $socket, $control, $_, $');
}
doafterplan($db, $socket, $control);
%planRecord = (); # Flush plan records
SendPlanCommand($socket, "c$file\n");
$socket->close;
}
sub conduitInit
{
$controldir = "SyncPlan";
$RCFILE = "SyncPlan/SyncPlan.prefs";
&loadPrefs;
$PREFS->{'NetplanPort'} = '5444'
unless (defined($PREFS->{'NetplanPort'}));
$PREFS->{'Debug'} = '1'
unless (defined($PREFS->{'Debug'}));
}
sub conduitQuit
{
}
sub conduitInfo
{
return
{
'version' => $VERSION,
'author' => 'Kenneth Albanowski',
'email' => 'kjahds@kjahds.com, alan.harder@sun.com'
};
}
sub conduitConfigure
{
my ($this, $wm) = @_;
unless (defined $gConfigDlg)
{
my ($frame, $subfr, $obj);
$gConfigDlg = $wm->Toplevel(-title => 'SyncPlan');
$gConfigDlg->transient($wm);
$gConfigDlg->Label(-text => "SyncPlan v$VERSION\nKenneth Albanowski kjahds\@kjahds.com\nConverted to PilotManager conduit by Alan Harder.\nPlan software by Thomas Driemeyer (thomas\@bitrot.in-berlin.de).\nhttp://www.in-berlin.de/User/bitrot/plan.html")->pack(-side => 'top');
$frame = $gConfigDlg->Frame(-relief => "ridge", -bd => 4);
$obj = TkUtils::Label($frame, "Settings");
$obj->pack(-anchor => "c");
$subfr = $frame->Frame;
$obj = $subfr->Label(-text => "Netplan Port: ");
$obj->pack(-side => "left", -anchor => "nw", -fill => "x");
$obj = $subfr->Entry(-textvariable => \$PREFS->{'NetplanPort'},
-relief => "sunken", -width => 6);
$obj->pack(-side => "left", -anchor => "w", -fill => "x");
TkUtils::LabelRadiobuttons($subfr, "Verbose Level:",
\$PREFS->{'Debug'},
"0", "1", "2", "3");
$subfr->pack(-anchor => "c");
$frame->pack(-side => 'top', -fill => "x");
$gConfigDlg->Button(-text => 'Setup Tips', -command => sub{ PilotMgr::showDoc('docs/SyncPlan.help', 'SyncPlan Tips'); })->pack(-side => 'left', -expand => 1, -anchor => 'c');
$gConfigDlg->Button(-text => 'Dismiss', -command => sub { &savePrefs; $gConfigDlg->withdraw })->pack(-side => 'left', -expand => 1, -anchor => 'c');
PilotMgr::setColors($gConfigDlg);
}
$gConfigDlg->Popup(-popanchor => 'c', -overanchor => 'c',
-popover => $wm);
}
sub conduitSync
{
$dlp = $_[1];
$info = $_[2];
# initialize variables that may still be set from last sync
#
%control = ();
%pilothash = ();
%pilotID = ();
%planID = ();
%exceptID = ();
%planRecord = ();
%dbname = ();
%sawName = ();
$pilotname = $db = $slowsync = $file = $maxseed = $netplanversion = undef;
if (! -d $controldir) {
croak "Directory $controldir does not exist. It must be created before $0 is run.\n\n";
}
if (! -f "$controldir/control") {
open(C, ">$controldir/control") || croak "Unable to write to $controldir/control";
print C "# this file is used to control which Pilots are allowed to sync, and what databases\n";
print C "# each Pilot will sync with. Each line consists of whitespace-separated fields, the\n";
print C "# first one being the name (and ID) of the Pilot, and subsequent fields listing\n";
print C "# all plan databases that Pilot will synchronize with.\n";
print C "#\n";
print C "# For example: Foo_s_Pilot_1234 myname\@localhost group\@host.io ro:all\@localhostn";
print C "#\n";
print C "# New entries on the Pilot are installed in the first database listed.\n";
print C "# Records will not exchanged between separate plan datatabses.\n";
print C "# A database may be prefixed with 'rw:' or 'ro:' to indicate read/write (the\n";
print C "# default) or read only access. If a database is read-only, any record changes\n";
print C "# on the Pilot will be discarded. However, for technical reasons, you must have\n";
print C "# read/write access to the plan database itself.\n";
close(C);
}
open(C,"<$controldir/control");
while(<C>) {
chomp;
next if /^#/;
my($i,@i) = split(/\s+/, $_);
my(@I);
my($first)=1;
my($defaultname);
foreach (@i) {
my($mode, $name, $host) = m/^(?:(wr|ro|rw):)?([^\@]+)(?:\@(.+))?$/;
if (not defined $mode) {
$mode = "rw";
}
if (not defined $host) {
$host = "localhost";
}
if ($mode !~ /^rw$/) {
croak "Access mode $mode (for Pilot '$i') at line $. of $controldir/control unknown or unsupported.\n";
}
if ($first) {
$defaultname = $name.'@'.$host;
}
push @I, {mode => $mode, name => $name.'@'.$host, dbname => $name, host => $host, port => $PREFS->{'NetplanPort'}, 'read' => ($mode =~ /r/), 'write' => ($mode =~ /w/), default => $first, defaultname => $defaultname};
$first = 0;
}
$control{$i} = [@I];
}
close(C);
if (loadpilotrecords) {
foreach (@{$control{$pilotname}}) {
$sawName{$_->{name}} = 1;
}
if (!@{$control{$pilotname}}) {
PilotMgr::msg( "No plan databases are registered for the '$pilotname' Pilot. Please\n" );
PilotMgr::msg( "edit $controldir/control and add one or more databases.\n" );
}
if (open (I, "<$controldir/ids.$pilotname"))
{
foreach (<I>) {
chop;
my($id, $hash, $except, $dbname) = split(/\s+/, $_);
$pilothash{$id} = $hash;
$exceptID{$id} = $except;
if (not defined $dbname or not length $dbname) {
$dbname = $control{$pilotname}->[0]->{name};
}
$dbname{$id} = $dbname if defined $dbname and length $dbname;
#print Dumper({dbname=>$dbname{$id}});
if (not defined $sawName{$dbname}) {
PilotMgr::msg( "Warning! The ID file, $controldir/ids.$pilotname, lists a record as belonging\n" );
PilotMgr::msg( "to database $dbname, but the control file $controldir/control does not list this\n" );
PilotMgr::msg( "this database. If you have renamed a database, please edit $controldir/ids.$pilotname\n" );
PilotMgr::msg( "so all references to this database match the new name.\n" );
PilotMgr::msg( "\nIf you wish to delete all on the Pilot that were originally from $dbname, then\n" );
PilotMgr::msg( "delete the database name from the end of each record's line.\n" );
PilotMgr::msg( "To merge the records into the default database, delete each affected line entirely.\n" );
$sawName{$dbname} = 1;
}
}
close (I);
}
foreach (keys %pilotID) {
if (not defined $dbname{$_}) {
$dbname{$_} = $control{$pilotname}->[0]->{name};
}
}
foreach (@{$control{$pilotname}}) {
next if not defined $_->{host}; # Sigh. Autoviv problem.
SyncDB($db, $_);
}
# Delete deleted & archived records
$db->purge;
# Clear modified flags, and set last sync time to now
$db->resetFlags;
$db->close;
open (I, ">$controldir/ids.$pilotname");
foreach (keys %pilothash) {
if ($dbname{$_} eq $control{$pilotname}->[0]{name}) {
$dbname{$_}="";
}
$exceptID{$_} = 0 unless (defined $exceptID{$_});
print I "$_ $pilothash{$_} $exceptID{$_} $dbname{$_}\n";
}
close(I);
}
}
sub loadPrefs
{
my ($lines);
open(FD, "<$RCFILE") || return;
$lines = join('', <FD>);
close(FD);
eval $lines;
# For some reason, we need to reference $PREFS here
# or the preferences won't get loaded properly.
#
$PREFS;
}
sub savePrefs
{
my ($var);
$Data::Dumper::Purity = 1;
$Data::Dumper::Deepcopy = 1;
if (open(FD, ">$RCFILE"))
{
if (defined &Data::Dumper::Dumpxs)
{
print FD Data::Dumper->Dumpxs([$PREFS], ['PREFS']);
}
else
{
print FD Data::Dumper->Dump([$PREFS], ['PREFS']);
}
print FD "1;\n";
close(FD);
}
else
{
PilotMgr::msg("Unable to save preferences to $RCFILE!");
}
}
1;
|