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 : // ignore: non_constant_identifier_names
274 0 : Future<void> ReconnectCwtchForeground() async {
275 0 : var reconnectCwtch = library.lookup<NativeFunction<Void Function()>>("c_ReconnectCwtchForeground");
276 : // ignore: non_constant_identifier_names
277 : final ReconnectCwtchForeground = reconnectCwtch.asFunction<void Function()>();
278 0 : ReconnectCwtchForeground();
279 : }
280 :
281 : // Called on object being disposed to (presumably on app close) to close the isolate that's listening to libcwtch-go events
282 0 : @override
283 : void dispose() {
284 0 : EnvironmentConfig.debugLog("tearing down cwtch FFI isolate");
285 0 : library.close();
286 0 : cwtchIsolate.kill(priority: Isolate.immediate);
287 : }
288 :
289 : // Entry point for an isolate to listen to a stream of events pulled from libcwtch-go and return them on the sendPort
290 0 : static void _checkAppbusEvents(SendPort sendPort) async {
291 0 : var stream = pollAppbusEvents();
292 0 : await for (var value in stream) {
293 0 : sendPort.send(value);
294 : }
295 0 : print("checkAppBusEvents finished...");
296 : }
297 :
298 : // Steam of appbus events. Call blocks in libcwtch-go GetAppbusEvent. Static so the isolate can use it
299 0 : static Stream<String> pollAppbusEvents() async* {
300 : late DynamicLibrary library = DynamicLibrary.open(getLibraryPath());
301 :
302 0 : var getAppbusEventC = library.lookup<NativeFunction<appbus_events_function>>("c_GetAppBusEvent");
303 : // ignore: non_constant_identifier_names
304 : final GetAppbusEvent = getAppbusEventC.asFunction<AppbusEventsFn>();
305 :
306 : // Embedded Version of _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved
307 0 : var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
308 : final Free = free.asFunction<FreeFn>();
309 :
310 : // ignore: non_constant_identifier_names
311 0 : final GetAppBusEvent = () {
312 : // ignore: non_constant_identifier_names
313 0 : Pointer<Utf8> result = GetAppbusEvent();
314 0 : String event = result.toDartString();
315 0 : Free(result);
316 : return event;
317 : };
318 :
319 : while (true) {
320 0 : final event = GetAppBusEvent();
321 :
322 0 : if (event.startsWith("{\"EventType\":\"Shutdown\"")) {
323 0 : print("Shutting down isolate thread: $event");
324 : return;
325 : }
326 : yield event;
327 : }
328 : }
329 :
330 : // ignore: non_constant_identifier_names
331 0 : void CreateProfile(String nick, String pass, bool autostart) {
332 0 : var createProfileC = library.lookup<NativeFunction<void_from_string_string_byte_function>>("c_CreateProfile");
333 : // ignore: non_constant_identifier_names
334 : final CreateProfile = createProfileC.asFunction<VoidFromStringStringByteFn>();
335 0 : final utf8nick = nick.toNativeUtf8();
336 0 : final ut8pass = pass.toNativeUtf8();
337 0 : CreateProfile(utf8nick, utf8nick.length, ut8pass, ut8pass.length, autostart ? 1 : 0);
338 0 : malloc.free(utf8nick);
339 0 : malloc.free(ut8pass);
340 : }
341 :
342 : // ignore: non_constant_identifier_names
343 0 : void ActivatePeerEngine(String profile) {
344 0 : var activatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_ActivatePeerEngine");
345 : final ActivatePeerEngine = activatePeerEngineC.asFunction<StringFn>();
346 0 : final ut8profile = profile.toNativeUtf8();
347 0 : ActivatePeerEngine(ut8profile, ut8profile.length);
348 0 : malloc.free(ut8profile);
349 : }
350 :
351 : // ignore: non_constant_identifier_names
352 0 : void DeactivatePeerEngine(String profile) {
353 0 : var deactivatePeerEngineC = library.lookup<NativeFunction<string_to_void_function>>("c_DeactivatePeerEngine");
354 : final DeactivatePeerEngine = deactivatePeerEngineC.asFunction<StringFn>();
355 0 : final ut8profile = profile.toNativeUtf8();
356 0 : DeactivatePeerEngine(ut8profile, ut8profile.length);
357 0 : malloc.free(ut8profile);
358 : }
359 :
360 : // ignore: non_constant_identifier_names
361 0 : void LoadProfiles(String pass) {
362 0 : var loadProfileC = library.lookup<NativeFunction<string_to_void_function>>("c_LoadProfiles");
363 : // ignore: non_constant_identifier_names
364 : final LoadProfiles = loadProfileC.asFunction<StringFn>();
365 0 : final ut8pass = pass.toNativeUtf8();
366 0 : LoadProfiles(ut8pass, ut8pass.length);
367 0 : malloc.free(ut8pass);
368 : }
369 :
370 : // ignore: non_constant_identifier_names
371 0 : Future<String> GetMessage(String profile, int handle, int index) async {
372 0 : var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessage");
373 : // ignore: non_constant_identifier_names
374 : final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
375 0 : final utf8profile = profile.toNativeUtf8();
376 0 : Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
377 0 : String jsonMessage = jsonMessageBytes.toDartString();
378 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
379 0 : malloc.free(utf8profile);
380 : return jsonMessage;
381 : }
382 :
383 : // ignore: non_constant_identifier_names
384 0 : Future<dynamic> GetMessages(String profile, int handle, int index, int count) async {
385 0 : var getMessagesC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_int_function>>("c_GetMessages");
386 : // ignore: non_constant_identifier_names
387 : final GetMessages = getMessagesC.asFunction<GetJsonBlobFromStrIntIntIntFn>();
388 0 : final utf8profile = profile.toNativeUtf8();
389 0 : Pointer<Utf8> jsonMessageBytes = GetMessages(utf8profile, utf8profile.length, handle, index, count);
390 0 : String jsonMessage = jsonMessageBytes.toDartString();
391 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
392 0 : malloc.free(utf8profile);
393 :
394 : return jsonMessage;
395 : }
396 :
397 0 : @override
398 : // ignore: non_constant_identifier_names
399 : void AcceptContact(String profileOnion, int contactHandle) {
400 0 : var acceptContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_AcceptConversation");
401 : // ignore: non_constant_identifier_names
402 : final AcceptContact = acceptContact.asFunction<VoidFromStringIntFn>();
403 0 : final u1 = profileOnion.toNativeUtf8();
404 0 : AcceptContact(u1, u1.length, contactHandle);
405 0 : malloc.free(u1);
406 : }
407 :
408 0 : @override
409 : // ignore: non_constant_identifier_names
410 : void BlockContact(String profileOnion, int contactHandle) {
411 0 : var blockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_BlockConversation");
412 : // ignore: non_constant_identifier_names
413 : final BlockContact = blockContact.asFunction<VoidFromStringIntFn>();
414 0 : final u1 = profileOnion.toNativeUtf8();
415 0 : BlockContact(u1, u1.length, contactHandle);
416 0 : malloc.free(u1);
417 : }
418 :
419 0 : @override
420 : // ignore: non_constant_identifier_names
421 : void UnblockContact(String profileOnion, int contactHandle) {
422 0 : var unblockContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_UnblockConversation");
423 : // ignore: non_constant_identifier_names
424 : final UnblockContact = unblockContact.asFunction<VoidFromStringIntFn>();
425 0 : final u1 = profileOnion.toNativeUtf8();
426 0 : UnblockContact(u1, u1.length, contactHandle);
427 0 : malloc.free(u1);
428 : }
429 :
430 0 : @override
431 : // ignore: non_constant_identifier_names
432 : Future<dynamic> SendMessage(String profileOnion, int contactHandle, String message) async {
433 0 : var sendMessage = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_SendMessage");
434 : // ignore: non_constant_identifier_names
435 : final SendMessage = sendMessage.asFunction<GetJsonBlobFromStrIntStrFn>();
436 0 : final u1 = profileOnion.toNativeUtf8();
437 0 : final u3 = message.toNativeUtf8();
438 0 : Pointer<Utf8> jsonMessageBytes = SendMessage(u1, u1.length, contactHandle, u3, u3.length);
439 0 : String jsonMessage = jsonMessageBytes.toDartString();
440 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
441 0 : malloc.free(u1);
442 0 : malloc.free(u3);
443 : return jsonMessage;
444 : }
445 :
446 0 : @override
447 : // ignore: non_constant_identifier_names
448 : Future<dynamic> SendInvitation(String profileOnion, int contactHandle, int target) async {
449 0 : var sendInvitation = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_SendInviteMessage");
450 : // ignore: non_constant_identifier_names
451 : final SendInvitation = sendInvitation.asFunction<GetJsonBlobFromStrIntIntFn>();
452 0 : final u1 = profileOnion.toNativeUtf8();
453 0 : Pointer<Utf8> jsonMessageBytes = SendInvitation(u1, u1.length, contactHandle, target);
454 0 : String jsonMessage = jsonMessageBytes.toDartString();
455 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
456 0 : malloc.free(u1);
457 : return jsonMessage;
458 : }
459 :
460 0 : @override
461 : // ignore: non_constant_identifier_names
462 : Future<dynamic> ShareFile(String profileOnion, int contactHandle, String filepath) async {
463 0 : var shareFile = library.lookup<NativeFunction<get_json_blob_from_string_int_string_function>>("c_ShareFile");
464 : // ignore: non_constant_identifier_names
465 : final ShareFile = shareFile.asFunction<GetJsonBlobFromStrIntStrFn>();
466 0 : final u1 = profileOnion.toNativeUtf8();
467 0 : final u3 = filepath.toNativeUtf8();
468 0 : Pointer<Utf8> jsonMessageBytes = ShareFile(u1, u1.length, contactHandle, u3, u3.length);
469 0 : String jsonMessage = jsonMessageBytes.toDartString();
470 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
471 0 : malloc.free(u1);
472 0 : malloc.free(u3);
473 : return jsonMessage;
474 : }
475 :
476 0 : @override
477 : // ignore: non_constant_identifier_names
478 : void DownloadFile(String profileOnion, int contactHandle, String filepath, String manifestpath, String filekey) {
479 0 : var dlFile = library.lookup<NativeFunction<void_from_string_int_string_string_string_function>>("c_DownloadFileDefaultLimit");
480 : // ignore: non_constant_identifier_names
481 : final DownloadFile = dlFile.asFunction<VoidFromStringIntStringStringStringFn>();
482 0 : final u1 = profileOnion.toNativeUtf8();
483 0 : final u3 = filepath.toNativeUtf8();
484 0 : final u4 = manifestpath.toNativeUtf8();
485 0 : final u5 = filekey.toNativeUtf8();
486 0 : DownloadFile(u1, u1.length, contactHandle, u3, u3.length, u4, u4.length, u5, u5.length);
487 0 : malloc.free(u1);
488 0 : malloc.free(u3);
489 0 : malloc.free(u4);
490 0 : malloc.free(u5);
491 : }
492 :
493 0 : @override
494 : // ignore: non_constant_identifier_names
495 : void CreateDownloadableFile(String profileOnion, int contactHandle, String filenameSuggestion, String filekey, String manifestPath) {
496 : // android only - do nothing
497 : }
498 :
499 : // ignore: non_constant_identifier_names
500 0 : void ExportPreviewedFile(String sourceFile, String suggestion) {
501 : // android only - do nothing
502 : }
503 :
504 0 : @override
505 : // ignore: non_constant_identifier_names
506 : void CheckDownloadStatus(String profileOnion, String fileKey) {
507 0 : var checkDownloadStatus = library.lookup<NativeFunction<string_string_to_void_function>>("c_CheckDownloadStatus");
508 : // ignore: non_constant_identifier_names
509 : final CheckDownloadStatus = checkDownloadStatus.asFunction<VoidFromStringStringFn>();
510 0 : final u1 = profileOnion.toNativeUtf8();
511 0 : final u2 = fileKey.toNativeUtf8();
512 0 : CheckDownloadStatus(u1, u1.length, u2, u2.length);
513 0 : malloc.free(u1);
514 0 : malloc.free(u2);
515 : }
516 :
517 0 : @override
518 : // ignore: non_constant_identifier_names
519 : void VerifyOrResumeDownload(String profileOnion, int contactHandle, String filekey) {
520 0 : var fn = library.lookup<NativeFunction<void_from_string_int_string_function>>("c_VerifyOrResumeDownloadDefaultLimit");
521 : // ignore: non_constant_identifier_names
522 : final VerifyOrResumeDownload = fn.asFunction<VoidFromStringIntStringFn>();
523 0 : final u1 = profileOnion.toNativeUtf8();
524 0 : final u3 = filekey.toNativeUtf8();
525 0 : VerifyOrResumeDownload(u1, u1.length, contactHandle, u3, u3.length);
526 0 : malloc.free(u1);
527 0 : malloc.free(u3);
528 : }
529 :
530 0 : @override
531 : // ignore: non_constant_identifier_names
532 : void ResetTor() {
533 0 : var resetTor = library.lookup<NativeFunction<Void Function()>>("c_ResetTor");
534 : // ignore: non_constant_identifier_names
535 : final ResetTor = resetTor.asFunction<void Function()>();
536 0 : ResetTor();
537 : }
538 :
539 0 : @override
540 : // ignore: non_constant_identifier_names
541 : Future<dynamic> ImportBundle(String profileOnion, String bundle) async {
542 0 : var importBundle = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportBundle");
543 : // ignore: non_constant_identifier_names
544 : final ImportBundle = importBundle.asFunction<StringFromStringStringFn>();
545 0 : final u1 = profileOnion.toNativeUtf8();
546 0 : final u2 = bundle.toNativeUtf8();
547 0 : Pointer<Utf8> responsePtr = ImportBundle(u1, u1.length, u2, u2.length);
548 0 : String response = responsePtr.toDartString();
549 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(responsePtr);
550 0 : malloc.free(u1);
551 0 : malloc.free(u2);
552 : return response;
553 : }
554 :
555 0 : @override
556 : // ignore: non_constant_identifier_names
557 : void CreateGroup(String profileOnion, String server, String groupName) {
558 0 : var createGroup = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_StartGroup");
559 : // ignore: non_constant_identifier_names
560 : final CreateGroup = createGroup.asFunction<VoidFromStringStringStringFn>();
561 0 : final u1 = profileOnion.toNativeUtf8();
562 0 : final u3 = server.toNativeUtf8();
563 0 : final u2 = groupName.toNativeUtf8();
564 0 : CreateGroup(u1, u1.length, u2, u2.length, u3, u3.length);
565 :
566 0 : malloc.free(u1);
567 0 : malloc.free(u2);
568 0 : malloc.free(u3);
569 : }
570 :
571 0 : @override
572 : // ignore: non_constant_identifier_names
573 : void ArchiveConversation(String profileOnion, int handle) {
574 0 : var archiveConversation = library.lookup<NativeFunction<string_int_to_void_function>>("c_ArchiveConversation");
575 : // ignore: non_constant_identifier_names
576 : final ArchiveConversation = archiveConversation.asFunction<VoidFromStringIntFn>();
577 0 : final u1 = profileOnion.toNativeUtf8();
578 0 : ArchiveConversation(u1, u1.length, handle);
579 0 : malloc.free(u1);
580 : }
581 :
582 0 : @override
583 : // ignore: non_constant_identifier_names
584 : void DeleteContact(String profileOnion, int handle) {
585 0 : var deleteContact = library.lookup<NativeFunction<string_int_to_void_function>>("c_DeleteConversation");
586 : // ignore: non_constant_identifier_names
587 : final DeleteContact = deleteContact.asFunction<VoidFromStringIntFn>();
588 0 : final u1 = profileOnion.toNativeUtf8();
589 0 : DeleteContact(u1, u1.length, handle);
590 0 : malloc.free(u1);
591 : }
592 :
593 0 : @override
594 : // ignore: non_constant_identifier_names
595 : void DeleteProfile(String onion, String currentPassword) {
596 0 : var deleteprofile = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteProfile");
597 : // ignore: non_constant_identifier_names
598 : final DeleteProfile = deleteprofile.asFunction<VoidFromStringStringFn>();
599 0 : final u1 = onion.toNativeUtf8();
600 0 : final u2 = currentPassword.toNativeUtf8();
601 0 : DeleteProfile(u1, u1.length, u2, u2.length);
602 0 : malloc.free(u1);
603 0 : malloc.free(u2);
604 : }
605 :
606 0 : @override
607 : // ignore: non_constant_identifier_names
608 : void SetProfileAttribute(String profile, String key, String val) {
609 0 : var setProfileAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetProfileAttribute");
610 : // ignore: non_constant_identifier_names
611 : final SetProfileAttribute = setProfileAttribute.asFunction<VoidFromStringStringStringFn>();
612 0 : final u1 = profile.toNativeUtf8();
613 0 : final u2 = key.toNativeUtf8();
614 0 : final u3 = val.toNativeUtf8();
615 0 : SetProfileAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
616 0 : malloc.free(u1);
617 0 : malloc.free(u2);
618 0 : malloc.free(u3);
619 : }
620 :
621 0 : @override
622 : // ignore: non_constant_identifier_names
623 : void SetConversationAttribute(String profile, int contact, String key, String val) {
624 0 : var setContactAttribute = library.lookup<NativeFunction<void_from_string_int_string_string_function>>("c_SetConversationAttribute");
625 : // ignore: non_constant_identifier_names
626 : final SetContactAttribute = setContactAttribute.asFunction<VoidFromStringIntStringStringFn>();
627 0 : final u1 = profile.toNativeUtf8();
628 0 : final u3 = key.toNativeUtf8();
629 0 : final u4 = val.toNativeUtf8();
630 0 : SetContactAttribute(u1, u1.length, contact, u3, u3.length, u4, u4.length);
631 0 : malloc.free(u1);
632 0 : malloc.free(u3);
633 0 : malloc.free(u4);
634 : }
635 :
636 0 : @override
637 : // ignore: non_constant_identifier_names
638 : void SetMessageAttribute(String profile, int conversation, int channel, int message, String key, String val) {
639 0 : var setMessageAttribute = library.lookup<NativeFunction<void_from_string_int_int_int_string_string_function>>("c_UpdateMessageAttribute");
640 : // ignore: non_constant_identifier_names
641 : final SetMessageAttribute = setMessageAttribute.asFunction<VoidFromStringIntIntIntStringStringFn>();
642 0 : final u1 = profile.toNativeUtf8();
643 0 : final u3 = key.toNativeUtf8();
644 0 : final u4 = val.toNativeUtf8();
645 0 : SetMessageAttribute(u1, u1.length, conversation, channel, message, u3, u3.length, u4, u4.length);
646 0 : malloc.free(u1);
647 0 : malloc.free(u3);
648 0 : malloc.free(u4);
649 : }
650 :
651 0 : @override
652 : // ignore: non_constant_identifier_names
653 : void LoadServers(String password) {
654 0 : var loadServers = library.lookup<NativeFunction<string_to_void_function>>("c_LoadServers");
655 : // ignore: non_constant_identifier_names
656 : final LoadServers = loadServers.asFunction<StringFn>();
657 0 : final u1 = password.toNativeUtf8();
658 0 : LoadServers(u1, u1.length);
659 0 : malloc.free(u1);
660 : }
661 :
662 0 : @override
663 : // ignore: non_constant_identifier_names
664 : void CreateServer(String password, String description, bool autostart) {
665 0 : var createServer = library.lookup<NativeFunction<void_from_string_string_byte_function>>("c_CreateServer");
666 : // ignore: non_constant_identifier_names
667 : final CreateServer = createServer.asFunction<VoidFromStringStringByteFn>();
668 0 : final u1 = password.toNativeUtf8();
669 0 : final u2 = description.toNativeUtf8();
670 0 : CreateServer(u1, u1.length, u2, u2.length, autostart ? 1 : 0);
671 0 : malloc.free(u1);
672 0 : malloc.free(u2);
673 : }
674 :
675 0 : @override
676 : // ignore: non_constant_identifier_names
677 : void DeleteServer(String serverOnion, String password) {
678 0 : var deleteServer = library.lookup<NativeFunction<string_string_to_void_function>>("c_DeleteServer");
679 : // ignore: non_constant_identifier_names
680 : final DeleteServer = deleteServer.asFunction<VoidFromStringStringFn>();
681 0 : final u1 = serverOnion.toNativeUtf8();
682 0 : final u2 = password.toNativeUtf8();
683 0 : DeleteServer(u1, u1.length, u2, u2.length);
684 0 : malloc.free(u1);
685 0 : malloc.free(u2);
686 : }
687 :
688 0 : @override
689 : // ignore: non_constant_identifier_names
690 : void LaunchServers() {
691 0 : var launchServers = library.lookup<NativeFunction<Void Function()>>("c_LaunchServers");
692 : // ignore: non_constant_identifier_names
693 : final LaunchServers = launchServers.asFunction<void Function()>();
694 0 : LaunchServers();
695 : }
696 :
697 0 : @override
698 : // ignore: non_constant_identifier_names
699 : void LaunchServer(String serverOnion) {
700 0 : var launchServer = library.lookup<NativeFunction<string_to_void_function>>("c_LaunchServer");
701 : // ignore: non_constant_identifier_names
702 : final LaunchServer = launchServer.asFunction<StringFn>();
703 0 : final u1 = serverOnion.toNativeUtf8();
704 0 : LaunchServer(u1, u1.length);
705 0 : malloc.free(u1);
706 : }
707 :
708 0 : @override
709 : // ignore: non_constant_identifier_names
710 : void StopServer(String serverOnion) {
711 0 : var shutdownServer = library.lookup<NativeFunction<string_to_void_function>>("c_StopServer");
712 : // ignore: non_constant_identifier_names
713 : final ShutdownServer = shutdownServer.asFunction<StringFn>();
714 0 : final u1 = serverOnion.toNativeUtf8();
715 0 : ShutdownServer(u1, u1.length);
716 0 : malloc.free(u1);
717 : }
718 :
719 0 : @override
720 : // ignore: non_constant_identifier_names
721 : void StopServers() {
722 0 : var shutdownServers = library.lookup<NativeFunction<Void Function()>>("c_StopServers");
723 : // ignore: non_constant_identifier_names
724 : final ShutdownServers = shutdownServers.asFunction<void Function()>();
725 0 : ShutdownServers();
726 : }
727 :
728 0 : @override
729 : // ignore: non_constant_identifier_names
730 : void DestroyServers() {
731 0 : var destroyServers = library.lookup<NativeFunction<Void Function()>>("c_DestroyServers");
732 : // ignore: non_constant_identifier_names
733 : final DestroyServers = destroyServers.asFunction<void Function()>();
734 0 : DestroyServers();
735 : }
736 :
737 0 : @override
738 : // ignore: non_constant_identifier_names
739 : void SetServerAttribute(String serverOnion, String key, String val) {
740 0 : var setServerAttribute = library.lookup<NativeFunction<void_from_string_string_string_function>>("c_SetServerAttribute");
741 : // ignore: non_constant_identifier_names
742 : final SetServerAttribute = setServerAttribute.asFunction<VoidFromStringStringStringFn>();
743 0 : final u1 = serverOnion.toNativeUtf8();
744 0 : final u2 = key.toNativeUtf8();
745 0 : final u3 = val.toNativeUtf8();
746 0 : SetServerAttribute(u1, u1.length, u2, u2.length, u3, u3.length);
747 0 : malloc.free(u1);
748 0 : malloc.free(u2);
749 0 : malloc.free(u3);
750 : }
751 :
752 0 : @override
753 : // ignore: non_constant_identifier_names
754 : Future<void> Shutdown() async {
755 0 : var shutdown = library.lookup<NativeFunction<void_from_void_funtion>>("c_ShutdownCwtch");
756 : // ignore: non_constant_identifier_names
757 :
758 : // Shutdown Cwtch + Tor...
759 : // ignore: non_constant_identifier_names
760 : final Shutdown = shutdown.asFunction<VoidFromVoidFunction>();
761 0 : Shutdown();
762 :
763 : // Kill our Isolate
764 0 : cwtchIsolate.kill(priority: Isolate.immediate);
765 0 : print("Isolate killed");
766 :
767 0 : _receivePort.close();
768 0 : print("Receive Port Closed");
769 : }
770 :
771 0 : @override
772 : // ignore: non_constant_identifier_names
773 : Future GetMessageByContentHash(String profile, int handle, String contentHash) async {
774 0 : var getMessagesByContentHashC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetMessageByContentHash");
775 : // ignore: non_constant_identifier_names
776 : final GetMessagesByContentHash = getMessagesByContentHashC.asFunction<GetJsonBlobFromStrIntStringFn>();
777 0 : final utf8profile = profile.toNativeUtf8();
778 0 : final utf8contentHash = contentHash.toNativeUtf8();
779 0 : Pointer<Utf8> jsonMessageBytes = GetMessagesByContentHash(utf8profile, utf8profile.length, handle, utf8contentHash, utf8contentHash.length);
780 0 : String jsonMessage = jsonMessageBytes.toDartString();
781 :
782 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
783 0 : malloc.free(utf8profile);
784 0 : malloc.free(utf8contentHash);
785 : return jsonMessage;
786 : }
787 :
788 : // ignore: non_constant_identifier_names
789 : // Incredibly dangerous function which invokes a free in libCwtch, should only be used
790 : // as documented in `MEMORY.md` in libCwtch repo.
791 0 : void _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(Pointer<Utf8> ptr) {
792 0 : var free = library.lookup<NativeFunction<free_function>>("c_FreePointer");
793 : final Free = free.asFunction<FreeFn>();
794 0 : Free(ptr);
795 : }
796 :
797 0 : @override
798 : String? defaultDownloadPath() {
799 0 : Map<String, String> envVars = Platform.environment;
800 0 : String nominalPath = path.join(envVars[Platform.isWindows ? 'UserProfile' : 'HOME']!, "Downloads");
801 0 : if (Directory(nominalPath).existsSync() == false) {
802 0 : return Directory.current.path;
803 : }
804 : return nominalPath;
805 : }
806 :
807 0 : @override
808 : // ignore: non_constant_identifier_names
809 : Future<String> GetMessageByID(String profile, int handle, int index) async {
810 0 : var getMessageC = library.lookup<NativeFunction<get_json_blob_from_str_int_int_function>>("c_GetMessageById");
811 : // ignore: non_constant_identifier_names
812 : final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrIntIntFn>();
813 0 : final utf8profile = profile.toNativeUtf8();
814 0 : Pointer<Utf8> jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index);
815 0 : String jsonMessage = jsonMessageBytes.toDartString();
816 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
817 0 : malloc.free(utf8profile);
818 : return jsonMessage;
819 : }
820 :
821 0 : @override
822 : // ignore: non_constant_identifier_names
823 : void ChangePassword(String profile, String pass, String newpass, String newpassAgain) {
824 0 : var changePasswordC = library.lookup<NativeFunction<void_from_string_string_string_string_function>>("c_ChangePassword");
825 : // ignore: non_constant_identifier_names
826 : final ChangePasswordFn = changePasswordC.asFunction<VoidFromStringStringStringStringFn>();
827 0 : final utf8profile = profile.toNativeUtf8();
828 0 : final utf8pass = pass.toNativeUtf8();
829 0 : final utf8newpass = newpass.toNativeUtf8();
830 0 : final utf8newpasssagain = newpassAgain.toNativeUtf8();
831 0 : ChangePasswordFn(utf8profile, utf8profile.length, utf8pass, utf8pass.length, utf8newpass, utf8newpass.length, utf8newpasssagain, utf8newpasssagain.length);
832 0 : malloc.free(utf8profile);
833 0 : malloc.free(utf8pass);
834 0 : malloc.free(utf8newpass);
835 0 : malloc.free(utf8newpasssagain);
836 : }
837 :
838 0 : @override
839 : bool isL10nInit() {
840 0 : return _isL10nInit;
841 : }
842 :
843 0 : @override
844 : void l10nInit(String notificationSimple, String notificationConversationInfo) {
845 0 : cwtchNotifier.l10nInit(notificationSimple, notificationConversationInfo);
846 0 : _isL10nInit = true;
847 : }
848 :
849 0 : @override
850 : // ignore: non_constant_identifier_names
851 : void ExportProfile(String profile, String file) {
852 0 : final utf8profile = profile.toNativeUtf8();
853 0 : final utf8file = file.toNativeUtf8();
854 0 : var exportProfileC = library.lookup<NativeFunction<void_from_string_string_function>>("c_ExportProfile");
855 : // ignore: non_constant_identifier_names
856 : final ExportProfileFn = exportProfileC.asFunction<VoidFromStringStringFn>();
857 0 : ExportProfileFn(utf8profile, utf8profile.length, utf8file, utf8file.length);
858 0 : malloc.free(utf8profile);
859 0 : malloc.free(utf8file);
860 : }
861 :
862 0 : @override
863 : // ignore: non_constant_identifier_names
864 : Future<String> ImportProfile(String file, String pass) async {
865 0 : final utf8pass = pass.toNativeUtf8();
866 0 : final utf8file = file.toNativeUtf8();
867 0 : var exportProfileC = library.lookup<NativeFunction<string_string_to_string_function>>("c_ImportProfile");
868 : // ignore: non_constant_identifier_names
869 : final ExportProfileFn = exportProfileC.asFunction<StringFromStringStringFn>();
870 0 : Pointer<Utf8> result = ExportProfileFn(utf8file, utf8file.length, utf8pass, utf8pass.length);
871 0 : String importResult = result.toDartString();
872 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
873 0 : malloc.free(utf8pass);
874 0 : malloc.free(utf8file);
875 : return importResult;
876 : }
877 :
878 0 : @override
879 : Future<String> GetDebugInfo() async {
880 0 : var getDebugInfo = library.lookup<NativeFunction<void_to_string>>("c_GetDebugInfo");
881 : final GetDebugInfo = getDebugInfo.asFunction<StringFromVoid>();
882 0 : Pointer<Utf8> result = GetDebugInfo();
883 0 : String debugResult = result.toDartString();
884 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(result);
885 : return debugResult;
886 : }
887 :
888 0 : @override
889 : Future<String> GetSharedFiles(String profile, int handle) async {
890 0 : var getSharedFiles = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_GetSharedFiles");
891 : final GetSharedFiles = getSharedFiles.asFunction<GetJsonBlobFromStrIntFn>();
892 0 : final utf8profile = profile.toNativeUtf8();
893 0 : Pointer<Utf8> jsonMessageBytes = GetSharedFiles(utf8profile, utf8profile.length, handle);
894 0 : String jsonMessage = jsonMessageBytes.toDartString();
895 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
896 0 : malloc.free(utf8profile);
897 : return jsonMessage;
898 : }
899 :
900 0 : @override
901 : void RestartSharing(String profile, String filekey) {
902 0 : var restartSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_RestartFileShare");
903 : // ignore: non_constant_identifier_names
904 : final RestartSharing = restartSharingC.asFunction<VoidFromStringStringFn>();
905 0 : final utf8profile = profile.toNativeUtf8();
906 0 : final ut8filekey = filekey.toNativeUtf8();
907 0 : RestartSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
908 0 : malloc.free(utf8profile);
909 0 : malloc.free(ut8filekey);
910 : }
911 :
912 0 : @override
913 : void StopSharing(String profile, String filekey) {
914 0 : var stopSharingC = library.lookup<NativeFunction<void_from_string_string_function>>("c_StopFileShare");
915 : // ignore: non_constant_identifier_names
916 : final StopSharing = stopSharingC.asFunction<VoidFromStringStringFn>();
917 0 : final utf8profile = profile.toNativeUtf8();
918 0 : final ut8filekey = filekey.toNativeUtf8();
919 0 : StopSharing(utf8profile, utf8profile.length, ut8filekey, ut8filekey.length);
920 0 : malloc.free(utf8profile);
921 0 : malloc.free(ut8filekey);
922 : }
923 :
924 0 : @override
925 : void DeleteServerInfo(String profile, String handle) {
926 0 : var deleteServerInfoC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DeleteServerInfo");
927 : // ignore: non_constant_identifier_names
928 : final StopSharing = deleteServerInfoC.asFunction<VoidFromStringStringFn>();
929 0 : final utf8profile = profile.toNativeUtf8();
930 0 : final ut8handle = handle.toNativeUtf8();
931 0 : StopSharing(utf8profile, utf8profile.length, ut8handle, ut8handle.length);
932 0 : malloc.free(utf8profile);
933 0 : malloc.free(ut8handle);
934 : }
935 :
936 0 : @override
937 : void UpdateSettings(String json) {
938 0 : var updateSettings = library.lookup<NativeFunction<string_to_void_function>>("c_UpdateSettings");
939 : // ignore: non_constant_identifier_names
940 : final UpdateSettingsFn = updateSettings.asFunction<VoidFromStringFn>();
941 0 : final u1 = json.toNativeUtf8();
942 0 : UpdateSettingsFn(u1, u1.length);
943 0 : malloc.free(u1);
944 : }
945 :
946 0 : @override
947 : bool IsServersCompiled() {
948 0 : return library.providesSymbol("c_LoadServers");
949 : }
950 :
951 0 : @override
952 : Future<String> SummarizeConversation(String profile, int conversation) async {
953 0 : if (!library.providesSymbol("c_Summarize")) {
954 0 : return Future.value("");
955 : }
956 0 : var summarize = library.lookup<NativeFunction<get_json_blob_from_str_int_function>>("c_Summarize");
957 : // ignore: non_constant_identifier_names
958 : final SummarizeFn = summarize.asFunction<GetJsonBlobFromStrIntFn>();
959 0 : final utf8profile = profile.toNativeUtf8();
960 0 : Pointer<Utf8> jsonMessageBytes = SummarizeFn(utf8profile, utf8profile.length, conversation);
961 0 : String jsonMessage = jsonMessageBytes.toDartString();
962 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
963 0 : malloc.free(utf8profile);
964 : return jsonMessage;
965 : }
966 :
967 0 : @override
968 : Future<String> TranslateMessage(String profile, int conversation, int message, String language) async {
969 0 : if (!library.providesSymbol("c_Translate")) {
970 0 : return Future.value("");
971 : }
972 0 : var translate = library.lookup<NativeFunction<get_json_blob_from_str_int_int_str_function>>("c_Translate");
973 : // ignore: non_constant_identifier_names
974 : final TranslateFn = translate.asFunction<GetJsonBlobFromStrIntIntStrFn>();
975 0 : final utf8profile = profile.toNativeUtf8();
976 0 : final utf8lang = language.toNativeUtf8();
977 0 : Pointer<Utf8> jsonMessageBytes = TranslateFn(
978 : utf8profile,
979 0 : utf8profile.length,
980 : conversation,
981 : message,
982 : utf8lang,
983 0 : utf8lang.length,
984 : );
985 0 : String jsonMessage = jsonMessageBytes.toDartString();
986 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
987 0 : malloc.free(utf8profile);
988 0 : malloc.free(utf8lang);
989 : return jsonMessage;
990 : }
991 :
992 0 : @override
993 : bool IsBlodeuweddSupported() {
994 0 : if (library.providesSymbol("c_Translate")) {
995 : return true;
996 : }
997 : return false;
998 : }
999 :
1000 0 : @override
1001 : Future<String?> GetProfileAttribute(String profile, String key) {
1002 0 : var getProfileAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_str_function>>("c_GetProfileAttribute");
1003 : // ignore: non_constant_identifier_names
1004 : final GetProfileAttribute = getProfileAttributeC.asFunction<GetJsonBlobFromStrStrFn>();
1005 0 : final utf8profile = profile.toNativeUtf8();
1006 0 : final utf8key = key.toNativeUtf8();
1007 0 : Pointer<Utf8> jsonMessageBytes = GetProfileAttribute(utf8profile, utf8profile.length, utf8key, utf8key.length);
1008 0 : String jsonMessage = jsonMessageBytes.toDartString();
1009 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
1010 0 : malloc.free(utf8profile);
1011 0 : malloc.free(utf8key);
1012 :
1013 : try {
1014 0 : dynamic attributeResult = json.decode(jsonMessage);
1015 0 : if (attributeResult["Exists"]) {
1016 0 : return Future.value(attributeResult["Value"]);
1017 : }
1018 : } catch (e) {
1019 0 : EnvironmentConfig.debugLog("error getting profile attribute: $e");
1020 : }
1021 :
1022 0 : return Future.value(null);
1023 : }
1024 :
1025 0 : @override
1026 : Future<String?> GetConversationAttribute(String profile, int conversation, String key) {
1027 0 : var getConversationAttributeC = library.lookup<NativeFunction<get_json_blob_from_str_int_string_function>>("c_GetConversationAttribute");
1028 : // ignore: non_constant_identifier_names
1029 : final GetConversationAttribute = getConversationAttributeC.asFunction<GetJsonBlobFromStrIntStringFn>();
1030 0 : final utf8profile = profile.toNativeUtf8();
1031 0 : final utf8key = key.toNativeUtf8();
1032 0 : Pointer<Utf8> jsonMessageBytes = GetConversationAttribute(utf8profile, utf8profile.length, conversation, utf8key, utf8key.length);
1033 0 : String jsonMessage = jsonMessageBytes.toDartString();
1034 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes);
1035 0 : malloc.free(utf8profile);
1036 0 : malloc.free(utf8key);
1037 :
1038 : try {
1039 0 : dynamic attributeResult = json.decode(jsonMessage);
1040 0 : if (attributeResult["Exists"]) {
1041 0 : return Future.value(attributeResult["Value"]);
1042 : }
1043 : } catch (e) {
1044 0 : EnvironmentConfig.debugLog("error getting profile attribute: $e");
1045 : }
1046 :
1047 0 : return Future.value(null);
1048 : }
1049 :
1050 0 : @override
1051 : void AttemptReconnection(String profile, String onion) {
1052 : // ignore: non_constant_identifier_names
1053 0 : var peerWithOnionC = library.lookup<NativeFunction<void_from_string_string_function>>("c_PeerWithOnion");
1054 : final PeerWithOnionF = peerWithOnionC.asFunction<VoidFromStringStringFn>();
1055 0 : final utf8profile = profile.toNativeUtf8();
1056 0 : final utf8onion = onion.toNativeUtf8();
1057 0 : PeerWithOnionF(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
1058 0 : malloc.free(utf8profile);
1059 0 : malloc.free(utf8onion);
1060 : }
1061 :
1062 0 : @override
1063 : void AttemptReconnectionServer(String profile, String onion) {
1064 : // ignore: non_constant_identifier_names
1065 0 : var queueJoinServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_QueueJoinServer");
1066 : final QueueJoinServerC = queueJoinServerC.asFunction<VoidFromStringStringFn>();
1067 0 : final utf8profile = profile.toNativeUtf8();
1068 0 : final utf8onion = onion.toNativeUtf8();
1069 0 : QueueJoinServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
1070 0 : malloc.free(utf8profile);
1071 0 : malloc.free(utf8onion);
1072 : }
1073 :
1074 0 : @override
1075 : void DisconnectFromPeer(String profile, String onion) {
1076 : // ignore: non_constant_identifier_names
1077 0 : var disconnectFromPeerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromPeer");
1078 : final DisconnectFromPeerC = disconnectFromPeerC.asFunction<VoidFromStringStringFn>();
1079 0 : final utf8profile = profile.toNativeUtf8();
1080 0 : final utf8onion = onion.toNativeUtf8();
1081 0 : DisconnectFromPeerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
1082 0 : malloc.free(utf8profile);
1083 0 : malloc.free(utf8onion);
1084 : }
1085 :
1086 0 : @override
1087 : void DisconnectFromServer(String profile, String onion) {
1088 : // ignore: non_constant_identifier_names
1089 0 : var disconnectFromServerC = library.lookup<NativeFunction<void_from_string_string_function>>("c_DisconnectFromServer");
1090 : final DisconnectFromServerC = disconnectFromServerC.asFunction<VoidFromStringStringFn>();
1091 0 : final utf8profile = profile.toNativeUtf8();
1092 0 : final utf8onion = onion.toNativeUtf8();
1093 0 : DisconnectFromServerC(utf8profile, utf8profile.length, utf8onion, utf8onion.length);
1094 0 : malloc.free(utf8profile);
1095 0 : malloc.free(utf8onion);
1096 : }
1097 :
1098 0 : @override
1099 : Future<String> SearchConversations(String profile, String pattern) async {
1100 0 : var searchConversationsC = library.lookup<NativeFunction<string_string_to_string_function>>("c_SearchConversations");
1101 : // ignore: non_constant_identifier_names
1102 : final SearchConversations = searchConversationsC.asFunction<StringFromStringStringFn>();
1103 0 : final utf8profile = profile.toNativeUtf8();
1104 0 : final utf8pattern = pattern.toNativeUtf8();
1105 0 : EnvironmentConfig.debugLog("Searching for $profile $pattern");
1106 0 : Pointer<Utf8> searchIDRaw = SearchConversations(utf8profile, utf8profile.length, utf8pattern, utf8pattern.length);
1107 0 : String searchID = searchIDRaw.toDartString();
1108 0 : _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(searchIDRaw);
1109 0 : malloc.free(utf8profile);
1110 0 : malloc.free(utf8pattern);
1111 : return searchID;
1112 : }
1113 :
1114 0 : @override
1115 : Future<HashMap<String, String>> PlatformChannelInfo() {
1116 0 : return Future.value(HashMap<String, String>());
1117 : }
1118 :
1119 0 : @override
1120 : Future<void> ConfigureConnections(String profile, bool listen, bool peers, bool servers) async {
1121 0 : var configureConnections = library.lookup<NativeFunction<void_from_string_bool_bool_bool>>("c_ConfigureConnections");
1122 : // ignore: non_constant_identifier_names
1123 : final ConfigureConnections = configureConnections.asFunction<VoidFromStringBoolBoolBool>();
1124 0 : final utf8profile = profile.toNativeUtf8();
1125 0 : ConfigureConnections(utf8profile, utf8profile.length, listen, peers, servers);
1126 0 : malloc.free(utf8profile);
1127 : return;
1128 : }
1129 :
1130 0 : @override
1131 : void PublishServerUpdate(String profile) {
1132 0 : var publishServerUpdate = library.lookup<NativeFunction<string_to_void_function>>("c_PublishServerUpdate");
1133 : // ignore: non_constant_identifier_names
1134 : final PublishServerUpdate = publishServerUpdate.asFunction<StringFn>();
1135 0 : final utf8profile = profile.toNativeUtf8();
1136 0 : PublishServerUpdate(utf8profile, utf8profile.length);
1137 0 : malloc.free(utf8profile);
1138 : }
1139 :
1140 0 : @override
1141 : bool IsLoaded() {
1142 0 : bool check = library.providesSymbol("c_UpdateSettings");
1143 0 : EnvironmentConfig.debugLog("Checking that the FFI Interface is Correctly Loaded... $check");
1144 : return check;
1145 : }
1146 : }
|