Ë
    £| kt  ã                  ó  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ  ej                  d	«      Z ej                  d
«      Z ej                  d«      Zdd„Zdd„Ze G d„ d«      «       Zdd„Zd„ Zdd„Zy)aQ  Find a channel from what a person types: @username, t.me link, numeric id (-100...), or an invite link.

Used by /shift. The bot can only talk to a channel it can see: registered channels (the database has their access
details), channels it has met in updates, public channels by @username, and channels it is a member of (invite link).
é    )ÚannotationsN)Ú	dataclass)ÚOptional)Ú	functionsÚtypesé   )ÚCtxÚ	UserErrorzV(?i)^(?:https?://)?(?:t\.me|telegram\.me)/(?:c/(\d+)|([A-Za-z0-9_]{3,32}))(?:/\d+)?/?$z^(?i)^(?:https?://)?(?:t\.me|telegram\.me|telegram\.dog)/(?:\+|joinchat/)([A-Za-z0-9_-]{6,})/?$z+(?i)^tg://join\?invite=([A-Za-z0-9_-]{6,})$c                óú  — | xs dj                  «       }t        j                  |«      }|r<|j                  d«      rt	        |j                  d«      «      S |j                  d«      S |j                  d«      }t        j                  d|«      rUt        t        t	        |«      «      «      }|j                  d«      rt        |«      dkD  rt	        |dd	 «      S t	        |«      S t        j                  d
|«      r|S y	)z[@name / t.me link / numeric id -> username (str) or channel id (int); None if unrecognised.Ú r   é   Ú@z-?\d+Ú100é
   é   Nz[A-Za-z0-9_]{3,32})ÚstripÚ_REF_RXÚmatchÚgroupÚintÚlstripÚreÚ	fullmatchÚstrÚabsÚ
startswithÚlen)ÚtextÚtÚmÚss       ú/app/app/channelref.pyÚparse_channel_refr#      sÀ   € à	Š�×ÑÓ€AÜ�‰�aÓ€AÙØ"#§'¡'¨!¤*Œs�1—7‘7˜1“:‹Ð<°!·'±'¸!³*Ð<Ø	�‰�‹€AÜ	‡|�|�H˜aÔ Ü””C˜“F“ÓˆØŸ\™\¨%Ô0´S¸³V¸b²[Œs�1�Q�R�5‹zÐLÄcÈ!ÃfÐLÜ	‡|�|Ð)¨1Ô-ØˆØó    c                ó  — | xs dj                  «       }t        j                  |«      xs t        j                  |«      }|rd|j	                  d«      fS t        |«      }t        |t        «      rd|fS t        |t        «      rd|fS y)zE-> ("invite", hash) | ("username", name) | ("id", int) | (None, None)r   Úinviter   ÚidÚusername)NN)	r   Ú
_INVITE_RXr   Ú_TG_INVITE_RXr   r#   Ú
isinstancer   r   )r   r   r    Úrefs       r"   Úclassify_refr-   $   s   € à	Š�×ÑÓ€AÜ×Ñ˜ÓÒ5œ}×2Ñ2°1Ó5€AÙØ˜Ÿ™ ›Ð#Ð#Ü
˜AÓ
€CÜ�#”sÔØ�SˆyÐÜ�#”sÔØ˜3ˆÐØr$   c                  óD   — e Zd ZU dZded<   ded<   ded<   ded<   d	ed
<   y)Ú
ChannelRefz"A channel as the bot can reach it.r   r'   Úaccess_hashr   ÚtitlezOptional[str]r(   ÚboolÚ
registeredN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© r$   r"   r/   r/   2   s!   … á,àƒGØÓØƒJØÓØÔr$   r/   c                ó8  — t        | t        j                  «      st        d«      ‚| j                  st        d«      ‚| j
                  rt        | dd«      rt        d«      ‚t        | j                  | j
                  | j                  xs d| j                  |«      S )Nz[That is not a channel (it looks like a user, bot or group). Only channels can be used here.z?That is a group, not a channel. Only channels can be used here.ÚminFz¦I can see that channel, but Telegram has not given me its access details. Use its @username, or add the bot to the channel as an admin and post something there first.r   )r+   r   ÚChannelr
   Ú	broadcastr0   Úgetattrr/   r'   r1   r(   )Úentr3   s     r"   Ú_from_entityr@   =   s}   € Ü�cœ5Ÿ=™=Ô)ÜÐuÓvÐvØ�=Š=ÜÐYÓZÐZØ�?Š?œg c¨5°%Ô8ÜðMó
ð 	
ô �c—f‘f˜cŸo™o¨s¯y©yª¸BÀÇÁÈjÓYÐYr$   c              ƒ  ó`   K  — 	 | j                  |«      ƒ d {  –—† S 7 Œ# t        $ r Y y w xY w­w)N)Ú
get_entityÚ	Exception)ÚclientÚwhats     r"   Ú_entityrF   J   s3   è ø€ ðØ×&Ñ& tÓ,×,Ð,Ð,ùÜò Ùðüs(   ‚.„ ˜™ œ.� Ÿ	+¨.ª+«.c              ƒ  ó  K  — t        |«      \  }}|€t        d|› d�«      ‚| j                  | j                  }}|dk(  rÀ|j	                  d¬«      ƒ d{  –—† D ]}  }|j
                  sŒ|j
                  j                  «       |j                  «       k(  sŒ<t        |j                  |j                  |j                  |j
                  |j                  «      c S  t        ||«      ƒ d{  –—† }|�€t        d|› d�«      ‚|d	k(  r±|j                  |«      ƒ d{  –—† }|�At        |j                  |j                  |j                  |j
                  |j                  «      S | j                  j                  |«      xs' t        |t!        j"                  |«      «      ƒ d{  –—† }|€Zt        d
|› d�«      ‚	  |t$        j&                  j)                  |¬«      «      ƒ d{  –—† }t1        |dd«      }
|
€t        d«      ‚|
}|j                  t1        |d	d«      «      ƒ d{  –—† }t3        ||duxr |j                  «      S 7 �Œì7 �Œ[7 �Œ.7 Œ©7 Œk# t*        $ r'}	t        dt-        |	«      j.                  › d�«      ‚d}	~	ww xY w7 Œ`­w)zBThe channel `text` points at, or a UserError that says what to do.Nu   â€œum   â€� is not a channel I understand. Use an @username, a t.me link, the channel id (-100â€¦) or an invite link.r(   F)Úactive_onlyz"I couldn't find a channel called @z3. Check the name, or add the bot to it as an admin.r'   z#I don't know a channel with the id z|. A bot can only use an id for channels it is part of; use the @username instead, or add the bot to the channel as an admin.)ÚhashzThat invite link doesn't work (z).Úchatz—The bot is not a member of that channel, so it cannot use that invite link. Add the bot to the channel as an admin first (a bot cannot join by itself).r   )r-   r
   ÚdbrD   Úlist_channelsr(   Úlowerr/   r'   r0   r1   ÚactiverF   Úget_channelÚseen_channelsÚgetr   ÚPeerChannelr   ÚmessagesÚCheckChatInviteRequestrC   Útyper4   r>   r@   )Úctxr   ÚkindÚvaluerK   rD   Úchr?   ÚinfoÚerJ   Úknowns               r"   Úresolve_channelr]   Q   sd  è ø€ ä˜tÓ$�K€Dˆ%Ø€|ÜØ�$�ð  Eð  Fó
ð 	
ð —‘˜Ÿ™ˆ€BØˆzÒØ×(Ñ(°UÐ(Ó;×;Ò;ˆBØ�{‹{˜rŸ{™{×0Ñ0Ó2°e·k±k³mÓCÜ! "§%¡%¨¯©¸¿¹À2Ç;Á;ÐPR×PYÑPYÓZÒZð <ô ˜F EÓ*×*ˆØ‰;ÜÐ@ÀÀÐGzÐ{Ó|Ð|Ø	�ŠØ—>‘> %Ó(×(ˆØˆ>Ü˜bŸe™e R§^¡^°R·X±X¸r¿{¹{ÈBÏIÉIÓVÐVØ×Ñ×#Ñ# EÓ*Ò]´G¸FÄE×DUÑDUÐV[ÓD\Ó4]×.]ˆØˆ;ÜØ5°e°Wð =Xð Xóð ð
	TÙ¤	× 2Ñ 2× IÑ IÈuÐ IÓ UÓV×VˆDô �t˜V TÓ*ˆØˆ<ÜðCóð ð ˆØ—.‘.¤¨¨d°AÓ!6Ó7×7€EÜ˜˜U¨$Ð.Ò?°5·<±<Ó@Ð@ð; <ùð +ùð )ùð /^øð WùÜò 	TÜÐ=¼dÀ1»g×>NÑ>NÐ=OÈrÐRÓSÐSûð	Túð 8ús˜   ‚AJÁI ÁJÁ)+JÂAJÃ(IÃ)/JÄIÄBJÆI	Æ JÆ6)I ÇIÇ I Ç$;JÈJ È !JÉJÉJÉ	JÉI É	I=É"I8É8I=É=J)r   r   )r   r   ÚreturnÚtuple)r3   r2   r^   r/   )rV   r	   r   r   r^   r/   )r7   Ú
__future__r   r   Údataclassesr   Útypingr   Útelethonr   r   Úcommonr	   r
   Úcompiler   r)   r*   r#   r-   r/   r@   rF   r]   r9   r$   r"   Ú<module>rf      s…   ðñõ
 #ã 	Ý !Ý ç %ç "à
ˆ"�*‰*ÐnÓ
o€ØˆR�Z‰ZÐyÓz€
Ø�—
‘
ÐIÓJ€óóð ÷ð ó ðó
Zòô&Ar$   