3 # Copyright (C) 2011-2012 W. Trevor King
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """LDAP jpegPhoto viewer using your mailcap-configured JPEG viewer.
21 import ConfigParser as _configparser
22 import itertools as _itertools
23 import mailcap as _mailcap
24 import mimetypes as _mimetypes
26 import os.path as _os_path
27 import shlex as _shlex
28 if not hasattr(_shlex, 'quote'): # Python < 3.3
29 import pipes as _pipes
30 _shlex.quote = _pipes.quote
31 import subprocess as _subprocess
32 import tempfile as _tempfile
35 import ldap.sasl as _ldap_sasl
37 CONFIG = _configparser.SafeConfigParser() # from mutt-ldap.py
38 CONFIG.add_section('connection')
39 CONFIG.set('connection', 'server', 'domaincontroller.yourdomain.com')
40 CONFIG.set('connection', 'port', '389') # set to 636 for default over SSL
41 CONFIG.set('connection', 'ssl', 'no')
42 CONFIG.set('connection', 'starttls', 'no')
43 CONFIG.set('connection', 'basedn', 'ou=x co.,dc=example,dc=net')
44 CONFIG.add_section('auth')
45 CONFIG.set('auth', 'user', '')
46 CONFIG.set('auth', 'password', '')
47 CONFIG.set('auth', 'gssapi', 'no')
48 CONFIG.read(_os_path.expanduser('~/.mutt-ldap.rc'))
50 _CAPS = _mailcap.getcaps() # from mailcap-test.py
53 """Duplicated from mutt-ldap.py"""
55 if CONFIG.getboolean('connection', 'ssl'):
57 url = '{}://{}:{}'.format(
59 CONFIG.get('connection', 'server'),
60 CONFIG.get('connection', 'port'))
61 connection = _ldap.initialize(url)
62 if CONFIG.getboolean('connection', 'starttls') and protocol == 'ldap':
63 connection.start_tls_s()
64 if CONFIG.getboolean('auth', 'gssapi'):
65 sasl = _ldap_sasl.gssapi()
66 connection.sasl_interactive_bind_s('', sasl)
69 CONFIG.get('auth', 'user'),
70 CONFIG.get('auth', 'password'),
74 def search(query, connection=None):
75 """Duplicated from mutt-ldap.py"""
76 local_connection = False
79 local_connection = True
80 connection = connect()
84 filterstr = u'(|{})'.format(
85 u' '.join([u'({}=*{}{})'.format(field, query, post)
86 for field in ['cn', 'displayName', 'uid', 'mail']]))
87 r = connection.search_s(
88 CONFIG.get('connection', 'basedn'),
90 filterstr.encode('utf-8'))
92 if local_connection and connection:
96 def view(filename, mime=None):
97 """Duplicated from mailcap-test.py"""
99 mime,encoding = _mimetypes.guess_type(filename)
102 print('guessed {} for {}'.format(mime, filename))
103 match = _mailcap.findmatch(_CAPS, mime, filename=_shlex.quote(filename))
106 print('view {} with {}'.format(filename, match[0]))
107 return _subprocess.call(match[0], shell=True)
109 def view_entry_photo(entry):
111 if 'jpegPhoto' in data:
112 for jpeg in data['jpegPhoto']:
113 name = data.get('displayName', data['cn'])[-1]
114 fd,filename = _tempfile.mkstemp(
115 prefix= name + '-', suffix='.jpeg')
118 view(filename=filename, mime='image/jpeg')
124 if __name__ == '__main__':
127 query = unicode(' '.join(sys.argv[1:]), 'utf-8')
128 entries = search(query)
129 for entry in sorted(entries):
130 view_entry_photo(entry)