#! /usr/bin/env python
#
# Copyright (C) 1998,1999,2000 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"Invoked by cron, checks for pending list requests and mails the admin if any."

import sys
import time
import string

import paths
from Mailman import MailList
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import Message
from Mailman.Handlers import HandlerAPI

# Work around known problems with some RedHat cron daemons
import signal
signal.signal(signal.SIGCHLD, signal.SIG_DFL)



def main():
    for name in Utils.list_names():
        # the list must be locked in order to open the requests database
	mlist = MailList.MailList(name)
        try:
            count = mlist.NumRequestsPending()
            if count:
                text = Utils.maketext(
                    'checkdbs.txt',
                    {'count'    : count,
                     'host_name': mlist.host_name,
                     'adminDB'  : mlist.GetScriptURL('admindb', absolute=1),
                     'real_name': mlist.real_name,
                     })
                text = text + '\n' + pending_requests(mlist)
                subject = '%d %s admin request(s) waiting' % (
                    count, mlist.real_name)
                admin = mlist.GetAdminEmail()
                msg = Message.UserNotification(admin, admin, subject, text)
                HandlerAPI.DeliverToUser(mlist, msg)
        finally:
            mlist.Save()
            mlist.Unlock()
            


def pending_requests(mlist):
    pending = []
    first = 1
    for id in mlist.GetSubscriptionIds():
        if first:
            pending.append('Pending subscriptions:')
            first = 0
        when, addr, passwd, digest = mlist.GetRecord(id)
        pending.append('    %s %s' % (addr, time.ctime(when)))
    first = 1
    for id in mlist.GetHeldMessageIds():
        if first:
            pending.append('\nPending posts:')
            first = 0
        info = mlist.GetRecord(id)
        if len(info) == 5:
            # pre-2.0beta3 compatibility
            when, sender, subject, reason, text = mlist.GetRecord(id)
        else:
            when, sender, subject, reason, text, msgdata = mlist.GetRecord(id)
        pending.append('     From: %s on %s\n    Cause: %s' %
                       (sender, time.ctime(when), reason))
        pending.append('')
    return string.join(pending, '\n')



if __name__ == '__main__':
    main()
