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