Don't use uwdir emails for expired accounts since we ask for ~/.forward adress now.
[public/pyceo-broken.git] / ceo / console / expiredaccounts.py
1 import sys, ldap
2 from ceo import members, uwldap, terms, ldapi
3
4 def max_term(term1, term2):
5     if terms.compare(term1, term2) > 0:
6         return term1
7     else:
8         return term2
9
10 class ExpiredAccounts:
11   help = '''
12 expiredaccounts [--email]
13
14 Displays a list of expired accounts. If --email is specified, expired account
15 owners will be emailed.
16 '''
17
18   def main(self, args):
19     send_email = False
20     if len(args) == 1 and args[0] == '--email':
21       sys.stderr.write("If you want to send an account expiration notice to " \
22         "these users then type 'Yes, do this' and hit enter\n")
23       if raw_input() == 'Yes, do this':
24         send_email = True
25     uwl = ldap.initialize(uwldap.uri())
26     mlist = members.expired_accounts()
27     for member in mlist.values():
28       term = "f0000"
29       term = reduce(max_term, member.get("term", []), term)
30       term = reduce(max_term, member.get("nonMemberTerm", []), term)
31       expiredfor = terms.delta(term, terms.current())
32
33       if expiredfor <= 3:
34         uid = member['uid'][0]
35         name = member['cn'][0]
36         email = None
37         print '%s (expired for %d terms)' % (uid.ljust(12), expiredfor)
38         if send_email:
39           print "  sending mail to %s" % uid
40           members.send_account_expired_email(name, uid)