B
    5Ù&e#+  ã               @   sº   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
T d dlmZ d dlmZ d dlmZ d dlmZmZ edƒZG dd„ deƒZG dd„ dejƒZeeƒZeZdS )é    N)Ú*)ÚPluginInternationalizationÚinternationalizeDocstringÚLaterc               @   s   e Zd ZdS )ÚQueueIsFullN)Ú__name__Ú
__module__Ú__qualname__© r
   r
   úK/home/dcms/DCMS/lib/python3.7/site-packages/supybot/plugins/Later/plugin.pyr   /   s   r   c                   sÜ   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zd$dd„Z	dd„ Z
edd„ ƒZeeeeddƒƒdgƒZedd„ ƒZeeedƒgƒZedd„ ƒZeeddgƒZedd„ ƒZeedgƒZdd„ Zd d!„ Zd"d#„ Z‡  ZS )%r   aH  
    Used to do things later; currently, it only allows the sending of
    nick-based notes.  Do note (haha!) that these notes are *not* private
    and don't even pretend to be; if you want such features, consider using the
    Note plugin.

    Use the ``later tell`` command to leave a message to a user.
    If you sent the message by accident or want to cancel it,
    you can use the `later undo` command to remove the latest later,
    which you have sent.

    You can also see the people who have notes waiting for them by using
    the `later notes` command. If you specify a nickname in ``later notes``
    command, you will see the notes, which are waiting for the nickname.

    Privacy
    -------

    As you probably noticed from above, this plugin isn't private.
    Everyone can see notes sent by anyone and the laters are sent on channel
    by default and as the "plugin help later" says::

        Used to do things later; currently, it only allows the sending of nick-based notes. Do note (haha!) that these notes are *not* private and don't even pretend to be; if you want such features, consider using the Note plugin.

    The Note plugin identifies people by username instead of nickname
    and allows only users to send notes.
    The only people who are able to read notes are the sender, receiver,
    and the owner.

    c                sF   t t| ƒ| _| j |¡ t ¡ | _g | _tj	j
j d¡| _|  ¡  d S )NzLater.db)Úsuperr   Z_Later__parentÚ__init__ÚircutilsZIrcDictÚ_notesÚ	wildcardsÚconfZsupybotÚdirectoriesÚdataZdirizeÚfilenameÚ
_openNotes)ÚselfÚirc)Ú	__class__r
   r   r   Q   s    
zLater.__init__c             C   s   |   ¡  d S )N)Ú_flushNotes)r   r
   r
   r   ÚdieY   s    z	Later.diec             C   sb   t j | j¡}t |¡}x<| j ¡ D ].\}}x$|D ]\}}}| ||||g¡ q2W q$W | 	¡  d S )N)
ÚutilsÚfileZ
AtomicFiler   ÚcsvÚwriterr   ÚitemsÚwriterowÚclose)r   Úfdr   ÚnickÚnotesÚtimeÚwhenceÚtextr
   r
   r   r   \   s    
zLater._flushNotesc          
   C   sŽ   yt j| jdd}W n4 tk
rH } z| j d| j|¡ d S d }~X Y nX t |¡}x,|D ]$\}}}}| j|||t	|ƒdd qZW | 
¡  d S )NÚutf8)ÚencodingzCouldn't open %s: %sr   )ÚatÚmaximum)ÚcodecsÚopenr   ÚEnvironmentErrorÚlogÚwarningr   ÚreaderÚ_addNoteÚfloatr!   )r   r"   Úer1   r#   r%   r&   r'   r
   r
   r   r   d   s    
zLater._openNotesc             C   s8   |t   ¡  }ytj|ddS  tk
r2   tdƒS X d S )NF)Úsecondszjust now)r%   r   ZtimeElapsedÚ
ValueErrorÚ_)r   ÚwhenÚdiffr
   r
   r   Ú
_timestampo   s
    zLater._timestampNc             C   s²   |d krt   ¡ }|d kr"|  d¡}y6| j| }|rFt|ƒ|krFtƒ ‚n| |||f¡ W n& tk
r~   |||fg| j|< Y nX tdƒt|ƒ@ r¦|| jkr¦| j |¡ |  	¡  d S )Nr+   z?*!@)
r%   ÚregistryValuer   Úlenr   ÚappendÚKeyErrorÚsetr   r   )r   r#   r&   r'   r*   r+   r$   r
   r
   r   r2   w   s    


zLater._addNotec             C   sÀ   |   d¡}t ¡ }g }x†| j ¡ D ]x\}}g }x<|D ]4\}}}	tj|| d}
|
j|kr4| |||	f¡ q4W x|D ]}| |¡ qrW t	|ƒdkr"| |¡ q"W x|D ]}| j|= q¤W |  
¡  d S )NZmessageExpiry)r5   r   )r;   r%   r   r   ÚdatetimeÚ	timedeltaÚdaysr=   Úremover<   r   )r   ZexpiryZcurtimeZnickremovalsr#   r$   ÚremovalsZnotetimer&   r'   ÚtdÚnoter
   r
   r   Ú_deleteExpired‰   s     



zLater._deleteExpiredc       
   	   C   s´   |   ¡  g }x:t|ƒD ].}t ||j¡r:| tdƒ¡ dS | |¡ qW g }x@|D ]8}	y|  |	|j	|¡ W qR t
k
rˆ   | |	¡ Y qRX qRW |r¨| ttdƒ|ƒ¡ n| ¡  dS )zÕ<nick1[,nick2[,...]]> <text>

        Tells each <nickX> <text> the next time <nickX> is seen.  <nickX> can
        contain wildcard characters, and the first matching nick will be
        given the note.
        zI can't send notes to myself.Nz4These recipients' message queue are already full: %L)rG   r?   r   ZstrEqualr#   Úerrorr7   r=   r2   Úprefixr   ÚformatÚreplySuccess)
r   r   ÚmsgÚargsÚnicksr'   Z
validnicksr#   Zfull_queuesZ	validnickr
   r
   r   Útell¢   s$    

z
Later.tellr#   Úhostmaskr'   c                sŒ   |rH|ˆ j kr8‡ fdd„ˆ j | D ƒ}| td|ƒ¡ qˆ| tdƒ¡ n@ˆ j  ¡ }|rzt tj	|¡ | ttdƒ|ƒ¡ n| tdƒ¡ dS )z©[<nick>]

        If <nick> is given, replies with what notes are waiting on <nick>,
        otherwise, replies with the nicks that have notes waiting for them.
        c                s    g | ]\}}}ˆ   |||¡‘qS r
   )Ú_formatNote)Ú.0r8   r&   rF   )r   r
   r   ú
<listcomp>È   s   zLater.notes.<locals>.<listcomp>z%LzI have no notes for that nick.z&I currently have notes waiting for %L.z(I have no notes waiting to be delivered.N)
r   ÚreplyrJ   rH   r7   Úkeysr   ZsortByr   ZtoLower)r   r   rL   rM   r#   r$   rN   r
   )r   r   r$   ¿   s    



zLater.notesZ	somethingc             C   sH   y| j |= |  ¡  | ¡  W n& tk
rB   | tdƒ| ¡ Y nX dS )z=<nick>

        Removes the notes waiting on <nick>.
        zThere were no notes for %rN)r   r   rK   r>   rH   r7   )r   r   rL   rM   r#   r
   r
   r   rC   ×   s    zLater.remove)ZcheckCapabilityZadminc             C   sª   || j kr | tdƒ| ¡ dS | j |  ¡  xd| j | D ]V}t |d ¡|jkr:| j |  |¡ t| j | ƒdkr|| j |= |  	¡  | 
¡  dS q:W | tdƒ| ¡ dS )zD<nick>

        Removes the latest note you sent to <nick>.
        z!There are no note waiting for %s.Né   r   z*There are no note from you waiting for %s.)r   rH   r7   Úreverser   ÚnickFromHostmaskr#   rC   r<   r   rK   )r   r   rL   rM   r#   rF   r
   r
   r   Úundoå   s    
z
Later.undoc             C   sì   t  |¡rt  |¡sd S | j |jg ¡}g }xR| jD ]H}t ||j	¡rb| 
|¡ | | j |¡¡ x|D ]}| j |¡ qhW q4W |rè|j}t ||¡}|  d¡}x2|D ]*\}	}
}|  |	|
|¡}|j||| d q¦W |  ¡  | d|¡ d S )NÚprivate)rZ   Z
prefixNickÚ	repliedTo)ÚircmsgsZisCtcpZisActionr   Úpopr#   r   r   ZhostmaskPatternEqualrI   r=   ÚextendrC   r[   Ú	callbacksZSimpleProxyr;   rQ   rT   r   Útag)r   r   rL   r$   rD   ZwildcardZremovalZold_repliedtorZ   r8   r&   rF   Úsr
   r
   r   Ú	doPrivmsgú   s&    


zLater.doPrivmsgc             C   s,   |   d¡st |¡}tdƒ|  |¡||f S )Nzformat.senderHostnamezSent %s: <%s> %s)r;   r   rX   r7   r:   )r   r8   r&   rF   r
   r
   r   rQ     s    

zLater._formatNotec             C   s   |   d¡r|  ||¡ d S )NZ
tellOnJoin)r;   rb   )r   r   rL   r
   r
   r   ÚdoJoin  s    
zLater.doJoin)NN)r   r   r	   Ú__doc__r   r   r   r   r:   r2   rG   r   rO   ÚwrapZ	commalistÚfirstr$   Ú
additionalrC   rY   rb   rQ   rc   Ú__classcell__r
   r
   )r   r   r   2   s&   
)r   r%   r,   r@   Zsupybot.logr/   Zsupybot.confr   Zsupybot.utilsr   Zsupybot.commandsZsupybot.ircmsgsr\   Zsupybot.ircutilsr   Zsupybot.callbacksr_   Zsupybot.i18nr   r   r7   Ú	Exceptionr   ZPluginr   ÚClassr
   r
   r
   r   Ú<module>    s"    g