~carifio/junk/xchat-ldap-plugin+carif-comments

« back to all changes in this revision

Viewing changes to lquery.py

  • Committer: Sean Feole
  • Date: 2012-12-01 01:46:35 UTC
  • Revision ID: sean.feole@canonical.com-20121201014635-pgb73s2g5s64tfzb
Another Revise

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Sean Feole 2012, LDAP Query
 
3
# Rev 1.0
 
4
 
 
5
import sys
 
6
import StringIO
 
7
import Image
 
8
import ldap
 
9
import re
 
10
import pygtk
 
11
pygtk.require('2.0')
 
12
import gtk
 
13
import argparse
 
14
 
 
15
 
 
16
class GtkLdap:
 
17
 
 
18
    def delete(self, widget, event=None):
 
19
        gtk.main_quit()
 
20
        return False
 
21
 
 
22
    def configfile(self, configlocation):
 
23
 
 
24
        print ("Using Config File at: %s" % configlocation)
 
25
 
 
26
        myfile = open(configlocation, "r").readlines()
 
27
        scaninit = myfile[5]
 
28
        configcheck = scaninit.find("Insert Host Address")
 
29
 
 
30
        if configcheck is 18:
 
31
            print("Please Configure the config.txt file")
 
32
            sys.exit()
 
33
        else:
 
34
            self.init = scaninit.split("=")[1].strip().strip("\"")
 
35
 
 
36
            scanbind = myfile[6]
 
37
            self.bind = scanbind.split("\"")[1]
 
38
 
 
39
            scanpasswd = myfile[7]
 
40
            self.passwd = scanpasswd.split("=")[1].strip().strip("\"")
 
41
 
 
42
            scanbasedn = myfile[8]
 
43
            self.basedn = scanbasedn.split("\"")[1]
 
44
 
 
45
    def searchstring(self, name):
 
46
        self.name = name
 
47
 
 
48
    def displayme(self):
 
49
        print ("Searching For: %s" % self.name)
 
50
 
 
51
    def ldap(self):
 
52
 
 
53
        init = "%s" % self.init
 
54
        bind = "%s" % self.bind
 
55
        passwd = "%s" % self.passwd
 
56
        basedn = "%s" % self.basedn
 
57
        searchstring = "%s" % self.name
 
58
 
 
59
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
60
        window.connect("delete_event", self.delete)
 
61
        window.set_border_width(10)
 
62
 
 
63
        table = gtk.Table(3, 6, False)
 
64
        window.add(table)
 
65
 
 
66
        notebook = gtk.Notebook()
 
67
        notebook.set_tab_pos(gtk.POS_LEFT)
 
68
        notebook.set_scrollable(True)
 
69
        notebook.set_current_page(1)
 
70
        table.attach(notebook, 0, 6, 0, 1)
 
71
        notebook.show()
 
72
        self.show_tabs = True
 
73
        self.show_border = True
 
74
        entryFound = False
 
75
 
 
76
        try:
 
77
            l = ldap.initialize(init)
 
78
            l.protocol_version = ldap.VERSION3
 
79
            l.bind_s(bind, passwd)
 
80
        except ldap.LDAPError:
 
81
            print ("Error Binding to Ldap Server")
 
82
            sys.exit()
 
83
 
 
84
        baseDN = (basedn)
 
85
        searchScope = ldap.SCOPE_SUBTREE
 
86
        retrieveAttributes = None
 
87
        searchFilter = "cn=*%s*" % searchstring
 
88
 
 
89
        ldap_result_id = l.search(baseDN, searchScope, searchFilter,
 
90
         retrieveAttributes)
 
91
        result_set = []
 
92
        print ("Processing")
 
93
        while True:
 
94
            result_type, result_data = l.result(ldap_result_id, 0)
 
95
            if not result_data:
 
96
                print ("No Additional Entries to Parse")
 
97
                break
 
98
            else:
 
99
                if result_type == ldap.RES_SEARCH_ENTRY:
 
100
                    count = 1
 
101
                    for number in result_set:
 
102
                        count += 1
 
103
                    result_set.append(result_data)
 
104
                    for i in range(len(result_set)):
 
105
                        for entry in result_set[i]:
 
106
                            #Find Each Entry, If the Entry does
 
107
                            #not exist then default "Not Found"
 
108
 
 
109
                            if 'cn' in entry[1]:
 
110
                                name = entry[1]['cn'][0]
 
111
                            else:
 
112
                                cn = "Not Found"
 
113
                            if 'title' in entry[1]:
 
114
                                title = entry[1]['title'][0]
 
115
                            else:
 
116
                                title = "Not Found"
 
117
                            if 'mail' in entry[1]:
 
118
                                email = entry[1]['mail'][0]
 
119
                            else:
 
120
                                email = "Not Found"
 
121
                            if 'telephoneNumber' in entry[1]:
 
122
                                telephone = entry[1]['telephoneNumber'][0]
 
123
                            else:
 
124
                                telephone = "Not Found"
 
125
                            if 'mobile' in entry[1]:
 
126
                                mobile = entry[1]['mobile'][0]
 
127
                            else:
 
128
                                mobile = "Not Found"
 
129
                            if 'ou' in entry[1]:
 
130
                                org = entry[1]['ou'][0]
 
131
                            else:
 
132
                                org = "Not Found"
 
133
                            if 'launchpadID' in entry[1]:
 
134
                                launchpad = entry[1]['launchpadID'][0]
 
135
                            else:
 
136
                                launchpad = "Not Found"
 
137
                            if 'mozillaNickname' in entry[1]:
 
138
                                irc = entry[1]['mozillaNickname'][0]
 
139
                            else:
 
140
                                irc = "Not Found"
 
141
                            jpeg = entry[1]['jpegPhoto'][0]
 
142
 
 
143
                myinfo = "\nName: %s\nTitle: %s\nOrg: %s\nPhone: %s\
 
144
                    \nMobile: %s\nEmail: %s\nLaunchpad: %s\nIRC: %s"\
 
145
                    % (name, title, org, telephone, mobile, email,
 
146
                       launchpad, irc)
 
147
 
 
148
                jpegnew = Image.open(StringIO.StringIO(jpeg))
 
149
                jpegnew.save("/tmp/image.jpg", "JPEG")
 
150
 
 
151
                image = gtk.Image()
 
152
                image.set_from_file("/tmp/image.jpg")
 
153
                image.show()
 
154
 
 
155
                bufferf = "%s" % (myinfo)
 
156
                bufferl = "%s" % (name)
 
157
 
 
158
                frame = gtk.Frame()
 
159
                frame.set_border_width(10)
 
160
                frame.set_size_request(500, 300)
 
161
                frame.show()
 
162
 
 
163
                bodywin = gtk.HBox(False, 0)
 
164
                bodywin.show()
 
165
 
 
166
                frame.add(bodywin)
 
167
                label = gtk.Label(bufferf)
 
168
                bodywin.pack_end(image, False, False, 0)
 
169
                bodywin.pack_end(label, True, True, 0)
 
170
                label.show()
 
171
 
 
172
                label = gtk.Label(bufferl)
 
173
                notebook.append_page(frame, label)
 
174
                entryFound = True
 
175
 
 
176
    #Create the Bottom Buttons
 
177
        button = gtk.Button("close")
 
178
        button.connect("clicked", self.delete)
 
179
        table.attach(button, 0, 1, 1, 2)
 
180
        button.show()
 
181
 
 
182
        button = gtk.Button("next page")
 
183
        button.connect("clicked", lambda w: notebook.next_page())
 
184
        table.attach(button, 1, 2, 1, 2)
 
185
        button.show()
 
186
 
 
187
        button = gtk.Button("prev page")
 
188
        button.connect("clicked", lambda w: notebook.prev_page())
 
189
        table.attach(button, 2, 3, 1, 2)
 
190
        button.show()
 
191
 
 
192
        table.show()
 
193
        window.show()
 
194
        return entryFound
 
195
 
 
196
 
 
197
def main():
 
198
 
 
199
    parser = argparse.ArgumentParser()
 
200
    parser.add_argument('-s', '--searchstring',
 
201
                        help="Name of the Individual to search", required=True)
 
202
    #parser.add_argument('-c', '--config',
 
203
    #                    help="Location of the config file", required=True)
 
204
    args = parser.parse_args()
 
205
 
 
206
    args.config = "/etc/lquery/config.txt"
 
207
    ldap = GtkLdap()
 
208
    ldap.configfile('%s' % args.config)
 
209
    ldap.searchstring('%s' % args.searchstring)
 
210
    print(ldap.displayme())
 
211
    if ldap.ldap():
 
212
        gtk.main()
 
213
    return 0
 
214
 
 
215
if __name__ == "__main__":
 
216
    GtkLdap()
 
217
    main()