î
_ó¤Wâ  ã               @   s_  d  Z  d d l m Z m Z m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l Z d d l Z d d l Z d d l Z d a d d	 „  Z d
 d „  Z i e g d 6Z e d d ƒ d d „  ƒ Z e d d ƒ d d „  ƒ Z e d d d d ƒd d „  ƒ Z e d d d d ƒd d „  ƒ Z e d  d! d d ƒd" d# „  ƒ Z e d$ d% „  ƒ Z d S)&z)
Info functionality

* depends: database
é   )ÚcommandÚinitÚprocess_privmsg)ÚSubstitution)Úget_docé    )ÚpartialNc              C   s;   t  j ƒ  d d j }  d d „  t j |  ƒ Dƒ } | d S)Nr   r   c             S   s%   g  |  ] } t  j | ƒ r | ‘ q S© )ÚinspectÚ
isfunction)Ú.0Úxr	   r	   ú./plugins/infobot.pyú
<listcomp>   s   	 zcaller.<locals>.<listcomp>)r
   ÚstackÚf_codeÚgcÚget_referrers)Úcode_objÚ	referrersr	   r	   r   Úcaller   s    r   c             C   sY  t  | ƒ \ } } } t j d | ƒ } | s1 d  Sd | k rM |  j | d ƒ S| d j d ƒ \ } } | j d ƒ d } | j d d ƒ d } |  j j | ƒ s± |  j | d ƒ Sd	 | k r%| j ƒ  d
 }	 t j	 d | |	 f ƒ j
 ƒ  d }
 |
 s|  j | d ƒ qU|  j | d | |	 f ƒ n0 t j	 d | | | | f ƒ |  j | d | ƒ d  S)Nz^!add .+ú zUsage: !add <info>Úhostú@ú!r   z@You are not registered with NickServ or not properly identified.Úaliasé   zSELECT addalias(%s, %s);r   zHError setting alias; you are creating an infinitely looping alias chain.z2The info of your current nick %s now points to %s.zSELECT addinfo(%s, %s, %s, %s);zInfo set to '%s')r   ÚreÚsearchÚmsgÚsplitÚauthÚ	is_authedÚnoticeÚdbÚexecuteÚfetchone)ÚbotÚpmsgÚnickÚchanr   ÚmÚuserr   Úinfor   Úsuccessr	   r	   r   Úaddinfo   s&    "r/   ÚPRIVMSGr-   z^(!|@)$name(\s|$)c             C   sÛ   | s |  j  | t ƒ  ƒ St j d | f ƒ j ƒ  } | d d k rY t |  j  | ƒ } n t |  j | ƒ } | s„ | d j | ƒ ƒ S| d j ƒ  | j ƒ  k r¸ | d | | d f ƒ S| d | | d | d f ƒ d S)	z0 !info <nick> -> get the info for a given user. z SELECT nick, info FROM info(%s);r   r   z:No info found for {0}. Use '!add <info>' to add your info.z%s: %sr   u   %s â†’  %s: %sN)	Ú_msgr   r$   r%   r&   r   r#   ÚformatÚlower)r'   r)   r*   ÚgrÚargr-   Úmsgfnr	   r	   r   Úgetinfo8   s    r7   zdel|rmz^!($name)(\s|$)c             C   s¡   | s | d	 k r& |  j  | t ƒ  ƒ S|  j j | ƒ sH |  j | d ƒ S| d k rz t j d | f ƒ |  j | d ƒ n# t j d | f ƒ |  j | d ƒ d S)
z) !del <type> -> delete 'alias' or 'info' r   r-   z@You are not registered with NickServ or not properly identified.zSELECT delalias(%s);z6Your nick now points to itself instead of to an alias.zSELECT delinfo(%s);zDeleted info.N)zaliaszinfo)r1   r   r!   r"   r#   r$   r%   )r'   r)   r*   Ú_r5   r	   r	   r   ÚrmaliasJ   s    r9   Úappendz^!$name(?:\s|$)ÚppmsgTc       	      C   sÊ   | s |  j  | t ƒ  ƒ S| d j d ƒ \ } } | j d ƒ d } |  j j | ƒ sg |  j | d ƒ St j d | f ƒ j ƒ  \ } } | d | 7} t j d | | | | f ƒ |  j | d	 | ƒ d
 S)z/ !append <info> -> Append <info> to your info. r   r   r   r   z@You are not registered with NickServ or not properly identified.zSELECT nick, info FROM info(%s)r   zSELECT addinfo(%s, %s, %s, %s);zInfo set to '%s'N)	r1   r   r    r!   r"   r#   r$   r%   r&   )	r'   r)   r*   r5   r(   r,   r   r   r-   r	   r	   r   Ú
appendinfoZ   s    !r<   Zsqlz
^&$name .+Úadminc             C   sZ   t  j | ƒ y4 |  j | d d j d d „  t  j ƒ  Dƒ ƒ ƒ Wn t j ƒ  Yn Xd  S)Nz%sz, c             S   s"   g  |  ] } t  t | ƒ ƒ ‘ q Sr	   )ÚstrÚlist)r   Úir	   r	   r   r   o   s   	 zexecsql.<locals>.<listcomp>)r$   r%   r1   ÚjoinÚfetchallÚ	tracebackÚ	print_exc)r'   r)   r*   r5   r	   r	   r   Úexecsqlk   s
    4rE   Úsedz
^!$name .+c             C   s/  t  j d | f ƒ j ƒ  } | d j d ƒ \ } } | j d ƒ d } |  j j | ƒ si |  j | d ƒ Sy t | ƒ } Wn9 t k
 r´ }	 z |  j | d |	 ƒ SWYd  d  }	 ~	 Xn X| j	 | d ƒ }
 | d j
 ƒ  | j
 ƒ  k rû |  j | d	 |
 ƒ n  t  j d
 | | | |
 f ƒ |  j | d |
 ƒ d  S)Nz SELECT nick, info FROM info(%s);r   r   r   r   z@You are not registered with NickServ or not properly identified.z	Error: %sr   zgNote: because your current nick is an alias, your alias willbe removed and your info will be set to %r.zSELECT addinfo(%s, %s, %s, %s);zInfo set to '%s')r$   r%   r&   r    r!   r"   r#   r   Ú	TypeErrorÚdor3   )r'   r)   r*   r5   r(   r-   r,   r   ÚsubÚeZnewinfor	   r	   r   Úsedinfos   s    'rK   c             C   s   |  j  d a d  S)Nr$   )Údatar$   )r'   r	   r	   r   r   Œ   s    r   )Ú__doc__Zutil.decoratorsr   r   r   rF   r   Z	util.datar   Ú	functoolsr   r   rC   r
   r   r$   r   r/   Ú__callbacks__r7   r9   r<   rE   rK   r	   r	   r	   r   Ú<module>   s$   !!!