o
    Óó·i�%  ã                   @   sÖ   d Z ddlmZ ddlmZmZ ddlm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mZ G d	d
„ d
ejƒZeejƒG dd„ dejƒƒZG dd„ dejejƒZeddœZeejƒG dd„ dejƒƒZdS )z2
L{twisted.words} support for Instance Messenger.
é    )Úimplementer)ÚdeferÚerror)Úlog)ÚFailure)Úpb)ÚbasesupportÚ
interfaces)ÚAWAYÚOFFLINEÚONLINEc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚTwistedWordsPersonzHI a facade for a person you can talk to through a twisted.words service.c                 C   s   t j | ||¡ t| _d S ©N)r   ÚAbstractPersonÚ__init__r   Ústatus)ÚselfÚnameZwordsAccount© r   ú`C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/words/im/pbsupport.pyr      ó   
zTwistedWordsPerson.__init__c                 C   s   | j tkp	| j tkS r   )r   r   r
   ©r   r   r   r   ÚisOnline   ó   zTwistedWordsPerson.isOnlinec                 C   s   | j S r   )r   r   r   r   r   Ú	getStatus   s   zTwistedWordsPerson.getStatusc                 C   sF   |r| j jj | j||¡}| | jd| ¡ |S | j jj d| j|¡S )zReturn a deferred...ú* ÚdirectMessageN)ÚaccountÚclientÚperspectiver   r   Ú
addErrbackÚmetadataFailedÚ
callRemote©r   ÚtextÚmetadataÚdr   r   r   ÚsendMessage!   s   
ÿzTwistedWordsPerson.sendMessagec                 C   s"   t d|d|ƒ | jjj | j|¡S )Núresult:útext:)Úprintr   r   r   r   r   ©r   Úresultr$   r   r   r   r!   ,   s   z!TwistedWordsPerson.metadataFailedc                 C   s   || _ | j ¡  | ¡ d S r   )r   ÚchatÚgetContactsListZsetContactStatus)r   r   r   r   r   Ú	setStatus0   s   zTwistedWordsPerson.setStatusN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r'   r!   r/   r   r   r   r   r      s    r   c                   @   sF   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )ÚTwistedWordsGroupc                 C   s   t j | ||¡ d| _d S ©Nr   )r   ÚAbstractGroupr   Újoined)r   r   ZwordsClientr   r   r   r   7   r   zTwistedWordsGroup.__init__Nc                 C   sH   |r| j jj d| j||¡}| | jd| ¡ |S | j jj d| j|¡S )zReturn a deferred.ÚgroupMessager   N)r   r   r   r"   r   r    r!   r#   r   r   r   ÚsendGroupMessage;   s   

ÿ
ÿz"TwistedWordsGroup.sendGroupMessagec                 C   s$   | j jj d|| jjdœ| j¡ d S )NZsetGroupMetadata)ÚtopicÚtopic_author©r   r   r   r"   r   )r   r$   r   r   r   ÚsetTopicI   s
   
ýzTwistedWordsGroup.setTopicc                 C   s$   t d|d|ƒ | jjj d| j|¡S )Nr(   r)   r8   )r*   r   r   r   r"   r   r+   r   r   r   r!   P   s   
ÿz TwistedWordsGroup.metadataFailedc                 C   ó
   d| _ d S )Né   ©r7   r   r   r   r   ÚjoiningV   ó   
zTwistedWordsGroup.joiningc                 C   r>   r5   r@   r   r   r   r   ÚleavingY   rB   zTwistedWordsGroup.leavingc                 C   s   | j jj d| j¡S ©NÚ
leaveGroupr<   r   r   r   r   Úleave\   r   zTwistedWordsGroup.leaver   )
r0   r1   r2   r   r9   r=   r!   rA   rC   rF   r   r   r   r   r4   5   s    
r4   c                   @   s¦   e Zd ZdZd'dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	d'dd„Z
dd„ Zdd„ Zdd„ Zd'dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdS )(ÚTwistedWordsClientz�In some cases, this acts as an Account, since it a source of text
    messages (multiple Words instances may be on a single PB connection)
    Nc                 C   s:   d  |j||¡| _|| _td||ƒ || _|| _|| _d S )Nz
{} ({}:{})zHELLO I AM A PB SERVICE)ÚformatÚaccountNamer   r*   r-   r   Ú_logonDeferred)r   ZacctÚserviceNameÚperspectiveNameÚchatuirJ   r   r   r   r   e   s   ý
zTwistedWordsClient.__init__c                 C   ó   | j  || ¡S r   )r-   Ú	getPerson©r   r   r   r   r   rO   q   ó   zTwistedWordsClient.getPersonc                 C   rN   r   )r-   ÚgetGrouprP   r   r   r   rR   t   rQ   zTwistedWordsClient.getGroupc                 C   s   | j  |  |¡¡S r   )r-   ÚgetGroupConversationrR   rP   r   r   r   rS   w   ó   z'TwistedWordsClient.getGroupConversationc                 C   s   | j  d|¡ d S )NÚ
addContact)r   r"   rP   r   r   r   rU   z   rT   zTwistedWordsClient.addContactc                 C   ó    t d||ƒ |  |¡ |¡ d S )Nzreceived group members:)r*   rS   ZsetGroupMembers)r   ÚnamesÚgroupr   r   r   Úremote_receiveGroupMembers}   ó   z-TwistedWordsClient.remote_receiveGroupMembersc                 C   s(   t d||||ƒ |  |¡ |||¡ d S )Nzreceived a group message)r*   rS   ÚshowGroupMessage)r   ZsenderrX   Úmessager%   r   r   r   Úremote_receiveGroupMessage�   s   z-TwistedWordsClient.remote_receiveGroupMessagec                 C   rV   )Nzmember joined)r*   rS   ZmemberJoined©r   ÚmemberrX   r   r   r   Úremote_memberJoined…   rZ   z&TwistedWordsClient.remote_memberJoinedc                 C   s   t dƒ |  |¡ |¡ d S )Nzmember left)r*   rS   Z
memberLeftr^   r   r   r   Úremote_memberLeft‰   s   z$TwistedWordsClient.remote_memberLeftc                 C   s   | j  || ¡ |¡ d S r   ©r-   rO   r/   )r   r   r   r   r   r   Úremote_notifyStatusChanged�   s   z-TwistedWordsClient.remote_notifyStatusChangedc                 C   s"   | j  | j  || ¡¡ ||¡ d S r   )r-   ZgetConversationrO   ZshowMessage)r   r   r\   r%   r   r   r   Úremote_receiveDirectMessage�   s   ÿz.TwistedWordsClient.remote_receiveDirectMessagec                 C   s&   |D ]\}}| j  || ¡ |¡ qd S r   rb   )r   Zclistr   r   r   r   r   Úremote_receiveContactList•   s   ÿz,TwistedWordsClient.remote_receiveContactListc                 C   s.   d|v r|   |¡ |d | dd ¡¡ d S d S )Nr:   r;   )rS   r=   Úget)r   Zdict_Z	groupNamer   r   r   Úremote_setGroupMetadata™   s
   
ÿÿz*TwistedWordsClient.remote_setGroupMetadatac                 C   ó&   |   |¡ ¡  | j d|¡ | j|¡S )NÚ	joinGroup)rR   rA   r   r"   ÚaddCallbackÚ_cbGroupJoinedrP   r   r   r   ri   Ÿ   ó   ÿzTwistedWordsClient.joinGroupc                 C   rh   rD   )rR   rC   r   r"   rj   Ú_cbGroupLeftrP   r   r   r   rE   ¥   rl   zTwistedWordsClient.leaveGroupc                 C   s0   | j  |  |¡¡}| dd¡ | j d|¡ d S )NÚsysz
you joinedZgetGroupMembers)r-   rS   rR   r[   r   r"   ©r   r,   r   Z	groupConvr   r   r   rk   «   s   z!TwistedWordsClient._cbGroupJoinedc                 C   s.   t d|ƒ | j |  |¡d¡}| dd¡ d S )NÚleftr?   rn   zyou left)r*   r-   rS   rR   r[   ro   r   r   r   rm   °   s   
zTwistedWordsClient._cbGroupLeftc                 C   s4   t d|ƒ | jd ur| j | ¡ || _| j ¡  d S )NzConnected Words Client!)r*   rJ   Úcallbackr   r-   r.   )r   r   r   r   r   Ú	connectedµ   s
   

zTwistedWordsClient.connectedr   )r0   r1   r2   r3   r   rO   rR   rS   rU   rY   r]   r`   ra   rc   rd   re   rg   ri   rE   rk   rm   rr   r   r   r   r   rG   `   s(    


rG   N)útwisted.wordsztwisted.realityc                   @   sT   e Zd ZdZeZeZ	ddd„Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ ZdS )Ú	PBAccountZPBNc              	   C   sX   t j | ||||||¡ g | _|sdd|fg}|D ]\}}	}
| j t| |	|
g¡ qdS )z_
        @param username: The name of your PB Identity.
        @type username: string
        rs   N)r   ÚAbstractAccountr   ÚservicesÚappendÚpbFrontEnds)r   rI   Z	autoLoginÚusernameÚpasswordÚhostÚportrv   ZserviceTyperK   rL   r   r   r   r   Æ   s   ÿÿÿzPBAccount.__init__c                    sN   ˆj s"ˆjs"dˆ_ ˆ ˆ ¡}| ˆj¡ ‡ ‡fdd„}| |¡ |S t d¡‚)z
        @returns: this breaks with L{interfaces.IAccount}
        @returntype: DeferredList of L{interfaces.IClient}s
        r?   c                    s6   | D ]\}}|rˆ   |¡ ˆ |¡ qt |¡ qd S r   )ZregisterAccountClientZ	_cb_logOnr   Úerr)ÚresultsÚsuccessr,   ©rM   r   r   r   ÚregisterManyå   s   
ûz%PBAccount.logOn.<locals>.registerManyzConnection in progressN)Z_isConnectingZ	_isOnlineÚ_startLogOnr    Z_loginFailedrj   r   ÚConnectionError)r   rM   r&   r�   r   r€   r   ÚlogOnØ   s   


zPBAccount.logOnc                 C   s   d S r   r   r   r   r   r   ÚlogOffò   s   zPBAccount.logOffc                 C   s6   t ddd� t | j| j¡}|j| j| j|fd� |S )NzConnecting...ú ©Úend©ZcallbackArgs)r*   r   ZgetObjectAtr{   r|   ÚaddCallbacksÚ_cbConnectedÚ_ebConnected)r   rM   r&   r   r   r   r‚   ö   s   zPBAccount._startLogOnc                 C   s@   t dƒ t ddd� t || j| j¡}|j| j| j|fd� |S )Nz
Connected!zIdentifying...r†   r‡   r‰   )r*   r   ZauthIdentityry   rz   rŠ   Ú_cbIdentrŒ   )r   ÚrootrM   r&   r   r   r   r‹   ü   s
   zPBAccount._cbConnectedc           	      C   s€   |st dƒ |  ttdƒƒ¡S t dƒ g }| jD ]"\}}}t ¡ }| |¡ || ||||ƒ}| d|||¡ 	|j
¡ qt |¡S )Nzfalsely identified.zusername or password incorrectzIdentified!Úattach)r*   rŒ   r   Ú	Exceptionrv   r   ZDeferredrw   r"   rj   rr   ZDeferredList)	r   ÚidentrM   ÚdlZhandlerClassZsnameZpnamer&   Úhandlerr   r   r   r�     s   
ÿ
ÿ
zPBAccount._cbIdentc                 C   s   t dƒ |S )NzNot connected.)r*   )r   r   r   r   r   rŒ     s   zPBAccount._ebConnectedr   )r0   r1   r2   ZgatewayTyper4   Z_groupFactoryr   Z_personFactoryr   r„   r…   r‚   r‹   r�   rŒ   r   r   r   r   rt   À   s    
ÿrt   )r3   Zzope.interfacer   Ztwisted.internetr   r   Ztwisted.pythonr   Ztwisted.python.failurer   Ztwisted.spreadr   Ztwisted.words.imr   r	   Ztwisted.words.im.localsr
   r   r   r   r   ZIGroupr6   r4   ZReferenceableZAbstractClientMixinrG   rx   ZIAccountru   rt   r   r   r   r   Ú<module>   s   !*
]