aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
blob: b8f37427e8bd139cf65bfb446ae5adb1390078f0 (plain)
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
#include "../../plugins/Matrix.hpp"
#include "../../include/Storage.hpp"
#include <json/reader.h>
#include <json/writer.h>
#include <fcntl.h>
#include <unistd.h>

// TODO: Update avatar/display name when its changed in the room/globally.
// Send read receipt to server and receive notifications in /sync and show the notifications.
// Delete messages.
// Edit messages.
// Show images/videos inline.
// TODO: Verify if buffer of size 512 is enough for endpoints
// TODO: POST /_matrix/client/r0/rooms/{roomId}/read_markers after 5 seconds of receiving a message when the client is focused
// to mark messages as read
// When reaching top/bottom message, show older/newer messages.
// Remove older messages (outside screen) to save memory. Reload them when the selected body item is the top/bottom one.

namespace QuickMedia {
    Matrix::Matrix() : Plugin("matrix") {
        
    }

    PluginResult Matrix::get_cached_sync(BodyItems &result_items) {
        /*
        Path sync_cache_path = get_cache_dir().join(name).join("sync.json");
        Json::Value root;
        if(!read_file_as_json(sync_cache_path, root))
            return PluginResult::ERR;
        return sync_response_to_body_items(root, result_items);
        */
        (void)result_items;
        return PluginResult::OK;
    }

    PluginResult Matrix::sync(RoomSyncMessages &room_messages) {
        std::vector<CommandArg> additional_args = {
            { "-H", "Authorization: Bearer " + access_token },
            { "-m", "35" }
        };

        std::string server_response;
        // timeout=30000, filter=0. First sync should be without filter and timeout=0, then all other sync should be with timeout=30000 and filter=0.
        // GET https://glowers.club/_matrix/client/r0/user/%40dec05eba%3Aglowers.club/filter/0 first to check if the filter is available
        // and if lazy load members is available and get limit to use with https://glowers.club/_matrix/client/r0/rooms/!oSXkiqBKooDcZsmiGO%3Aglowers.club/
        // when first launching the client. This call to /rooms/ should be called before /sync/, when accessing a room. But only the first time
        // (for the session).

        // Note: the first sync call with always exclude since= (next_batch) because we want to receive the latest messages in a room,
        // which is important if we for example login to matrix after having not been online for several days and there are many new messages.
        // We should be shown the latest messages first and if the user wants to see older messages then they should scroll up.
        // Note: missed mentions are received in /sync and they will remain in new /sync unless we send a read receipt that we have read them.

        char url[512];
        if(next_batch.empty())
            snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?timeout=0", homeserver.c_str());
        else
            snprintf(url, sizeof(url), "%s/_matrix/client/r0/sync?timeout=30000&since=%s", homeserver.c_str(), next_batch.c_str());

        if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;
        
        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix sync response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        PluginResult result = sync_response_to_body_items(json_root, room_messages);
        if(result != PluginResult::OK)
            return result;

        const Json::Value &next_batch_json = json_root["next_batch"];
        if(next_batch_json.isString()) {
            next_batch = next_batch_json.asString();
            fprintf(stderr, "Matrix: next batch: %s\n", next_batch.c_str());
        } else {
            fprintf(stderr, "Matrix: missing next batch\n");
        }

        // TODO: Only create the first time sync is called?
        /*
        Path sync_cache_path = get_cache_dir().join(name);
        if(create_directory_recursive(sync_cache_path) == 0) {
            sync_cache_path.join("sync.json");
            if(!save_json_to_file_atomic(sync_cache_path, json_root)) {
                fprintf(stderr, "Warning: failed to save sync response to %s\n", sync_cache_path.data.c_str());
            }
        } else {
            fprintf(stderr, "Warning: failed to create directory: %s\n", sync_cache_path.data.c_str());
        }
        */

        return PluginResult::OK;
    }

    PluginResult Matrix::get_joined_rooms(BodyItems &result_items) {
        std::vector<CommandArg> additional_args = {
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/joined_rooms", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;
        
        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix joined rooms response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &joined_rooms_json = json_root["joined_rooms"];
        if(!joined_rooms_json.isArray())
            return PluginResult::ERR;

        for(const Json::Value &room_id_json : joined_rooms_json) {
            if(!room_id_json.isString())
                continue;

            std::string room_id_str = room_id_json.asString();
            std::string room_name;
            std::string avatar_url;

            auto room_it = room_data_by_id.find(room_id_str);
            if(room_it == room_data_by_id.end()) {
                auto room_data = std::make_unique<RoomData>();
                room_data->id = room_id_json.asString();
                room_data_by_id.insert(std::make_pair(room_id_str, std::move(room_data)));
                room_name = room_id_str;
                fprintf(stderr, "Missing room %s from /sync, adding in joined_rooms\n", room_id_str.c_str());
            } else {
                room_name = room_it->second->name;
                if(room_name.empty())
                    room_name = room_id_str;
                avatar_url = room_it->second->avatar_url;
            }

            auto body_item = BodyItem::create(std::move(room_name));
            body_item->url = room_id_str;
            body_item->thumbnail_url = std::move(avatar_url);
            result_items.push_back(std::move(body_item));
        }

        return PluginResult::OK;
    }

    static void room_messages_to_body_items(RoomData *room_data, std::shared_ptr<Message> *messages, size_t num_messages, BodyItems &result_items) {
        for(size_t i = 0; i < num_messages; ++i) {
            const UserInfo &user_info = room_data->user_info[messages[i]->user_id];
            auto body_item = BodyItem::create("");
            body_item->set_author(user_info.display_name);
            body_item->set_description(messages[i]->body);
            if(!messages[i]->thumbnail_url.empty())
                body_item->thumbnail_url = messages[i]->thumbnail_url;
            else if(!messages[i]->url.empty() && messages[i]->type == MessageType::IMAGE)
                body_item->thumbnail_url = messages[i]->url;
            else
                body_item->thumbnail_url = user_info.avatar_url;
            // TODO: Show image thumbnail inline instead of url to image and showing it as the thumbnail of the body item
            body_item->url = messages[i]->url;
            body_item->author_color = user_info.display_name_color;
            body_item->userdata = (void*)messages[i].get(); // Note: messages[i] has to be valid as long as body_item is used!
            result_items.push_back(std::move(body_item));
        }
    }

    // TODO: Merge common code with |get_new_room_messages|
    PluginResult Matrix::get_all_synced_room_messages(const std::string &room_id, BodyItems &result_items) {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return PluginResult::ERR;
        }

        if(!room_it->second->initial_fetch_finished) {
            PluginResult result = get_previous_room_messages(room_id, room_it->second.get());
            if(result == PluginResult::OK) {
                room_it->second->initial_fetch_finished = true;
            } else {
                fprintf(stderr, "Initial sync failed for room: %s\n", room_id.c_str());
                return result;
            }
        }

        room_messages_to_body_items(room_it->second.get(), room_it->second->messages.data(), room_it->second->messages.size(), result_items);
        room_it->second->last_read_index = room_it->second->messages.size();
        return PluginResult::OK;
    }

    PluginResult Matrix::get_new_room_messages(const std::string &room_id, BodyItems &result_items) {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return PluginResult::ERR;
        }

        if(!room_it->second->initial_fetch_finished) {
            PluginResult result = get_previous_room_messages(room_id, room_it->second.get());
            if(result == PluginResult::OK) {
                room_it->second->initial_fetch_finished = true;
            } else {
                fprintf(stderr, "Initial sync failed for room: %s\n", room_id.c_str());
                return result;
            }
        }

        size_t num_new_messages = room_it->second->messages.size() - room_it->second->last_read_index;
        room_messages_to_body_items(room_it->second.get(), room_it->second->messages.data() + room_it->second->last_read_index, num_new_messages, result_items);
        room_it->second->last_read_index = room_it->second->messages.size();
        return PluginResult::OK;
    }

    PluginResult Matrix::get_previous_room_messages(const std::string &room_id, BodyItems &result_items) {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return PluginResult::ERR;
        }
        
        size_t num_messages_before = room_it->second->messages.size();
        PluginResult result = get_previous_room_messages(room_id, room_it->second.get());
        if(result != PluginResult::OK)
            return result;

        size_t num_messages_after = room_it->second->messages.size();
        size_t num_new_messages = num_messages_after - num_messages_before;
        room_messages_to_body_items(room_it->second.get(), room_it->second->messages.data(), num_new_messages, result_items);
        return PluginResult::OK;
    }

    PluginResult Matrix::sync_response_to_body_items(const Json::Value &root, RoomSyncMessages &room_messages) {
        if(!root.isObject())
            return PluginResult::ERR;

        const Json::Value &rooms_json = root["rooms"];
        if(!rooms_json.isObject())
            return PluginResult::OK;

        const Json::Value &join_json = rooms_json["join"];
        if(!join_json.isObject())
            return PluginResult::OK;

        for(Json::Value::const_iterator it = join_json.begin(); it != join_json.end(); ++it) {
            if(!it->isObject())
                continue;

            Json::Value room_id = it.key();
            if(!room_id.isString())
                continue;

            std::string room_id_str = room_id.asString();

            auto room_it = room_data_by_id.find(room_id_str);
            if(room_it == room_data_by_id.end()) {
                auto room_data = std::make_unique<RoomData>();
                room_data->id = room_id_str;
                room_data_by_id.insert(std::make_pair(room_id_str, std::move(room_data)));
                room_it = room_data_by_id.find(room_id_str); // TODO: Get iterator from above insert
            }

            const Json::Value &state_json = (*it)["state"];
            if(!state_json.isObject())
                continue;

            const Json::Value &events_json = state_json["events"];
            events_add_user_info(events_json, room_it->second.get());
            events_set_room_name(events_json, room_it->second.get());
        }

        for(Json::Value::const_iterator it = join_json.begin(); it != join_json.end(); ++it) {
            if(!it->isObject())
                continue;

            Json::Value room_id = it.key();
            if(!room_id.isString())
                continue;

            std::string room_id_str = room_id.asString();

            auto room_it = room_data_by_id.find(room_id_str);
            if(room_it == room_data_by_id.end()) {
                auto room_data = std::make_unique<RoomData>();
                room_data->id = room_id_str;
                room_data_by_id.insert(std::make_pair(room_id_str, std::move(room_data)));
                room_it = room_data_by_id.find(room_id_str); // TODO: Get iterator from above insert
            }

            const Json::Value &timeline_json = (*it)["timeline"];
            if(!timeline_json.isObject())
                continue;

            if(room_it->second->prev_batch.empty()) {
                // This may be non-existent if this is the first event in the room
                const Json::Value &prev_batch_json = timeline_json["prev_batch"];
                if(prev_batch_json.isString())
                    room_it->second->prev_batch = prev_batch_json.asString();
            }

            // TODO: Is there no better way to check for notifications? this is not robust...
            bool has_unread_notifications = false;
            const Json::Value &unread_notification_json = (*it)["unread_notifications"];
            if(unread_notification_json.isObject()) {
                const Json::Value &highlight_count_json = unread_notification_json["highlight_count"];
                if(highlight_count_json.isNumeric() && highlight_count_json.asInt64() > 0)
                    has_unread_notifications = true;
            }

            const Json::Value &events_json = timeline_json["events"];
            events_add_user_info(events_json, room_it->second.get());
            events_add_messages(events_json, room_it->second.get(), MessageDirection::AFTER, &room_messages, has_unread_notifications);
            events_set_room_name(events_json, room_it->second.get());
        }

        return PluginResult::OK;
    }

    static sf::Color user_id_to_color(const std::string &user_id) {
        uint32_t color = 2166136261;
        for(unsigned char c : user_id) {
            color = (color * 16777619) ^ c;
        }
        sf::Color result = (sf::Color)color;
        result.r = 64 + std::max(0, (int)result.r - 64);
        result.g = 64 + std::max(0, (int)result.g - 64);
        result.b = 64 + std::max(0, (int)result.b - 64);
        result.a = 255;
        return result;
    }

    void Matrix::events_add_user_info(const Json::Value &events_json, RoomData *room_data) {
        if(!events_json.isArray())
            return;

        for(const Json::Value &event_item_json : events_json) {
            if(!event_item_json.isObject())
                continue;

            const Json::Value &type_json = event_item_json["type"];
            if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.member") != 0)
                continue;

            const Json::Value &sender_json = event_item_json["sender"];
            if(!sender_json.isString())
                continue;
            
            const Json::Value &content_json = event_item_json["content"];
            if(!content_json.isObject())
                continue;

            const Json::Value &membership_json = content_json["membership"];
            if(!membership_json.isString() || strcmp(membership_json.asCString(), "join") != 0)
                continue;

            const Json::Value &avatar_url_json = content_json["avatar_url"];
            if(!avatar_url_json.isString())
                continue;

            const Json::Value &display_name_json = content_json["displayname"];
            if(!display_name_json.isString())
                continue;
            
            std::string sender_json_str = sender_json.asString();
            auto user_it = room_data->user_info_by_user_id.find(sender_json_str);
            if(user_it != room_data->user_info_by_user_id.end())
                continue;

            UserInfo user_info;
            user_info.user_id = sender_json_str;
            user_info.avatar_url = avatar_url_json.asString();
            if(strncmp(user_info.avatar_url.c_str(), "mxc://", 6) == 0)
                user_info.avatar_url.erase(user_info.avatar_url.begin(), user_info.avatar_url.begin() + 6);
            // TODO: What if the user hasn't selected an avatar?
            user_info.avatar_url = homeserver + "/_matrix/media/r0/thumbnail/" + user_info.avatar_url + "?width=32&height=32&method=crop";
            user_info.display_name = display_name_json.asString();
            user_info.display_name_color = user_id_to_color(sender_json_str);
            room_data->user_info.push_back(user_info);
            room_data->user_info_by_user_id.insert(std::make_pair(sender_json_str, room_data->user_info.size() - 1));
        }
    }

    static std::string message_content_extract_thumbnail_url(const Json::Value &content_json, const std::string &homeserver) {
        const Json::Value &info_json = content_json["info"];
        if(info_json.isObject()) {
            const Json::Value &thumbnail_url_json = info_json["thumbnail_url"];
            if(thumbnail_url_json.isString()) {
                std::string thumbnail_str = thumbnail_url_json.asString();
                if(strncmp(thumbnail_str.c_str(), "mxc://", 6) == 0) {
                    thumbnail_str.erase(thumbnail_str.begin(), thumbnail_str.begin() + 6);
                    return homeserver + "/_matrix/media/r0/download/" + std::move(thumbnail_str);
                }
            }
        }
        return "";
    }

    // TODO: Is this really the proper way to check for username mentions?
    static bool is_username_seperating_character(char c) {
        switch(c) {
            case ' ':
            case '\n':
            case '\t':
            case '\v':
            case '.':
            case ',':
            case '@':
            case ':':
            case '?':
            case '!':
            case '<':
            case '>':
            case '\0':
                return true;
            default:
                return false;
        }
        return false;
    }

    // TODO: Do not show notification if mention is a reply to somebody else that replies to me? also dont show notification everytime a mention is edited
    static bool message_contains_user_mention(const std::string &msg, const std::string &username) {
        if(msg.empty())
            return false;

        size_t index = 0;
        while(index < msg.size()) {
            size_t found_index = msg.find(username, index);
            if(found_index == std::string::npos)
                return false;

            char prev_char = ' ';
            if(found_index > 0)
                prev_char = msg[found_index - 1];

            char next_char = '\0';
            if(found_index + username.size() < msg.size() - 1)
                next_char = msg[found_index + username.size()];

            if(is_username_seperating_character(prev_char) && is_username_seperating_character(next_char))
                return true;

            index += username.size();
        }

        return false;
    }

    void Matrix::events_add_messages(const Json::Value &events_json, RoomData *room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications) {
        if(!events_json.isArray())
            return;

        std::vector<std::shared_ptr<Message>> *room_sync_messages = nullptr;
        if(room_messages)
            room_sync_messages = &(*room_messages)[room_data];

        std::vector<std::shared_ptr<Message>> new_messages;

        for(const Json::Value &event_item_json : events_json) {
            if(!event_item_json.isObject())
                continue;

            const Json::Value &type_json = event_item_json["type"];
            if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.message") != 0)
                continue;

            const Json::Value &sender_json = event_item_json["sender"];
            if(!sender_json.isString())
                continue;

            std::string sender_json_str = sender_json.asString();

            const Json::Value &event_id_json = event_item_json["event_id"];
            if(!event_id_json.isString())
                continue;

            std::string event_id_str = event_id_json.asString();
            
            const Json::Value &content_json = event_item_json["content"];
            if(!content_json.isObject())
                continue;

            const Json::Value &content_type = content_json["msgtype"];
            if(!content_type.isString())
                continue;

            auto user_it = room_data->user_info_by_user_id.find(sender_json_str);
            if(user_it == room_data->user_info_by_user_id.end()) {
                // Note: this is important because otherwise replying and such is broken
                fprintf(stderr, "Warning: skipping unknown user: %s\n", sender_json_str.c_str());
                continue;
            }

            const Json::Value &body_json = content_json["body"];
            if(!body_json.isString())
                continue;

            time_t timestamp = 0;
            const Json::Value &origin_server_ts = event_item_json["origin_server_ts"];
            if(origin_server_ts.isNumeric())
                timestamp = origin_server_ts.asInt64();

            std::string replaces_event_id;
            const Json::Value &relates_to_json = content_json["m.relates_to"];
            if(relates_to_json.isObject()) {
                const Json::Value &replaces_event_id_json = relates_to_json["event_id"];
                const Json::Value &rel_type_json = relates_to_json["rel_type"];
                if(replaces_event_id_json.isString() && rel_type_json.isString() && strcmp(rel_type_json.asCString(), "m.replace") == 0)
                    replaces_event_id = replaces_event_id_json.asString();
            }

            auto message = std::make_shared<Message>();
            std::string prefix;

            // TODO: Also show joins, leave, invites, bans, kicks, mutes, etc

            if(strcmp(content_type.asCString(), "m.text") == 0) {
                message->type = MessageType::TEXT;
            } else if(strcmp(content_type.asCString(), "m.image") == 0) {
                const Json::Value &url_json = content_json["url"];
                if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0)
                    continue;

                message->url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6);
                message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver);
                message->type = MessageType::IMAGE;
            } else if(strcmp(content_type.asCString(), "m.video") == 0) {
                const Json::Value &url_json = content_json["url"];
                if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0)
                    continue;

                message->url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6);
                message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver);
                message->type = MessageType::VIDEO;
            } else if(strcmp(content_type.asCString(), "m.audio") == 0) {
                const Json::Value &url_json = content_json["url"];
                if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0)
                    continue;

                message->url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6);
                message->type = MessageType::AUDIO;
            } else if(strcmp(content_type.asCString(), "m.file") == 0) {
                const Json::Value &url_json = content_json["url"];
                if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0)
                    continue;

                message->url = homeserver + "/_matrix/media/r0/download/" + url_json.asString().substr(6);
                message->type = MessageType::FILE;
            } else if(strcmp(content_type.asCString(), "m.emote") == 0) { // this is a /me message, TODO: show /me messages differently
                message->type = MessageType::TEXT;
                prefix = "*" + room_data->user_info[user_it->second].display_name + "* ";
            } else if(strcmp(content_type.asCString(), "m.notice") == 0) { // TODO: show notices differently
                message->type = MessageType::TEXT;
                prefix = "* NOTICE * ";
            } else if(strcmp(content_type.asCString(), "m.location") == 0) { // TODO: show locations differently
                const Json::Value &geo_uri_json = content_json["geo_uri"];
                if(geo_uri_json.isString())
                    prefix = geo_uri_json.asString() + " | ";

                message->type = MessageType::TEXT;
                message->thumbnail_url = message_content_extract_thumbnail_url(content_json, homeserver);
            } else {
                continue;
            }

            message->user_id = user_it->second;
            message->event_id = event_id_str;
            message->body = prefix + body_json.asString();
            message->replaces_event_id = std::move(replaces_event_id);
            // TODO: Is @room ok? shouldn't we also check if the user has permission to do @room? (only when notifications are limited to @mentions)
            if(has_unread_notifications && !username.empty())
                message->mentions_me = message_contains_user_mention(message->body, username) || message_contains_user_mention(message->body, "@room");
            message->timestamp = timestamp;
            new_messages.push_back(message);
            room_data->message_by_event_id[event_id_str] = message;
            if(room_sync_messages)
                room_sync_messages->push_back(message);
        }

        // TODO: Loop and std::move instead? doesn't insert create copies?
        if(message_dir == MessageDirection::BEFORE) {
            room_data->messages.insert(room_data->messages.begin(), new_messages.rbegin(), new_messages.rend());
            if(room_data->last_read_index != 0)
                room_data->last_read_index += new_messages.size();
        } else if(message_dir == MessageDirection::AFTER) {
            room_data->messages.insert(room_data->messages.end(), new_messages.begin(), new_messages.end());
        }
    }

    // Returns empty string on error
    static std::string extract_user_name_from_user_id(const std::string &user_id) {
        size_t index = user_id.find(':');
        if(index == std::string::npos || index == 0 || user_id.empty() || user_id[0] != '@')
            return "";
        return user_id.substr(1, index - 1);
    }

    void Matrix::events_set_room_name(const Json::Value &events_json, RoomData *room_data) {
        if(!events_json.isArray())
            return;

        for(const Json::Value &event_item_json : events_json) {
            if(!event_item_json.isObject())
                continue;

            const Json::Value &type_json = event_item_json["type"];
            if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.name") != 0)
                continue;
            
            const Json::Value &content_json = event_item_json["content"];
            if(!content_json.isObject())
                continue;

            const Json::Value &name_json = content_json["name"];
            if(!name_json.isString())
                continue;

            room_data->name = name_json.asString();
        }

        for(const Json::Value &event_item_json : events_json) {
            if(!event_item_json.isObject())
                continue;

            const Json::Value &type_json = event_item_json["type"];
            if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.create") != 0)
                continue;
            
            const Json::Value &content_json = event_item_json["content"];
            if(!content_json.isObject())
                continue;

            const Json::Value &creator_json = content_json["creator"];
            if(!creator_json.isString())
                continue;

            if(room_data->name.empty())
                room_data->name = extract_user_name_from_user_id(creator_json.asString());

            if(room_data->avatar_url.empty()) {
                auto user_it = room_data->user_info_by_user_id.find(creator_json.asString());
                if(user_it != room_data->user_info_by_user_id.end())
                    room_data->avatar_url = room_data->user_info[user_it->second].avatar_url;
            }
        }

        for(const Json::Value &event_item_json : events_json) {
            if(!event_item_json.isObject())
                continue;

            const Json::Value &type_json = event_item_json["type"];
            if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.avatar") != 0)
                continue;
            
            const Json::Value &content_json = event_item_json["content"];
            if(!content_json.isObject())
                continue;

            const Json::Value &url_json = content_json["url"];
            if(!url_json.isString() || strncmp(url_json.asCString(), "mxc://", 6) != 0)
                continue;

            std::string url_json_str = url_json.asCString() + 6;
            room_data->avatar_url = homeserver + "/_matrix/media/r0/thumbnail/" + std::move(url_json_str) + "?width=32&height=32&method=crop";
        }
    }

    PluginResult Matrix::get_previous_room_messages(const std::string &room_id, RoomData *room_data) {
        std::string from = room_data->prev_batch;
        if(from.empty()) {
            fprintf(stderr, "Info: missing previous batch for room: %s, using /sync next batch\n", room_id.c_str());
            from = next_batch;
            if(from.empty()) {
                fprintf(stderr, "Error: missing next batch!\n");
                return PluginResult::OK;
            }
        }

        Json::Value request_data(Json::objectValue);
        request_data["lazy_load_members"] = true;

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string filter = url_param_encode(Json::writeString(builder, std::move(request_data)));

        char url[512];
        snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/messages?from=%s&limit=20&dir=b&filter=%s", homeserver.c_str(), room_id.c_str(), from.c_str(), filter.c_str());
        fprintf(stderr, "load initial room data, url: |%s|\n", url);

        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;
        
        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix /rooms/<room_id>/messages/ response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &state_json = json_root["state"];
        events_add_user_info(state_json, room_data);
        events_set_room_name(state_json, room_data);

        const Json::Value &chunk_json = json_root["chunk"];
        events_add_messages(chunk_json, room_data, MessageDirection::BEFORE, nullptr, false);

        const Json::Value &end_json = json_root["end"];
        if(!end_json.isString()) {
            fprintf(stderr, "Warning: matrix messages response is missing 'end', this could happen if we received the very first messages in the room\n");
            return PluginResult::OK;
        }

        room_data->prev_batch = end_json.asString();
        return PluginResult::OK;
    }

    SearchResult Matrix::search(const std::string&, BodyItems&) {
        return SearchResult::OK;
    }

    static bool generate_random_characters(char *buffer, int buffer_size) {
        int fd = open("/dev/urandom", O_RDONLY);
        if(fd == -1) {
            perror("/dev/urandom");
            return false;
        }

        if(read(fd, buffer, buffer_size) < buffer_size) {
            fprintf(stderr, "Failed to read %d bytes from /dev/urandom\n", buffer_size);
            close(fd);
            return false;
        }

        close(fd);
        return true;
    }

    static std::string random_characters_to_readable_string(const char *buffer, int buffer_size) {
        std::ostringstream result;
        result << std::hex;
        for(int i = 0; i < buffer_size; ++i)
            result << (int)(unsigned char)buffer[i];
        return result.str();
    }

    static const char* content_type_to_message_type(ContentType content_type) {
        if(is_content_type_video(content_type))
            return "m.video";
        else if(is_content_type_audio(content_type))
            return "m.audio";
        else if(is_content_type_image(content_type))
            return "m.image";
        else
            return "m.file";
    }

    PluginResult Matrix::post_message(const std::string &room_id, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info) {
        char random_characters[18];
        if(!generate_random_characters(random_characters, sizeof(random_characters)))
            return PluginResult::ERR;

        std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));

        std::string formatted_body;
        bool contains_formatted_text = false;
        if(!file_info) {
            int line = 0;
            string_split(body, '\n', [&formatted_body, &contains_formatted_text, &line](const char *str, size_t size){
                if(line > 0)
                    formatted_body += "<br/>";
                if(size > 0 && str[0] == '>') {
                    std::string line(str, size);
                    html_escape_sequences(line);
                    formatted_body += "<font color=\"#789922\">";
                    formatted_body += line;
                    formatted_body += "</font>";
                    contains_formatted_text = true;
                } else {
                    formatted_body.append(str, size);
                }
                ++line;
                return true;
            });
        }

        Json::Value request_data(Json::objectValue);
        request_data["msgtype"] = (file_info ? content_type_to_message_type(file_info->content_type) : "m.text");
        request_data["body"] = body;
        if(contains_formatted_text) {
            request_data["format"] = "org.matrix.custom.html";
            request_data["formatted_body"] = std::move(formatted_body);
        }

        // TODO: Add hashblur?
        if(file_info) {
            Json::Value info_json(Json::objectValue);
            info_json["size"] = file_info->file_size;
            info_json["mimetype"] = content_type_to_string(file_info->content_type);
            if(file_info->dimensions) {
                info_json["w"] = file_info->dimensions->width;
                info_json["h"] = file_info->dimensions->height;
            }
            if(file_info->duration_seconds) {
                // TODO: Check for overflow?
                info_json["duration"] = (int)file_info->duration_seconds.value() * 1000;
            }

            if(thumbnail_info) {
                Json::Value thumbnail_info_json(Json::objectValue);
                thumbnail_info_json["size"] = thumbnail_info->file_size;
                thumbnail_info_json["mimetype"] = content_type_to_string(thumbnail_info->content_type);
                if(thumbnail_info->dimensions) {
                    thumbnail_info_json["w"] = thumbnail_info->dimensions->width;
                    thumbnail_info_json["h"] = thumbnail_info->dimensions->height;
                }

                info_json["thumbnail_url"] = thumbnail_info->content_uri;
                info_json["info"] = std::move(thumbnail_info_json);
            }

            request_data["info"] = std::move(info_json);
            request_data["url"] = file_info->content_uri;
        }

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "-H", "Authorization: Bearer " + access_token },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) }
        };

        char request_url[512];
        snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str());
        fprintf(stderr, "Post message to |%s|\n", request_url);

        std::string server_response;
        if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &event_id_json = json_root["event_id"];
        if(!event_id_json.isString())
            return PluginResult::ERR;

        fprintf(stderr, "Matrix post message, response event id: %s\n", event_id_json.asCString());
        return PluginResult::OK;
    }

    static std::string remove_reply_formatting(const std::string &str) {
        if(strncmp(str.c_str(), "> <@", 4) == 0) {
            size_t index = str.find("> ", 4);
            if(index != std::string::npos) {
                size_t msg_begin = str.find("\n\n", index + 2);
                if(msg_begin != std::string::npos)
                    return str.substr(msg_begin + 2);
            }
        }
        return str;
    }

    static std::string block_quote(const std::string &str) {
        std::string result = "> ";
        for(char c : str) {
            if(c == '>') {
                result += "\\>";
            } else if(c == '\n') {
                result += "\n> ";
            } else {
                result += c;
            }
        }
        return result;
    }

    static std::string create_body_for_message_reply(const RoomData *room_data, const Message *message, const std::string &body) {
        std::string related_to_body;
        switch(message->type) {
            case MessageType::TEXT: {
                if(!message->replaces_event_id.empty() && strncmp(message->body.c_str(), " * ", 3) == 0)
                    related_to_body = remove_reply_formatting(message->body.substr(3));
                else
                    related_to_body = remove_reply_formatting(message->body);
                break;
            }
            case MessageType::IMAGE:
                related_to_body = "sent an image";
                break;
            case MessageType::VIDEO:
                related_to_body = "sent a video";
                break;
            case MessageType::AUDIO:
                related_to_body = "sent an audio file";
                break;
            case MessageType::FILE:
                related_to_body = "sent a file";
                break;
        }
        return block_quote("<" + room_data->user_info[message->user_id].user_id + "> " + std::move(related_to_body)) + "\n\n" + body;
    }   

    // TODO: Add formatted_body just like element does with <mx-reply><blockquote... and also support greentext with that
    PluginResult Matrix::post_reply(const std::string &room_id, const std::string &body, void *relates_to) {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return PluginResult::ERR;
        }

        Message *relates_to_message_raw = (Message*)relates_to;
        std::shared_ptr<Message> relates_to_message_shared = room_it->second->message_by_event_id[relates_to_message_raw->event_id];
        std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room_it->second.get(), relates_to_message_shared);
        if(!relates_to_message_original) {
            fprintf(stderr, "Failed to get the original message for message with event id: %s\n", relates_to_message_raw->event_id.c_str());
            return PluginResult::ERR;
        }

        char random_characters[18];
        if(!generate_random_characters(random_characters, sizeof(random_characters)))
            return PluginResult::ERR;

        std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));

        Json::Value in_reply_to_json(Json::objectValue);
        in_reply_to_json["event_id"] = relates_to_message_original->event_id;

        Json::Value relates_to_json(Json::objectValue);
        relates_to_json["m.in_reply_to"] = std::move(in_reply_to_json);

        Json::Value request_data(Json::objectValue);
        request_data["msgtype"] = "m.text"; // TODO: Allow image reply? element doesn't do that but we could!
        request_data["body"] = create_body_for_message_reply(room_it->second.get(), relates_to_message_raw, body); // Yes, the reply is to the edited message but the event_id reference is to the original message...
        request_data["m.relates_to"] = std::move(relates_to_json);

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "-H", "Authorization: Bearer " + access_token },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) }
        };

        char request_url[512];
        snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str());
        fprintf(stderr, "Post message to |%s|\n", request_url);

        std::string server_response;
        if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &event_id_json = json_root["event_id"];
        if(!event_id_json.isString())
            return PluginResult::ERR;

        fprintf(stderr, "Matrix post reply, response event id: %s\n", event_id_json.asCString());
        return PluginResult::OK;
    }

    PluginResult Matrix::post_edit(const std::string &room_id, const std::string &body, void *relates_to) {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return PluginResult::ERR;
        }

        Message *relates_to_message_raw = (Message*)relates_to;
        std::shared_ptr<Message> relates_to_message_shared = room_it->second->message_by_event_id[relates_to_message_raw->event_id];
        std::shared_ptr<Message> relates_to_message_original = get_edited_message_original_message(room_it->second.get(), relates_to_message_shared);
        if(!relates_to_message_original) {
            fprintf(stderr, "Failed to get the original message for message with event id: %s\n", relates_to_message_raw->event_id.c_str());
            return PluginResult::ERR;
        }

        char random_characters[18];
        if(!generate_random_characters(random_characters, sizeof(random_characters)))
            return PluginResult::ERR;

        std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));

        std::string formatted_body;
        bool contains_formatted_text = false;
        int line = 0;
        string_split(body, '\n', [&formatted_body, &contains_formatted_text, &line](const char *str, size_t size){
            if(line > 0)
                formatted_body += "<br/>";
            if(size > 0 && str[0] == '>') {
                std::string line(str, size);
                html_escape_sequences(line);
                formatted_body += "<font color=\"#789922\">";
                formatted_body += line;
                formatted_body += "</font>";
                contains_formatted_text = true;
            } else {
                formatted_body.append(str, size);
            }
            ++line;
            return true;
        });

        Json::Value new_content_json(Json::objectValue);
        new_content_json["msgtype"] = "m.text";
        new_content_json["body"] = body;
        if(contains_formatted_text) {
            new_content_json["format"] = "org.matrix.custom.html";
            new_content_json["formatted_body"] = formatted_body;
        }

        Json::Value relates_to_json(Json::objectValue);
        relates_to_json["event_id"] = relates_to_message_original->event_id;
        relates_to_json["rel_type"] = "m.replace";

        Json::Value request_data(Json::objectValue);
        request_data["msgtype"] = "m.text"; // TODO: Allow other types of edits
        request_data["body"] = " * " + body;
        if(contains_formatted_text) {
            request_data["format"] = "org.matrix.custom.html";
            request_data["formatted_body"] = " * " + formatted_body;
        }
        request_data["m.new_content"] = std::move(new_content_json);
        request_data["m.relates_to"] = std::move(relates_to_json);

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "-H", "Authorization: Bearer " + access_token },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) }
        };

        char request_url[512];
        snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str());
        fprintf(stderr, "Post message to |%s|\n", request_url);

        std::string server_response;
        if(download_to_string(request_url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix post message response parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &event_id_json = json_root["event_id"];
        if(!event_id_json.isString())
            return PluginResult::ERR;

        fprintf(stderr, "Matrix post edit, response event id: %s\n", event_id_json.asCString());
        return PluginResult::OK;
    }

    // TODO: Right now this recursively calls /rooms/<room_id>/context/<event_id> and trusts server to not make it recursive. To make this robust, check iteration count and do not trust server.
    // TODO: Optimize?
    std::shared_ptr<Message> Matrix::get_edited_message_original_message(RoomData *room_data, std::shared_ptr<Message> message) {
        if(message->replaces_event_id.empty())
            return message;

        auto message_it = room_data->message_by_event_id.find(message->replaces_event_id);
        if(message_it == room_data->message_by_event_id.end()) {
            Json::Value request_data(Json::objectValue);
            request_data["lazy_load_members"] = true;

            Json::StreamWriterBuilder builder;
            builder["commentStyle"] = "None";
            builder["indentation"] = "";

            std::vector<CommandArg> additional_args = {
                { "-H", "Authorization: Bearer " + access_token }
            };

            std::string filter = url_param_encode(Json::writeString(builder, std::move(request_data)));

            char url[512];
            snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/context/%s?limit=0&filter=%s", homeserver.c_str(), room_data->id.c_str(), message->event_id.c_str(), filter.c_str());
            fprintf(stderr, "get message context, url: |%s|\n", url);

            std::string server_response;
            if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
                return nullptr;

            if(server_response.empty())
                return nullptr;
            
            Json::Value json_root;
            Json::CharReaderBuilder json_builder;
            std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
            std::string json_errors;
            if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
                fprintf(stderr, "Matrix /rooms/<room_id>/context/<event_id> response parse error: %s\n", json_errors.c_str());
                return nullptr;
            }

            if(!json_root.isObject())
                return nullptr;

            const Json::Value &event_json = json_root["event"];
            if(!event_json.isObject())
                return nullptr;

            const Json::Value &event_id_json = event_json["event_id"];
            if(!event_id_json.isString())
                return nullptr;

            const Json::Value &content_json = event_json["content"];
            if(!content_json.isObject())
                return nullptr;

            const Json::Value &body_json = content_json["body"];
            if(!body_json.isString())
                return nullptr;

            std::string replaces_event_id;
            const Json::Value &relates_to_json = content_json["m.relates_to"];
            if(relates_to_json.isObject()) {
                const Json::Value &event_id_json = relates_to_json["event_id"];
                const Json::Value &rel_type_json = relates_to_json["rel_type"];
                if(event_id_json.isString() && rel_type_json.isString() && strcmp(rel_type_json.asCString(), "m.replace") == 0)
                    replaces_event_id = event_id_json.asString();
            }

            const Json::Value &content_type = content_json["msgtype"];
            if(!content_type.isString())
                return nullptr;

            auto new_message = std::make_shared<Message>();
            new_message->user_id = -1;
            new_message->event_id = event_id_json.asString();
            new_message->replaces_event_id = std::move(replaces_event_id);
            if(strcmp(content_type.asCString(), "m.text") == 0) {
                new_message->type = MessageType::TEXT;
            } else if(strcmp(content_type.asCString(), "m.image") == 0) {
                new_message->type = MessageType::IMAGE;
            } else if(strcmp(content_type.asCString(), "m.video") == 0) {
                new_message->type = MessageType::VIDEO;
            } else if(strcmp(content_type.asCString(), "m.audio") == 0) {
                new_message->type = MessageType::AUDIO;
            } else if(strcmp(content_type.asCString(), "m.file") == 0) {
                new_message->type = MessageType::FILE;
            } else {
                return nullptr;
            }

            return get_edited_message_original_message(room_data, std::move(new_message));
        } else {
            return get_edited_message_original_message(room_data, message_it->second);
        }
    }

    // Returns empty string on error
    static const char* file_get_filename(const std::string &filepath) {
        size_t index = filepath.rfind('/');
        if(index == std::string::npos)
            return "";
        const char *filename = filepath.c_str() + index + 1;
        if(filename[0] == '\0')
            return "";
        return filename;
    }

    PluginResult Matrix::post_file(const std::string &room_id, const std::string &filepath, std::string &err_msg) {
        UploadInfo file_info;
        UploadInfo thumbnail_info;
        PluginResult upload_file_result = upload_file(room_id, filepath, file_info, thumbnail_info, err_msg);
        if(upload_file_result != PluginResult::OK)
            return upload_file_result;

        std::optional<UploadInfo> file_info_opt = std::move(file_info);
        std::optional<UploadInfo> thumbnail_info_opt;
        if(!thumbnail_info.content_uri.empty())
            thumbnail_info_opt = std::move(thumbnail_info);

        const char *filename = file_get_filename(filepath);
        return post_message(room_id, filename, file_info_opt, thumbnail_info_opt);
    }

    PluginResult Matrix::upload_file(const std::string &room_id, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg) {
        FileAnalyzer file_analyzer;
        if(!file_analyzer.load_file(filepath.c_str())) {
            err_msg = "Failed to load " + filepath;
            return PluginResult::ERR;
        }

        file_info.content_type = file_analyzer.get_content_type();
        file_info.file_size = file_analyzer.get_file_size();
        file_info.dimensions = file_analyzer.get_dimensions();
        file_info.duration_seconds = file_analyzer.get_duration_seconds();

        int upload_limit;
        PluginResult config_result = get_config(&upload_limit);
        if(config_result != PluginResult::OK) {
            err_msg = "Failed to get file size limit from server";
            return config_result;
        }

        // Checking for sane file size limit client side, to prevent loading a huge file and crashing
        if(file_analyzer.get_file_size() > 300 * 1024 * 1024) { // 300mb
            err_msg = "File is too large! client-side limit is set to 300mb";
            return PluginResult::ERR;
        }

        if((int)file_analyzer.get_file_size() > upload_limit) {
            err_msg = "File is too large! max upload size on your homeserver is " + std::to_string(upload_limit) + " bytes, the file you tried to upload is " + std::to_string(file_analyzer.get_file_size()) + " bytes";
            return PluginResult::ERR;
        }

        if(is_content_type_video(file_analyzer.get_content_type())) {
            // TODO: Also upload thumbnail for images. Take into consideration below upload_file, we dont want to upload thumbnail of thumbnail
            char tmp_filename[] = "/tmp/quickmedia_video_frame_XXXXXX";
            int tmp_file = mkstemp(tmp_filename);
            if(tmp_file != -1) {
                if(video_get_first_frame(filepath.c_str(), tmp_filename)) {
                    UploadInfo upload_info_ignored; // Ignore because it wont be set anyways. Thumbnails dont have thumbnails.
                    PluginResult upload_thumbnail_result = upload_file(room_id, tmp_filename, thumbnail_info, upload_info_ignored, err_msg);
                    if(upload_thumbnail_result != PluginResult::OK) {
                        close(tmp_file);
                        remove(tmp_filename);
                        return upload_thumbnail_result;
                    }
                } else {
                    fprintf(stderr, "Failed to get first frame of video, ignoring thumbnail...\n");
                }
                close(tmp_file);
                remove(tmp_filename);
            } else {
                fprintf(stderr, "Failed to create temporary file for video thumbnail, ignoring thumbnail...\n");
            }
        }

        std::vector<CommandArg> additional_args = {
            { "-X", "POST" },
            { "-H", std::string("content-type: ") + content_type_to_string(file_analyzer.get_content_type()) },
            { "-H", "Authorization: Bearer " + access_token },
            { "--data-binary", "@" + filepath }
        };

        const char *filename = file_get_filename(filepath);
        std::string filename_escaped = url_param_encode(filename);

        char url[512];
        snprintf(url, sizeof(url), "%s/_matrix/media/r0/upload?filename=%s", homeserver.c_str(), filename_escaped.c_str());
        fprintf(stderr, "Upload url: |%s|\n", url);

        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) {
            err_msg = "Upload request failed, reason: " + server_response;
            return PluginResult::NET_ERR;
        }

        if(server_response.empty()) {
            err_msg = "Got corrupt response from server";
            return PluginResult::ERR;
        }

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            err_msg = "Matrix upload response parse error: " + json_errors;
            return PluginResult::ERR;
        }

        if(!json_root.isObject()) {
            err_msg = "Got corrupt response from server";
            return PluginResult::ERR;
        }

        const Json::Value &error_json = json_root["error"];
        if(error_json.isString()) {
            err_msg = error_json.asString();
            return PluginResult::ERR;
        }

        const Json::Value &content_uri_json = json_root["content_uri"];
        if(!content_uri_json.isString()) {
            err_msg = "Missing content_uri is server response";
            return PluginResult::ERR;
        }

        fprintf(stderr, "Matrix upload, response content uri: %s\n", content_uri_json.asCString());
        file_info.content_uri = content_uri_json.asString();
        return PluginResult::OK;
    }

    PluginResult Matrix::login(const std::string &username, const std::string &password, const std::string &homeserver, std::string &err_msg) {
        Json::Value identifier_json(Json::objectValue);
        identifier_json["type"] = "m.id.user"; // TODO: What if the server doesn't support this login type? redirect to sso web page etc
        identifier_json["user"] = username;

        Json::Value request_data(Json::objectValue);
        request_data["type"] = "m.login.password";
        request_data["identifier"] = std::move(identifier_json);
        request_data["password"] = password;
        request_data["initial_device_display_name"] = "QuickMedia"; // :^)

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "POST" },
            { "-H", "content-type: application/json" },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/login", server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) {
            err_msg = std::move(server_response);
            return PluginResult::NET_ERR;
        }

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            err_msg = "Matrix login response parse error: " + json_errors;
            return PluginResult::ERR;
        }

        if(!json_root.isObject()) {
            err_msg = "Failed to parse matrix login response";
            return PluginResult::ERR;
        }

        const Json::Value &error_json = json_root["error"];
        if(error_json.isString()) {
            err_msg = error_json.asString();
            return PluginResult::ERR;
        }

        const Json::Value &user_id_json = json_root["user_id"];
        if(!user_id_json.isString()) {
            err_msg = "Failed to parse matrix login response";
            return PluginResult::ERR;
        }

        const Json::Value &access_token_json = json_root["access_token"];
        if(!access_token_json.isString()) {
            err_msg = "Failed to parse matrix login response";
            return PluginResult::ERR;
        }

        // Use the user-provided homeserver instead of the one the server tells us about, otherwise this wont work with a proxy
        // such as pantalaimon
        json_root["homeserver"] = homeserver;

        this->user_id = user_id_json.asString();
        this->username = extract_user_name_from_user_id(this->user_id);
        this->access_token = access_token_json.asString();
        this->homeserver = homeserver;

        // TODO: Handle well_known field. The spec says clients SHOULD handle it if its provided

        Path session_path = get_storage_dir().join(name);
        if(create_directory_recursive(session_path) == 0) {
            session_path.join("session.json");
            if(!save_json_to_file_atomic(session_path, json_root)) {
                fprintf(stderr, "Warning: failed to save login response to %s\n", session_path.data.c_str());
            }
        } else {
            fprintf(stderr, "Warning: failed to create directory: %s\n", session_path.data.c_str());
        }

        return PluginResult::OK;
    }

    PluginResult Matrix::logout() {
        Path session_path = get_storage_dir().join(name).join("session.json");
        remove(session_path.data.c_str());

        std::vector<CommandArg> additional_args = {
            { "-X", "POST" },
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/logout", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        // Make sure all fields are reset here!
        room_data_by_id.clear();
        user_id.clear();
        username.clear();
        access_token.clear();
        homeserver.clear();
        upload_limit.reset();
        next_batch.clear();

        return PluginResult::OK;
    }

    PluginResult Matrix::delete_message(const std::string &room_id, void *message, std::string &err_msg){
        char random_characters[18];
        if(!generate_random_characters(random_characters, sizeof(random_characters)))
            return PluginResult::ERR;

        std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));

        Message *message_typed = (Message*)message;

        // request_data could contains "reason", maybe it should be added sometime in the future?
        Json::Value request_data(Json::objectValue);
        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "-H", "Authorization: Bearer " + access_token },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) }
        };

        char url[512];
        snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/redact/%s/m%ld.%.*s", homeserver.c_str(), room_id.c_str(), message_typed->event_id.c_str(), time(NULL), (int)random_readable_chars.size(), random_readable_chars.c_str());
        fprintf(stderr, "load initial room data, url: |%s|\n", url);

        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_tor, true, false) != DownloadResult::OK) {
            err_msg = std::move(server_response);
            return PluginResult::NET_ERR;
        }

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            err_msg = "Matrix delete message response parse error: " + json_errors;
            return PluginResult::ERR;
        }

        if(!json_root.isObject()) {
            err_msg = "Failed to parse matrix login response";
            return PluginResult::ERR;
        }

        const Json::Value &error_json = json_root["error"];
        if(error_json.isString()) {
            err_msg = error_json.asString();
            return PluginResult::ERR;
        }

        const Json::Value &event_id_json = json_root["event_id"];
        if(!event_id_json.isString())
            return PluginResult::ERR;

        fprintf(stderr, "Matrix delete message, response event id: %s\n", event_id_json.asCString());
        return PluginResult::OK;
    }

    PluginResult Matrix::load_and_verify_cached_session() {
        Path session_path = get_storage_dir().join(name).join("session.json");
        std::string session_json_content;
        if(file_get_content(session_path, session_json_content) != 0) {
            fprintf(stderr, "Info: failed to read matrix session from %s. Either its missing or we failed to read the file\n", session_path.data.c_str());
            return PluginResult::ERR;
        }

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&session_json_content[0], &session_json_content[session_json_content.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix cached session parse error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &user_id_json = json_root["user_id"];
        if(!user_id_json.isString()) {
            fprintf(stderr, "Failed to parse matrix cached session response\n");
            return PluginResult::ERR;
        }

        const Json::Value &access_token_json = json_root["access_token"];
        if(!access_token_json.isString()) {
            fprintf(stderr, "Failed to parse matrix cached session response\n");
            return PluginResult::ERR;
        }

        const Json::Value &homeserver_json = json_root["homeserver"];
        if(!homeserver_json.isString()) {
            fprintf(stderr, "Failed to parse matrix cached session response\n");
            return PluginResult::ERR;
        }

        std::string user_id = user_id_json.asString();
        std::string access_token = access_token_json.asString();
        std::string homeserver = homeserver_json.asString();

        std::vector<CommandArg> additional_args = {
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        // We want to make any request to the server that can verify that our token is still valid, doesn't matter which call
        if(download_to_string(homeserver + "/_matrix/client/r0/account/whoami", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) {
            fprintf(stderr, "Matrix whoami response: %s\n", server_response.c_str());
            return PluginResult::NET_ERR;
        }

        this->user_id = std::move(user_id);
        this->username = extract_user_name_from_user_id(this->user_id);
        this->access_token = std::move(access_token);
        this->homeserver = std::move(homeserver);
        return PluginResult::OK;
    }

    PluginResult Matrix::on_start_typing(const std::string &room_id) {
        Json::Value request_data(Json::objectValue);
        request_data["typing"] = true;
        request_data["timeout"] = 30000; // 30 sec timeout

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) },
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id) , server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        return PluginResult::OK;
    }

    PluginResult Matrix::on_stop_typing(const std::string &room_id) {
        Json::Value request_data(Json::objectValue);
        request_data["typing"] = false;

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "PUT" },
            { "-H", "content-type: application/json" },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) },
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id), server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        return PluginResult::OK;
    }

    PluginResult Matrix::set_read_marker(const std::string &room_id, const Message *message) {
        Json::Value request_data(Json::objectValue);
        request_data["m.fully_read"] = message->event_id;
        request_data["m.read"] = message->event_id;
        request_data["m.hidden"] = false; // What is this for? element sends it but its not part of the documentation. Is it for hiding read receipt from other users? in that case, TODO: make it configurable

        Json::StreamWriterBuilder builder;
        builder["commentStyle"] = "None";
        builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "POST" },
            { "-H", "content-type: application/json" },
            { "--data-binary", Json::writeString(builder, std::move(request_data)) },
            { "-H", "Authorization: Bearer " + access_token }
        };

        std::string server_response;
        if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/read_markers", server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        return PluginResult::OK;
    }

    bool Matrix::was_message_posted_by_me(const std::string &room_id, void *message) const {
        auto room_it = room_data_by_id.find(room_id);
        if(room_it == room_data_by_id.end()) {
            fprintf(stderr, "Error: no such room: %s\n", room_id.c_str());
            return false;
        }
        Message *message_typed = (Message*)message;
        return user_id == room_it->second->user_info[message_typed->user_id].user_id;
    }

    std::string Matrix::message_get_author_displayname(RoomData *room_data, Message *message) const {
        if(message->user_id >= room_data->user_info.size()) {
            fprintf(stderr, "Warning: no user with the index: %zu\n", message->user_id);
            return "";
        }
        return room_data->user_info[message->user_id].display_name;
    }

    PluginResult Matrix::get_config(int *upload_size) {
        // TODO: What if the upload limit changes? is it possible for the upload limit to change while the server is running?
        if(upload_limit) {
            *upload_size = upload_limit.value();
            return PluginResult::OK;
        }

        *upload_size = 0;

        std::vector<CommandArg> additional_args = {
            { "-H", "Authorization: Bearer " + access_token }
        };

        char url[512];
        snprintf(url, sizeof(url), "%s/_matrix/media/r0/config", homeserver.c_str());
        fprintf(stderr, "load initial room data, url: |%s|\n", url);

        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) {
            fprintf(stderr, "Matrix /config failed\n");
            return PluginResult::NET_ERR;
        }

        if(server_response.empty())
            return PluginResult::ERR;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Matrix parsing /config response failed, error: %s\n", json_errors.c_str());
            return PluginResult::ERR;
        }

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &upload_size_json = json_root["m.upload.size"];
        if(!upload_size_json.isNumeric())
            return PluginResult::ERR;

        upload_limit = upload_size_json.asInt();
        *upload_size = upload_limit.value();
        return PluginResult::OK;
    }
}