TelegramMessenger / Telegram-iOS

Telegram-iOS
6.28k stars 1.64k forks source link

Error sharing contact #91

Open SALITEL opened 5 years ago

SALITEL commented 5 years ago

When someone shares a contact without a country and the third digit is a zero, Telegram eliminates that number.

Example: Phone 630123456 In Telegram for iOS it shows +63 123456

The actual full telephone would be +34630123456 (Spain number)

shivanraptor commented 5 years ago

so what do you expect when no country code is supplied? Telegram can't guess which country does the number belong to.

SALITEL commented 5 years ago

That respects the contact number that is WITHOUT deleting any digit.

If the telephone number is +34123456789 that shows +34 123456789 and if it is 123456789 that shows that number even if it does not have a country code.

The important thing is that you do not eliminate the number zero in the third position.

The way to reproduce the problem is: Create a contact with the phone number 120123456 If you share the contact you will see that in Telegram for iOS shows +12 123456 (Error) in Telegram for Desktop shows +12 0123456 (Correct)

That's the problem.

shivanraptor commented 5 years ago

To follow up this case, I created the contact with the number "120123456", not specifying any country code.

When I share a contact via Telegram 5.9, it shows +1 20123456 (same for Telegram Mac desktop client). Screenshot as follow:

IMG_8724

IMG_8725

Using iOS 12.3.1 (tested on iOS 12.4 just now, result is the same), both Telegram 5.9.1 official client downloaded from iOS App Store & Telegram Fork 5.9 using release-5.9.

There are 2 related settings in iOS Settings (not Telegram settings):

  1. General > Phone > Dial Assist: iOS will automatically "guess" the country code
  2. General > Language & Region > Region: iOS will append the country code based on your Region. I am in +852, but I'm unsure why iOS guess the number is a +1 US number.

Since the behavior of the Sharing Contact feature in Telegram varies based on different iOS Settings, I'm unsure whether it can be fixed via Telegram source code.

SALITEL commented 5 years ago

Please, test again with number 10000123

In your case, the country is only one digit. Testing the phone 1000123, for example, the problem will be seen.

SALITEL commented 5 years ago

I created this contact: Contact

Shared with Telegram for iOS (Latest version): photo_2019-07-24_10-18-39

Contact detail: photo_2019-07-24_10-18-52

Press the number for call: photo_2019-07-24_10-19-10 (Incorrect number)

Now, show on Telegram for Desktop: Desktop (Correct)

Telegram for Android work fine. Show the full number.

shivanraptor commented 5 years ago

Also confirmed on my phone. The only difference at my side is, my old Telegram Mac (Telegram Mac v4.2.2) desktop app shows only +1 0123 as well.

Anyways, it's time to hunt for the bug!

shivanraptor commented 5 years ago

After debugging for some time, the problem lies in a third-party submodule of TelegramCore, named libphonenumber-iOS, which is the iOS port of the Google phone number library.

Assume we're focusing on the previous number's case (the number is 100000123). In the class NBPhoneNumberUtil.parse() function, the input parameter numberToParse is already missing a 0, which becomes +10000123. Function signature:

- (NBPhoneNumber*)parse:(NSString*)numberToParse defaultRegion:(NSString*)defaultRegion error:(NSError**)error;

The + sign is from TelegramCore > Utils > PhoneNumbers.swift > formatPhoneNumber(), which appends the sign at line 12. As you might expect, the input parameter string in formatPhoneNumber() also misses a 0, which is 10000123.

Therefore I look for the codes which calls the formatPhoneNumber() function... still working on it. There are loads of codes using this function.

One of the places that use this formatPhoneNumber() function frequently is the one named ChatMessageContactBubbleContentNode > asyncLayoutContent(). As expected, the input phone number has already missed that 0, and the source is directly from TelegramMediaContact. This view:

IMG_8724

I will continue to trace the issue tomorrow (need to rest a bit now). I have a gut feeling that iOS is feeding the wrong number (the one with missing 0) to Telegram.

SALITEL commented 5 years ago

Thank you for your work ;)

I think the best thing is that Telegram doesn't add the "+" to the phone.

Another option is to add the "+ and the country code" when the phone does not start with "+" but you would have to do it completely, that is, adding the country of the user who shares the contact. In my case "+34" and in your case "+1".

shivanraptor commented 5 years ago

After I further traced the issue. The number from Postbox is already missing a 0 (see TelegramMediaContact.swift line 34:

self.phoneNumber = decoder.decodeStringForKey("pn", orElse: "")

Then I dig into Coding.swift > decodeStringForKey() of the module Postbox, the function is to decode information (actually anything from the user profile to message) from buffer memory to some useful string data.

The phone number freshly decoded from this function is already missing a 0. I can't dig further as the data is fed from the Telegram API itself.


Replying to @SALITEL, if the + sign is not added by Telegram, the problem still exists, as the 0 is already missing in a couple of steps earlier.

Also, it might lead to unexpected behavior, as each user sees a different number, if the default country code is added. For example, you sent me the contact 100000123, at your side you will see +34 100000123, at my side I will see +1 100000123 (actually I'm from +852 (confirmed in iOS Settings), but I'm unsure why I get the US country code +1).

Need Telegram's author's further advice at this point.

shivanraptor commented 5 years ago

The issue still happens in release-5.10. Nothing changed.