Changeset 374

Show
Ignore:
Timestamp:
01/19/06 20:02:16 (3 years ago)
Author:
athomas
Message:

WikiNotifyScript:

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wikinotifyscript/0.8/trac-wiki-notify

    r49 r374  
    33# Usage: 
    44#   trac-wiki-notify <trac-url> <trac-path> <wiki-notification-page> \ 
    5 #     <time-delta> <from-address> [<smtp-server>] 
     5#     <time-delta> <from-address> [<smtp-server>] [</path/to/logfile/logfiel.log>] 
    66# 
    77# This script extracts a list of subscribers from a trac page and mails them a 
     
    1414# E-Mail addresses. ie. "<space><space>*<space><email>" 
    1515 
    16 import sqlite 
     16 
     17# changes by Viktor Errath 
     18# Date: 2006-01-13 13:51 
     19# email: v.errath(at)bcom.at 
     20#  
     21# Changes: 
     22#    - Now this script (this version of the trac-wiki-notification) work with  
     23#      plone 2.4 and pysqlite2 
     24
     25#    - Added one optional parameter 'LOGFILE' 
     26
     27#    - Re-Ordered the creation of the email-Body: Now the email has filled in 
     28#      a correct Reciever-Adress (instead of an empty field) 
     29 
     30 
     31 
     32from pysqlite2 import dbapi2 as sqlite 
    1733import sys 
    1834import smtplib 
     
    2137 
    2238if len(sys.argv) < 5: 
    23        raise StandardError("Not enough arguments") 
     39    raise StandardError("Not enough arguments") 
    2440 
    2541trac_url, trac_location, notify_page, time_delta, smtp_from = sys.argv[1:6] 
    2642smtp_server = "localhost" 
    2743if len(sys.argv) > 6: smtp_server = sys.argv[6] 
     44if len(sys.argv) > 7: logfile = sys.argv[7] 
    2845 
    29 db = sqlite.connect(trac_location + "/db/trac.db", mode=0555
     46db = sqlite.connect(trac_location + "/db/trac.db"
    3047q = db.cursor() 
    3148 
    3249# Get list of users 
    33 q.execute("select text from wiki where name=%s order by time desc limit 1", notify_page) 
     50q.execute("select text from wiki where name='%s' order by time desc limit 1" % notify_page) 
    3451row = q.fetchone() 
    3552 
     53smtp_body='' 
     54 
    3655if row: 
    37        smtp_to = [x[4:].strip() for x in row[0].split("\n") if x[0:4] == '  * '] 
     56    smtp_to = [x[4:].strip() for x in row[0].split("\n") if x[0:4] == '  * '] 
    3857 
    39         # Get list of pages 
    40         q.execute("select name, time, author, comment, version from wiki where time > strftime('%%s','now') - %i order by time desc", int(time_delta)) 
    41         changes = q.fetchall() 
     58    # Get list of pages 
     59    act_time = int(strftime('%s', localtime())) - int(time_delta) 
     60    q.execute("select name, time, author, comment, version from wiki where time > %s order by time desc" % str(act_time)) 
     61    changes = q.fetchall() 
    4262 
    43         if changes: 
    44                 smtp_body = "From: %s\n" % smtp_from 
    45                 smtp_body += "Reply-To: %s\n" % smtp_from 
    46                 smtp_body += "Subject: Notification of %i changes to Trac Wiki %s\n\n" % (len(changes), trac_url) 
    47                 smtp_body += "Changes since %s\n\n" % strftime("%Y/%d/%m %H:%M:%S", localtime(time() - float(time_delta))) 
     63    if changes: 
    4864 
    49                 for change in changes: 
    50                         change_time = strftime("%Y/%d/%m %H:%M:%S", localtime(int(change[1]))) 
    51                         smtp_body += "  %s (version %s) modified %s by %s" % (change[0], change[4], change_time, change[2]) 
    52                         smtp_body += "\n    %swiki/%s?version=%s\n" % (trac_url, change[0], change[4]) 
    53                         if change[3]: smtp_body += "    \"%s\"\n" % change[3] 
    54                         smtp_body += "\n" 
     65        # Create the email-body 
     66        email_body = "Changes since %s\n\n" % strftime("%Y/%d/%m %H:%M:%S", localtime(time() - float(time_delta))) 
    5567 
    56                 smtp_body += "You can remove yourself from notifications at this URL:\n  %swiki/%s\n" % (trac_url, notify_page) 
    57                 # Send the mail 
    58                 smtp = smtplib.SMTP(smtp_server) 
    59                 smtp.sendmail(smtp_from, smtp_to, smtp_body) 
    60                 smtp.quit() 
     68        for change in changes: 
     69            change_time = strftime("%Y/%d/%m %H:%M:%S", localtime(int(change[1]))) 
     70            email_body += "  %s (version %s) modified %s by %s" % (change[0], change[4], change_time, change[2]) 
     71            email_body += "\n    %swiki/%s?version=%s\n" % (trac_url, change[0], change[4]) 
     72            if change[3]: email_body += "    \"%s\"\n" % change[3] 
     73            email_body += "\n" 
     74 
     75        email_body += "\nYou can remove yourself from notifications at this URL:\n  %swiki/%s\n" % (trac_url, notify_page) 
     76 
     77        smtp = smtplib.SMTP(smtp_server) 
     78        smtp = smtplib.SMTP(smtp_server) 
     79        # Finish the email 
     80        for email in smtp_to: 
     81            smtp_body = "From: %s\n" % smtp_from 
     82            smtp_body += "To: %s\n" % email 
     83            smtp_body += "Reply-To: %s\n" % smtp_from 
     84            smtp_body += "Subject: Notification of %i changes to Trac Wiki %s\n\n" % (len(changes), trac_url) 
     85            smtp_body += email_body 
     86            # Send the mail 
     87            smtp.sendmail(smtp_from, email, smtp_body) 
     88             
     89        smtp.quit() 
     90 
     91        # Write logfile (or not) 
     92        if len(sys.argv) > 7: 
     93            fileobj = open(sys.argv[7], 'au') 
     94            fileobj.write('\n#######################################################\n') 
     95            fileobj.write('%s\n' % str(strftime('%Y-%m-%d %H:%M:%S', localtime()))) 
     96            fileobj.write('Trac-Wiki-Notification-Email sent to: %s' % smtp_to) 
     97            fileobj.write('\n\n %s \n\n' % smtp_body) 
     98            fileobj.close()