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
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <array>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <sys/stat.h>
#include <ctime>
#include <unistd.h>
//set to 1 to enable some debug std::cout statements
#define DEBUG 0
#include "pcoin_defs.cpp"
/* OLD VALUES */
/*
#define TCOIN_PATH "/home/login/tcoin"
#define TCOIN_MSG_PATH "/home/login/tcoin/messages/"
#define TCOIN_SALT_PATH "/home/login/tcoin/salts/"
#define TCOIN_PASS_PATH "/home/login/tcoin/passwords/"
#define TCOIN_PROG_ACT_PATH "/home/login/tcoin/program_accounting/"
#define PROG_ACT_W_SLASH "program_accounting/"
#define PCOIN_KEY_PATH_W_SLASH "/home/login/bin/pcoin_keys/"
#define LS_PCOIN_KEY_CMD "/bin/ls /home/login/bin/pcoin_keys"
#define TCOIN_CODEZ_PATH "/home/login/bin/tcoin_codez"
#define TCOIN_BIN_PATH_W_SPACE "/home/login/bin/tcoin "
#define PCOIN_BIN_PATH "/home/login/bin/pcoin"
#define PCOIN_BIN_PATH_W_SPACE "/home/login/bin/pcoin "
#define TCOIN_PATH_W_SLASH "/home/login/tcoin/"
#define TCOIN_SCRYPT_PATH "/home/login/bin/scrypt"
*/
#define LS_HOME_CMD "/bin/ls /home"
#define BIN_ECHO_CMD "/bin/echo $$"
#ifndef KROWBAR_OFF
#define KROWBAR_SCORE_PATH "/home/krowbar/Code/irc/data/tildescores.txt"
#endif
#ifndef DA_OFF
#define TROIDO_DACOINS_CMD "cd /home/troido/daily_adventure/client/ && /home/troido/daily_adventure/client/daclient printinfo 2>&1 | /bin/grep -oP '(?<=\"Coins\", )\[[:digit:]]+'"
#endif
#ifndef MINERCOIN_OFF
#define MINERCOIN_CMD_PRE_USERNAME "/bin/grep -oP '(?<=\"~"
#define MINERCOIN_CMD_POST_USERNAME "\": )[[:digit:]]+' /home/minerobber/Code/minerbot/minercoin.json"
#endif
#define USERNAME_LENGTH_LIMIT 25
#define TCOIN_MSG_LENGTH_LIMIT 280
#define ERR_MAIN_SEND_TOO_FEW_ARGS 6
#define ERR_MAIN_SEND_TOO_MANY_ARGS 7
#define ERR_MAIN_SEND_TOO_MANY_ARGS_MSG 104
#define ERR_SILENTSEND 2
#define ERR_MAIN_MSG_TOO_LONG 19
#define ERR_UNKNOWN_ARG 3
#define ERR_TCOIN_TO_SELF 5
#define ERR_RECEIVER_BLOCKED 4
#define ERR_NEGATIVE_SEND_AMOUNT 2
#define ERR_INSUFFICIENT_FUNDS 3
#define ERR_RECEIVER_NOT_FOUND 1
#define ERR_IN_GET_INTERNAL_BALANCE -1
#define ERR_IN_ADD_INTERNAL_BALANCE_GET_INTERNAL_TOTAL_OWED_FAILED -3
#define ERR_ADD_INTERNAL_BALANCE_VALUE_TO_ADD_UNFULFILLABLE_USING_OWN_CURRENT_FUNDS -1
#define ERR_ADD_INTERNAL_BALANCE_USERNAME_DOESNT_EXIST -2
#define ERR_ADD_FILE_VALUE_INSUFFICIENT_FUNDS 1
#define ERR_ADD_FILE_VALUE_FATAL 999
#define ERR_IN_GET_INTERNAL_TOTAL_OWED_SELF_PROGRAM_DOESNT_EXIST -1
#define ERR_GET_FILE_VALUE_COULDNT_OPEN_BASEFILE 1
#define ERR_ADD_FILE_VALUE_COULDNT_OPEN_BASEFILE 1
#define ERR_SEND_MESSAGE_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL 101
#define ERR_SEND_MESSAGE_PROGRAM_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL 103
#define ERR_SEND_MESSAGE_SENDER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL 102
#define ERR_SEND_MESSAGE_SENDER_PROGRAM_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL 104
#define ERR_KEY_NOT_IN_USE 9
#define ERR_NO_ARGS 8
#define ERR_IN_MAIN_GET_INTERNAL_TOTAL_OWED_FAILED 18
#define ERR_INTERNAL_BALANCE_USERNAME_NOT_FOUND 17
#define ERR_INTERNAL_BALANCE_NO_USERNAME_SUPPLIED 10
#define ERR_INTERNAL_BALANCE_TOO_MANY_ARGS 11
#define ERR_ADD_INTERNAL_BALANCE_TOO_FEW_ARGS 12
#define ERR_ADD_INTERNAL_BALANCE_TOO_MANY_ARGS 13
#define ERR_IN_MAIN_ADD_INTERNAL_BALANCE_AMOUNT_LARGER_THAN_COVERABLE_BY_UNOWED_BALANCE 14
#define ERR_IN_MAIN_ADD_INTERNAL_BALANCE_AMOUNT_MAKING_USER_INTERNAL_BALANCE_NEGATIVE 16
#define ERR_IN_MAIN_ADD_INTERNAL_BALANCE_NO_SUCH_USERNAME_FOUND 15
void exit_program(const int error_number)
{
// Cleanup to do before exiting the program
// Finally, we can exit
std::exit(error_number);
}
//custom function to convert ("abcd.de") to ("abcde")
long long int strtol100(const char* amount_str)
{
long long int result = 0;
int multiplier = 1;
int i=0;
if(amount_str[i]=='-')
{
multiplier = -1;
++i;
}
else if(amount_str[i]=='+')
{
++i;
}
else if(amount_str[i]=='\0') //empty string
{
return (long long int)(0);
}
//before the decimal point
while(amount_str[i]!='.')
{
if(amount_str[i]>='0' && amount_str[i]<='9')
result = result*10 + ((long long int)(amount_str[i]) - (long long int)('0'));
else if(amount_str[i]=='\0') //e.g. "500"
{
result *= (multiplier*100); //multiplied by 100 to get centitildecoins
return result;
}
else //error
{
return (long long int)(0);
}
++i;
}
//at decimal point
++i;
//after decimal point (i.e., tenth's place)
if(amount_str[i]>='0' && amount_str[i]<='9')
result = result*100 + ((long long int)(amount_str[i]) - (long long int)('0'))*10;
else if(amount_str[i]=='\0') //e.g. "500."
{
result *= (multiplier*100); //multiplied by 100 to get centitildecoins
return result;
}
else //error
{
return (long long int)(0);
}
//before hundredth's place
++i;
//at hundredth's place
if(amount_str[i]>='0' && amount_str[i]<='9')
{
result += ((long long int)(amount_str[i]) - (long long int)('0'));
}
else if(amount_str[i]=='\0') //e.g. "500.3"
{
result *= multiplier;
return result;
}
else //error
{
return (long long int)(0);
}
result *= multiplier;
return result;
}
//custom function to convert integer string to long long int fast
long long int strtol_fast(const char* amount_str)
{
long long int result = 0;
int multiplier = 1;
int i=0;
if(amount_str[i]=='-')
{
multiplier = -1;
++i;
}
else if(amount_str[i]=='+')
{
++i;
}
else if(amount_str[i]=='\0') //empty string
{
return (long long int)(0);
}
//before the end of the string
while(amount_str[i]>='0' && amount_str[i]<='9')
{
result = result*10 + ((long long int)(amount_str[i]) - (long long int)('0'));
++i;
}
result *= multiplier;
return result;
}
//string constant time compare (only checks for equality (return 0 if equal))
int strctcmp(const char*a, const char*b)
{
if(!(*a) || !(*b)) //a or b are empty (start with a NULL character)
return 1;
int r = 0;
for (; *a && *b; ++a, ++b)
{
r |= *a != *b;
}
return r;
}
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
long long int get_file_value(const char* file_name)
{
char* file_path = new char[strlen(file_name)+sizeof(TCOIN_PATH_W_SLASH)+4];
std::strcpy(file_path, TCOIN_PATH_W_SLASH);
std::strcat(file_path, file_name);
std::strcat(file_path, ".txt");
std::ifstream file(file_path);
if(!file)
{
if(!strcmp(file_name, "base/base"))
{
std::cerr << "\nError! Could not open file at " << file_path << "!\n\n";
exit_program(ERR_GET_FILE_VALUE_COULDNT_OPEN_BASEFILE);
}
else {
std::cerr << "\nError! Could not open file at " << file_path << "! Assuming its internal content is \"0\\n\".\n\n";
return (long long int)(0);
}
}
std::ostringstream ss;
ss << file.rdbuf();
delete[] file_path;
return strtol_fast(ss.str().c_str());
}
int add_file_value(const char* file_name, const long long int &value_to_add, const long long int &base_amount)
{
char* file_path = new char[strlen(file_name)+sizeof(TCOIN_PATH_W_SLASH)+4];
char* temp_file_path = new char[strlen(file_name)+sizeof(TCOIN_PATH_W_SLASH)+8];
std::strcpy(file_path, TCOIN_PATH_W_SLASH);
std::strcat(file_path, file_name);
std::strcpy(temp_file_path, file_path);
std::strcat(file_path, ".txt");
std::strcat(temp_file_path, "_tmp");
std::strcat(temp_file_path, ".txt");
std::ifstream file(file_path);
if(!file)
{
if(!strcmp(file_name, "base/base"))
{
std::cerr << "\nError! Could not open file at " << file_path << "!\n\n";
file.close();
delete[] file_path;
delete[] temp_file_path;
exit_program(ERR_ADD_FILE_VALUE_COULDNT_OPEN_BASEFILE);
}
}
std::ostringstream ss;
ss << file.rdbuf();
long long int old_value = strtol_fast(ss.str().c_str());
//sufficient funds check
if(value_to_add < 0 && (old_value + base_amount + value_to_add < 0))
{
file.close();
delete[] file_path;
delete[] temp_file_path;
return ERR_ADD_FILE_VALUE_INSUFFICIENT_FUNDS;
}
long long int new_value = old_value + value_to_add;
// Writing new value to file
file.close();
std::ofstream file2(temp_file_path);
file2 << new_value << "\n";
file2.close();
chmod(temp_file_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
if(!file2) //error
{
std::cerr << "Fatal error " << ERR_ADD_FILE_VALUE_FATAL << ": the file \"" << file_name << "\" was unable to be updated. Please contact login@tilde.town (town-only) or login@tilde.team (internet-wide to report this error (because it requires manual recovery).";
exit_program(ERR_ADD_FILE_VALUE_FATAL);
}
else
{
std::remove(file_path);
while(1)
{
if(!std::rename(temp_file_path, file_path))
{
chmod(file_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
break;
}
}
}
delete[] file_path;
delete[] temp_file_path;
return 0;
}
std::string global_username;
std::string get_username()
{
return global_username;
}
int set_username(std::string &username)
{
global_username.assign(username);
return 0;
}
std::string formatted_amount(long long int const& amount, char const* appended_chars_default = "", char const* appended_chars_singular = "")
{
std::ostringstream ss;
bool is_non_negative = amount >= 0 ? true : false;
if(!is_non_negative) //i.e., is negative
ss << "-";
if(((is_non_negative*2-1)*amount) % 100 == 0)
ss << (is_non_negative*2-1)*amount/100;
else if((is_non_negative*2-1)*amount % 100 < 10)
ss << (is_non_negative*2-1)*amount/100 << ".0" << (is_non_negative*2-1)*amount % 100;
else
ss << (is_non_negative*2-1)*amount/100 << "." << (is_non_negative*2-1)*amount % 100;
if(((is_non_negative*2-1)*amount == 100) && strcmp(appended_chars_singular, ""))
ss << appended_chars_singular;
else
ss << appended_chars_default;
std::string formatted_string(ss.str());
return formatted_string;
}
void cout_formatted_amount(long long int const& amount, char const* appended_chars_default = "", char const* appended_chars_singular = "", bool negative_with_parentheses = false)
{
bool amount_is_negative = (amount < 0);
if(negative_with_parentheses && amount_is_negative) std::cout << "(";
std::cout << formatted_amount(amount, appended_chars_default, appended_chars_singular);
if(negative_with_parentheses && amount_is_negative) std::cout << ")";
}
long long int base_amount;
long long int user_amount;
long long int krowbar_amount; //krowbar's tilde game amount
long long int minercoin_amount; //minerobber's !minercoin game amount
void show_breakdown(const long long int &amount0 = 0, char const* amount0_source = "", const long long int &amount1 = 0, char const* amount1_source = "", const long long int &amount2 = 0, char const* amount2_source = "", const long long int &amount3 = 0, char const* amount3_source = "", const long long int &amount4 = 0, char const* amount4_source = "")
{
bool a0 = (amount0 != 0 && strcmp(amount0_source, ""));
bool a1 = (amount1 != 0 && strcmp(amount1_source, ""));
bool a2 = (amount2 != 0 && strcmp(amount2_source, ""));
bool a3 = (amount3 != 0 && strcmp(amount3_source, ""));
bool a4 = (amount4 != 0 && strcmp(amount4_source, ""));
if(a0 || a1 || a2 || a3 || a4)
{
if(a0)
{
std::cout << amount0_source << ",";
cout_formatted_amount(amount0);
if(a1 || a2 || a3 || a4)
{
std::cout << ";";
}
}
if(a1)
{
std::cout << amount1_source << ",";
cout_formatted_amount(amount1);
if(a2 || a3 || a4)
{
std::cout << ";";
}
}
if(a2)
{
std::cout << amount2_source << ",";
cout_formatted_amount(amount2);
if(a3 || a4)
{
std::cout << ";";
}
}
if(a3)
{
std::cout << amount3_source << ",";
cout_formatted_amount(amount3);
if(a4)
{
std::cout << ";";
}
}
if(a4)
{
std::cout << amount4_source << ",";
cout_formatted_amount(amount4);
}
std::cout << "\n";
}
}
void show_messages(const char* username)
{
std::string messages_path = std::string(TCOIN_MSG_PATH) + std::string(username) + std::string("_messages.txt");
std::ifstream fin(messages_path.c_str());
char ch;
bool first_char_is_newline = false;
bool reached_eof = false;
for(int i=0; i < 2; ++i)
{
if(ch = fin.get())
{
if(ch == std::istream::traits_type::eof()) //https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work
{
if(first_char_is_newline && i==1)
{
reached_eof = true;
std::cout << "No messages found.\n";
}
break;
}
else
{
if(i==0 && ch=='\n')
{
first_char_is_newline = true;
}
std::cout << ch;
}
}
}
if(!reached_eof)
{
std::cout << fin.rdbuf();
}
//removing eofbit
fin.clear();
//moving back two places from the end to read the last two characters
fin.seekg(-2, std::ios::end);
char chs[2]; //chs = characters
chs[0] = fin.get();
chs[1] = fin.get();
fin.get(); //to set eofbit again because I like it to be just the way it was before removing the eofbit
if(chs[0]!='\n' && chs[1]=='\n') //if only one newline at the end of the file
std::cout << "\n"; //print another one
fin.close();
}
void show_tsv_messages(const char* username) //tab-separated-value messages (tsv)
{
std::string messages_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(username) + std::string("/_MESSAGES.txt");
std::ifstream fin(messages_path.c_str());
std::cout << fin.rdbuf();
std::cout << "\n";
fin.close();
}
void show_messages_tail(const char* username, int lineCount)
{
size_t const granularity = 100 * lineCount;
std::string messages_path = std::string(TCOIN_MSG_PATH) + std::string(username) + std::string("_messages.txt");
std::ifstream source(messages_path.c_str(), std::ios_base::binary);
source.seekg(0, std::ios_base::end);
size_t size = static_cast<size_t>(source.tellg());
std::vector<char> buffer;
int newlineCount = 0; //pseudo newline count
while(source && buffer.size() != size && newlineCount <= lineCount)
{
buffer.resize(std::min(buffer.size() + granularity, size));
source.seekg(-static_cast<std::streamoff>(buffer.size()), std::ios_base::end);
source.read(buffer.data(), buffer.size());
newlineCount = std::count(buffer.begin(), buffer.end(), '\n');
for(std::vector<char>::size_type i = 0; i < (buffer.size()-1); ++i)
if(buffer[i] == '\n' && (buffer[i+1] == '\n' || buffer[i+1] == ' '))
{
newlineCount--; // An entry as follows: "<stuff>\n \_message>\n\n" must be treated as a single newline-ended line (and thus count 1 newline (not 3))
++i; //three consecutive newlines should not be "two pairs" of newlines
}
}
std::vector<char>::iterator start = buffer.begin();
while(newlineCount > lineCount)
{
start = std::find(start, buffer.end(), '\n') + 1;
if(*start == ' ' || *start == '\n')
continue; //we're counting cutting off a '\n ' (and '\n\n') as zero (and one) newline cut off because "<stuff>\n \_message>\n\n" is one message
--newlineCount;
}
std::cout << "Last " << lineCount << " Messages:\n";
std::vector<char>::iterator end = remove(start, buffer.end(), '\r');
if((start == (end-1)) && (*(start) == '\n')) //there is only one character, and it is a newline (i.e.. no messages)
std::cout << "\nNo messages found.\n\n";
else
{
if(*(start) != '\n') //if it starts with a newline, don't put another one
std::cout << "\n";
std::cout << std::string(start, end);
if(*(end-2) != '\n' && *(end-1) == '\n') //if it ends with two newlines, don't put another one
std::cout << "\n";
}
}
void show_tsv_messages_tail(const char* username, int lineCount)
{
size_t const granularity = 100 * lineCount;
std::string messages_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(username) + std::string("/_MESSAGES.txt");
std::ifstream source(messages_path.c_str(), std::ios_base::binary);
source.seekg(0, std::ios_base::end);
size_t size = static_cast<size_t>(source.tellg());
std::vector<char> buffer;
int newlineCount = 0; //pseudo newline count
while(source && buffer.size() != size && newlineCount <= lineCount)
{
buffer.resize(std::min(buffer.size() + granularity, size));
source.seekg(-static_cast<std::streamoff>(buffer.size()), std::ios_base::end);
source.read(buffer.data(), buffer.size());
newlineCount = std::count(buffer.begin(), buffer.end(), '\n');
}
std::vector<char>::iterator start = buffer.begin();
while(newlineCount > lineCount)
{
start = std::find(start, buffer.end(), '\n') + 1;
--newlineCount;
}
std::vector<char>::iterator end = remove(start, buffer.end(), '\r');
std::cout << std::string(start, end);
}
bool program_exists(const char* username)
{
char *program_key_path = new char[strlen(username) + sizeof(PCOIN_KEY_PATH_W_SLASH) + 4]; //sizeof counts NULL char at the end too
std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH);
std::strcat(program_key_path, username);
std::strcat(program_key_path, ".txt");
std::ifstream fin(program_key_path);
bool return_value = false;
if(!fin) //file doesn't exist
{
fin.close();
delete[] program_key_path;
return_value = false; //program account not found
}
else
{
fin.close();
delete[] program_key_path;
return_value = true; //program account found
}
return return_value;
}
bool username_exists(const char* username)
{
const static std::string all_usernames = exec(LS_HOME_CMD);
std::istringstream iss(all_usernames);
static std::vector<std::string> usernames{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
if(std::find(usernames.begin(), usernames.end(), username) != usernames.end())
{
return true;
}
return program_exists(username);
}
bool file_is_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
bool files_are_same(const char* file_path1, const char* file_path2)
{
std::ifstream fin1(file_path1);
if(!fin1)
return false;
std::ifstream fin2(file_path2);
if(!fin2)
return false;
char c1;
char c2;
while(fin1.get(c1) && fin2.get(c2)) //we need to go inside the loop body even if one of the reads fails, so we use || instead of &&
{
if(c2 != c1)
return false;
}
fin2.get(c2); //when fin1.get(c1) fails, fin2.get(c2) is not executed because of short-circuited boolean operators. This line compensates for that.
if(fin1 || fin2) //one of the files must still be valid while both files are not simulatenously valid (after the while loop), which means the files are of different sizes
return false;
return true;
//Because of (!fin1 && fin2) || (fin1 && !fin2), if any one
//file is larger than the other, the files are deemed not the same.
//If the last characters of the two files have not been read, then
//both "fin1" and "fin2" will return true in the next iteration, and
//in the current iteration, c1 and c2 have valid values that can be compared.
//If the last characters of the two files were read, then "fin1" and "fin2"
//will still return true until the next time fin1.get() or fin2.get() is called.
//c1 and c2 still carry valid values (namely, the last characters of fin1 and fin2)
//which are compared. At the next iteration, both fin1 and fin2 fail and the loop exits.
//This means all c1's and c2's were equal in the iterations before. Thus, the two files are
//deemed the same.
}
int send_message(const char* sender_username, const char* receiver_username, const char* message, const long long int &amount_sent, const char* option)
{
std::string random_string = std::to_string(rand());
char *receiver_path = new char[strlen(receiver_username) + sizeof(TCOIN_MSG_PATH) + 13]; //sizeof() includes '\0'
char *temp_receiver_path = new char[strlen(receiver_username) + strlen(random_string.c_str()) + sizeof(TCOIN_MSG_PATH) + 13];
std::strcpy(receiver_path, TCOIN_MSG_PATH);
std::strcat(receiver_path, receiver_username);
std::strcat(receiver_path, "_messages.txt"); // length = 13
std::strcpy(temp_receiver_path, TCOIN_MSG_PATH);
std::strcat(temp_receiver_path, receiver_username);
std::strcat(temp_receiver_path, random_string.c_str());
std::strcat(temp_receiver_path, "_messages.txt"); // length = 13
//create receiver's message file if none exists
//the message will be included in the receiver's
//account when she/he initialises her/his account
//at a later time
char *receiver_salt_path = new char[strlen(receiver_username) + sizeof(TCOIN_SALT_PATH) + 9];
char *receiver_salt_logged_in_path = new char[strlen(receiver_username) + sizeof(TCOIN_SALT_PATH) + 19];
std::strcpy(receiver_salt_path, TCOIN_SALT_PATH);
std::strcat(receiver_salt_path, receiver_username);
std::strcpy(receiver_salt_logged_in_path, receiver_salt_path);
std::strcat(receiver_salt_path, "_salt.txt"); // length = 9
std::strcat(receiver_salt_logged_in_path, "_salt_logged_in.txt"); // length = 19
std::ifstream fin(receiver_path);
std::ifstream fin2(receiver_salt_path);
std::ifstream fin3(receiver_salt_logged_in_path);
if((!fin || file_is_empty(fin)) && ((!program_exists(receiver_username)) || ((!fin2 || file_is_empty(fin2)) && (!fin3 || file_is_empty(fin3)))))
{
fin.close();
std::ofstream fout(receiver_path, std::fstream::trunc);
fout << "\n";
fout.close();
}
else
fin.close();
fin2.close();
fin3.close();
chmod(receiver_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
delete[] receiver_salt_path;
delete[] receiver_salt_logged_in_path;
while(1)
{
if(!std::rename(receiver_path, temp_receiver_path))
{
char *really_temp_receiver_path = new char[strlen(temp_receiver_path) + 5];
std::strcpy(really_temp_receiver_path, temp_receiver_path);
std::strcat(really_temp_receiver_path, "_tmp");
std::ifstream fin(temp_receiver_path);
std::ofstream fout(really_temp_receiver_path);
if(!file_is_empty(fin))
fout << fin.rdbuf();
fin.close();
time_t now = std::time(NULL);
char dt[26];
#ifndef TILDEINSTITUTE
std::strftime(dt, 26, "%a %b %_d %T %Y", std::gmtime(&now));
#else
std::strftime(dt, 26, "%a %b %e %T %Y", std::gmtime(&now));
#endif
char sender_formatted_string[26];
char sender_arrow_formatted_string[47];
char sender_arrow_string[47];
char receiver_formatted_string[26];
std::snprintf(sender_formatted_string, 26, "%25s", sender_username);
std::snprintf(receiver_formatted_string, 26, "%-25s", receiver_username);
int sender_username_length = std::strlen(sender_username);
int number_of_chars = 26 >= sender_username_length ? 26 : sender_username_length;
std::strncpy(sender_arrow_string, sender_username, number_of_chars);
sender_arrow_string[number_of_chars] = '\0';
std::strcat(sender_arrow_string, " ----");
std::string amount_sent_str = formatted_amount(amount_sent);
std::strncat(sender_arrow_string, amount_sent_str.c_str(), 10);
std::strcat(sender_arrow_string, "----> ");
std::snprintf(sender_arrow_formatted_string, 47, "%46s", sender_arrow_string);
fout << dt << ": " << sender_arrow_formatted_string << receiver_username;
if(!strcmp(message, "")) //if message is empty
fout << "\n";
else
{
fout << "\n \\_ " << sender_username << " said: ";
for(int i=0; message[i]!='\0'; ++i)
{
if(message[i] == '\n')
fout << "<new-line>"; //return-key pressed
else if(message[i] == '\v')
fout << "<vertical-tab>"; //vertical-tab symbol entered
else
fout << message[i];
}
fout << "\n\n";
}
fout.close();
chmod(really_temp_receiver_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
if(!fout) //error
{
std::cerr << "Fatal error " << ERR_SEND_MESSAGE_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL << ": the receiver message file was unable to be updated. Please contact login@tilde.town (town-only) or login@tilde.team (internet-wide to report this error (because it requires manual recovery).";
exit_program(ERR_SEND_MESSAGE_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL);
}
else
{
std::remove(temp_receiver_path);
}
while(1)
{
if(!std::rename(really_temp_receiver_path, temp_receiver_path))
break;
}
// unlock_receiver_messages
while(1)
{
if(!std::rename(temp_receiver_path, receiver_path))
break;
}
chmod(receiver_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
delete[] really_temp_receiver_path;
delete[] temp_receiver_path;
delete[] receiver_path;
//additional place to write if sending to a program:
if(program_exists(receiver_username))
{
random_string = std::string("rand");
std::string program_receiver_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/_MESSAGES.txt");
std::string temp_program_receiver_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/_MESSAGES") + random_string + std::string(".txt");
std::string really_temp_program_receiver_path = temp_program_receiver_path + std::string("_tmp");
//create program receiver's _MESSAGES file if none exists
{
std::ifstream fin(program_receiver_path.c_str());
std::ifstream fin2(temp_program_receiver_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_receiver_path.c_str(), std::fstream::trunc);
fout << "\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(program_receiver_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_receiver_path.c_str(), temp_program_receiver_path.c_str()))
{
std::ifstream fin(temp_program_receiver_path.c_str());
std::ofstream fout(really_temp_program_receiver_path.c_str());
if(!file_is_empty(fin))
fout << fin.rdbuf();
fin.close();
//now, sender_username, receiver_username, amount_sent
char sender_formatted_string[26];
char receiver_formatted_string[26];
std::snprintf(sender_formatted_string, 26, "%s", sender_username);
std::snprintf(receiver_formatted_string, 26, "%s", receiver_username);
std::string amount_sent_str = formatted_amount(amount_sent);
fout << now << "\t" << sender_formatted_string << "\t" << receiver_formatted_string << "\t" << amount_sent_str;
if(!strcmp(message, "")) //if message is empty
fout << "\n";
else
{
fout << "\t" << sender_formatted_string << "\t";
for(int i=0; message[i]!='\0'; ++i)
{
if(message[i] == '\n')
fout << "<new-line>"; //return-key pressed
else if(message[i] == '\v')
fout << "<vertical-tab>"; //vertical-tab symbol entered
else
fout << message[i];
}
fout << "\n";
}
fout.close();
chmod(really_temp_program_receiver_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
if(!fout) //error
{
std::cerr << "Fatal error " << ERR_SEND_MESSAGE_PROGRAM_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL << ": the receiver program_message file was unable to be updated. Please contact login@tilde.town (town-only) or login@tilde.team (internet-wide to report this error (because it requires manual recovery).";
exit_program(ERR_SEND_MESSAGE_PROGRAM_RECEIVER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL);
}
else
{
std::remove(temp_program_receiver_path.c_str());
}
while(1)
{
if(!std::rename(really_temp_program_receiver_path.c_str(), temp_program_receiver_path.c_str()))
break;
}
// unlock_receiver_program_messages
while(1)
{
if(!std::rename(temp_program_receiver_path.c_str(), program_receiver_path.c_str()))
break;
}
chmod(program_receiver_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
break;
}//if statement with !std::rename for receiver's program accounting _messages file
}//while loop for program accounting receiver's _messages file
}//receiver is program account
//locking sender_messages_after_receiver_messages_unlocked
random_string = std::to_string(rand());
char *sender_path = new char[strlen(sender_username) + sizeof(TCOIN_MSG_PATH) + 13];
char *temp_sender_path = new char[strlen(sender_username) + strlen(random_string.c_str()) + sizeof(TCOIN_MSG_PATH) + 13];
std::strcpy(sender_path, TCOIN_MSG_PATH);
std::strcat(sender_path, sender_username);
std::strcat(sender_path, "_messages.txt"); // length = 13
std:strcpy(temp_sender_path, TCOIN_MSG_PATH);
std::strcat(temp_sender_path, sender_username);
std::strcat(temp_sender_path, random_string.c_str());
std::strcat(temp_sender_path, "_messages.txt"); // length = 13
while(1)
{
if(!std::rename(sender_path, temp_sender_path))
{
char *really_temp_sender_path = new char[strlen(temp_sender_path) + 5];
std::strcpy(really_temp_sender_path, temp_sender_path);
std::strcat(really_temp_sender_path, "_tmp");
fin.open(temp_sender_path);
fout.open(really_temp_sender_path);
chmod(really_temp_sender_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
fout << fin.rdbuf();
now = std::time(NULL);
#ifndef TILDEINSTITUTE
std::strftime(dt, 26, "%a %b %_d %T %Y", std::gmtime(&now));
#else
std::strftime(dt, 26, "%a %b %e %T %Y", std::gmtime(&now));
#endif
char sender_formatted_string_right_aligned[26];
char receiver_arrow_formatted_string[47];
char receiver_arrow_string[47];
std::snprintf(receiver_formatted_string, 26, "%25s", receiver_username);
std::snprintf(sender_formatted_string, 26, "%-25s", sender_username);
std::snprintf(sender_formatted_string_right_aligned, 26, "%25s", sender_username);
int receiver_username_length = std::strlen(receiver_username);
int number_of_chars = 26 >= receiver_username_length ? 26 : receiver_username_length;
std::strncpy(receiver_arrow_string, receiver_username, number_of_chars);
receiver_arrow_string[number_of_chars] = '\0';
std::strcat(receiver_arrow_string, " <---");
std::string amount_sent_str = formatted_amount(amount_sent);
std::strncat(receiver_arrow_string, amount_sent_str.c_str(), 10);
std::strcat(receiver_arrow_string, "----- ");
std::snprintf(receiver_arrow_formatted_string, 47, "%46s", receiver_arrow_string);
fout << dt << ": " << receiver_arrow_formatted_string << sender_username;
if(!strcmp(message, "")) //if message is empty
fout << "\n";
else
{
fout << "\n \\_ " << sender_username << " said: ";
for(int i=0; message[i]!='\0'; ++i)
{
if(message[i] == '\n')
fout << "<new-line>"; //return-key pressed
else if(message[i] == '\v')
fout << "<vertical-tab>"; //vertical-tab symbol entered
else
fout << message[i];
}
fout << "\n\n";
}
fin.close();
fout.close();
if(!fout) //error
{
std::cerr << "Fatal error " << ERR_SEND_MESSAGE_SENDER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL << ": the sender message file was unable to be updated. Please contact login@tilde.town (town-only) or login@tilde.team (internet-wide to report this error (because it requires manual recovery).";
exit_program(ERR_SEND_MESSAGE_SENDER_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL);
}
else
{
std::remove(temp_sender_path);
}
while(1)
{
if(!std::rename(really_temp_sender_path, temp_sender_path))
break;
}
while(1)
{
if(!std::rename(temp_sender_path, sender_path))
break;
}
delete[] really_temp_sender_path;
delete[] temp_sender_path;
delete[] sender_path;
//additional place to write if sending from a program:
if(program_exists(sender_username))
{
random_string = std::string("rand");
std::string program_sender_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/_MESSAGES.txt");
std::string temp_program_sender_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/_MESSAGES") + random_string + std::string(".txt");
std::string really_temp_program_sender_path = temp_program_sender_path + std::string("_tmp");
//create program sender's _MESSAGES file if none exists
{
std::ifstream fin(program_sender_path.c_str());
std::ifstream fin2(temp_program_sender_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_sender_path.c_str(), std::fstream::trunc);
fout << "\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(program_sender_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_sender_path.c_str(), temp_program_sender_path.c_str()))
{
std::ifstream fin(temp_program_sender_path.c_str());
std::ofstream fout(really_temp_program_sender_path.c_str());
if(!file_is_empty(fin))
fout << fin.rdbuf();
fin.close();
//now (updated by code above to reflect "sending time"), sender_username, receiver_username, amount_sent
char sender_formatted_string[26];
char receiver_formatted_string[26];
std::snprintf(sender_formatted_string, 26, "%s", sender_username);
std::snprintf(receiver_formatted_string, 26, "%s", receiver_username);
std::string amount_sent_str = formatted_amount(-1*amount_sent);
fout << now << "\t" << receiver_formatted_string << "\t" << sender_formatted_string << "\t" << amount_sent_str;
if(!strcmp(message, "")) //if message is empty
fout << "\n";
else
{
fout << "\t" << sender_formatted_string << "\t";
for(int i=0; message[i]!='\0'; ++i)
{
if(message[i] == '\n')
fout << "<new-line>"; //return-key pressed
else if(message[i] == '\v')
fout << "<vertical-tab>"; //vertical-tab symbol entered
else
fout << message[i];
}
fout << "\n";
}
fout.close();
chmod(really_temp_program_sender_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
if(!fout) //error
{
std::cerr << "Fatal error " << ERR_SEND_MESSAGE_SENDER_PROGRAM_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL << ": the sender program message file was unable to be updated. Please contact login@tilde.town (town-only) or login@tilde.team (internet-wide to report this error (because it requires manual recovery).";
exit_program(ERR_SEND_MESSAGE_SENDER_PROGRAM_MSG_FILE_UNABLE_TO_BE_UPDATED_FATAL);
}
else
{
std::remove(temp_program_sender_path.c_str());
}
while(1)
{
if(!std::rename(really_temp_program_sender_path.c_str(), temp_program_sender_path.c_str()))
break;
}
// unlock_sender_program_messages
while(1)
{
if(!std::rename(temp_program_sender_path.c_str(), program_sender_path.c_str()))
break;
}
chmod(program_sender_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
break;
}//if statement with !std::rename for sender's program accounting _messages file
}//while loop for program accounting sender's _messages file
}//sender is program account
break;
}
}
break;
}
}
//finally, everything ran well and we can send the message to stdout if verbose is turned on
if(!strcmp(option, "verbose") && strcmp(message, "")) //message should not be empty
{
//since the message to stdout from send() ends with "\n\n", we have commented this below line out
//std::cout << "\n";
std::cout << "In addition, the transaction message \"" << message << "\" was ";
std::cout << "sent from `" << sender_username << "` to `" << receiver_username << "` successfully.";
std::cout << "\n\n";
}
return 0;
}
bool user_is_locked(const char* username)
{
std::ifstream fin((std::string(TCOIN_PATH_W_SLASH) + std::string(username) + std::string("_locked.txt")).c_str());
if(!fin)
return false;
return true;
}
int send(const char* sender_username, const char* receiver_username, const long long int &amount_to_send, const long long int &base_amount, const char* option)
{
int final_return_value = 0;
//receiver usrname check
if(username_exists(receiver_username))
{
if(!strcmp(sender_username, receiver_username))
{
std::cout << "\nSorry, you cannot send tildecoins to yourself.\n\n";
return ERR_TCOIN_TO_SELF;
}
if(user_is_locked(receiver_username))
{
if(!strcmp(option, "verbose"))
std::cout << "\nSorry, `" << receiver_username << "` does not wish to receive any tildecoins at this time.\n\n";
return ERR_RECEIVER_BLOCKED;
}
if(amount_to_send <= 0)
{
if(!strcmp(option, "verbose"))
std::cout << "\nSorry, that amount is not valid. The amount should be a positive decimal number when truncated to two decimal places.\n\n";
return ERR_NEGATIVE_SEND_AMOUNT;
}
else
{
std::string random_string = std::to_string(rand());
int return_value = -1;
//additional place to deduct from if the sender is a program (which is
//always the case when `pcoin` is used, but we'll check anyway
//we do this before the "tcoin send" part so that if program tries to send
//more than it owes receiver, it's checked before checking "tcoin send's"
//conditions
if(program_exists(sender_username))
{
random_string = std::string("rand"); //not really random
std::string program_sender_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/") + std::string(receiver_username) + std::string(".txt");
std::string temp_program_sender_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/") + std::string(receiver_username) + random_string + std::string(".txt");
std::string temp_program_sender_username = std::string(PROG_ACT_W_SLASH) + std::string(sender_username) + std::string("/") + std::string(receiver_username) + random_string;
//create program sender's "receiver's balance file" if none exists
{
std::ifstream fin(program_sender_path.c_str());
std::ifstream fin2(temp_program_sender_path.c_str());
#if DEBUG
std::cout << program_sender_path << "," << temp_program_sender_path << "," << !fin << "," << file_is_empty(fin) << "," << !fin2 << std::endl;
#endif
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_sender_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
#if DEBUG
char dummy;
std::cout << "Press enter to continue"; std::cin >> dummy;
#endif
}
else
fin.close();
fin2.close();
chmod(program_sender_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_sender_path.c_str(), temp_program_sender_path.c_str()))
{
//Insufficient funds check in add_file_value() itself
//third argument, base_amount, is 0 because program
//should not be allowed to send more than what is owed
//to the receiver
return_value = add_file_value(temp_program_sender_username.c_str(), -1 * amount_to_send, 0);
//The same amount must also be deducted from the "_total.txt" file
//which records the total amount owed to others
if(return_value == 0)
{
random_string = std::string("rand");
std::string program_sender_total_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/_TOTAL.txt");
std::string temp_program_sender_total_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(sender_username) + std::string("/_TOTAL") + random_string + std::string(".txt");
std::string temp_program_sender_total_username = std::string(PROG_ACT_W_SLASH) + std::string(sender_username) + std::string("/_TOTAL") + random_string;
//create program sender's "total balance file" if none exists
{
std::ifstream fin(program_sender_total_path.c_str());
std::ifstream fin2(temp_program_sender_total_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_sender_total_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(program_sender_total_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_sender_total_path.c_str(), temp_program_sender_total_path.c_str()))
{
//Insufficient funds check in add_file_value() itself
//third argument, base_amount, is 0 because program
//should not be allowed to send more than what is owed
//to the receiver
add_file_value(temp_program_sender_total_username.c_str(), -1 * amount_to_send, 0);
random_string = std::to_string(rand());
// sizeof() includes '\0'
char* temp_sender_path = new char[strlen(sender_username) + strlen(random_string.c_str()) + sizeof(TCOIN_PATH_W_SLASH) + 4];
char* sender_path = new char[strlen(sender_username) + sizeof(TCOIN_PATH_W_SLASH) + 4];
char* temp_sender_username = new char[strlen(sender_username) + strlen(random_string.c_str()) + 1];
std::strcpy(temp_sender_username, sender_username);
std::strcat(temp_sender_username, random_string.c_str());
std::strcpy(temp_sender_path, TCOIN_PATH_W_SLASH);
std::strcat(temp_sender_path, temp_sender_username);
std::strcat(temp_sender_path, ".txt"); // length = 4
std::strcpy(sender_path, TCOIN_PATH_W_SLASH);
std::strcat(sender_path, sender_username);
std::strcat(sender_path, ".txt"); // length = 4
while(1)
{
if(!std::rename(sender_path, temp_sender_path))
{
//Insufficient funds check is in add_file_value()
return_value = add_file_value(temp_sender_username, -1 * amount_to_send, base_amount);
if(return_value == 0) // Funds sucessfully deducted from sender_username
{
random_string = std::string("rand");
// sizeof() includes '\0'
char *temp_receiver_path = new char[strlen(receiver_username) + strlen(random_string.c_str()) + sizeof(TCOIN_PATH_W_SLASH) + 4];
char *receiver_path = new char[strlen(receiver_username) + sizeof(TCOIN_PATH_W_SLASH) + 4];
char *temp_receiver_username = new char[strlen(receiver_username) + strlen(random_string.c_str()) + 1];
std::strcpy(temp_receiver_username, receiver_username);
std::strcat(temp_receiver_username, random_string.c_str());
std::strcpy(temp_receiver_path, TCOIN_PATH_W_SLASH);
std::strcat(temp_receiver_path, temp_receiver_username);
std::strcat(temp_receiver_path, ".txt");
std::strcpy(receiver_path, TCOIN_PATH_W_SLASH);
std::strcat(receiver_path, receiver_username);
std::strcat(receiver_path, ".txt");
//create receiver's balance file if none exists
//the balance will be included in the receiver's
//account when she/he initialises her/his account
//at a later time
char *receiver_salt_path = new char[strlen(receiver_username) + sizeof(TCOIN_SALT_PATH) + 9]; // sizeof() includes '\0'
char *receiver_salt_logged_in_path = new char[strlen(receiver_username) + sizeof(TCOIN_SALT_PATH) + 19];
std::strcpy(receiver_salt_path, TCOIN_SALT_PATH);
std::strcat(receiver_salt_path, receiver_username);
std::strcpy(receiver_salt_logged_in_path, receiver_salt_path);
std::strcat(receiver_salt_path, "_salt.txt"); // length = 9
std::strcat(receiver_salt_logged_in_path, "_salt_logged_in.txt"); // length = 19
std::ifstream fin(receiver_path);
std::ifstream fin2(receiver_salt_path);
std::ifstream fin3(receiver_salt_logged_in_path);
if((!fin || file_is_empty(fin)) && ((!program_exists(receiver_username)) || ((!fin2 || file_is_empty(fin2)) && (!fin3 || file_is_empty(fin3)))))
{
fin.close();
std::ofstream fout(receiver_path, std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
fin3.close();
chmod(receiver_path, (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
delete[] receiver_salt_path;
delete[] receiver_salt_logged_in_path;
while(1)
{
if(!std::rename(receiver_path, temp_receiver_path))
{
//Insufficient funds check in add_file_value() itself
add_file_value(temp_receiver_username, amount_to_send, base_amount);
//additional place to write if sending to a program:
if(program_exists(receiver_username))
{
random_string = std::string("rand");
std::string program_receiver_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/") + std::string(sender_username) + std::string(".txt");
std::string temp_program_receiver_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/") + std::string(sender_username) + random_string + std::string(".txt");
std::string temp_program_receiver_username = std::string(PROG_ACT_W_SLASH) + std::string(receiver_username) + std::string("/") + std::string(sender_username) + random_string;
//create program receiver's balance file if none exists
{
std::ifstream fin(program_receiver_path.c_str());
std::ifstream fin2(temp_program_receiver_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_receiver_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(program_receiver_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_receiver_path.c_str(), temp_program_receiver_path.c_str()))
{
//Insufficient funds check in add_file_value() itself
add_file_value(temp_program_receiver_username.c_str(), amount_to_send, base_amount);
//Value must also be added to a _total.txt file
{
random_string = std::string("rand");
std::string program_receiver_total_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/_TOTAL.txt");
std::string temp_program_receiver_total_path = std::string(TCOIN_PROG_ACT_PATH) + std::string(receiver_username) + std::string("/_TOTAL") + random_string + std::string(".txt");
std::string temp_program_receiver_total_username = std::string(PROG_ACT_W_SLASH) + std::string(receiver_username) + std::string("/_TOTAL") + random_string;
//create program receiver's "total balance file" if none exists
{
std::ifstream fin(program_receiver_total_path.c_str());
std::ifstream fin2(temp_program_receiver_total_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(program_receiver_total_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(program_receiver_total_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(program_receiver_total_path.c_str(), temp_program_receiver_total_path.c_str()))
{
//Insufficient funds check in add_file_value() itself
add_file_value(temp_program_receiver_total_username.c_str(), amount_to_send, base_amount);
while(1)
{
if(!std::rename(temp_program_receiver_total_path.c_str(), program_receiver_total_path.c_str()))
break;
}
break;
}
}
}
while(1)
{
if(!std::rename(temp_program_receiver_path.c_str(), program_receiver_path.c_str()))
break;
}
break;
}
}
}
if(!strcmp(option, "verbose"))
{
std::cout << "\n";
cout_formatted_amount(amount_to_send, " tildecoins were ", " tildecoin was ");
std::cout << "sent from `" << sender_username << "` to `" << receiver_username << "`.";
std::cout << "\n\n";
}
while(1)
{
if(!std::rename(temp_receiver_path, receiver_path))
break;
}
delete[] temp_receiver_path;
delete[] receiver_path;
delete[] temp_receiver_username;
break;
}
}
}
else if(return_value == 1)
{
if(!strcmp(option, "verbose"))
{
long long int amount_of_funds = base_amount + get_file_value(temp_sender_username);
std::cout << "\nSorry, you do not have sufficient funds to execute this transaction. ";
std::cout << "Your current balance is ";
cout_formatted_amount(amount_of_funds, " tildecoins.\n\n", " tildecoin.\n\n");
}
final_return_value = ERR_INSUFFICIENT_FUNDS; //we don't simply "return 3" here because we want temp_sender_path to get renamed again
}
while(1)
{
if(!std::rename(temp_sender_path, sender_path))
break;
}
delete[] temp_sender_path;
delete[] sender_path;
delete[] temp_sender_username;
break;
}
}
while(1)
{
if(!std::rename(temp_program_sender_total_path.c_str(), program_sender_total_path.c_str()))
break;
}
break;
}
}
}
else if(return_value == 1)
{
long long int amount_owed = get_file_value(temp_program_sender_username.c_str());
long long int amount_to_aib = (long long int)(amount_to_send) - amount_owed;
std::cout << "\nSorry, you only owe `" << receiver_username << "` ";
cout_formatted_amount(amount_owed, " tildecoins", " tildecoin");
std::cout << ", not ";
cout_formatted_amount(amount_to_send, " tildecoins. ", " tildecoin. ");
std::cout << "Please run `" << PCOIN_BIN_PATH_W_SPACE << "add_internal_balance " << receiver_username << " ";
cout_formatted_amount(amount_to_aib);
std::cout << "` to sufficiently increase the amount owed to `" << receiver_username << "`.\n\n";
final_return_value = ERR_INSUFFICIENT_FUNDS; //we don't simply "return 3" here because we want temp_program_sender_path to get renamed again
}
while(1)
{
if(!std::rename(temp_program_sender_path.c_str(), program_sender_path.c_str()))
break;
}
break;
}
}
}
}
}
else
{
if(!strcmp(option, "verbose"))
std::cout << "\nSorry, no user with the username `" << receiver_username << "` was found.\n\n";
return ERR_RECEIVER_NOT_FOUND;
}
return final_return_value;
}
void help()
{
std::cout <<"\npcoin is meant for programs. After each of the following commands, you will have to input a valid key to stdin.";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "messages` or `" << PCOIN_BIN_PATH_W_SPACE << "-m`: check your messages";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "messages <num>` or `" << PCOIN_BIN_PATH_W_SPACE << "-m <num>`: print the last <num> messages";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "messages_tsv` or `" << PCOIN_BIN_PATH_W_SPACE << "-mtsv`: check your messages in tab-separated-values format";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "messages_tsv <num>` or `" << PCOIN_BIN_PATH_W_SPACE << "-mtsv <num>`: print the last <num> messages in tab-separated-values format";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "balance` or `" << PCOIN_BIN_PATH_W_SPACE << "-b`: print the number representing your balance";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "total_owed` or `" << PCOIN_BIN_PATH_W_SPACE << "-to`: print the total amount owed to others";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "internal_balance <username>` or `" << PCOIN_BIN_PATH_W_SPACE << "-ib <username>`: print the amount you owe <username>";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "add_internal_balance <username>` or `" << PCOIN_BIN_PATH_W_SPACE << "-aib <username> <amount>`: add <amount> to the amount you owe <username>";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "send <username> <amount>` or `" << PCOIN_BIN_PATH_W_SPACE << "-s <username> <amount>`: send <amount> tildecoins to <username>";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "send <username> <amount> \"<message>\"`: optionally, include a message to be sent to <username>";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "silentsend <username> <amount> [\"<message>\"]`, `" << PCOIN_BIN_PATH_W_SPACE << "send -s <username> <amount> [\"<message>\"]` or `" << PCOIN_BIN_PATH_W_SPACE << "-ss <username> <amount> [\"<message>\"]`: send <amount> tildecoins to <username> with an optional (as indicated by [ and ], which should not be included in the actual comment) message included without printing anything";
std::cout << "\nIn the commands with `<username> <amount>`, switching the two arguments around (i.e., from `<username> <amount>` to `<amount> <username>`) will also work";
std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "--help`, `" << PCOIN_BIN_PATH_W_SPACE << "help` or `" << PCOIN_BIN_PATH_W_SPACE << "-h`: print this help text";
std::cout << "\nSend an email to `login@tilde.town` (tilde.town local email) or `login@tilde.team` (internet-wide email), or `/query login` on IRC to report any errors or request a key for your program.\n\n";
}
bool is_number(const char* test_string)
{
char* p;
strtod(test_string, &p);
return *p == 0;
}
std::string get_username_from_key(std::string &key)
{
const static std::string all_usernames_dot_txt = exec(LS_PCOIN_KEY_CMD);
std::istringstream iss(all_usernames_dot_txt);
std::string word1, word2;
//first word is program username with .txt on the end, second word is key
while(iss >> word1)
{
char *program_key_path = new char[strlen(word1.c_str()) + sizeof(PCOIN_KEY_PATH_W_SLASH)]; //sizeof counts NULL char at the end too
std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH);
std::strcat(program_key_path, word1.c_str());
std::ifstream fin(program_key_path);
fin >> word2;
if(!strctcmp(word2.c_str(), key.c_str()))
{
fin.close();
delete[] program_key_path;
return word1.substr(0, word1.size()-4); //removing .txt from the username returned
}
fin.close();
delete[] program_key_path;
}
word1.assign("n/a");
return word1;
}
long long int get_internal_balance(const char* username)
{
//sometimes, it just helps to double-check things
//this makes the security of this function decoupled
//from the 'deny access' mechanism in the "main"
//function when an incorrect key is entered
if(program_exists(get_username().c_str()) && username_exists(username))
{
std::string internal_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/") + std::string(username) + std::string(".txt");
//create internal balance file if none exists
{
std::ifstream fin(internal_path.c_str());
if(!fin || file_is_empty(fin))
{
fin.close();
std::ofstream fout(internal_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
chmod(internal_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
std::string internal_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/") + std::string(username);
return get_file_value(internal_username.c_str());
}
return ERR_IN_GET_INTERNAL_BALANCE;
}
long long int get_internal_total_owed()
{
if(program_exists(get_username().c_str()))
{
std::string internal_total_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/_TOTAL.txt");
//create internal total file if none exists
{
std::ifstream fin(internal_total_path.c_str());
if(!fin || file_is_empty(fin))
{
fin.close();
std::ofstream fout(internal_total_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
chmod(internal_total_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
std::string internal_total_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/_TOTAL");
return get_file_value(internal_total_username.c_str());
}
return ERR_IN_GET_INTERNAL_TOTAL_OWED_SELF_PROGRAM_DOESNT_EXIST;
}
int add_internal_balance(const char* username, const long long int value_to_add)
{
if(program_exists(get_username().c_str()) && username_exists(username))
{
std::string random_string = std::string("rand");
std::string internal_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/") + std::string(username);
std::string temp_internal_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/") + std::string(username) + random_string;
long long int internal_total_owed = get_internal_total_owed();
if(internal_total_owed == ERR_IN_GET_INTERNAL_TOTAL_OWED_SELF_PROGRAM_DOESNT_EXIST)
{
std::cerr << "\nError in add_internal_balance()! get_internal_total_owed() failed!\n\n";
return ERR_IN_ADD_INTERNAL_BALANCE_GET_INTERNAL_TOTAL_OWED_FAILED;
}
if((value_to_add > 0) && (value_to_add > (base_amount + user_amount - internal_total_owed)))
{
return ERR_ADD_INTERNAL_BALANCE_VALUE_TO_ADD_UNFULFILLABLE_USING_OWN_CURRENT_FUNDS; //value_to_add is more than what the program can fulfil using its own current funds
}
std::string internal_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/") + std::string(username) + std::string(".txt");
std::string temp_internal_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/") + std::string(username) + random_string + std::string(".txt");
//create internal file if none exists
{
std::ifstream fin(internal_path.c_str());
std::ifstream fin2(temp_internal_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(internal_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(internal_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
int return_value;
int final_return_value = 0;
while(1)
{
if(!std::rename(internal_path.c_str(), temp_internal_path.c_str()))
{
return_value = add_file_value(temp_internal_username.c_str(), value_to_add, 0); //cannot make user's internal balance negative, so base_amount is 0
if(return_value) //if return value is non-zero
{
final_return_value = return_value; //we don't simply "return return_value" because we want files to get renamed back to their original filenames
}
else //return value is zero, i.e., add_file_value succeeded
//also need to update _total.txt
{
random_string = std::string("rand");
std::string internal_total_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/_TOTAL");
std::string temp_internal_total_username = std::string(PROG_ACT_W_SLASH) + get_username() + std::string("/_TOTAL") + random_string;
std::string internal_total_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/_TOTAL.txt");
std::string temp_internal_total_path = std::string(TCOIN_PROG_ACT_PATH) + get_username() + std::string("/_TOTAL") + random_string + std::string(".txt");
//create _total.txt file if none exists
{
std::ifstream fin(internal_total_path.c_str());
std::ifstream fin2(temp_internal_total_path.c_str());
if((!fin || file_is_empty(fin)) && (!fin2))
{
fin.close();
std::ofstream fout(internal_total_path.c_str(), std::fstream::trunc);
fout << "0\n";
fout.close();
}
else
fin.close();
fin2.close();
chmod(internal_total_path.c_str(), (S_IRUSR | S_IWUSR) & ~S_IRWXG & ~S_IRWXO);
}
while(1)
{
if(!std::rename(internal_total_path.c_str(), temp_internal_total_path.c_str()))
{
return_value = add_file_value(temp_internal_total_username.c_str(), value_to_add, 0);
while(1)
{
if(!std::rename(temp_internal_total_path.c_str(), internal_total_path.c_str()))
break;
}
break;
}
}
if(return_value) //if return value is non-zero
final_return_value = return_value; //we don't simply "return return_value" because we want files to get renamed back to their original filenames
}
while(1)
{
if(!std::rename(temp_internal_path.c_str(), internal_path.c_str()))
break;
}
break;
}
}
return final_return_value;
}
return ERR_ADD_INTERNAL_BALANCE_USERNAME_DOESNT_EXIST;
}
bool message_is_long(const char* test_string)
{
for(int i=0; i < TCOIN_MSG_LENGTH_LIMIT+1; ++i)
if(test_string[i] == '\0')
{
return false; //message is shorter than TCOIN_MSG_LENGTH_LIMIT characters
}
return true; //message is longer than TCOIN_MSG_LENGTH_LIMIT characters
}
int main(int argc, char *argv[])
{
if(argc > 1 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "help") || !strcmp(argv[1], "-h")))
{
help();
return 0;
}
{
std::string key;
std::string program_username;
std::getline(std::cin, key);
program_username.assign(get_username_from_key(key));
if(!program_username.compare("n/a"))
{
std::cout << "\nSorry, the key you specified is not in use.\n\n";
return ERR_KEY_NOT_IN_USE;
}
set_username(program_username);
}
base_amount = 0;
long long int unaltered_base_amount = base_amount;
user_amount = 0;
krowbar_amount = 0;
minercoin_amount = 0;
#ifndef KROWBAR_OFF
//adding tildebot scores from krowbar to base amount
{
std::string line;
const std::string username = get_username();
const int username_length = username.length();
std::ifstream fin(KROWBAR_SCORE_PATH);
while(std::getline(fin, line))
{
char* line_c_string = new char[line.length()+1];
std::strcpy(line_c_string, line.c_str());
const int irc_username_length = username_length > USERNAME_LENGTH_LIMIT ? USERNAME_LENGTH_LIMIT : username_length;
if(!strncasecmp(username.c_str(), line_c_string, irc_username_length)) //username starts with capital letter, but name in database does not
{
char number_of_tildes[21];
number_of_tildes[0] = '0'; //just in case the loop below doesn't detect any digits
number_of_tildes[1] = '\0';
for(int i=0; i < 20; ++i)
{
if(std::isdigit(line_c_string[irc_username_length+3+i]))
number_of_tildes[i] = line_c_string[irc_username_length+3+i];
else
{
number_of_tildes[i] = '\0'; //manually terminating the string
break;
}
}
number_of_tildes[20] = '\0'; //incase the number overflows 20 characters
krowbar_amount += strtol100(number_of_tildes);
base_amount += krowbar_amount;
//multiplied by 100 inside strtol100() to convert tildecoins to centitildecoins, which
//is the unit used throughout the program (and converted appropriately when displayed)
}
delete[] line_c_string;
}
}
#endif
#ifndef MINERCOIN_OFF
//adding minercoin scores from minerobber to base amount
{
std::string command_to_exec = std::string(MINERCOIN_CMD_PRE_USERNAME) + get_username() + std::string(MINERCOIN_CMD_POST_USERNAME);
std::string number_of_tildes = exec(command_to_exec.c_str());
number_of_tildes.pop_back();
//to get rid of the newline at the end
if(is_number(number_of_tildes.c_str()))
minercoin_amount += strtol100(number_of_tildes.c_str());
base_amount += minercoin_amount;
//multiplied by 100 to convert tildecoins to centitildecoins, which
//is the unit used throughout the program (and converted appropriately when displayed)
}
#endif
user_amount = get_file_value(get_username().c_str());
srand((long int)(std::time(NULL)) + strtol_fast(exec(BIN_ECHO_CMD).c_str()));
long long int total_amount = base_amount + user_amount;
if(argc < 2)
{
std::cout << "\nSorry, `" << PCOIN_BIN_PATH << "` doesn't work. Please use `" << PCOIN_BIN_PATH_W_SPACE << "-m` for messages or `" << PCOIN_BIN_PATH_W_SPACE << "-b` to check your balance. `" << PCOIN_BIN_PATH_W_SPACE << "--help` prints the help text.\n\n";
return ERR_NO_ARGS;
}
else if(!strcmp(argv[1], "breakdown") || !strcmp(argv[1], "-bd"))
{
std::cout << "total,";
cout_formatted_amount(total_amount, ";", ";");
show_breakdown(unaltered_base_amount, "baseamount", user_amount, "transfers", krowbar_amount, "tildegame", minercoin_amount, "minercoin");
}
else if(!strcmp(argv[1], "messages") || !strcmp(argv[1], "-m"))
{
double number_of_messages = 0.0;
bool number_of_messages_is_specified = argc > 2 && is_number(argv[2]); //number of messages specified
if(number_of_messages_is_specified)
number_of_messages = std::strtod(argv[2], NULL);
if(number_of_messages >= 1.0) //number of messages specified is a valid number
show_messages_tail(get_username().c_str(), (long long int)(number_of_messages));
else //show all messages
show_messages(get_username().c_str());
}
else if(!strcmp(argv[1], "messages_tsv") || !strcmp(argv[1], "-mtsv"))
{
double number_of_messages = 0.0;
bool number_of_messages_is_specified = argc > 2 && is_number(argv[2]); //number of messages specified
if(number_of_messages_is_specified)
number_of_messages = std::strtod(argv[2], NULL);
if(number_of_messages >= 1.0) //number of messages specified is a valid number
show_tsv_messages_tail(get_username().c_str(), (long long int)(number_of_messages));
else //show all messages
show_tsv_messages(get_username().c_str());
}
else if(!strcmp(argv[1], "balance") || !strcmp(argv[1], "-b"))
cout_formatted_amount(total_amount, "\n");
else if(!strcmp(argv[1], "total_owed") || !strcmp(argv[1], "-to"))
{
long long int total_owed = get_internal_total_owed();
if(total_owed == ERR_IN_GET_INTERNAL_TOTAL_OWED_SELF_PROGRAM_DOESNT_EXIST)
{
std::cerr << "\nError in main()! get_internal_total_owed() failed!\n\n";
return ERR_IN_MAIN_GET_INTERNAL_TOTAL_OWED_FAILED;
}
cout_formatted_amount(total_owed, "\n");
}
else if(!strcmp(argv[1], "internal_balance") || !strcmp(argv[1], "-ib"))
{
if(argc == 3) //second argument (the one right after "-ib") is the username
{
long long int internal_balance = get_internal_balance(argv[2]);
if(internal_balance == ERR_IN_GET_INTERNAL_BALANCE) //username check doesn't pass
{
std::cout << "\nSorry, no user with the username `" << argv[2] << "` was found.\n\n";
return ERR_INTERNAL_BALANCE_USERNAME_NOT_FOUND;
}
cout_formatted_amount(internal_balance, "\n");
}
else if(argc == 2) //no username supplied (too few arguments supplied)
{
std::cout << "\nSorry, too few command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "internal_balance <username>`.\n\n";
return ERR_INTERNAL_BALANCE_NO_USERNAME_SUPPLIED;
}
else if(argc > 3) //too many arguments supplied
{
std::cout << "\nSorry, too many command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "internal_balance <username>`.\n\n";
return ERR_INTERNAL_BALANCE_TOO_MANY_ARGS;
}
}
else if(!strcmp(argv[1], "add_internal_balance") || !strcmp(argv[1], "-aib"))
{
if(argc < 4)
{
std::cout << "\nSorry, too few command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "add_internal_balance <username> <amount>`.\n\n";
return ERR_ADD_INTERNAL_BALANCE_TOO_FEW_ARGS;
}
else if(argc > 4)
{
std::cout << "\nSorry, too many command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "add_internal_balance <username> <amount>`.\n\n";
return ERR_ADD_INTERNAL_BALANCE_TOO_MANY_ARGS;
}
// number of arguments is exactly 3
{
int return_value = ERR_IN_ADD_INTERNAL_BALANCE_GET_INTERNAL_TOTAL_OWED_FAILED, return_value2 = ERR_IN_ADD_INTERNAL_BALANCE_GET_INTERNAL_TOTAL_OWED_FAILED;
if(is_number(argv[3]))
return_value = add_internal_balance(argv[2], strtol100(argv[3]));
else
return_value2 = add_internal_balance(argv[3], strtol100(argv[2]));
if(return_value == ERR_ADD_INTERNAL_BALANCE_VALUE_TO_ADD_UNFULFILLABLE_USING_OWN_CURRENT_FUNDS || return_value2 == ERR_ADD_INTERNAL_BALANCE_VALUE_TO_ADD_UNFULFILLABLE_USING_OWN_CURRENT_FUNDS) //value_to_add was too large
{
std::cout << "\nSorry, the amount was larger than what the program's current unowed balance could cover.\n\n";
return ERR_IN_MAIN_ADD_INTERNAL_BALANCE_AMOUNT_LARGER_THAN_COVERABLE_BY_UNOWED_BALANCE;
}
if(return_value == ERR_ADD_FILE_VALUE_INSUFFICIENT_FUNDS) //value_to_add was too negative
{
std::cout << "\nSorry, the amount was more negative than what `" << argv[2] << "` could cover.\n\n";
return ERR_IN_MAIN_ADD_INTERNAL_BALANCE_AMOUNT_MAKING_USER_INTERNAL_BALANCE_NEGATIVE;
}
if(return_value2 == ERR_ADD_FILE_VALUE_INSUFFICIENT_FUNDS) //value_to_add was too negative
{
std::cout << "\nSorry, the amount was more negative than what `" << argv[3] << "` could cover.\n\n";
return ERR_IN_MAIN_ADD_INTERNAL_BALANCE_AMOUNT_MAKING_USER_INTERNAL_BALANCE_NEGATIVE;
}
if(return_value == ERR_ADD_INTERNAL_BALANCE_USERNAME_DOESNT_EXIST) //username check doesn't pass
{
std::cout << "\nSorry, no user with the username `" << argv[2] << "` was found.\n\n";
return ERR_IN_MAIN_ADD_INTERNAL_BALANCE_NO_SUCH_USERNAME_FOUND;
}
if(return_value2 == ERR_ADD_INTERNAL_BALANCE_USERNAME_DOESNT_EXIST) //username check doesn't pass
{
std::cout << "\nSorry, no user with the username `" << argv[3] << "` was found.\n\n";
return ERR_IN_MAIN_ADD_INTERNAL_BALANCE_NO_SUCH_USERNAME_FOUND;
}
}
}
else if(!strcmp(argv[1], "send") || !strcmp(argv[1], "-s"))
{
if(argc == 5)
{
if(!strcmp(argv[2], "-s"))
{
int return_value;
long long int amount = 0;
char *receiver = NULL;
if(is_number(argv[3]))
{
amount = strtol100(argv[3]);
receiver = argv[4];
}
else
{
amount = strtol100(argv[4]);
receiver = argv[3];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "silent");
if(!return_value) //send was successful
send_message(get_username().c_str(), receiver, "", amount, "silent");
}
else //argument count is 5 because a custom message was included
{
int return_value;
long long int amount = 0;
char* receiver = NULL;
return_value = message_is_long(argv[4]);
if(return_value) //message is too long
{
std::cout << "\nSorry, the message was longer than " << TCOIN_MSG_LENGTH_LIMIT << " characters. Please keep messages at or below this limit.\n\n";
return ERR_MAIN_MSG_TOO_LONG;
}
if(is_number(argv[2]))
{
amount = strtol100(argv[2]);
receiver = argv[3];
}
else
{
amount = strtol100(argv[3]);
receiver = argv[2];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "verbose");
if(!return_value) //send was successful
send_message(get_username().c_str(), receiver, argv[4], amount, "verbose");
}
}
else if(argc == 6)
{
if(!strcmp(argv[2], "-s"))
{ //argument count is 6 because of silent send with custom message included
int return_value;
long long int amount = 0;
char *receiver = NULL;
return_value = message_is_long(argv[5]);
if(return_value) //message is too long
{
std::cout << "\nSorry, the message was longer than " << TCOIN_MSG_LENGTH_LIMIT << " characters. Please keep messages at or below this limit.\n\n";
return ERR_MAIN_MSG_TOO_LONG;
}
if(is_number(argv[3]))
{
amount = strtol100(argv[3]);
receiver = argv[4];
}
else
{
amount = strtol100(argv[4]);
receiver = argv[3];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "silent");
if(!return_value) //send was successful
send_message(get_username().c_str(), receiver, argv[5], amount, "silent");
}
else
{
//too many command-line arguments were passed (6 args) (probably a message was intended)
std::cout << "\nSorry, too many command-line arguments were passed. The correct format is `tcoin send <username> <amount> \"<message>\"`.\n\n";
return ERR_MAIN_SEND_TOO_MANY_ARGS_MSG;
}
}
else if(argc < 4)
{
std::cout << "\nSorry, too few command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "send <username> <amount>`.\n\n";
return ERR_MAIN_SEND_TOO_FEW_ARGS;
}
else if(argc > 4)
{
std::cout << "\nSorry, too many command-line arguments were passed. The correct format is `" << PCOIN_BIN_PATH_W_SPACE << "send <username> <amount>`.\n\n";
return ERR_MAIN_SEND_TOO_MANY_ARGS;
}
else
{
int return_value;
long long int amount = 0;
char* receiver = NULL;
if(is_number(argv[2]))
{
amount = strtol100(argv[2]);
receiver = argv[3];
}
else
{
amount = strtol100(argv[3]);
receiver = argv[2];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "verbose");
if(!return_value) //send was successful
send_message(get_username().c_str(), receiver, "", amount, "verbose");
}
}
else if(!strcmp(argv[1], "silentsend") || !strcmp(argv[1], "-ss"))
{
if(argc==4)
{
int return_value;
long long int amount = 0;
char* receiver = NULL;
if(is_number(argv[2]))
{
amount = strtol100(argv[2]);
receiver = argv[3];
}
else
{
amount = strtol100(argv[3]);
receiver = argv[2];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "silent");
if(!return_value) //send was successful
send_message(get_username().c_str(), receiver, "", amount, "silent");
}
if(argc==5) //custom message included
{
int return_value;
long long int amount = 0;
char* receiver = NULL;
return_value = message_is_long(argv[4]);
if(return_value) //message is too long
{
std::cout << "\nSorry, the message was longer than " << TCOIN_MSG_LENGTH_LIMIT << " characters. Please keep messages at or below this limit.\n\n";
return ERR_MAIN_MSG_TOO_LONG;
}
if(is_number(argv[2]))
{
amount = strtol100(argv[2]);
receiver = argv[3];
}
else
{
amount = strtol100(argv[3]);
receiver = argv[2];
}
return_value = send(get_username().c_str(), receiver, amount, base_amount, "silent");
if(!return_value) //send was successful
send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2]), "silent");
}
else
return ERR_SILENTSEND;
}
else
{
std::cout << "\nSorry, an unknown command-line argument was received. `" << PCOIN_BIN_PATH_W_SPACE << "help` will print the help text.\n\n";
return ERR_UNKNOWN_ARG;
}
return 0;
}
|