o
    ¯b�%  ã                   @   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ÚnameÚwordsAccount© r   ú</usr/lib/python3/dist-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...ú* ÚdirectMessage)Ú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ÚgetContactsListÚsetContactStatus)r   r   r   r   r   Ú	setStatus0   s   zTwistedWordsPerson.setStatusN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r(   r"   r1   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   Ú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   )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 )NÚ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*   r;   )r+   r   r   r    r#   r   r,   r   r   r   r"   P   s   
ÿz TwistedWordsGroup.metadataFailedc                 C   ó
   d| _ d S )Né   ©r9   r   r   r   r   ÚjoiningV   ó   
zTwistedWordsGroup.joiningc                 C   rB   r7   rD   r   r   r   r   ÚleavingY   rF   zTwistedWordsGroup.leavingc                 C   s   | j jj d| j¡S ©NÚ
leaveGroupr@   r   r   r   r   Úleave\   r   zTwistedWordsGroup.leaver   )
r2   r3   r4   r   r<   rA   r"   rE   rG   rJ   r   r   r   r   r6   5   s    
r6   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   ÚacctÚserviceNameÚperspectiveNameÚchatuirN   r   r   r   r   e   s   ý
zTwistedWordsClient.__init__c                 C   ó   | j  || ¡S r   )r.   Ú	getPerson©r   r   r   r   r   rT   q   ó   zTwistedWordsClient.getPersonc                 C   rS   r   )r.   ÚgetGrouprU   r   r   r   rW   t   rV   zTwistedWordsClient.getGroupc                 C   s   | j  |  |¡¡S r   )r.   ÚgetGroupConversationrW   rU   r   r   r   rX   w   ó   z'TwistedWordsClient.getGroupConversationc                 C   s   | j  d|¡ d S )NÚ
addContact)r    r#   rU   r   r   r   rZ   z   rY   zTwistedWordsClient.addContactc                 C   ó    t d||ƒ |  |¡ |¡ d S )Nzreceived group members:)r+   rX   ÚsetGroupMembers)r   ÚnamesÚgroupr   r   r   Úremote_receiveGroupMembers}   ó   z-TwistedWordsClient.remote_receiveGroupMembersc                 C   s(   t d||||ƒ |  |¡ |||¡ d S )Nzreceived a group message)r+   rX   ÚshowGroupMessage)r   Úsenderr^   Úmessager&   r   r   r   Úremote_receiveGroupMessage�   s   z-TwistedWordsClient.remote_receiveGroupMessagec                 C   r[   )Nzmember joined)r+   rX   ÚmemberJoined©r   Úmemberr^   r   r   r   Úremote_memberJoined…   r`   z&TwistedWordsClient.remote_memberJoinedc                 C   s   t dƒ |  |¡ |¡ d S )Nzmember left)r+   rX   Ú
memberLeftrf   r   r   r   Úremote_memberLeft‰   s   z$TwistedWordsClient.remote_memberLeftc                 C   s   | j  || ¡ |¡ d S r   ©r.   rT   r1   )r   r   r   r   r   r   Úremote_notifyStatusChanged�   s   z-TwistedWordsClient.remote_notifyStatusChangedc                 C   s"   | j  | j  || ¡¡ ||¡ d S r   )r.   ÚgetConversationrT   ÚshowMessage)r   r   rc   r&   r   r   r   Úremote_receiveDirectMessage�   s   ÿz.TwistedWordsClient.remote_receiveDirectMessagec                 C   s&   |D ]\}}| j  || ¡ |¡ qd S r   rk   )r   Ú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?   )rX   rA   Úget)r   Údict_Ú	groupNamer   r   r   Úremote_setGroupMetadata™   s
   
ÿÿz*TwistedWordsClient.remote_setGroupMetadatac                 C   ó&   |   |¡ ¡  | j d|¡ | j|¡S )NÚ	joinGroup)rW   rE   r    r#   ÚaddCallbackÚ_cbGroupJoinedrU   r   r   r   rw   Ÿ   ó   ÿzTwistedWordsClient.joinGroupc                 C   rv   rH   )rW   rG   r    r#   rx   Ú_cbGroupLeftrU   r   r   r   rI   ¥   rz   zTwistedWordsClient.leaveGroupc                 C   s0   | j  |  |¡¡}| dd¡ | j d|¡ d S )NÚsysz
you joinedÚgetGroupMembers)r.   rX   rW   ra   r    r#   ©r   r-   r   Ú	groupConvr   r   r   ry   «   s   z!TwistedWordsClient._cbGroupJoinedc                 C   s.   t d|ƒ | j |  |¡d¡}| dd¡ d S )NÚleftrC   r|   zyou left)r+   r.   rX   rW   ra   r~   r   r   r   r{   °   s   
zTwistedWordsClient._cbGroupLeftc                 C   s4   t d|ƒ | jd ur| j | ¡ || _| j ¡  d S )NzConnected Words Client!)r+   rN   Úcallbackr    r.   r/   )r   r    r   r   r   Ú	connectedµ   s
   

zTwistedWordsClient.connectedr   )r2   r3   r4   r5   r   rT   rW   rX   rZ   r_   rd   rh   rj   rl   ro   rq   ru   rw   rI   ry   r{   r‚   r   r   r   r   rK   `   s(    


rK   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 )Ú	PBAccountÚ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
        rƒ   N)r   ÚAbstractAccountr   ÚservicesÚappendÚpbFrontEnds)r   rM   Ú	autoLoginÚusernameÚpasswordÚhostÚportr‡   ÚserviceTyperP   rQ   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
        rC   c                    s6   | D ]\}}|rˆ   |¡ ˆ |¡ qt |¡ qd S r   )ÚregisterAccountClientÚ	_cb_logOnr   Úerr)ÚresultsÚsuccessr-   ©rR   r   r   r   ÚregisterManyå   s   
ûz%PBAccount.logOn.<locals>.registerManyzConnection in progress)Ú_isConnectingÚ	_isOnlineÚ_startLogOnr!   Ú_loginFailedrx   r   ÚConnectionError)r   rR   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©ÚcallbackArgs)r+   r   ÚgetObjectAtr�   rŽ   ÚaddCallbacksÚ_cbConnectedÚ_ebConnected)r   rR   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   ÚauthIdentityr‹   rŒ   r¤   Ú_cbIdentr¦   )r   ÚrootrR   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   Ú	Exceptionr‡   r   ÚDeferredrˆ   r#   rx   r‚   ÚDeferredList)	r   ÚidentrR   ÚdlÚhandlerClassÚsnameÚ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   )r2   r3   r4   ÚgatewayTyper6   Ú_groupFactoryr   Ú_personFactoryr   rœ   r�   r™   r¥   r¨   r¦   r   r   r   r   r„   À   s    
ÿr„   )r5   Úzope.interfacer   Útwisted.internetr   r   Útwisted.pythonr   Útwisted.python.failurer   Útwisted.spreadr   Útwisted.words.imr   r	   Útwisted.words.im.localsr
   r   r   r   r   ÚIGroupr8   r6   ÚReferenceableÚAbstractClientMixinrK   r‰   ÚIAccountr†   r„   r   r   r   r   Ú<module>   s   !*
]