aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 15c02ea153131170cbce9abf4c868725dbd157d2 (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
#include "../include/Channel.hpp"
#include "../include/ChannelSidePanel.hpp"
#include "../include/UsersSidePanel.hpp"
#include "../include/ChannelTopPanel.hpp"
#include "../include/Cache.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Video.hpp"
#include "../include/Command.hpp"
#include "../include/Settings.hpp"
#include "../include/ColorScheme.hpp"
#include "../include/GlobalContextMenu.hpp"
#include "../include/StringUtils.hpp"
#include "../include/ImagePreview.hpp"
#include "../include/Rpc.hpp"
#include <msgpack.hpp>
#include <sstream>
#include <string>
#include <SFML/Graphics.hpp>
#include <cstring>
#include <boost/filesystem.hpp>
#include <odhtdb/Database.hpp>
#include <odhtdb/Signature.hpp>
#include <odhtdb/bin2hex.hpp>
#include <odhtdb/hex2bin.hpp>
#include <ntp/NtpClient.hpp>
#include <sibs/SafeSerializer.hpp>
#include <X11/Xlib.h>
#include <math.h>

using namespace std;
using namespace dchat;

static bool focused = true;
static bool windowFocused = true;

static void channelChangeUserNickname(Channel *channel, const StringView data, const odhtdb::Signature::PublicKey &userPublicKey)
{
    auto user = channel->getUserByPublicKey(userPublicKey);
    if(!user)
    {
        fprintf(stderr, "Nickname change: user with public key %s not found in channel %s\n", userPublicKey.toString().c_str(), channel->getName().c_str());
        return;
    }
    
    sibs::SafeDeserializer deserializer((const u8*)data.data, data.size);
    u8 nameLength = deserializer.extract<u8>();
    if(nameLength > 0)
    {
        user->name.resize(nameLength);
        deserializer.extract((u8*)&user->name[0], nameLength);
    }
    // We dont care if there is more data to read (malicious packet), we already got all the data we need
}

static bool looksLikeUrl(const string &str)
{
    if(str.find('.') == string::npos)
        return false;
    
    if(str.size() >= 7 && strncmp(str.c_str(), "http://", 7) == 0)
        return true;
    
    if(str.size() >= 8 && strncmp(str.c_str(), "https://", 8) == 0)
        return true;
    
    return false;
}

static void channelChangeUserAvatar(Channel *channel, const StringView data, const odhtdb::Signature::PublicKey &userPublicKey)
{
    auto user = channel->getUserByPublicKey(userPublicKey);
    if(!user)
    {
        fprintf(stderr, "Avatar change: user with public key %s not found in channel %s\n", userPublicKey.toString().c_str(), channel->getName().c_str());
        return;
    }
    
    sibs::SafeDeserializer deserializer((const u8*)data.data, data.size);
    u16 avatarLength = deserializer.extract<u16>();
    if(avatarLength >= 10 && avatarLength <= 512)
    {
        string avatarUrl;
        avatarUrl.resize(avatarLength);
        deserializer.extract((u8*)&avatarUrl[0], avatarLength);
        if(looksLikeUrl(avatarUrl))
        {
            user->avatarUrl = move(avatarUrl);
        }
    }
    // We dont care if there is more data to read (malicious packet), we already got all the data we need
}

static void channelChangeChannelName(Channel *channel, const StringView data, const odhtdb::Signature::PublicKey &userPublicKey)
{
    auto user = channel->getUserByPublicKey(userPublicKey);
    if(!user)
    {
        fprintf(stderr, "Channel change name: user with public key %s not found in channel %s\n", userPublicKey.toString().c_str(), channel->getName().c_str());
        return;
    }

    int userPermissionLevel = channel->getUserLowestPermissionLevel(user);
    if(userPermissionLevel != odhtdb::PERMISSION_LEVEL_ADMIN)
    {
        fprintf(stderr, "Channel change name: attempted by user %s who is not an admin (permission level: %d)\n", user->getName().c_str(), userPermissionLevel);
        return;
    }
    
    sibs::SafeDeserializer deserializer((const u8*)data.data, data.size);
    u16 channelNameLength = deserializer.extract<u16>();
    if(channelNameLength > 0 && channelNameLength <= 32)
    {
        string channelName;
        channelName.resize(channelNameLength);
        deserializer.extract((u8*)&channelName[0], channelNameLength);
        channel->setNameLocally(channelName);
    }
    // We dont care if there is more data to read (malicious packet), we already got all the data we need
}

static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &requestHash, const odhtdb::Signature::PublicKey &creatorPublicKey, const StringView decryptedObject, u64 timestamp, bool loadedFromCache)
{
    User *user = channel->getUserByPublicKey(creatorPublicKey);
    if(!user)
    {
        fprintf(stderr, "Missing user? %s\n", creatorPublicKey.toString().c_str());
        return;
    }
    
    if(decryptedObject.size > 1)
    {
        auto channelDataType = (ChannelDataType)static_cast<const char*>(decryptedObject.data)[0];
        StringView decryptedData((const char*)decryptedObject.data + 1, decryptedObject.size - 1);
        switch(channelDataType)
        {
            case ChannelDataType::ADD_MESSAGE:
            {
                string msg(decryptedData.data, decryptedData.size);
                Message *latestMessage = channel->getLatestMessage();
                auto timestampSeconds = ntp::NtpTimestamp::fromCombined(timestamp).seconds;
                channel->addLocalMessage(msg, user, timestampSeconds, requestHash);
                if(!loadedFromCache && !windowFocused && channel->getLocalUser()->isOnlineUser() && (!latestMessage || (latestMessage && timestampSeconds >= latestMessage->timestampSeconds)))
                {
                    auto onlineLocalUser = static_cast<OnlineLocalUser*>(channel->getLocalUser());
                    if(creatorPublicKey != onlineLocalUser->getPublicKey())
                    {
                        stringReplaceChar(msg, "'", "");
                        stringReplaceChar(msg, "\\", "");
                        string cmd = "notify-send dchat '";
                        cmd += msg;
                        cmd += "'";
                        system(cmd.c_str());
                    }
                }
                break;
            }
            case ChannelDataType::DELETE_MESSAGE:
            {
                sibs::SafeDeserializer deserializer((const u8*)decryptedData.data, decryptedData.size);
                odhtdb::Hash messageId;
                deserializer.extract((u8*)messageId.getData(), odhtdb::HASH_BYTE_SIZE);
                channel->deleteLocalMessage(messageId, creatorPublicKey);
                break;
            }
            case ChannelDataType::NICKNAME_CHANGE:
            {
                try
                {
                    channelChangeUserNickname(channel, decryptedData, creatorPublicKey);
                }
                catch(sibs::DeserializeException &e)
                {
                    fprintf(stderr, "Failed to deserialize nick change\n");
                }
                break;
            }
            case ChannelDataType::CHANGE_AVATAR:
            {
                try
                {
                    channelChangeUserAvatar(channel, decryptedData, creatorPublicKey);
                }
                catch(sibs::DeserializeException &e)
                {
                    fprintf(stderr, "Failed to deserialize avatar change\n");
                }
                break;
            }
            case ChannelDataType::CHANGE_CHANNEL_NAME:
            {
                try
                {
                    channelChangeChannelName(channel, decryptedData, creatorPublicKey);
                }
                catch(sibs::DeserializeException &e)
                {
                    fprintf(stderr, "Failed to deserialize channel name change\n");
                }
                break;
            }
            default:
                fprintf(stderr, "Got unexpected channel data type: %u\n", channelDataType);
                break;
        }
    }
    else
    {
        fprintf(stderr, "ADD_DATA packet too small, ignoring...\n");
    }
}

int main(int argc, char **argv)
{
    if(argc > 1)
    {    
        boost::filesystem::path resourcesPath(argv[1]);
        boost::filesystem::current_path(resourcesPath);
        printf("Resource path set to: %s\n", resourcesPath.string().c_str());
    }
    else
        printf("Resource directory not defined, using current directory\n");
    
    const sf::Int64 FRAMERATE_FOCUSED = 144;
    const sf::Int64 FRAMERATE_NOT_FOCUSED = 10;
    
    XInitThreads();
    sf::RenderWindow window(sf::VideoMode(1280, 768), "dchat");
    window.setVerticalSyncEnabled(false);
    window.setFramerateLimit(FRAMERATE_FOCUSED);
    
    //Video video(500, 500, "https://www.youtube.com/watch?v=bs0-EX9mJmg");
    
    Cache cache;
    Cache::loadBindsFromFile();
    
    Channel offlineChannel("Offline");
    ChannelSidePanel::addChannel(&offlineChannel);
    Channel::setCurrent(&offlineChannel);
    
    vector<Channel*> channels;
    
    vector<odhtdb::DatabaseNode> waitingToJoinChannels;
    odhtdb::MapHash<odhtdb::StoredNodeInfo> localNodeUsers;
    string currentUsername;
    string currentPassword;
    recursive_mutex channelMessageMutex;
    bool waitingToJoin = false;
    bool loggedIn = false;
    sf::Clock lastFocusedTimer;
    
    shared_ptr<odhtdb::Database> database = nullptr;
    odhtdb::DatabaseCallbackFuncs callbackFuncs;

    using LocalUserMessageCallback = function<void(const odhtdb::DatabaseAddNodeRequest &request)>;
    LocalUserMessageCallback onMessageByLocalUser = nullptr;
    
    callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, &database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        printf("Create node callback func %s\n", request.nodeHash->toString().c_str());
        
        auto nodeUserData = localNodeUsers.find(*request.nodeHash);
        if(nodeUserData == localNodeUsers.end()) return;
        
        User *localUser;
        if(nodeUserData->second.userKeyPair->getPublicKey() == *request.creatorPublicKey)
            localUser = new OnlineLocalUser("NoName", *nodeUserData->second.userKeyPair);
        else
            localUser = new OfflineUser("You");
        
        shared_ptr<odhtdb::Hash> databaseNodeHash = make_shared<odhtdb::Hash>();
        memcpy(databaseNodeHash->getData(), request.nodeHash->getData(), odhtdb::HASH_BYTE_SIZE);
        odhtdb::DatabaseNode databaseNode(nodeUserData->second.nodeEncryptionKey, databaseNodeHash);
        Channel *channel = new Channel("NoChannelName", databaseNode, localUser, database);
        printf("database: %llu\n", database.get());
        ChannelSidePanel::addChannel(channel);
        channels.push_back(channel);
        Channel::setCurrent(channel);
        lastFocusedTimer.restart();
        
        if(localUser->type == User::Type::OFFLINE)
        {
            User *nodeCreatorUser = new OnlineRemoteUser("NoName", *request.creatorPublicKey);
            channel->addUserLocally(nodeCreatorUser);
        }
        
        for(vector<odhtdb::DatabaseNode>::iterator it = waitingToJoinChannels.begin(); it != waitingToJoinChannels.end(); ++it)
        {
            if(*request.nodeHash == *it->getRequestHash())
            {
                waitingToJoin = true;
                waitingToJoinChannels.erase(it);
                return;
            }
        }
    };
    
    callbackFuncs.addNodeCallbackFunc = [&channels, &channelMessageMutex, &lastFocusedTimer, &onMessageByLocalUser](const odhtdb::DatabaseAddNodeRequest &request)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        //printf("Add node callback func %s\n", request.requestHash->toString().c_str());
        for(Channel *channel : channels)
        {
            if(*request.nodeHash == *channel->getNodeInfo().getRequestHash())
            {
                channelAddStoredMessage(channel, *request.requestHash, *request.creatorPublicKey, StringView((const char*)request.decryptedData.data, request.decryptedData.size), request.timestamp, request.loadedFromCache);
                if(channel == Channel::getCurrent())
                    lastFocusedTimer.restart();

                if(!request.loadedFromCache && *request.creatorPublicKey == static_cast<OnlineLocalUser*>(channel->getLocalUser())->getPublicKey())
                {
                    if(onMessageByLocalUser)
                        onMessageByLocalUser(request);
                }
                return;
            }
        }
    };
    
    callbackFuncs.addUserCallbackFunc = [&channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseAddUserRequest &request)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        printf("Add user callback. Channel to add user to: %s\n", request.nodeHash->toString().c_str());
        for(Channel *channel : channels)
        {
            printf("My channel: %s (%s)\n", channel->getNodeInfo().getRequestHash()->toString().c_str(), channel->getName().c_str());
            if(*request.nodeHash == *channel->getNodeInfo().getRequestHash())
            {
                printf("Add user to one of my channels\n");
                auto nodeUserData = localNodeUsers.find(*request.nodeHash);
                
                User *userToAdd = channel->getUserByPublicKey(*request.userToAddPublicKey);
                if(userToAdd)
                {
                    fprintf(stderr, "User %s already exists in channel\n", request.userToAddPublicKey->toString().c_str());
                    return;
                }
                
                if(channel == Channel::getCurrent())
                    lastFocusedTimer.restart();
                
                if(*request.userToAddPublicKey == nodeUserData->second.userKeyPair->getPublicKey())
                {
                    printf("You were added to channel %s by %s\n", request.nodeHash->toString().c_str(), request.creatorPublicKey->toString().c_str());
                    channel->replaceLocalUser(new OnlineLocalUser("NoName", *nodeUserData->second.userKeyPair));
                    waitingToJoin = false;
                    return;
                }
                
                User *newRemoteUser = new OnlineRemoteUser("NoName", *request.userToAddPublicKey);
                channel->addUserLocally(newRemoteUser);
                return;
            }
        }
    };
    database = odhtdb::Database::connect("127.0.0.1", 27130, Cache::getDchatDir(), callbackFuncs).get();
    
    auto addSystemMessage = [&lastFocusedTimer](const std::string &msg, bool plainText = true)
    {
        Channel::getCurrent()->addSystemMessage(msg, plainText);
        lastFocusedTimer.restart();
    };
    
    // Login to account
    Command::add("login", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, &currentUsername, &currentPassword](const vector<string> &args)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        if(args.size() != 2)
        {
            fprintf(stderr, "Expected 2 arguments for command login (username and password), got %u argument(s)\n", args.size());
            return;
        }
        
        try
        {
            localNodeUsers = database->getStoredNodeUserInfoDecrypted(args[0], args[1]);
            
            ChannelSidePanel::removeAllChannels();
            for(Channel *channel : channels)
            {
                delete channel;
            }
            channels.clear();
            
            printf("Loading %u channel(s) for user\n", localNodeUsers.size());
            for(auto &localNodeUser : localNodeUsers)
            {
                database->loadNode(localNodeUser.first);
            }
            
            printf("Successfully logged into user %s\n", args[0].c_str());
            currentUsername = args[0];
            currentPassword = args[1];
            loggedIn = true;
        }
        catch(std::exception &e)
        {
            fprintf(stderr, "Failed to login, reason: %s\n", e.what());
        }
    });
    
    // Register account
    Command::add("register", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, &currentUsername, &currentPassword](const vector<string> &args)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        if(args.size() != 2)
        {
            fprintf(stderr, "Expected 2 arguments for command register (username and password), got %u argument(s)\n", args.size());
            return;
        }
        
        try
        {
            database->storeUserWithoutNodes(args[0], args[1]);
        }
        catch(odhtdb::SqlExecException &e)
        {
            fprintf(stderr, "User with name %s already exists in storage\n", args[0].c_str());
            return;
        }
        
        printf("Registered user %s\n", args[0].c_str());
        
        ChannelSidePanel::removeAllChannels();
        for(Channel *channel : channels)
        {
            delete channel;
        }
        channels.clear();
        localNodeUsers.clear();
        
        printf("Successfully logged into user %s\n", args[0].c_str());
        currentUsername = args[0];
        currentPassword = args[1];
        loggedIn = true;
    });
    
    // TODO: Use database->addData to change channel name
    // Create channel
    Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, &currentUsername, &currentPassword, &lastFocusedTimer, addSystemMessage](const vector<string> &args)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        if(args.size() != 1)
        {
            fprintf(stderr, "Expected 1 argument for command cc (channel name), got %u argument(s)\n", args.size());
            return;
        }
        
        if(!loggedIn)
        {
            fprintf(stderr, "You are not logged in. Please login before creating a channel\n");
            return;
        }
        
        auto createResponse = database->create();
        printf("Created channel %s\n", createResponse->getRequestHash()->toString().c_str());
        
        User *newLocalUser = new OnlineLocalUser("NoName", *createResponse->getNodeAdminKeyPair());
        odhtdb::DatabaseNode databaseNode(createResponse->getNodeEncryptionKey(), createResponse->getRequestHash());
        Channel *channel = new Channel(args[0], databaseNode, newLocalUser, database);
        ChannelSidePanel::addChannel(channel);
        channels.push_back(channel);
        Channel::setCurrent(channel);
        channel->setName(args[0]);
        lastFocusedTimer.restart();
        
        localNodeUsers[*createResponse->getRequestHash()] = { createResponse->getNodeEncryptionKey(), createResponse->getNodeAdminKeyPair() };
        database->storeNodeInfoForUserEncrypted(databaseNode, currentUsername, currentPassword, *createResponse->getNodeAdminKeyPair());
        addSystemMessage("Channel created and stored in database");
    });
    
    // Create invite key
    Command::add("invite", [&channelMessageMutex, &offlineChannel, &database, addSystemMessage](const vector<string> &args)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        if(args.size() != 0)
        {
            fprintf(stderr, "Expected 0 arguments for command invite, got %u argument(s)\n", args.size());
            return;
        }
        
        Channel *currentChannel = Channel::getCurrent();
        if(currentChannel == &offlineChannel)
        {
            addSystemMessage("You need to be in a channel to create an invite key");
            return;
        }
        
        // TODO: Verify user has permission to add users before generating invite key, otherwise invite will fail and the users attempting to join will wait in futile
        
        auto channelNodeHash = currentChannel->getNodeInfo().getRequestHash();
        auto channelEncryptionKey = currentChannel->getNodeInfo().getNodeEncryptionKey();
        shared_ptr<odhtdb::OwnedByteArray> encryptionKey = make_shared<odhtdb::OwnedByteArray>(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
        odhtdb::Encryption::generateKey((unsigned char*)encryptionKey->data);
        
        string inviteKey = odhtdb::bin2hex((const char*)channelNodeHash->getData(), channelNodeHash->getSize());
        inviteKey += '&';
        inviteKey += odhtdb::bin2hex((const char*)encryptionKey->data, odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
        
        string msg = "You are now listening for users to join the channel using the key: ";
        msg += inviteKey;
        
        addSystemMessage(msg);
        printf("%s\n", msg.c_str());
        
        sibs::SafeSerializer keySerializer;
        keySerializer.add((const u8*)channelNodeHash->getData(), channelNodeHash->getSize());
        keySerializer.add((const u8*)encryptionKey->data, odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
        odhtdb::InfoHash key = odhtdb::Database::getInfoHash(keySerializer.getBuffer().data(), keySerializer.getBuffer().size());
        database->receiveCustomMessage(key, [&channelMessageMutex, encryptionKey, channelEncryptionKey, currentChannel, database](const void *data, usize size)
        {
            // TODO: User can remove channel @currentChannel before we get here, meaning @currentChannel is deleted and would be invalid; causing the program to crash
            try
            {
                sibs::SafeDeserializer deserializer((const u8*)data, size);
                u8 userToAddPublicKeyRaw[odhtdb::PUBLIC_KEY_NUM_BYTES];
                deserializer.extract(userToAddPublicKeyRaw, odhtdb::PUBLIC_KEY_NUM_BYTES);
                odhtdb::Signature::PublicKey userToAddPublicKey((const char*)userToAddPublicKeyRaw, odhtdb::PUBLIC_KEY_NUM_BYTES);
                string unsignedEncryptedMsg = userToAddPublicKey.unsign(odhtdb::DataView((void*)deserializer.getBuffer(), deserializer.getSize()));
                
                sibs::SafeDeserializer encryptedDataDeserializer((const u8*)unsignedEncryptedMsg.data(), unsignedEncryptedMsg.size());
                u8 nonce[odhtdb::ENCRYPTION_NONCE_BYTE_SIZE];
                encryptedDataDeserializer.extract(nonce, odhtdb::ENCRYPTION_NONCE_BYTE_SIZE);
                odhtdb::Decryption decryptedMsg(odhtdb::DataView((void*)encryptedDataDeserializer.getBuffer(), encryptedDataDeserializer.getSize()), 
                                                       odhtdb::DataView(nonce, odhtdb::ENCRYPTION_NONCE_BYTE_SIZE), 
                                                       encryptionKey->getView());
                
                // TODO: Create GUI for accepting users into channel instead of accepting ALL users
                sibs::SafeSerializer encryptedDataSerializer;
                encryptedDataSerializer.add((const u8*)channelEncryptionKey->data, channelEncryptionKey->size);
                encryptedDataSerializer.add((const u8*)userToAddPublicKey.getData(), userToAddPublicKey.getSize());
                odhtdb::Encryption encryptedChannelKey(odhtdb::DataView((void*)encryptedDataSerializer.getBuffer().data(), encryptedDataSerializer.getBuffer().size()), encryptionKey->getView());
                
                sibs::SafeSerializer serializer;
                serializer.add((const u8*)encryptedChannelKey.getNonce().data, encryptedChannelKey.getNonce().size);
                serializer.add((const u8*)encryptedChannelKey.getCipherText().data, encryptedChannelKey.getCipherText().size);
                const auto &localUserPublicKey = static_cast<OnlineLocalUser*>(currentChannel->getLocalUser())->getPublicKey();
                auto localUserGroups = database->getUserGroups(*currentChannel->getNodeInfo().getRequestHash(), localUserPublicKey);
                if(localUserGroups.empty())
                {
                    fprintf(stderr, "No group to add user to...\n");
                    return sibs::SafeSerializer();
                }
                lock_guard<recursive_mutex> lock(channelMessageMutex);
                currentChannel->addUser(userToAddPublicKey, localUserGroups[0].getView());
                return serializer;
            }
            catch(std::exception &e)
            {
                fprintf(stderr, "Failed while parsing user data to add to channel, reason: %s\n", e.what());
            }
            return sibs::SafeSerializer();
        });
    });
    
    // Join channel using invite key
    Command::add("jc", [&loggedIn, &database, &localNodeUsers, &channelMessageMutex, &waitingToJoin, &waitingToJoinChannels, &currentUsername, &currentPassword](const vector<string> &args)
    {
        lock_guard<recursive_mutex> lock(channelMessageMutex);
        if(args.size() != 1)
        {
            fprintf(stderr, "Expected 1 argument for command jc (channel join key), got %u argument(s)\n", args.size());
            return;
        }
        
        if(args[0].size() != 129)
        {
            fprintf(stderr, "Expected join key to be 129 characters, was %u character(s)\n", args[0].size());
            return;
        }
        
        if(!loggedIn)
        {
            fprintf(stderr, "You are not logged in. Please login before joining a channel\n");
            return;
        }
        
        string nodeHashBinRaw = odhtdb::hex2bin(args[0].c_str(), 64);
        shared_ptr<odhtdb::Hash> nodeHash = make_shared<odhtdb::Hash>();
        memcpy(nodeHash->getData(), nodeHashBinRaw.data(), nodeHashBinRaw.size());
        auto nodeUserIt = localNodeUsers.find(*nodeHash);
        if(nodeUserIt != localNodeUsers.end())
        {
            fprintf(stderr, "You have already joined the channel %s\n", args[0].c_str());
            return;
        }
        
        string encryptionKeyBinRaw = odhtdb::hex2bin(args[0].c_str() + 65, 64);
        shared_ptr<odhtdb::OwnedByteArray> encryptionKey = make_shared<odhtdb::OwnedByteArray>(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
        memcpy(encryptionKey->data, encryptionKeyBinRaw.data(), encryptionKeyBinRaw.size());
        
        shared_ptr<odhtdb::Signature::KeyPair> keyPair = make_shared<odhtdb::Signature::KeyPair>();
        sibs::SafeSerializer serializer;
        serializer.add((const u8*)keyPair->getPublicKey().getData(), keyPair->getPublicKey().getSize());
        
        const char *msg = "please let me join";
        odhtdb::Encryption encryptedJoinMsg(odhtdb::DataView((void*)msg, strlen(msg)), encryptionKey->getView());
        sibs::SafeSerializer encryptedDataSerializer;
        encryptedDataSerializer.add((const u8*)encryptedJoinMsg.getNonce().data, encryptedJoinMsg.getNonce().size);
        encryptedDataSerializer.add((const u8*)encryptedJoinMsg.getCipherText().data, encryptedJoinMsg.getCipherText().size);
        string signedEncryptedMsg = keyPair->getPrivateKey().sign(odhtdb::DataView(encryptedDataSerializer.getBuffer().data(), encryptedDataSerializer.getBuffer().size()));
        serializer.add((const u8*)signedEncryptedMsg.data(), signedEncryptedMsg.size());
        
        sibs::SafeSerializer keySerializer;
        keySerializer.add((const u8*)nodeHash->getData(), nodeHash->getSize());
        keySerializer.add((const u8*)encryptionKeyBinRaw.data(), odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
        odhtdb::InfoHash key = odhtdb::Database::getInfoHash(keySerializer.getBuffer().data(), keySerializer.getBuffer().size());
        database->sendCustomMessage(key, serializer.getBuffer().data(), serializer.getBuffer().size(), 
            [&database, nodeHash, encryptionKey, &waitingToJoinChannels, &channelMessageMutex, keyPair, &currentUsername, &currentPassword, &localNodeUsers]
            (bool gotResponse, const void *data, usize size)
        {
            if(!gotResponse)
            {
                printf("We didn't get a response from anybody in the channel. Is there nobody that can add us (nobody with the permission required to do so or no online users) or is the node hash invalid?\n");
                return false;
            }
            
            try
            {
                sibs::SafeDeserializer deserializer((const u8*)data, size);
                u8 nonce[odhtdb::ENCRYPTION_NONCE_BYTE_SIZE];
                deserializer.extract(nonce, odhtdb::ENCRYPTION_NONCE_BYTE_SIZE);
                odhtdb::Decryption decryptedMsg(odhtdb::DataView((void*)deserializer.getBuffer(), deserializer.getSize()), 
                                                       odhtdb::DataView(nonce, odhtdb::ENCRYPTION_NONCE_BYTE_SIZE), 
                                                       encryptionKey->getView());
                if(decryptedMsg.getDecryptedText().size != odhtdb::ENCRYPTION_KEY_BYTE_SIZE + odhtdb::PUBLIC_KEY_NUM_BYTES)
                {
                    fprintf(stderr, "Invite response was of unexpected size, maybe it wasn't meant for us?\n");
                    return true;
                }
                
                odhtdb::DataView channelEncryptionKeyRaw(decryptedMsg.getDecryptedText().data, odhtdb::ENCRYPTION_KEY_BYTE_SIZE);
                odhtdb::DataView invitedUserPublicKey((u8*)decryptedMsg.getDecryptedText().data + odhtdb::ENCRYPTION_KEY_BYTE_SIZE, odhtdb::PUBLIC_KEY_NUM_BYTES);
                if(memcmp(keyPair->getPublicKey().getData(), invitedUserPublicKey.data, odhtdb::PUBLIC_KEY_NUM_BYTES) != 0)
                {
                    fprintf(stderr, "Invite response was not meant for us\n");
                    return true;
                }
                
                shared_ptr<odhtdb::OwnedByteArray> channelEncryptionKey = make_shared<odhtdb::OwnedByteArray>(new u8[channelEncryptionKeyRaw.size], channelEncryptionKeyRaw.size);
                memcpy(channelEncryptionKey->data, channelEncryptionKeyRaw.data, channelEncryptionKeyRaw.size);
                odhtdb::DatabaseNode databaseNode(channelEncryptionKey, nodeHash);
                localNodeUsers[*nodeHash] = { channelEncryptionKey, keyPair };
                database->storeNodeInfoForUserEncrypted(databaseNode, currentUsername, currentPassword, *keyPair);
                printf("Got a response from a person in the channel, we might get added...\n");
                waitingToJoinChannels.push_back(databaseNode);
                lock_guard<recursive_mutex> lock(channelMessageMutex);
                database->seed(databaseNode, odhtdb::DatabaseFetchOrder::NEWEST_FIRST);
                return false;
            }
            catch(std::exception &e)
            {
                fprintf(stderr, "Failed while parsing join response for invite link, reason: %s\n", e.what());
            }
            return true;
        });
        waitingToJoin = true;
    });
    
    // Scale UI
    Command::add("scale", [](const vector<string> &args)
    {
        if(args.size() != 1)
        {
            fprintf(stderr, "Expected 1 argument for command scale, got %u argument(s)\n", args.size());
            return;
        }
        
        float scaling = stof(args[0]);
        Settings::setScaling(scaling);
        printf("UI scaling set to %f\n", scaling);
    });
    
    Command::add("addbind", [addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 2)
        {
            string errMsg = "Expected 2 arguments for command addbind, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }
        
        string key = ":";
        key += args[0];
        key += ":";
        
        if(key.size() > 255)
        {
            // 253 = bind + two colons
            addSystemMessage("Bind is too long. Max size is 253 bytes");
            return;
        }
        
        bool bindAdded = Chatbar::addBind(key, args[1]);
        if(bindAdded)
            addSystemMessage("Bind added");
        else
            addSystemMessage("Bind already exists. Remove it first if you want to replace it");
    });
    
    Command::add("removebind", [addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 1)
        {
            string errMsg = "Expected 1 argument for command removebind, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }
        
        string key = ":";
        key += args[0];
        key += ":";
        
        if(key.size() > 255)
        {
            // 253 = bind + two colons
            addSystemMessage("Bind is too long. Max size is 253 bytes");
            return;
        }
        
        bool bindRemoved = Chatbar::removeBind(key);
        if(bindRemoved)
            addSystemMessage("Bind removed");
        else
            addSystemMessage("Bind doesn't exist, nothing was removed");
    });
    
    Command::add("binds", [addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 0)
        {
            string errMsg = "Expected 0 arguments for command removebind, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }
        
        string msg = "Binds:";
        auto binds = Chatbar::getBinds();
        for(auto &bind : binds)
        {
            msg += "\n";
            msg += bind.first;
            msg += " ";
            msg += bind.second;
        }
        addSystemMessage(msg, false);
    });
    
    // Change nick of current user in current channel
    Command::add("nick", [&loggedIn, &offlineChannel, addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 1)
        {
            string errMsg = "Expected 1 argument for command nick, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }
        
        if(!loggedIn)
        {
            addSystemMessage("You need to be logged in to change your nickname");
            return;
        }
        
        if(Channel::getCurrent() == &offlineChannel)
        {
            addSystemMessage("You need to be in a channel to change your nickname");
            return;
        }
        
        if(args[0].size() == 0 || args[0].size() > 255)
        {
            addSystemMessage("Invalid nickname. Nickname has to be between 1 and 255 characters");
            return;
        }
        
        Channel::getCurrent()->changeNick(args[0]);
        string msg = "Your nickname was changed to ";
        msg += args[0];
        addSystemMessage(msg);
    });
    
    // Change avatar of current user in current channel
    Command::add("avatar", [&loggedIn, &offlineChannel, addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 1)
        {
            string errMsg = "Expected 1 argument for command avatar, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }
        
        if(!loggedIn)
        {
            addSystemMessage("You need to be logged in to change your avatar");
            return;
        }
        
        if(Channel::getCurrent() == &offlineChannel)
        {
            addSystemMessage("You need to be in a channel to change your avatar");
            return;
        }
        
        if(args[0].size() < 10 || args[0].size() > 512)
        {
            addSystemMessage("Invalid avatar url size, expected to be between 10 and 512 bytes");
            return;
        }
        
        if(looksLikeUrl(args[0]))
        {
            Channel::getCurrent()->setAvatar(args[0]);
            addSystemMessage("Your avatar has been changed (Note: max avatar size is 1 Mb, if your avatar is larger then it will not be visible)");
        }
        else
        {
            addSystemMessage("Avatar url needs to start with either http:// or https:// and include a dot");
        }
    });
    
    // Change name of the current channel
    Command::add("channelname", [&offlineChannel, addSystemMessage](const vector<string> &args)
    {
        if(args.size() != 1)
        {
            string errMsg = "Expected 1 argument for command channelname, got ";
            errMsg += to_string(args.size());
            errMsg += " argument(s)";
            addSystemMessage(errMsg);
            return;
        }

        Channel *currentChannel = Channel::getCurrent();
        if(currentChannel == &offlineChannel)
        {
            addSystemMessage("You need to be in a channel to change channel name");
            return;
        }
        
        if(!currentChannel->getLocalUser()->isOnlineUser())
        {
            addSystemMessage("You need to be logged in to change channel name");
            return;
        }
        
        if(args[0].size() == 0 || args[0].size() > 32)
        {
            addSystemMessage("Channel name has to be between 1 and 32 bytes long");
            return;
        }

        int localUserPermissionLevel = currentChannel->getUserLowestPermissionLevel(static_cast<OnlineLocalUser*>(currentChannel->getLocalUser()));
        if(localUserPermissionLevel != odhtdb::PERMISSION_LEVEL_ADMIN)
        {
            addSystemMessage("You need to be admin to change channel name");
            return;
        }
        
        currentChannel->setName(args[0]);
        string msg = "Channel name has been changed to ";
        msg += args[0];
        addSystemMessage(msg);
    });
    
    Command::add("clearcache", [database](const vector<string> &args)
    {
        printf("Cleared cache (%d bytes)\n", database->clearCache());
    });
    
    string commandsMsg = "Available commands: ";
    for(const auto &commandIt : Command::getCommands())
    {
        commandsMsg += "\n/";
        commandsMsg += commandIt.first;
    }
    addSystemMessage(commandsMsg);

    odhtdb::MapHash<u64> myDiscordIdsByChannel;

    bool running = true;
    thread rpcThread([&running, &onMessageByLocalUser, &channels, &lastFocusedTimer, &myDiscordIdsByChannel]()
    {
        Rpc rpc(5555);
        mutex messageMutex;
        vector<msgpack::sbuffer> messagesToSend;
        onMessageByLocalUser = [&messagesToSend, &messageMutex](const odhtdb::DatabaseAddNodeRequest &request)
        {
            lock_guard<mutex> lock(messageMutex);
            auto channelDataType = (ChannelDataType)static_cast<const char*>(request.decryptedData.data)[0];
            usize size = request.decryptedData.size - 1;
            const char *data = (const char*)request.decryptedData.data + 1;
            const char *action = nullptr;
            if(channelDataType == ChannelDataType::ADD_MESSAGE)
                action = "addMessage";

            if(!action) return;
            vector<string> msg = { action, string(data, data + size), request.nodeHash->toString() };
            msgpack::sbuffer buffer;
            msgpack::pack(buffer, msg);
            messagesToSend.emplace_back(move(buffer));
        };

        while(running)
        {
            rpc.recv([&channels, &lastFocusedTimer, &myDiscordIdsByChannel](zmq::message_t *message)
            {
                try
                {
                    msgpack::object_handle oh = msgpack::unpack((const char*)message->data(), message->size());
                    auto deserialized = oh.get();
                    vector<string> msg;
                    deserialized.convert(msg);
                    if(msg.size() < 2)
                    {
                        fprintf(stderr, "Rpc receive, data length expected to be at least 2, was %u\n", msg.size());
                        return;
                    }
                    auto &action = msg[0];

                    string dchatChannelIdRaw = odhtdb::hex2bin(msg[1].c_str(), msg[1].size());
                    odhtdb::Hash dchatChannelId;
                    memcpy(dchatChannelId.getData(), dchatChannelIdRaw.data(), dchatChannelIdRaw.size());
                    Channel *bridgedChannel = nullptr;
                    for(Channel *channel : channels)
                    {
                        if(*channel->getNodeInfo().getRequestHash() == dchatChannelId)
                        {
                            bridgedChannel = channel;
                            break;
                        }
                    }

                    if(!bridgedChannel)
                    {
                        fprintf(stderr, "Rcp addMessage, invalid dchat channel %s\n", msg[1].c_str());
                        return;
                    }

                    if(bridgedChannel == Channel::getCurrent())
                        lastFocusedTimer.restart();

                    fprintf(stderr, "Received rpc, action: %s\n", action.c_str());
                    if(action == "addMessage")
                    {
                        if((msg.size() - 2) % 4 != 0)
                        {
                            fprintf(stderr, "Rpc addMessage, request was malformed\n");
                            return;
                        }

                        for(size_t i = 2; i < msg.size(); i += 4)
                        {
                            auto &content = msg[i];
                            auto &discordUserId = msg[i + 1];
                            u64 discordUserIdNumber = 0;
                            auto &discordUserName = msg[i + 2];
                            auto &messageTimestampMillisec = msg[i + 3];
                            u64 messageTimestampSecondsNumber = 0;

                            try
                            {
                                discordUserIdNumber = stoull(discordUserId);
                            }
                            catch(...)
                            {
                                fprintf(stderr, "Rpc receive, failed to convert discord id to uint64_t: %s\n", discordUserId.c_str());
                                return;
                            }

                            try
                            {
                                messageTimestampSecondsNumber = stoull(messageTimestampMillisec) / 1000;
                            }
                            catch(...)
                            {
                                fprintf(stderr, "Rpc receive, failed to convert discord message timestamp to uint64_t: %s\n", messageTimestampMillisec.c_str());
                                return;
                            }

                            auto myDiscordIdIt = myDiscordIdsByChannel.find(dchatChannelId);
                            if(myDiscordIdIt != myDiscordIdsByChannel.end() && myDiscordIdIt->second == discordUserIdNumber)
                            {
                                auto &channelMessages = bridgedChannel->getMessageBoard().getMessages();
                                Message *myLatestMessage = nullptr;
                                for(auto it = channelMessages.rbegin(), end = channelMessages.rend(); it != end; ++it)
                                {
                                    if((*it)->user == bridgedChannel->getLocalUser())
                                    {
                                        myLatestMessage = *it;
                                        break;
                                    }
                                }

                                if(myLatestMessage && (i64)messageTimestampSecondsNumber - (i64)myLatestMessage->timestampSeconds <= 3)
                                {
                                    return;
                                    /*
                                    auto myMessageUtf8 = myLatestMessage->text.getString().toUtf8();
                                    odhtdb::Hash myMessageHash(myMessageUtf8.data(), myMessageUtf8.size());
                                    odhtdb::Hash myDiscordMessageHash(content.data(), content.size());
                                    if(myMessageHash == myDiscordMessageHash)
                                        return;
                                    */
                                }
                            }

                            bridgedChannel->addLocalDiscordMessage(discordUserName, discordUserIdNumber, content, bridgedChannel->getLocalUser(), messageTimestampSecondsNumber, odhtdb::Hash(messageTimestampMillisec.c_str(), messageTimestampMillisec.size()));
                        }
                    }
                    else if(action == "addUser")
                    {
                        if((msg.size() - 2) % 4 != 0)
                        {
                            fprintf(stderr, "Rpc addUser, request was malformed\n");
                            return;
                        }

                        for(size_t i = 2; i < msg.size(); i += 4)
                        {
                            auto &discordUsername = msg[i];
                            auto &discordUserId = msg[i + 1];
                            auto &userStatus = msg[i + 2];
                            auto &avatarURL = msg[i + 3];

                            try
                            {
                                u64 discordUserIdNumber = stoull(discordUserId);
                                bool online = (userStatus != "offline");
                                printf("Rpc, adding user %s with status %s\n", discordUsername.c_str(), userStatus.c_str());
                                DiscordServiceUser *discordUser = bridgedChannel->getDiscordService()->getUserById(discordUserIdNumber);
                                if(discordUser)
                                {
                                    discordUser->connected = online;
                                }
                                else
                                {
                                    discordUser = new DiscordServiceUser(discordUsername, discordUserIdNumber, online);
                                    bridgedChannel->getDiscordService()->addUser(discordUser);
                                }
                                discordUser->avatarUrl = avatarURL;
                            }
                            catch(...)
                            {
                                fprintf(stderr, "Warning: Rpc receive, failed to convert discord id to uint64_t: %s\n", discordUserId.c_str());
                                // Ignore for now.. should we really handle this error other than showing warning?
                            }
                        }
                    }
                    else if(action == "removeUser")
                    {
                        for(size_t i = 2; i < msg.size(); ++i)
                        {
                            auto &discordUserId = msg[i];
                            try
                            {
                                u64 discordUserIdNumber = stoull(discordUserId);
                                bridgedChannel->getDiscordService()->removeUser(discordUserIdNumber);
                            }
                            catch(...)
                            {
                                fprintf(stderr, "Warning: Rpc receive, failed to convert discord id to uint64_t: %s\n", discordUserId.c_str());
                                // Ignore for now.. should we really handle this error other than showing warning?
                            }
                        }
                    }
                    else if(action == "statusChange")
                    {
                        if((msg.size() - 2) != 2)
                        {
                            fprintf(stderr, "Rpc statusChange, request was malformed\n");
                            return;
                        }

                        auto &discordUserId = msg[2];
                        auto &userStatus = msg[3];

                        try
                        {
                            u64 discordUserIdNumber = stoull(discordUserId);
                            DiscordServiceUser *discordUser = bridgedChannel->getDiscordService()->getUserById(discordUserIdNumber);
                            if(discordUser)
                            {
                                discordUser->connected = (userStatus != "offline");
                                printf("Rcp statusChange, changed user %s (%s) status to %s\n", discordUserId.c_str(), discordUser->getName().c_str(), userStatus.c_str());
                            }
                        }
                        catch(...)
                        {
                            fprintf(stderr, "Warning: Rpc receive, failed to convert discord id to uint64_t: %s\n", discordUserId.c_str());
                            // Ignore for now.. should we really handle this error other than showing warning?
                        }
                    }
                    else if(action == "addMe")
                    {
                        if((msg.size() - 2) != 1)
                        {
                            fprintf(stderr, "Rpc addMe, request was malformed\n");
                            return;
                        }

                        auto &myDiscordId = msg[2];
                        try
                        {
                            u64 myDiscordIdNumber = stoull(myDiscordId);
                            myDiscordIdsByChannel[dchatChannelId] = myDiscordIdNumber;
                        }
                        catch(...)
                        {
                            fprintf(stderr, "Warning: Rpc receive, failed to convert discord id to uint64_t: %s\n", myDiscordId.c_str());
                            // Ignore for now.. should we really handle this error other than showing warning?
                        }
                    }
                    else
                    {
                        fprintf(stderr, "Rcp received unknown action %s\n", action.c_str());
                    }
                }
                catch(msgpack::type_error &e)
                {
                    fprintf(stderr, "Failed to deserialize received rpc, error: %s\n", e.what());
                }
            });
            {
                lock_guard<mutex> lock(messageMutex);
                for(auto &messageToSend : messagesToSend)
                {
                    fprintf(stderr, "Rpc, sending message\n");
                    rpc.send(messageToSend.data(), messageToSend.size());
                }
                messagesToSend.clear();
            }
            this_thread::sleep_for(chrono::milliseconds(50));
        }
    });
    
    sf::Clock frameTimer;
    
    while (running)
    {
        frameTimer.restart();
        Channel *currentChannel =  Channel::getCurrent();
        
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
                running = false;
            }
            else if(event.type == sf::Event::Resized)
            {
                sf::FloatRect viewRect(0.0f, 0.0f, event.size.width, event.size.height);
                /* // TODO: Use xlib/xcb to set window minimum size instead
                const int minWidth = 800;
                if(event.size.width < minWidth)
                {
                    viewRect.width = minWidth;
                    window.setSize(sf::Vector2u(minWidth, event.size.height));
                }
                */
                sf::View view(viewRect);
                window.setView(view);
            }
            else if(event.type == sf::Event::MouseEntered)
                window.setFramerateLimit(FRAMERATE_FOCUSED);
            //else if(event.type == sf::Event::MouseLeft)
            //    window.setFramerateLimit(FRAMERATE_NOT_FOCUSED);
            
            if(event.type == sf::Event::GainedFocus)
                windowFocused = true;
            else if(event.type == sf::Event::LostFocus)
                windowFocused = false;
            
            if(event.type == sf::Event::MouseEntered)
                focused = true;
            else if(event.type == sf::Event::MouseLeft)
                focused = false;
            
            if(focused)
            {
                ImagePreview::processEvent(event);
            }
            
            if(!ImagePreview::getPreviewContentPtr() && ImagePreview::getTimeSinceLastSeenMs() > 250)
            {
                GlobalContextMenu::processEvent(event);
                currentChannel->processEvent(event, cache);
            }
            
            lastFocusedTimer.restart();
        }
        
        for(Channel *channel : channels)
        {
            channel->update();
        }

        if((!windowFocused || !focused) && lastFocusedTimer.getElapsedTime().asMilliseconds() > 5000)
        {
            this_thread::sleep_for(chrono::milliseconds(250));
            continue;
        }
        
        window.clear(ColorScheme::getBackgroundColor());
        ChannelSidePanel::draw(window);
        currentChannel->draw(window, cache);
        UsersSidePanel::draw(window, cache);
        ChannelTopPanel::draw(window);
        GlobalContextMenu::draw(window);
        ImagePreview::draw(window);
        
        if(waitingToJoin)
        {
            auto windowSize = window.getSize();
            
            sf::RectangleShape shadeRect(sf::Vector2f(windowSize.x, windowSize.y));
            shadeRect.setFillColor(sf::Color(0, 0, 0, 200));
            window.draw(shadeRect);
            
            const sf::Font *FONT = ResourceCache::getFont("fonts/Nunito-Regular.ttf");
            const float FONT_SIZE = (double)windowSize.x / 40.0;
            sf::Text text("Wait until you are added to the channel", *FONT, FONT_SIZE);
            text.setPosition(floor((float)windowSize.x * 0.5f - text.getLocalBounds().width * 0.5f), floor((float)windowSize.y * 0.5f - FONT->getLineSpacing(FONT_SIZE) * 0.5f));
            window.draw(text);
        }
        
        //video.draw(window);
        window.display();
    }

    onMessageByLocalUser = nullptr;
    rpcThread.join();
    
    for(Channel *channel : channels)
    {
        delete channel;
    }
    
    // We need to wait until our `ping` packet for disconnecting is sent to all channels
    printf("Shutting down...\n");
    this_thread::sleep_for(chrono::seconds(3));

    return 0;
}