itsjunetime / libsmserver

Companion tweak to SMServer iPhone app
13 stars 4 forks source link

[Bug] Crashes when sending texts #1

Closed sgtaziz closed 3 years ago

sgtaziz commented 3 years ago

dispatch_sync found on here: https://github.com/iandwelker/libsmserver/blob/00110c9bd48008842c75aa851a97d67dcb1eb6ca/Tweak.xm#L54 can cause crashes in some instances. There is a few explanations I found on stackoverflow that cause this, I will link one of them here.

I was going to push a PR, but I noticed you are sending a return value to ensure the text is sent. I wasn't sure how you wanted to handle it, as I'm not sure how you would pass a void through the function with libMRYIPC. If you have an idea, feel free to let me know and I will be more than happy to send a PR your way :)

itsjunetime commented 3 years ago

Theoretically, it should be fine to just run just the [CKMediaObjectManager mediaObjectWithFileURL:...] on the main thread and let all else run on whatever thread it's currently running on. I'm never run into this problem, though, so I'm not certain that would completely mitigate it. What do you think?

sgtaziz commented 3 years ago

That is possible. However, it would still result in a crash if it is somehow run on the main thread, which can happen with some jailbreak setups.

I think a combination of what you suggested, and adding an if statement to check if it is the main thread or not, would work best! Something like:

if ([NSThread isMainThread]) {
   [CKMediaObjectManager mediaObjectWithFileURL:...]
} else {
   dispatch_sync(dispatch_get_main_queue(), ^{ 
      [CKMediaObjectManager mediaObjectWithFileURL:...]
   });
}

This is unrelated, but I noticed you were also having issues hooking "__kIMChatRegistryMessageSentNotification" I was able to get around this by hooking the notification center itself. However, it does cause the Messages app to crash on some setups for a reason I have yet to figure out. So it is probably a good idea to add some kind of toggle for it within the app. Here is an example from my current code:

%hook NSNotificationCenter
// This doesn't hook instantly. Also causes crashes at times
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo {
  NSDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:@"/User/Library/Preferences/com.sgtaziz.webmessage.plist"];
  BOOL enableHook = settings[@"sendnotificationhook"] ? [settings[@"sendnotificationhook"] boolValue] : YES;

  if (enableHook && [notificationName isEqualToString:@"__kIMChatRegistryMessageSentNotification"]) {
    IMMessage *msg = [userInfo objectForKey:@"__kIMChatRegistryMessageSentMessageKey"];
    NSString* msgGUID = [msg guid];
    Do stuff...
  }

  %orig;
}
%end
itsjunetime commented 3 years ago

Yeah, that seems like that would probably be the best solution -- I read a bit further down in that stackoverflow post you linked above, and realize they recommend that as well.

With regards to the __kIMChatRegistryMessageSentNotification, I actually did try implementing that a bit ago, but the notification doesn't seem to reliably fire whenever a message is sent; I can't quite figure out when it sends and when it doesn't, but it kind of seems that the notification is only posted when I'm not doing anything with it (e.g. just logging every notification that is posted) but whenever I try to hook NSNotificationCenter like you do above, or set an observer for that notification, it just stops being posted and I can't quite figure out why. I assume you're not running into that?

sgtaziz commented 3 years ago

Correct. When setting an observer it will never fire. I've tried booking a few NSNotification functions, and the one posted above was the only one that has worked for me 👍

itsjunetime commented 3 years ago

Alright, I've fixed this issue in version 0.6.2. It's not exactly what we settled on, but similar and should solve the issue.