LCOV - code coverage report
Current view: top level - lib/cwtch - ffi.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 513 0.0 %
Date: 2024-04-16 06:18:13 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:collection';
       2             : import 'dart:convert';
       3             : import 'dart:ffi';
       4             : import 'dart:io';
       5             : import 'dart:isolate';
       6             : import 'package:cwtch/cwtch/cwtchNotifier.dart';
       7             : import 'package:path/path.dart' as path;
       8             : 
       9             : import 'package:ffi/ffi.dart';
      10             : import 'package:cwtch/cwtch/cwtch.dart';
      11             : 
      12             : import '../config.dart';
      13             : 
      14             : import "package:path/path.dart" show dirname, join;
      15             : 
      16             : /////////////////////
      17             : ///   Cwtch API   ///
      18             : /////////////////////
      19             : 
      20             : typedef start_cwtch_function = Int8 Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      21             : typedef StartCwtchFn = int Function(Pointer<Utf8> dir, int len, Pointer<Utf8> tor, int torLen);
      22             : 
      23             : typedef void_from_void_funtion = Void Function();
      24             : typedef VoidFromVoidFunction = void Function();
      25             : 
      26             : typedef free_function = Void Function(Pointer<Utf8>);
      27             : typedef FreeFn = void Function(Pointer<Utf8>);
      28             : 
      29             : typedef void_from_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      30             : typedef VoidFromStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      31             : typedef VoidFromStringFn = void Function(Pointer<Utf8>, int);
      32             : 
      33             : typedef void_from_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      34             : typedef VoidFromStringStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      35             : 
      36             : typedef void_from_string_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      37             : typedef VoidFromStringStringStringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      38             : 
      39             : // DownloadFile
      40             : typedef void_from_string_int_string_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      41             : typedef VoidFromStringIntStringStringStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
      42             : 
      43             : typedef void_from_string_string_int_int_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int64, Int64);
      44             : typedef VoidFromStringStringIntIntFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int, int);
      45             : 
      46             : typedef void_from_string_bool_bool_bool = Void Function(Pointer<Utf8>, Int32, Bool, Bool, Bool);
      47             : typedef VoidFromStringBoolBoolBool = void Function(Pointer<Utf8>, int, bool, bool, bool);
      48             : 
      49             : typedef void_from_string_string_byte_function = Void Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int8);
      50             : typedef VoidFromStringStringByteFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
      51             : 
      52             : typedef string_to_void_function = Void Function(Pointer<Utf8> str, Int32 length);
      53             : typedef StringFn = void Function(Pointer<Utf8> dir, int);
      54             : 
      55             : typedef string_string_to_void_function = Void Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      56             : typedef StringStringFn = void Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      57             : 
      58             : typedef string_string_to_string_function = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length, Pointer<Utf8> str2, Int32 length2);
      59             : typedef StringFromStringStringFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      60             : 
      61             : typedef string_int_to_void_function = Void Function(Pointer<Utf8> str, Int32 length, Int32 handle);
      62             : typedef VoidFromStringIntFn = void Function(Pointer<Utf8>, int, int);
      63             : 
      64             : typedef get_json_blob_string_function = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
      65             : typedef GetJsonBlobStringFn = Pointer<Utf8> Function(Pointer<Utf8> str, int len);
      66             : 
      67             : typedef get_json_blob_from_string_int_string_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
      68             : typedef GetJsonBlobFromStrIntStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int);
      69             : 
      70             : //func GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char {
      71             : typedef get_json_blob_from_str_str_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int32);
      72             : typedef GetJsonBlobFromStrStrIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
      73             : 
      74             : typedef get_json_blob_from_str_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32);
      75             : typedef GetJsonBlobFromStrIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int);
      76             : 
      77             : typedef get_json_blob_from_str_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
      78             : typedef GetJsonBlobFromStrStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int);
      79             : 
      80             : typedef get_json_blob_from_str_int_int_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32, Pointer<Utf8>, Int32);
      81             : typedef GetJsonBlobFromStrIntIntStrFn = Pointer<Utf8> Function(
      82             :   Pointer<Utf8>,
      83             :   int,
      84             :   int,
      85             :   int,
      86             :   Pointer<Utf8>,
      87             :   int,
      88             : );
      89             : 
      90             : typedef get_json_blob_from_str_int_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32);
      91             : typedef GetJsonBlobFromStrIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, int);
      92             : 
      93             : typedef get_json_blob_from_str_int_int_int_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Int32, Int32);
      94             : typedef GetJsonBlobFromStrIntIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, int, int, int);
      95             : 
      96             : typedef get_json_blob_from_str_int_string_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
      97             : typedef GetJsonBlobFromStrIntStringFn = Pointer<Utf8> Function(
      98             :   Pointer<Utf8>,
      99             :   int,
     100             :   int,
     101             :   Pointer<Utf8>,
     102             :   int,
     103             : );
     104             : 
     105             : // func c_GetMessagesByContentHash(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, contenthash_ptr *C.char, contenthash_len C.int) *C.char
     106             : typedef get_json_blob_from_str_str_str_function = Pointer<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     107             : typedef GetJsonBlobFromStrStrStrFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     108             : 
     109             : typedef void_from_string_int_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
     110             : typedef VoidFromStringIntStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int);
     111             : 
     112             : typedef void_from_string_int_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     113             : typedef VoidFromStringIntStringStringFn = void Function(Pointer<Utf8>, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     114             : 
     115             : typedef void_from_string_int_int_int_string_string_function = Void Function(Pointer<Utf8>, Int32, Int32, Int32, Int32, Pointer<Utf8>, Int32, Pointer<Utf8>, Int32);
     116             : typedef VoidFromStringIntIntIntStringStringFn = void Function(Pointer<Utf8>, int, int, int, int, Pointer<Utf8>, int, Pointer<Utf8>, int);
     117             : 
     118             : typedef void_from_string_int_int_function = Void Function(Pointer<Utf8>, Int32, Int32, Int32);
     119             : typedef VoidFromStringIntIntFn = void Function(Pointer<Utf8>, int, int, int);
     120             : 
     121             : typedef appbus_events_function = Pointer<Utf8> Function();
     122             : typedef AppbusEventsFn = Pointer<Utf8> Function();
     123             : 
     124             : typedef void_to_string = Pointer<Utf8> Function();
     125             : typedef StringFromVoid = Pointer<Utf8> Function();
     126             : 
     127             : const String UNSUPPORTED_OS = "unsupported-os";
     128             : 
     129             : class CwtchFfi implements Cwtch {
     130             :   late DynamicLibrary library;
     131             :   late CwtchNotifier cwtchNotifier;
     132             :   late Isolate cwtchIsolate;
     133             :   ReceivePort _receivePort = ReceivePort();
     134             :   bool _isL10nInit = false;
     135             :   String _assetsDir = path.join(Directory.current.path, "data", "flutter_assets");
     136             :   String _cwtchDir = "";
     137             : 
     138           0 :   static String getLibraryPath() {
     139           0 :     if (Platform.isWindows) {
     140             :       return "libCwtch.dll";
     141           0 :     } else if (Platform.isLinux) {
     142             :       return "libCwtch.so";
     143           0 :     } else if (Platform.isMacOS) {
     144           0 :       if (Abi.current() == Abi.macosX64) {
     145             :         return "libCwtch.x64.dylib";
     146             :       } else {
     147             :         return "libCwtch.arm64.dylib";
     148             :       }
     149             :     } else {
     150             :       return UNSUPPORTED_OS;
     151             :     }
     152             :   }
     153             : 
     154           0 :   CwtchFfi(CwtchNotifier _cwtchNotifier) {
     155           0 :     String libraryPath = getLibraryPath();
     156           0 :     if (libraryPath == UNSUPPORTED_OS) {
     157           0 :       print("OS ${Platform.operatingSystem} not supported by cwtch/ffi");
     158             :       // emergency, ideally the app stays on splash and just posts the error till user closes
     159           0 :       exit(0);
     160             :     }
     161           0 :     library = DynamicLibrary.open(libraryPath);
     162           0 :     cwtchNotifier = _cwtchNotifier;
     163           0 :     cwtchNotifier.setMessageSeenCallback((String profile, int conversation, DateTime time) => {this.SetConversationAttribute(profile, conversation, LastMessageSeenTimeKey, time.toIso8601String())});
     164             :   }
     165             : 
     166             :   // ignore: non_constant_identifier_names
     167           0 :   Future<void> Start() async {
     168             :     String home = "";
     169             :     String bundledTor = "";
     170           0 :     Map<String, String> envVars = Platform.environment;
     171           0 :     if (Platform.isLinux) {
     172           0 :       home = envVars['HOME'] ?? "";
     173             :       if (EnvironmentConfig.TEST_MODE) {
     174           0 :         _cwtchDir = envVars['CWTCH_HOME']!;
     175             :       } else {
     176           0 :         _cwtchDir = envVars['CWTCH_HOME'] ?? path.join(envVars['HOME']!, ".cwtch");
     177             :       }
     178             : 
     179           0 :       if (await File("linux/Tor/tor").exists()) {
     180             :         bundledTor = "linux/Tor/tor";
     181           0 :       } else if (await File("lib/Tor/tor").exists()) {
     182             :         bundledTor = "lib/Tor/tor";
     183           0 :       } else if (await File(path.join(home, ".local/lib/cwtch/Tor/tor")).exists()) {
     184           0 :         bundledTor = path.join(home, ".local/lib/cwtch/Tor/tor");
     185           0 :         _assetsDir = path.join(home, ".local", "share", "cwtch", "data", "flutter_assets");
     186           0 :       } else if (await File("/usr/lib/cwtch/Tor/tor").exists()) {
     187             :         bundledTor = "/usr/lib/cwtch/Tor/tor";
     188           0 :         _assetsDir = path.join("usr", "share", "cwtch", "data", "flutter_assets");
     189             :       } else {
     190             :         bundledTor = "tor";
     191             :       }
     192           0 :     } else if (Platform.isWindows) {
     193           0 :       _cwtchDir = envVars['CWTCH_DIR'] ?? path.join(envVars['UserProfile']!, ".cwtch");
     194           0 :       String currentTor = path.join(Directory.current.absolute.path, "Tor\\Tor\\tor.exe");
     195           0 :       if (await File(currentTor).exists()) {
     196             :         bundledTor = currentTor;
     197           0 :         _assetsDir = path.join(Directory.current.absolute.path, "data", "flutter_assets");
     198             :       } else {
     199           0 :         String exeDir = path.dirname(Platform.resolvedExecutable);
     200           0 :         bundledTor = path.join(exeDir, "Tor\\Tor\\tor.exe");
     201           0 :         _assetsDir = path.join(exeDir, "data", "flutter_assets");
     202             :       }
     203           0 :     } else if (Platform.isMacOS) {
     204           0 :       _cwtchDir = envVars['CWTCH_HOME'] ?? path.join(envVars['HOME']!, "Library/Application Support/Cwtch");
     205           0 :       _assetsDir = "/Applications/Cwtch.app/Contents/Frameworks/App.framework/Versions/Current/Resources/flutter_assets/";
     206           0 :       if (await File("Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     207             :         bundledTor = "Cwtch.app/Contents/MacOS/Tor/tor";
     208           0 :         _assetsDir = "Cwtch.app/Contents/Frameworks/App.framework/Versions/Current/Resources/flutter_assets/";
     209           0 :       } else if (await File("/Applications/Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     210             :         bundledTor = "/Applications/Cwtch.app/Contents/MacOS/Tor/tor";
     211           0 :       } else if (await File("/Volumes/Cwtch/Cwtch.app/Contents/MacOS/Tor/tor").exists()) {
     212             :         bundledTor = "/Volumes/Cwtch/Cwtch.app/Contents/MacOS/Tor/tor";
     213           0 :       } else if (await File("/Applications/Tor Browser.app/Contents/MacOS/Tor/tor").exists()) {
     214             :         bundledTor = "/Applications/Tor Browser.app/Contents/MacOS/Tor/tor";
     215           0 :         print("We couldn't find Tor in the Cwtch app directory, however we can fall back to the Tor Browser binary");
     216             :       } else {
     217           0 :         var splitPath = path.split(dirname(Platform.script.path));
     218           0 :         if (splitPath[0] == "/" && splitPath[1] == "Applications") {
     219           0 :           var appName = splitPath[2];
     220           0 :           print("We're running in /Applications in a non standard app name: $appName");
     221           0 :           if (await File("/Applications/$appName/Contents/MacOS/Tor/tor").exists()) {
     222           0 :             bundledTor = "/Applications/$appName/Contents/MacOS/Tor/tor";
     223             :           }
     224             :         }
     225             :       }
     226             :     }
     227             : 
     228             :     // the first Cwtch MacOS release (1.2) accidently was a dev build
     229             :     // we need to temporarily remedy this for a release or two then delete
     230             :     // if macOs and release build and no profile and is dev profile
     231             :     // copy dev profile to release profile
     232           0 :     if (Platform.isMacOS && EnvironmentConfig.BUILD_VER != dev_version) {
     233           0 :       var devProfileExists = await Directory(path.join(_cwtchDir, "dev", "profiles")).exists();
     234           0 :       var releaseProfileExists = await Directory(path.join(_cwtchDir, "profiles")).exists();
     235             :       if (devProfileExists && !releaseProfileExists) {
     236           0 :         print("MacOS one time dev -> release profile migration...");
     237           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "profiles"), _cwtchDir]);
     238           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "SALT"), _cwtchDir]);
     239           0 :         await Process.run("cp", ["-r", "-p", path.join(_cwtchDir, "dev", "ui.globals"), _cwtchDir]);
     240             :       }
     241             :     }
     242             : 
     243           0 :     if (EnvironmentConfig.BUILD_VER == dev_version) {
     244           0 :       _cwtchDir = path.join(_cwtchDir, "dev");
     245             :     }
     246             : 
     247           0 :     print("StartCwtch( cwtchdir: $_cwtchDir, torPath: $bundledTor )");
     248             : 
     249           0 :     var startCwtchC = library.lookup<NativeFunction<start_cwtch_function>>("c_StartCwtch");
     250             :     // ignore: non_constant_identifier_names
     251             :     final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
     252             : 
     253           0 :     final utf8CwtchDir = _cwtchDir.toNativeUtf8();
     254           0 :     StartCwtch(utf8CwtchDir, utf8CwtchDir.length, bundledTor.toNativeUtf8(), bundledTor.length);
     255           0 :     malloc.free(utf8CwtchDir);
     256             : 
     257             :     // Spawn an isolate to listen to events from libcwtch-go and then dispatch them when received on main thread to cwtchNotifier
     258           0 :     cwtchIsolate = await Isolate.spawn(_checkAppbusEvents, _receivePort.sendPort);
     259           0 :     _receivePort.listen((message) {
     260           0 :       var env = jsonDecode(message);
     261           0 :       cwtchNotifier.handleMessage(env["EventType"], env["Data"]);
     262             :     });
     263             :   }
     264             : 
     265           0 :   String getAssetsDir() {
     266           0 :     return _assetsDir;
     267             :   }
     268             : 
     269           0 :   Future<String> getCwtchDir() async {
     270           0 :     return _cwtchDir;
     271             :   }
     272             : 
     273             :   // Called on object being disposed to (presumably on app close) to close the isolate that's listening to libcwtch-go events
     274           0 :   @override
     275             :   void dispose() {
     276           0 :     EnvironmentConfig.debugLog("tearing down cwtch FFI isolate");
     277           0 :     library.close();
     278           0 :     cwtchIsolate.kill(priority: Isolate.immediate);
     279             :   }
     280             : 
     281             :   // Entry point for an isolate to listen to a stream of events pulled from libcwtch-go and return them on the sendPort
     282           0 :   static void _checkAppbusEvents(SendPort sendPort) async {
     283           0 :     var stream = pollAppbusEvents();
     284           0 :     await for (var value in stream) {
     285           0 :       sendPort.send(value);
     286             :     }
     287           0 :     print("checkAppBusEvents finished...");
     288             :   }
     289             : 
     290             :   // Steam of appbus events. Call blocks in libcwtch-go GetAppbusEvent.  Static so the isolate can use it
     291           0 :   static Stream<String> pollAppbusEvents() async* {
     292             :     late DynamicLibrary library = DynamicLibrary.open(getLibraryPath());
     293             : 
     294           0 :     var getAppbusEventC = library.lookup<NativeFunction<appbus_events_function>>("c_GetAppBusEvent");
     295             :     // ignore: non_constant_identifier_names
     296             :     final GetAppbusEvent = getAppbusEventC.asFunction<AppbusEventsFn>();
     297             : 
     298             :     // Embedded Version of _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved
     299           0 :     var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
     300             :     final Free = free.asFunction<FreeFn>();
     301             : 
     302             :     // ignore: non_constant_identifier_names
     303           0 :     final GetAppBusEvent = () {
     304             :       // ignore: non_constant_identifier_names
     305           0 :       Pointer<Utf8> result = GetAppbusEvent();
     306           0 :       String event = result.toDartString();
     307           0 :       Free(result);
     308             :       return event;
     309             :     };
     310             : 
     311             :     while (true) {
     312           0 :       final event = GetAppBusEvent();
     313             : 
     314           0 :       if (event.startsWith("{\"EventType\":\"Shutdown\"")) {
     315           0 :         print("Shutting down isolate thread: $event");
     316             :         return;
     317             :       }
     318             :       yield event;
     319             :     }
     320             :   }
     321             : 
     322             :   // ignore: non_constant_identifier_names
     323           0 :   void CreateProfile(String nick, String pass, bool autostart) {
     324           0 :     var createProfileC = library.lookup<NativeFunction<void_from_string_string_byte_function>>("c_CreateProfile");
     325             :     // ignore: non_constant_identifier_names
     326             :     final CreateProfile = createProfileC.asFunction<VoidFromStringStringByteFn>();
     327           0 :     final utf8nick = nick.toNativeUtf8();
     328           0 :     final ut8pass = pass.toNativeUtf8();
     329           0 :     CreateProfile(utf8nick, utf8nick.length, ut8pass, ut8pass.length, autostart ? 1 : 0);
     330           0 :     malloc.free(utf8nick);
     331           0 :     malloc.free(ut8pass);
     332             :   }
     333             : 
     334             :   // ignore: non_constant_identifier_names
     335           0 :   void ActivatePeerEngine(String profile) {
     336           0 :     var activatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_ActivatePeerEngine");
     337             :     final ActivatePeerEngine = activatePeerEngineC.asFunction<StringFn>();
     338           0 :     final ut8profile = profile.toNativeUtf8();
     339           0 :     ActivatePeerEngine(ut8profile, ut8profile.length);
     340           0 :     malloc.free(ut8profile);
     341             :   }
     342             : 
     343             :   // ignore: non_constant_identifier_names
     344           0 :   void DeactivatePeerEngine(String profile) {
     345           0 :     var deactivatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_DeactivatePeerEngine");
     346             :     final DeactivatePeerEngine = deactivatePeerEngineC.asFunction<StringFn>();
     347           0 :     final ut8profile = profile.toNativeUtf8();
     348           0 :     DeactivatePeerEngine(ut8profile, ut8profile.length);
     349           0 :     malloc.free(ut8profile);
     350             :   }
     351             : 
     352             :   // ignore: non_constant_identifier_names
     353           0 :   void LoadProfiles(String pass) {
     354           0 :     var loadProfileC = library.lookup<NativeFunction<string_to_void_function>>("c_LoadProfiles");
     355             :     // ignore: non_constant_identifier_names
     356             :     final LoadProfiles = loadProfileC.asFunction<StringFn>();
     357           0 :     final ut8pass = pass.toNativeUtf8();
     358           0 :     LoadProfiles(ut8pass, ut8pass.length);
     359           0 :     malloc.free(ut8pass);
     360             :   }
     361             : 
     362             :   // ignore: non_constant_identifier_names
     363           0 :   Future<String> GetMessage(String profile, int handle, int index) async {
     364           0 :     var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessage");
     365             :     // ignore: non_constant_identifier_names
     366             :     final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
     367           0 :     final utf8profile = profile.toNativeUtf8();
     368           0 :     Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
     369           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     370           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     371           0 :     malloc.free(utf8profile);
     372             :     return jsonMessage;
     373             :   }
     374             : 
     375             :   // ignore: non_constant_identifier_names
     376           0 :   Future<dynamic> GetMessages(String profile, int handle, int index, int count) async {
     377           0 :     var getMessagesC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_int_function>>("c_GetMessages");
     378             :     // ignore: non_constant_identifier_names
     379             :     final GetMessages = getMessagesC.asFunction<GetJsonBlobFromStrIntIntIntFn>();
     380           0 :     final utf8profile = profile.toNativeUtf8();
     381           0 :     Pointer<Utf8> jsonMessageBytes = GetMessages(utf8profile, utf8profile.length, handle, index, count);
     382           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     383           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     384           0 :     malloc.free(utf8profile);
     385             : 
     386             :     return jsonMessage;
     387             :   }
     388             : 
     389           0 :   @override
     390             :   // ignore: non_constant_identifier_names
     391             :   void AcceptContact(String profileOnion, int contactHandle) {
     392           0 :     var acceptContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_AcceptConversation");
     393             :     // ignore: non_constant_identifier_names
     394             :     final AcceptContact = acceptContact.asFunction<VoidFromStringIntFn>();
     395           0 :     final u1 = profileOnion.toNativeUtf8();
     396           0 :     AcceptContact(u1, u1.length, contactHandle);
     397           0 :     malloc.free(u1);
     398             :   }
     399             : 
     400           0 :   @override
     401             :   // ignore: non_constant_identifier_names
     402             :   void BlockContact(String profileOnion, int contactHandle) {
     403           0 :     var blockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_BlockConversation");
     404             :     // ignore: non_constant_identifier_names
     405             :     final BlockContact = blockContact.asFunction<VoidFromStringIntFn>();
     406           0 :     final u1 = profileOnion.toNativeUtf8();
     407           0 :     BlockContact(u1, u1.length, contactHandle);
     408           0 :     malloc.free(u1);
     409             :   }
     410             : 
     411           0 :   @override
     412             :   // ignore: non_constant_identifier_names
     413             :   void UnblockContact(String profileOnion, int contactHandle) {
     414           0 :     var unblockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_UnblockConversation");
     415             :     // ignore: non_constant_identifier_names
     416             :     final UnblockContact = unblockContact.asFunction<VoidFromStringIntFn>();
     417           0 :     final u1 = profileOnion.toNativeUtf8();
     418           0 :     UnblockContact(u1, u1.length, contactHandle);
     419           0 :     malloc.free(u1);
     420             :   }
     421             : 
     422           0 :   @override
     423             :   // ignore: non_constant_identifier_names
     424             :   Future<dynamic> SendMessage(String profileOnion, int contactHandle, String message) async {
     425           0 :     var sendMessage = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_SendMessage");
     426             :     // ignore: non_constant_identifier_names
     427             :     final SendMessage = sendMessage.asFunction<GetJsonBlobFromStrIntStrFn>();
     428           0 :     final u1 = profileOnion.toNativeUtf8();
     429           0 :     final u3 = message.toNativeUtf8();
     430           0 :     Pointer<Utf8> jsonMessageBytes = SendMessage(u1, u1.length, contactHandle, u3, u3.length);
     431           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     432           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     433           0 :     malloc.free(u1);
     434           0 :     malloc.free(u3);
     435             :     return jsonMessage;
     436             :   }
     437             : 
     438           0 :   @override
     439             :   // ignore: non_constant_identifier_names
     440             :   Future<dynamic> SendInvitation(String profileOnion, int contactHandle, int target) async {
     441           0 :     var sendInvitation = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_SendInviteMessage");
     442             :     // ignore: non_constant_identifier_names
     443             :     final SendInvitation = sendInvitation.asFunction<GetJsonBlobFromStrIntIntFn>();
     444           0 :     final u1 = profileOnion.toNativeUtf8();
     445           0 :     Pointer<Utf8> jsonMessageBytes = SendInvitation(u1, u1.length, contactHandle, target);
     446           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     447           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     448           0 :     malloc.free(u1);
     449             :     return jsonMessage;
     450             :   }
     451             : 
     452           0 :   @override
     453             :   // ignore: non_constant_identifier_names
     454             :   Future<dynamic> ShareFile(String profileOnion, int contactHandle, String filepath) async {
     455           0 :     var shareFile = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_ShareFile");
     456             :     // ignore: non_constant_identifier_names
     457             :     final ShareFile = shareFile.asFunction<GetJsonBlobFromStrIntStrFn>();
     458           0 :     final u1 = profileOnion.toNativeUtf8();
     459           0 :     final u3 = filepath.toNativeUtf8();
     460           0 :     Pointer<Utf8> jsonMessageBytes = ShareFile(u1, u1.length, contactHandle, u3, u3.length);
     461           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     462           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     463           0 :     malloc.free(u1);
     464           0 :     malloc.free(u3);
     465             :     return jsonMessage;
     466             :   }
     467             : 
     468           0 :   @override
     469             :   // ignore: non_constant_identifier_names
     470             :   void DownloadFile(String profileOnion, int contactHandle, String filepath, String manifestpath, String filekey) {
     471           0 :     var dlFile = library.lookup<NativeFunction<void_from_string_int_string_string_string_function>>("c_DownloadFileDefaultLimit");
     472             :     // ignore: non_constant_identifier_names
     473             :     final DownloadFile = dlFile.asFunction<VoidFromStringIntStringStringStringFn>();
     474           0 :     final u1 = profileOnion.toNativeUtf8();
     475           0 :     final u3 = filepath.toNativeUtf8();
     476           0 :     final u4 = manifestpath.toNativeUtf8();
     477           0 :     final u5 = filekey.toNativeUtf8();
     478           0 :     DownloadFile(u1, u1.length, contactHandle, u3, u3.length, u4, u4.length, u5, u5.length);
     479           0 :     malloc.free(u1);
     480           0 :     malloc.free(u3);
     481           0 :     malloc.free(u4);
     482           0 :     malloc.free(u5);
     483             :   }
     484             : 
     485           0 :   @override
     486             :   // ignore: non_constant_identifier_names
     487             :   void CreateDownloadableFile(String profileOnion, int contactHandle, String filenameSuggestion, String filekey, String manifestPath) {
     488             :     // android only - do nothing
     489             :   }
     490             : 
     491             :   // ignore: non_constant_identifier_names
     492           0 :   void ExportPreviewedFile(String sourceFile, String suggestion) {
     493             :     // android only - do nothing
     494             :   }
     495             : 
     496           0 :   @override
     497             :   // ignore: non_constant_identifier_names
     498             :   void CheckDownloadStatus(String profileOnion, String fileKey) {
     499           0 :     var checkDownloadStatus = library.lookup<NativeFunction<string_string_to_void_function>>("c_CheckDownloadStatus");
     500             :     // ignore: non_constant_identifier_names
     501             :     final CheckDownloadStatus = checkDownloadStatus.asFunction<VoidFromStringStringFn>();
     502           0 :     final u1 = profileOnion.toNativeUtf8();
     503           0 :     final u2 = fileKey.toNativeUtf8();
     504           0 :     CheckDownloadStatus(u1, u1.length, u2, u2.length);
     505           0 :     malloc.free(u1);
     506           0 :     malloc.free(u2);
     507             :   }
     508             : 
     509           0 :   @override
     510             :   // ignore: non_constant_identifier_names
     511             :   void VerifyOrResumeDownload(String profileOnion, int contactHandle, String filekey) {
     512           0 :     var fn = library.lookup<NativeFunction<void_from_string_int_string_function>>("c_VerifyOrResumeDownloadDefaultLimit");
     513             :     // ignore: non_constant_identifier_names
     514             :     final VerifyOrResumeDownload = fn.asFunction<VoidFromStringIntStringFn>();
     515           0 :     final u1 = profileOnion.toNativeUtf8();
     516           0 :     final u3 = filekey.toNativeUtf8();
     517           0 :     VerifyOrResumeDownload(u1, u1.length, contactHandle, u3, u3.length);
     518           0 :     malloc.free(u1);
     519           0 :     malloc.free(u3);
     520             :   }
     521             : 
     522           0 :   @override
     523             :   // ignore: non_constant_identifier_names
     524             :   void ResetTor() {
     525           0 :     var resetTor = library.lookup<NativeFunction<Void Function()>>("c_ResetTor");
     526             :     // ignore: non_constant_identifier_names
     527             :     final ResetTor = resetTor.asFunction<void Function()>();
     528           0 :     ResetTor();
     529             :   }
     530             : 
     531           0 :   @override
     532             :   // ignore: non_constant_identifier_names
     533             :   Future<dynamic> ImportBundle(String profileOnion, String bundle) async {
     534           0 :     var importBundle = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportBundle");
     535             :     // ignore: non_constant_identifier_names
     536             :     final ImportBundle = importBundle.asFunction<StringFromStringStringFn>();
     537           0 :     final u1 = profileOnion.toNativeUtf8();
     538           0 :     final u2 = bundle.toNativeUtf8();
     539           0 :     Pointer<Utf8> responsePtr = ImportBundle(u1, u1.length, u2, u2.length);
     540           0 :     String response = responsePtr.toDartString();
     541           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(responsePtr);
     542           0 :     malloc.free(u1);
     543           0 :     malloc.free(u2);
     544             :     return response;
     545             :   }
     546             : 
     547           0 :   @override
     548             :   // ignore: non_constant_identifier_names
     549             :   void CreateGroup(String profileOnion, String server, String groupName) {
     550           0 :     var createGroup = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_StartGroup");
     551             :     // ignore: non_constant_identifier_names
     552             :     final CreateGroup = createGroup.asFunction<VoidFromStringStringStringFn>();
     553           0 :     final u1 = profileOnion.toNativeUtf8();
     554           0 :     final u3 = server.toNativeUtf8();
     555           0 :     final u2 = groupName.toNativeUtf8();
     556           0 :     CreateGroup(u1, u1.length, u2, u2.length, u3, u3.length);
     557             : 
     558           0 :     malloc.free(u1);
     559           0 :     malloc.free(u2);
     560           0 :     malloc.free(u3);
     561             :   }
     562             : 
     563           0 :   @override
     564             :   // ignore: non_constant_identifier_names
     565             :   void ArchiveConversation(String profileOnion, int handle) {
     566           0 :     var archiveConversation = library.lookup<NativeFunction<string_int_to_void_function>>("c_ArchiveConversation");
     567             :     // ignore: non_constant_identifier_names
     568             :     final ArchiveConversation = archiveConversation.asFunction<VoidFromStringIntFn>();
     569           0 :     final u1 = profileOnion.toNativeUtf8();
     570           0 :     ArchiveConversation(u1, u1.length, handle);
     571           0 :     malloc.free(u1);
     572             :   }
     573             : 
     574           0 :   @override
     575             :   // ignore: non_constant_identifier_names
     576             :   void DeleteContact(String profileOnion, int handle) {
     577           0 :     var deleteContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_DeleteConversation");
     578             :     // ignore: non_constant_identifier_names
     579             :     final DeleteContact = deleteContact.asFunction<VoidFromStringIntFn>();
     580           0 :     final u1 = profileOnion.toNativeUtf8();
     581           0 :     DeleteContact(u1, u1.length, handle);
     582           0 :     malloc.free(u1);
     583             :   }
     584             : 
     585           0 :   @override
     586             :   // ignore: non_constant_identifier_names
     587             :   void DeleteProfile(String onion, String currentPassword) {
     588           0 :     var deleteprofile = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteProfile");
     589             :     // ignore: non_constant_identifier_names
     590             :     final DeleteProfile = deleteprofile.asFunction<VoidFromStringStringFn>();
     591           0 :     final u1 = onion.toNativeUtf8();
     592           0 :     final u2 = currentPassword.toNativeUtf8();
     593           0 :     DeleteProfile(u1, u1.length, u2, u2.length);
     594           0 :     malloc.free(u1);
     595           0 :     malloc.free(u2);
     596             :   }
     597             : 
     598           0 :   @override
     599             :   // ignore: non_constant_identifier_names
     600             :   void SetProfileAttribute(String profile, String key, String val) {
     601           0 :     var setProfileAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetProfileAttribute");
     602             :     // ignore: non_constant_identifier_names
     603             :     final SetProfileAttribute = setProfileAttribute.asFunction<VoidFromStringStringStringFn>();
     604           0 :     final u1 = profile.toNativeUtf8();
     605           0 :     final u2 = key.toNativeUtf8();
     606           0 :     final u3 = val.toNativeUtf8();
     607           0 :     SetProfileAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
     608           0 :     malloc.free(u1);
     609           0 :     malloc.free(u2);
     610           0 :     malloc.free(u3);
     611             :   }
     612             : 
     613           0 :   @override
     614             :   // ignore: non_constant_identifier_names
     615             :   void SetConversationAttribute(String profile, int contact, String key, String val) {
     616           0 :     var setContactAttribute = library.lookup<NativeFunction<void_from_string_int_string_string_function>>("c_SetConversationAttribute");
     617             :     // ignore: non_constant_identifier_names
     618             :     final SetContactAttribute = setContactAttribute.asFunction<VoidFromStringIntStringStringFn>();
     619           0 :     final u1 = profile.toNativeUtf8();
     620           0 :     final u3 = key.toNativeUtf8();
     621           0 :     final u4 = val.toNativeUtf8();
     622           0 :     SetContactAttribute(u1, u1.length, contact, u3, u3.length, u4, u4.length);
     623           0 :     malloc.free(u1);
     624           0 :     malloc.free(u3);
     625           0 :     malloc.free(u4);
     626             :   }
     627             : 
     628           0 :   @override
     629             :   // ignore: non_constant_identifier_names
     630             :   void SetMessageAttribute(String profile, int conversation, int channel, int message, String key, String val) {
     631           0 :     var setMessageAttribute = library.lookup<NativeFunction<void_from_string_int_int_int_string_string_function>>("c_UpdateMessageAttribute");
     632             :     // ignore: non_constant_identifier_names
     633             :     final SetMessageAttribute = setMessageAttribute.asFunction<VoidFromStringIntIntIntStringStringFn>();
     634           0 :     final u1 = profile.toNativeUtf8();
     635           0 :     final u3 = key.toNativeUtf8();
     636           0 :     final u4 = val.toNativeUtf8();
     637           0 :     SetMessageAttribute(u1, u1.length, conversation, channel, message, u3, u3.length, u4, u4.length);
     638           0 :     malloc.free(u1);
     639           0 :     malloc.free(u3);
     640           0 :     malloc.free(u4);
     641             :   }
     642             : 
     643           0 :   @override
     644             :   // ignore: non_constant_identifier_names
     645             :   void LoadServers(String password) {
     646           0 :     var loadServers = library.lookup<NativeFunction<string_to_void_function>>("c_LoadServers");
     647             :     // ignore: non_constant_identifier_names
     648             :     final LoadServers = loadServers.asFunction<StringFn>();
     649           0 :     final u1 = password.toNativeUtf8();
     650           0 :     LoadServers(u1, u1.length);
     651           0 :     malloc.free(u1);
     652             :   }
     653             : 
     654           0 :   @override
     655             :   // ignore: non_constant_identifier_names
     656             :   void CreateServer(String password, String description, bool autostart) {
     657           0 :     var createServer = library.lookup<NativeFunction<void_from_string_string_byte_function>>("c_CreateServer");
     658             :     // ignore: non_constant_identifier_names
     659             :     final CreateServer = createServer.asFunction<VoidFromStringStringByteFn>();
     660           0 :     final u1 = password.toNativeUtf8();
     661           0 :     final u2 = description.toNativeUtf8();
     662           0 :     CreateServer(u1, u1.length, u2, u2.length, autostart ? 1 : 0);
     663           0 :     malloc.free(u1);
     664           0 :     malloc.free(u2);
     665             :   }
     666             : 
     667           0 :   @override
     668             :   // ignore: non_constant_identifier_names
     669             :   void DeleteServer(String serverOnion, String password) {
     670           0 :     var deleteServer = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteServer");
     671             :     // ignore: non_constant_identifier_names
     672             :     final DeleteServer = deleteServer.asFunction<VoidFromStringStringFn>();
     673           0 :     final u1 = serverOnion.toNativeUtf8();
     674           0 :     final u2 = password.toNativeUtf8();
     675           0 :     DeleteServer(u1, u1.length, u2, u2.length);
     676           0 :     malloc.free(u1);
     677           0 :     malloc.free(u2);
     678             :   }
     679             : 
     680           0 :   @override
     681             :   // ignore: non_constant_identifier_names
     682             :   void LaunchServers() {
     683           0 :     var launchServers = library.lookup<NativeFunction<Void Function()>>("c_LaunchServers");
     684             :     // ignore: non_constant_identifier_names
     685             :     final LaunchServers = launchServers.asFunction<void Function()>();
     686           0 :     LaunchServers();
     687             :   }
     688             : 
     689           0 :   @override
     690             :   // ignore: non_constant_identifier_names
     691             :   void LaunchServer(String serverOnion) {
     692           0 :     var launchServer = library.lookup<NativeFunction<string_to_void_function>>("c_LaunchServer");
     693             :     // ignore: non_constant_identifier_names
     694             :     final LaunchServer = launchServer.asFunction<StringFn>();
     695           0 :     final u1 = serverOnion.toNativeUtf8();
     696           0 :     LaunchServer(u1, u1.length);
     697           0 :     malloc.free(u1);
     698             :   }
     699             : 
     700           0 :   @override
     701             :   // ignore: non_constant_identifier_names
     702             :   void StopServer(String serverOnion) {
     703           0 :     var shutdownServer = library.lookup<NativeFunction<string_to_void_function>>("c_StopServer");
     704             :     // ignore: non_constant_identifier_names
     705             :     final ShutdownServer = shutdownServer.asFunction<StringFn>();
     706           0 :     final u1 = serverOnion.toNativeUtf8();
     707           0 :     ShutdownServer(u1, u1.length);
     708           0 :     malloc.free(u1);
     709             :   }
     710             : 
     711           0 :   @override
     712             :   // ignore: non_constant_identifier_names
     713             :   void StopServers() {
     714           0 :     var shutdownServers = library.lookup<NativeFunction<Void Function()>>("c_StopServers");
     715             :     // ignore: non_constant_identifier_names
     716             :     final ShutdownServers = shutdownServers.asFunction<void Function()>();
     717           0 :     ShutdownServers();
     718             :   }
     719             : 
     720           0 :   @override
     721             :   // ignore: non_constant_identifier_names
     722             :   void DestroyServers() {
     723           0 :     var destroyServers = library.lookup<NativeFunction<Void Function()>>("c_DestroyServers");
     724             :     // ignore: non_constant_identifier_names
     725             :     final DestroyServers = destroyServers.asFunction<void Function()>();
     726           0 :     DestroyServers();
     727             :   }
     728             : 
     729           0 :   @override
     730             :   // ignore: non_constant_identifier_names
     731             :   void SetServerAttribute(String serverOnion, String key, String val) {
     732           0 :     var setServerAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetServerAttribute");
     733             :     // ignore: non_constant_identifier_names
     734             :     final SetServerAttribute = setServerAttribute.asFunction<VoidFromStringStringStringFn>();
     735           0 :     final u1 = serverOnion.toNativeUtf8();
     736           0 :     final u2 = key.toNativeUtf8();
     737           0 :     final u3 = val.toNativeUtf8();
     738           0 :     SetServerAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
     739           0 :     malloc.free(u1);
     740           0 :     malloc.free(u2);
     741           0 :     malloc.free(u3);
     742             :   }
     743             : 
     744           0 :   @override
     745             :   // ignore: non_constant_identifier_names
     746             :   Future<void> Shutdown() async {
     747           0 :     var shutdown = library.lookup<NativeFunction<void_from_void_funtion>>("c_ShutdownCwtch");
     748             :     // ignore: non_constant_identifier_names
     749             : 
     750             :     // Shutdown Cwtch + Tor...
     751             :     // ignore: non_constant_identifier_names
     752             :     final Shutdown = shutdown.asFunction<VoidFromVoidFunction>();
     753           0 :     Shutdown();
     754             : 
     755             :     // Kill our Isolate
     756           0 :     cwtchIsolate.kill(priority: Isolate.immediate);
     757           0 :     print("Isolate killed");
     758             : 
     759           0 :     _receivePort.close();
     760           0 :     print("Receive Port Closed");
     761             :   }
     762             : 
     763           0 :   @override
     764             :   // ignore: non_constant_identifier_names
     765             :   Future GetMessageByContentHash(String profile, int handle, String contentHash) async {
     766           0 :     var getMessagesByContentHashC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetMessageByContentHash");
     767             :     // ignore: non_constant_identifier_names
     768             :     final GetMessagesByContentHash = getMessagesByContentHashC.asFunction<GetJsonBlobFromStrIntStringFn>();
     769           0 :     final utf8profile = profile.toNativeUtf8();
     770           0 :     final utf8contentHash = contentHash.toNativeUtf8();
     771           0 :     Pointer<Utf8> jsonMessageBytes = GetMessagesByContentHash(utf8profile, utf8profile.length, handle, utf8contentHash, utf8contentHash.length);
     772           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     773             : 
     774           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     775           0 :     malloc.free(utf8profile);
     776           0 :     malloc.free(utf8contentHash);
     777             :     return jsonMessage;
     778             :   }
     779             : 
     780             :   // ignore: non_constant_identifier_names
     781             :   // Incredibly dangerous function which invokes a free in libCwtch, should only be used
     782             :   // as documented in `MEMORY.md` in libCwtch repo.
     783           0 :   void _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(Pointer<Utf8> ptr) {
     784           0 :     var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
     785             :     final Free = free.asFunction<FreeFn>();
     786           0 :     Free(ptr);
     787             :   }
     788             : 
     789           0 :   @override
     790             :   String? defaultDownloadPath() {
     791           0 :     Map<String, String> envVars = Platform.environment;
     792           0 :     String nominalPath = path.join(envVars[Platform.isWindows ? 'UserProfile' : 'HOME']!, "Downloads");
     793           0 :     if (Directory(nominalPath).existsSync() == false) {
     794           0 :       return Directory.current.path;
     795             :     }
     796             :     return nominalPath;
     797             :   }
     798             : 
     799           0 :   @override
     800             :   // ignore: non_constant_identifier_names
     801             :   Future<String> GetMessageByID(String profile, int handle, int index) async {
     802           0 :     var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessageById");
     803             :     // ignore: non_constant_identifier_names
     804             :     final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
     805           0 :     final utf8profile = profile.toNativeUtf8();
     806           0 :     Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
     807           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     808           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     809           0 :     malloc.free(utf8profile);
     810             :     return jsonMessage;
     811             :   }
     812             : 
     813           0 :   @override
     814             :   // ignore: non_constant_identifier_names
     815             :   void ChangePassword(String profile, String pass, String newpass, String newpassAgain) {
     816           0 :     var changePasswordC = library.lookup<NativeFunction<void_from_string_string_string_string_function>>("c_ChangePassword");
     817             :     // ignore: non_constant_identifier_names
     818             :     final ChangePasswordFn = changePasswordC.asFunction<VoidFromStringStringStringStringFn>();
     819           0 :     final utf8profile = profile.toNativeUtf8();
     820           0 :     final utf8pass = pass.toNativeUtf8();
     821           0 :     final utf8newpass = newpass.toNativeUtf8();
     822           0 :     final utf8newpasssagain = newpassAgain.toNativeUtf8();
     823           0 :     ChangePasswordFn(utf8profile, utf8profile.length, utf8pass, utf8pass.length, utf8newpass, utf8newpass.length, utf8newpasssagain, utf8newpasssagain.length);
     824           0 :     malloc.free(utf8profile);
     825           0 :     malloc.free(utf8pass);
     826           0 :     malloc.free(utf8newpass);
     827           0 :     malloc.free(utf8newpasssagain);
     828             :   }
     829             : 
     830           0 :   @override
     831             :   bool isL10nInit() {
     832           0 :     return _isL10nInit;
     833             :   }
     834             : 
     835           0 :   @override
     836             :   void l10nInit(String notificationSimple, String notificationConversationInfo) {
     837           0 :     cwtchNotifier.l10nInit(notificationSimple, notificationConversationInfo);
     838           0 :     _isL10nInit = true;
     839             :   }
     840             : 
     841           0 :   @override
     842             :   // ignore: non_constant_identifier_names
     843             :   void ExportProfile(String profile, String file) {
     844           0 :     final utf8profile = profile.toNativeUtf8();
     845           0 :     final utf8file = file.toNativeUtf8();
     846           0 :     var exportProfileC = library.lookup<NativeFunction<void_from_string_string_function>>("c_ExportProfile");
     847             :     // ignore: non_constant_identifier_names
     848             :     final ExportProfileFn = exportProfileC.asFunction<VoidFromStringStringFn>();
     849           0 :     ExportProfileFn(utf8profile, utf8profile.length, utf8file, utf8file.length);
     850           0 :     malloc.free(utf8profile);
     851           0 :     malloc.free(utf8file);
     852             :   }
     853             : 
     854           0 :   @override
     855             :   // ignore: non_constant_identifier_names
     856             :   Future<String> ImportProfile(String file, String pass) async {
     857           0 :     final utf8pass = pass.toNativeUtf8();
     858           0 :     final utf8file = file.toNativeUtf8();
     859           0 :     var exportProfileC = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportProfile");
     860             :     // ignore: non_constant_identifier_names
     861             :     final ExportProfileFn = exportProfileC.asFunction<StringFromStringStringFn>();
     862           0 :     Pointer<Utf8> result = ExportProfileFn(utf8file, utf8file.length, utf8pass, utf8pass.length);
     863           0 :     String importResult = result.toDartString();
     864           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
     865           0 :     malloc.free(utf8pass);
     866           0 :     malloc.free(utf8file);
     867             :     return importResult;
     868             :   }
     869             : 
     870           0 :   @override
     871             :   Future<String> GetDebugInfo() async {
     872           0 :     var getDebugInfo = library.lookup<NativeFunction<void_to_string>>("c_GetDebugInfo");
     873             :     final GetDebugInfo = getDebugInfo.asFunction<StringFromVoid>();
     874           0 :     Pointer<Utf8> result = GetDebugInfo();
     875           0 :     String debugResult = result.toDartString();
     876           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
     877             :     return debugResult;
     878             :   }
     879             : 
     880           0 :   @override
     881             :   Future<String> GetSharedFiles(String profile, int handle) async {
     882           0 :     var getSharedFiles = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_GetSharedFiles");
     883             :     final GetSharedFiles = getSharedFiles.asFunction<GetJsonBlobFromStrIntFn>();
     884           0 :     final utf8profile = profile.toNativeUtf8();
     885           0 :     Pointer<Utf8> jsonMessageBytes = GetSharedFiles(utf8profile, utf8profile.length, handle);
     886           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     887           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     888           0 :     malloc.free(utf8profile);
     889             :     return jsonMessage;
     890             :   }
     891             : 
     892           0 :   @override
     893             :   void RestartSharing(String profile, String filekey) {
     894           0 :     var restartSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_RestartFileShare");
     895             :     // ignore: non_constant_identifier_names
     896             :     final RestartSharing = restartSharingC.asFunction<VoidFromStringStringFn>();
     897           0 :     final utf8profile = profile.toNativeUtf8();
     898           0 :     final ut8filekey = filekey.toNativeUtf8();
     899           0 :     RestartSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
     900           0 :     malloc.free(utf8profile);
     901           0 :     malloc.free(ut8filekey);
     902             :   }
     903             : 
     904           0 :   @override
     905             :   void StopSharing(String profile, String filekey) {
     906           0 :     var stopSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_StopFileShare");
     907             :     // ignore: non_constant_identifier_names
     908             :     final StopSharing = stopSharingC.asFunction<VoidFromStringStringFn>();
     909           0 :     final utf8profile = profile.toNativeUtf8();
     910           0 :     final ut8filekey = filekey.toNativeUtf8();
     911           0 :     StopSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
     912           0 :     malloc.free(utf8profile);
     913           0 :     malloc.free(ut8filekey);
     914             :   }
     915             : 
     916           0 :   @override
     917             :   void DeleteServerInfo(String profile, String handle) {
     918           0 :     var deleteServerInfoC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DeleteServerInfo");
     919             :     // ignore: non_constant_identifier_names
     920             :     final StopSharing = deleteServerInfoC.asFunction<VoidFromStringStringFn>();
     921           0 :     final utf8profile = profile.toNativeUtf8();
     922           0 :     final ut8handle = handle.toNativeUtf8();
     923           0 :     StopSharing(utf8profile, utf8profile.length, ut8handle, ut8handle.length);
     924           0 :     malloc.free(utf8profile);
     925           0 :     malloc.free(ut8handle);
     926             :   }
     927             : 
     928           0 :   @override
     929             :   void UpdateSettings(String json) {
     930           0 :     var updateSettings = library.lookup<NativeFunction<string_to_void_function>>("c_UpdateSettings");
     931             :     // ignore: non_constant_identifier_names
     932             :     final UpdateSettingsFn = updateSettings.asFunction<VoidFromStringFn>();
     933           0 :     final u1 = json.toNativeUtf8();
     934           0 :     UpdateSettingsFn(u1, u1.length);
     935           0 :     malloc.free(u1);
     936             :   }
     937             : 
     938           0 :   @override
     939             :   bool IsServersCompiled() {
     940           0 :     return library.providesSymbol("c_LoadServers");
     941             :   }
     942             : 
     943           0 :   @override
     944             :   Future<String> SummarizeConversation(String profile, int conversation) async {
     945           0 :     if (!library.providesSymbol("c_Summarize")) {
     946           0 :       return Future.value("");
     947             :     }
     948           0 :     var summarize = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_Summarize");
     949             :     // ignore: non_constant_identifier_names
     950             :     final SummarizeFn = summarize.asFunction<GetJsonBlobFromStrIntFn>();
     951           0 :     final utf8profile = profile.toNativeUtf8();
     952           0 :     Pointer<Utf8> jsonMessageBytes = SummarizeFn(utf8profile, utf8profile.length, conversation);
     953           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     954           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     955           0 :     malloc.free(utf8profile);
     956             :     return jsonMessage;
     957             :   }
     958             : 
     959           0 :   @override
     960             :   Future<String> TranslateMessage(String profile, int conversation, int message, String language) async {
     961           0 :     if (!library.providesSymbol("c_Translate")) {
     962           0 :       return Future.value("");
     963             :     }
     964           0 :     var translate = library.lookup<NativeFunction<get_json_blob_from_str_int_int_str_function>>("c_Translate");
     965             :     // ignore: non_constant_identifier_names
     966             :     final TranslateFn = translate.asFunction<GetJsonBlobFromStrIntIntStrFn>();
     967           0 :     final utf8profile = profile.toNativeUtf8();
     968           0 :     final utf8lang = language.toNativeUtf8();
     969           0 :     Pointer<Utf8> jsonMessageBytes = TranslateFn(
     970             :       utf8profile,
     971           0 :       utf8profile.length,
     972             :       conversation,
     973             :       message,
     974             :       utf8lang,
     975           0 :       utf8lang.length,
     976             :     );
     977           0 :     String jsonMessage = jsonMessageBytes.toDartString();
     978           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
     979           0 :     malloc.free(utf8profile);
     980           0 :     malloc.free(utf8lang);
     981             :     return jsonMessage;
     982             :   }
     983             : 
     984           0 :   @override
     985             :   bool IsBlodeuweddSupported() {
     986           0 :     if (library.providesSymbol("c_Translate")) {
     987             :       return true;
     988             :     }
     989             :     return false;
     990             :   }
     991             : 
     992           0 :   @override
     993             :   Future<String?> GetProfileAttribute(String profile, String key) {
     994           0 :     var getProfileAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_str_function>>("c_GetProfileAttribute");
     995             :     // ignore: non_constant_identifier_names
     996             :     final GetProfileAttribute = getProfileAttributeC.asFunction<GetJsonBlobFromStrStrFn>();
     997           0 :     final utf8profile = profile.toNativeUtf8();
     998           0 :     final utf8key = key.toNativeUtf8();
     999           0 :     Pointer<Utf8> jsonMessageBytes = GetProfileAttribute(utf8profile, utf8profile.length, utf8key, utf8key.length);
    1000           0 :     String jsonMessage = jsonMessageBytes.toDartString();
    1001           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
    1002           0 :     malloc.free(utf8profile);
    1003           0 :     malloc.free(utf8key);
    1004             : 
    1005             :     try {
    1006           0 :       dynamic attributeResult = json.decode(jsonMessage);
    1007           0 :       if (attributeResult["Exists"]) {
    1008           0 :         return Future.value(attributeResult["Value"]);
    1009             :       }
    1010             :     } catch (e) {
    1011           0 :       EnvironmentConfig.debugLog("error getting profile attribute: $e");
    1012             :     }
    1013             : 
    1014           0 :     return Future.value(null);
    1015             :   }
    1016             : 
    1017           0 :   @override
    1018             :   Future<String?> GetConversationAttribute(String profile, int conversation, String key) {
    1019           0 :     var getConversationAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetConversationAttribute");
    1020             :     // ignore: non_constant_identifier_names
    1021             :     final GetConversationAttribute = getConversationAttributeC.asFunction<GetJsonBlobFromStrIntStringFn>();
    1022           0 :     final utf8profile = profile.toNativeUtf8();
    1023           0 :     final utf8key = key.toNativeUtf8();
    1024           0 :     Pointer<Utf8> jsonMessageBytes = GetConversationAttribute(utf8profile, utf8profile.length, conversation, utf8key, utf8key.length);
    1025           0 :     String jsonMessage = jsonMessageBytes.toDartString();
    1026           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
    1027           0 :     malloc.free(utf8profile);
    1028           0 :     malloc.free(utf8key);
    1029             : 
    1030             :     try {
    1031           0 :       dynamic attributeResult = json.decode(jsonMessage);
    1032           0 :       if (attributeResult["Exists"]) {
    1033           0 :         return Future.value(attributeResult["Value"]);
    1034             :       }
    1035             :     } catch (e) {
    1036           0 :       EnvironmentConfig.debugLog("error getting profile attribute: $e");
    1037             :     }
    1038             : 
    1039           0 :     return Future.value(null);
    1040             :   }
    1041             : 
    1042           0 :   @override
    1043             :   void AttemptReconnection(String profile, String onion) {
    1044             :     // ignore: non_constant_identifier_names
    1045           0 :     var peerWithOnionC = library.lookup<NativeFunction<void_from_string_string_function>>("c_PeerWithOnion");
    1046             :     final PeerWithOnionF = peerWithOnionC.asFunction<VoidFromStringStringFn>();
    1047           0 :     final utf8profile = profile.toNativeUtf8();
    1048           0 :     final utf8onion = onion.toNativeUtf8();
    1049           0 :     PeerWithOnionF(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1050           0 :     malloc.free(utf8profile);
    1051           0 :     malloc.free(utf8onion);
    1052             :   }
    1053             : 
    1054           0 :   @override
    1055             :   void AttemptReconnectionServer(String profile, String onion) {
    1056             :     // ignore: non_constant_identifier_names
    1057           0 :     var queueJoinServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_QueueJoinServer");
    1058             :     final QueueJoinServerC = queueJoinServerC.asFunction<VoidFromStringStringFn>();
    1059           0 :     final utf8profile = profile.toNativeUtf8();
    1060           0 :     final utf8onion = onion.toNativeUtf8();
    1061           0 :     QueueJoinServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1062           0 :     malloc.free(utf8profile);
    1063           0 :     malloc.free(utf8onion);
    1064             :   }
    1065             : 
    1066           0 :   @override
    1067             :   void DisconnectFromPeer(String profile, String onion) {
    1068             :     // ignore: non_constant_identifier_names
    1069           0 :     var disconnectFromPeerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromPeer");
    1070             :     final DisconnectFromPeerC = disconnectFromPeerC.asFunction<VoidFromStringStringFn>();
    1071           0 :     final utf8profile = profile.toNativeUtf8();
    1072           0 :     final utf8onion = onion.toNativeUtf8();
    1073           0 :     DisconnectFromPeerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1074           0 :     malloc.free(utf8profile);
    1075           0 :     malloc.free(utf8onion);
    1076             :   }
    1077             : 
    1078           0 :   @override
    1079             :   void DisconnectFromServer(String profile, String onion) {
    1080             :     // ignore: non_constant_identifier_names
    1081           0 :     var disconnectFromServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromServer");
    1082             :     final DisconnectFromServerC = disconnectFromServerC.asFunction<VoidFromStringStringFn>();
    1083           0 :     final utf8profile = profile.toNativeUtf8();
    1084           0 :     final utf8onion = onion.toNativeUtf8();
    1085           0 :     DisconnectFromServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
    1086           0 :     malloc.free(utf8profile);
    1087           0 :     malloc.free(utf8onion);
    1088             :   }
    1089             : 
    1090           0 :   @override
    1091             :   Future<String> SearchConversations(String profile, String pattern) async {
    1092           0 :     var searchConversationsC = library.lookup<NativeFunction<string_string_to_string_function>>("c_SearchConversations");
    1093             :     // ignore: non_constant_identifier_names
    1094             :     final SearchConversations = searchConversationsC.asFunction<StringFromStringStringFn>();
    1095           0 :     final utf8profile = profile.toNativeUtf8();
    1096           0 :     final utf8pattern = pattern.toNativeUtf8();
    1097           0 :     EnvironmentConfig.debugLog("Searching for $profile $pattern");
    1098           0 :     Pointer<Utf8> searchIDRaw = SearchConversations(utf8profile, utf8profile.length, utf8pattern, utf8pattern.length);
    1099           0 :     String searchID = searchIDRaw.toDartString();
    1100           0 :     _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(searchIDRaw);
    1101           0 :     malloc.free(utf8profile);
    1102           0 :     malloc.free(utf8pattern);
    1103             :     return searchID;
    1104             :   }
    1105             : 
    1106           0 :   @override
    1107             :   Future<HashMap<String, String>> PlatformChannelInfo() {
    1108           0 :     return Future.value(HashMap<String, String>());
    1109             :   }
    1110             : 
    1111           0 :   @override
    1112             :   Future<void> ConfigureConnections(String profile, bool listen, bool peers, bool servers) async {
    1113           0 :     var configureConnections = library.lookup<NativeFunction<void_from_string_bool_bool_bool>>("c_ConfigureConnections");
    1114             :     // ignore: non_constant_identifier_names
    1115             :     final ConfigureConnections = configureConnections.asFunction<VoidFromStringBoolBoolBool>();
    1116           0 :     final utf8profile = profile.toNativeUtf8();
    1117           0 :     ConfigureConnections(utf8profile, utf8profile.length, listen, peers, servers);
    1118           0 :     malloc.free(utf8profile);
    1119             :     return;
    1120             :   }
    1121             : 
    1122           0 :   @override
    1123             :   void PublishServerUpdate(String profile) {
    1124           0 :     var publishServerUpdate = library.lookup<NativeFunction<string_to_void_function>>("c_PublishServerUpdate");
    1125             :     // ignore: non_constant_identifier_names
    1126             :     final PublishServerUpdate = publishServerUpdate.asFunction<StringFn>();
    1127           0 :     final utf8profile = profile.toNativeUtf8();
    1128           0 :     PublishServerUpdate(utf8profile, utf8profile.length);
    1129           0 :     malloc.free(utf8profile);
    1130             :   }
    1131             : 
    1132           0 :   @override
    1133             :   bool IsLoaded() {
    1134           0 :     bool check = library.providesSymbol("c_UpdateSettings");
    1135           0 :     EnvironmentConfig.debugLog("Checking that the FFI Interface is Correctly Loaded... $check");
    1136             :     return check;
    1137             :   }
    1138             : }

Generated by: LCOV version 1.14