
    KcF?                     ^   d dl Z d dlT 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
Z
d dlZd dlZd dlmZ d dlmZ d dlZ	 d dlZdZn#  d	Z G d
 d          ZY nxY wd Zd Z G d d          Z G d dej                  Z G d d          Zedk    rd dlZ eej                  dk     r ed            ej        d            ed            ej                    Z d Z!ej        d         Z" e            Z#e#$                    e"e!           e %                                 dS dS )    N)*)Gdk)Gtk)TimedOperation)GObject)GLibTFc                   (    e Zd Z G d d          ZdS )pysmbc                       e Zd ZdS )pysmb.AuthContextN)__name__
__module____qualname__     1/usr/share/system-config-printer/probe_printer.pyAuthContextr   )   s        Dr   r   N)r   r   r   r   r   r   r   r
   r
   (   s<        	 	 	 	 	 	 	 	 	 	r   r
   c                    g }d}d}d}d}t          |           }t          |          D ]}| |         }|r||z  }d}|dk    rd}d}|rN|r|dk    rd},||z  }2|                                r|                    |           d}d}`|dk    rd}i||z  }o|dk    rd}d}z|                                sd}||z  }|dk    r|                    |           |S )NF \T")lenrangeisspaceappend)	linewordsescapedquotedin_wordwordnichs	            r   wordsepr%   ,   s;   EGFGDT

AAYY    !W 	BJDG::GG 	 99"FFBJDD d###s
SyyZZ]] 
rzzdLr   c                 *   	 |                      dd          \  }}n# t          $ r | }Y nw xY wd }	 t          j        ||t          j        t          j                  }n## t          j        t          j        f$ r g }Y nw xY w|D ]}|\  }}}}	}
	 t          j        |||          }|                    d           n# t          j        $ r d }Y Jw xY w	 |	                    |
           n+# t          j        $ r |
                                 d }Y w xY w |S )N:   g      ?)split
ValueErrorsocketgetaddrinfo	AF_UNSPECSOCK_STREAMgaierrorerror
settimeoutconnectclose)hostnameporthostsairesafsocktypeproto	canonnamesas              r   open_socketr?   ]   si   ^^C++
dd    	AdF,< & 24 4OV\*      -0*HeY	b(E22ALL| 	 	 	AH		IIbMMMM| 	 	 	GGIIIAH	 	HsA    +++A A=<A=+B99CCC''%DDc                   ,    e Zd Zd Zd Zd Zd Zd ZdS )	LpdServerc                 0    || _         d| _        d| _        d S )N   F)r4   max_lpt_comstop)selfr4   s     r   __init__zLpdServer.__init__|   s     			r   c                    t          | j        d          }|sd S t          |           	 |                    d|z                      d                     |                    d                              d          }t          t          |                     nI# t          j	        $ r7}t          |           	 |
                                 n#  Y nxY wY d }~dS d }~ww xY wt          |          dk    rbt          |d                   dk    rI	 |                    d           |
                                 n#  Y nxY w|                    |           dS 	 |
                                 n#  Y nxY wdS )	Ni  z%s
zUTF-8i   Fr   s   
T)r?   r4   
debugprintsendencoderecvdecodereprr+   r0   r3   r   ordr   )rF   nameresultr7   datamsgs         r   probe_queuezLpdServer.probe_queue   sx   s++ 	44	FFHtO++G4455566$<<&&w//DtDzz""""| 	 	 	sOOO



55555	 t99Q;;3tAw<<??w



MM$4	GGIIII	DusH   A/B C )C9CCCCC )D: :D>E. .E2c                     g d}t          | j                  D ]'}|                    d|z  d|z  d|z  d|z  g           (t          d          D ]}|                    d|z             |S )N)PASSTHRUpslpPORT1r   zLPT%dzLPT%d_PASSTHRUzCOM%dzCOM%d_PASSTHRU2   zpr%d)r   rD   extendr   )rF   	candidatenrs      r   get_possible_queue_namesz"LpdServer.get_possible_queue_names   s    999	)** 	7 	7Bw|/"4&|/"46 7 7 7 7 ** 	+ 	+Bfrk****r   c                 2    t          d           d| _        d S )Nz!LpdServer exiting: destroy calledT)rI   rE   rF   s    r   destroyzLpdServer.destroy   s    7888			r   c                 J   g }|                                  D ]}t          j                    r&t          j                     t          j                    &| j        r nH|                     ||          }| n.|s|                    d          r nt          j        d           |S )Npr皙?)	r^   r   events_pendingmain_iterationrE   rT   
startswithtimesleep)rF   rQ   rP   founds       r   probezLpdServer.probe   s    1144 	 	D$'' &"%%% $'' & y $$T622E} T__d33 JsOOOOr   N)r   r   r   rG   rT   r^   ra   rk   r   r   r   rA   rA   {   s`          
" " "H
 
 
      r   rA   c                   $    e Zd ZdZd Zd Zd ZdS )BackgroundSmbAuthContextzIAn SMB AuthContext class that is only ever run from
    a non-GUI thread.c                 j    t          j                    | _        t          j        j        | g|R i | d S N)	threadingEvent
_gui_eventr
   r   rG   )rF   argskwargss      r   rG   z!BackgroundSmbAuthContext.__init__   s;    #/,,"D:4:::6:::::r   c                     t          j                     t          j                            |           }t          j                     || _        | j                                         d S ro   )	r   threads_enterr
   r   perform_authenticationthreads_leave!_do_perform_authentication_resultrr   set)rF   rQ   s     r   _do_perform_authenticationz3BackgroundSmbAuthContext._do_perform_authentication   sX    "994@@17.r   c                 4   | j         dk    s| j        r| j        r| j        r&| j        st          j                            |           S | j                                         t          j
        d| j                   | j                                         | j        S )Nr   r(   )passes
has_failedauth_calledtried_guestr
   r   rw   rr   clearr   timeout_addr{   waitry   r`   s    r   rw   z/BackgroundSmbAuthContext.perform_authentication   s    K1      '+&6 
 $;;TBBB   !T<===55r   N)r   r   r   __doc__rG   r{   rw   r   r   r   rm   rm      sK         ; ; ;  6 6 6 6 6r   rm   c                   R    e Zd Zd Zd Zd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd ZdS )PrinterFinderc                     d| _         d S )NF)quitr`   s    r   rG   zPrinterFinder.__init__   s    			r   c                 Z    || _         || _        t          | j        d           | _        d S )Nc                     d S ro   r   )xys     r   <lambda>z$PrinterFinder.find.<locals>.<lambda>   s    t r   )callback)r4   callback_fnr   _do_findop)rF   r4   r   s      r   findzPrinterFinder.find   s/     & $-:K:KLLLr   c                 F    | j                                          d| _        d S )NT)r   cancelr   r`   s    r   r   zPrinterFinder.cancel   s    			r   c                     t                      | _        | j        | j        | j        | j        | j        | j        fD ]5}| j        r d S 	  |             # t          $ r t                       Y 2w xY w| j        s|                     d            d S d S ro   )dict_cached_attributes_probe_hplip_probe_jetdirect
_probe_ipp_probe_snmp
_probe_lpd
_probe_smbr   	ExceptionnonfatalExceptionr   )rF   fns     r   r   zPrinterFinder._do_find   s    "&&&$(?#??$ 	% 	%B y % % % %!$$$$$% y 	$d#####	$ 	$s   
AA-,A-Nc                     dd|z  d}|r||d<   |                     | j                   t          j        |fi |}t	          d|z             |                     |           d S )Nnetworkz%s)device-classdevice-infodevice-locationDevice found: %s)updater   cupshelpersDevicerI   r   )rF   uriinfolocationdevice_dict
new_devices         r   _new_devicezPrinterFinder._new_device  s    (1'+d{5 5 	4+3K)*D3444 '<<<<
&,---*%%%%%r   c                    	 t          d           t          j        d| j        gdt          j        t          j        t          j                  }n=# t          $ r0}t          d           |j        t          j        k    rY d }~d S  d }~ww xY w|	                                \  }}|j
        dk    rt          d|j
        z             d S | j        rt          d           d S |                                                    d          D ]}t          |          }t          |          }|d	k    r
|\  }}	}
}}}n|d
k    r	|\  }}	}
}}n|dk    r|\  }}	}
}nN||
|d}|d
k    rt          d|z             ||d<   |d	k    r||d<   t!          j        |	fi |}t          d|	z             |                     |           |
| j        d<   || j        d<   t          d           d S )Nzsnmp: tryingz/usr/lib/cups/backend/snmpTrs   	close_fdsstdinstdoutstderrzsnmp: no goodr   zsnmp: no good (return code %d)
         )r   device-make-and-modelr   zsnmp: Device ID found:
%sz	device-idr   r   r   	device_idz
snmp: done)rI   
subprocessPopenr4   DEVNULLPIPEOSErrorerrnoENOENTcommunicate
returncoder   rM   r)   r%   r   r   r   r   r   )rF   per   r   r   r   r"   device_classr   make_and_modelr   r   device_locationr   devices                   r   r   zPrinterFinder._probe_snmp  s]   	''' (D(,(7,0(2(:)3)3);= = =AA  	 	 	(((w%,&&	 ==++<181<GHHHF9 	(((FMM$$**D11 	= 	=DTNNEUAAvv5:3sNy//aGLDsND))a<A9sNDD,85C+/2 2K Avv8%& ' ' '+4K(Avv1@-. '<<<<F*S0111f%%% @ND#$;<3<D#K00L!!!!!s   AA 
B
$BBB
c                    t          d           t          | j                  }|                                D ]}| j        rt          d            d S |                    |g           }|t          d            nX|r(d| j        d|}|                     || j                   |s|                    d          r nt          j	        d           t          d           d S )	Nzlpd: tryingzlpd: no goodzlpd: couldn't connectzlpd:///rc   rd   z	lpd: done)
rI   rA   r4   r^   r   rT   r   rg   rh   ri   )rF   lpdrP   rj   r   s        r   r   zPrinterFinder._probe_lpdL  s    M"""''0033 	 	Dy N+++OOT2..E}3444 5 5'+}}}dd;  dm444 T__d33 JsOOOOK     r   c                    d| j         v r| j         d         dk    r| j         d                                                             d          sQ| j         d                                                             d          st          d| j         d         z             d S 	 t          d           t	          j        dd| j        gd	t          j        t          j        t          j        
          }n.# t          $ r!}|j
        t          j        k    rY d }~d S  d }~ww xY w|                                \  }}|j        dk    rt          d|j        z             d S | j        rt          d           d S |                                                                }t          d|z             |                    d          dk    r|                     ||           t          d           d S )Nr   Unknownhphewlettz#hplip: no good (Non-HP printer: %s)zhplip: tryingz
hp-makeuriz-cTr   r   zhplip: no good (return code %d)zhplip: no goodzhplip: uri is %sr'   zhplip: done)r   lowerrg   rI   r   r   r4   r   r   r   r   r   r   r   r   rM   stripr   r   )rF   r   r   r   r   r   s         r   r   zPrinterFinder._probe_hplipe  s   #t'>>>#$;<	II'(?@FFIITTVZ[[ J'(?@FFIITTV_`` J =/0GHI J J JF	((( dDM'J,0(2(:)3)3);	= = =AA
  	 	 	w%,&&		 ==++<19ALHIIIF9 	()))Fmm$$''&,---88S>>RS#&&&M"""""s   AC. .
D8DDDc                 >   t           sd S t                      }d}t                      rd}t          j                            ||j                  }g }d| j        z  }t          d           	 |	                                dk    r| j
        rt          d           d S 	 |                    |                                          }n,# t          $ r}|                    |           Y d }~nd }~ww xY w|	                                dk    nw# t          $ rZ}|j        \  }}|t"          j        t"          j        t"          j        fvr!t          dt+          ||f          z             Y d }~nd }~w t-                       Y nxY w| j
        rt          d           d S |D ]r}|j        t          j        j        k    rVdt3          j        | j                  d	t3          j        |j                  }d
| j        z  }	|                     ||	           st          d           d S )Nr   
   )debugauth_fnz	smb://%s/zsmb: tryingzsmb: no goodzRuntime error: %szsmb://r   zSMB (%s)z	smb: done)PYSMB_AVAILABLErm   get_debuggingr
   smbcContextr   r4   rI   rw   r   opendirgetdentsr   failedRuntimeErrorrs   r   r   EACCESEPERMrN   r   	smbc_typePRINTER_SHAREsmburiurlquoterP   r   )
rF   	smbc_authr   ctxentriesr   r   r7   entryr   s
             r   r   zPrinterFinder._probe_smb  sQ    	F,//	 	Ej  *3*< ! > >DM)M"""	!2255999 ///F)!kk3//88;;GG  ) ) )$$a(((((((() 225599  	A 	A 	AVFQu|U[AAA/$A--?@@@	!     9 	'''F 	, 	,E%*"::::'-'F'F'F'F'-
'C'C'CE!DM1  d+++K     sC   ,.D
 'C D
 
C.C)$D
 )C..D
 

E>AE))E>c                 B   d}| j         |f}t          d           t          | j         |          }|st          d|z             nKt          d|z             d|z  }d| j         z  }|                     ||           |                                 t          d           d S )Ni#  zjetdirect: tryingzjetdirect: %s:%d CLOSEDzjetdirect %s:%d OPENzsocket://%s:%dzJetDirect (%s)zjetdirect: done)r4   rI   r?   r   r3   )rF   r5   sock_addressr7   r   r   s         r   r   zPrinterFinder._probe_jetdirect  s    t,'(((t,, 	1L@AAAA .=>>>"\1C#dm3DS$'''GGJJJ%&&&&&r   c                 
   t          d           	 t          j        | j        dt          j        t          j                  }n<# t          j        $ r* t          d| j        z             t          d           Y d S w xY w|D ]c}|\  }}}}}|t          j        k    r|d         dk    s|t          j        k    r-|d         dk    r!t          d           t          d            d S d	 t          j
        | j        	          }n/# t          $ r" t          d
           t          d           Y d S w xY w	 |                                }	nf# t          j        $ rT t          d| j        z             d| j        z  }
d| j        z  }|                     |
|           t          d           Y d S w xY w|	                                D ]4\  }}|d         }
|d         }|d         }|                     |
||           5t          d           d S )Nzipp: tryingiw  zipp: can't resolve %szipp: no goodr   z	127.0.0.1z::1z#ipp: do not probe local cups server)r6   z$ipp: can't connect to server/printerz0%s is probably not a cups server but IPP printerzipp://%s:631/ippzIPP (%s)z	ipp: donezprinter-uri-supportedzprinter-infozprinter-location)rI   r+   r,   r4   r-   r.   r/   AF_INETAF_INET6cups
Connectionr   getPrintersIPPErrorr   items)rF   r8   r9   r:   r;   r<   r=   r>   cprintersr   r   rP   queuer   s                  r   r   zPrinterFinder._probe_ipp  sY   M"""	#DM38H$*$68 8BB 	 	 	/$-?@@@'''FF	  	 	C14.B%Bfn$$A+)=)=fo%%"Q%5..ABBBN+++	666AA 	 	 	>???'''FF	
		}}''HH} 	 	 	J& ' ' '$6C-DS$'''$$$FF	 $>>,, 	2 	2KD%/0C(D/0HS$1111K     s6   0A 5A;:A;%D   (D,+D,0E AF('F(ro   )r   r   r   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s          M M M
  $ $ $(& & & &:" :" :"x! ! !2$# $# $#L)! )! )!V' ' '")! )! )! )! )!r   r   __main__   zNeed printer addressr(   c                 @    | t                                            d S d S ro   )loopr   )r   s    r   displayr     s    >IILLLLL >r   )&r   r   r   r+   rh   gi.repositoryr   r   timedopsr   r   rp   r   r   r   r   r
   r   r%   r?   rA   r   rm   r   r   sysr   argvprintexitset_debuggingMainLoopr   r   addrr   r   runr   r   r   <module>r     s  *                              # # # # # #          ! ! ! ! ! !       LLLOOO         , , ,b  <L L L L L L L L\6 6 6 6 6u0 6 6 68K! K! K! K! K! K! K! K!Z zJJJ
sCH~~$%%%!M47D   8A;DAFFD'HHKKKKK! s   A A