summaryrefslogtreecommitdiffstats
path: root/processmail
blob: 36935185e7a47a919cd9997d9e41aff70a2d0829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#! /usr/bonsaitools/bin/mysqltcl
# -*- Mode: tcl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is the Bugzilla Bug Tracking System.
# 
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998
# Netscape Communications Corporation. All Rights Reserved.
# 
# Contributor(s): Terry Weissman <terry@mozilla.org>


# To recreate the shadow database, nuke all the entires and then run
# processmail regenerate <last>, where <last> is the biggest bug number
# currently used.


source "globals.tcl"

umask 0

proc Different {file1 file2} {
    if {[file size $file1] != [file size $file2]} {
        return 1
    }
    set f1 [open $file1 "r"]
    set f2 [open $file2 "r"]
    set d1 [read $f1]
    set d2 [read $f2]
    close $f1
    close $f2
    return [expr ![cequal $d1 $d2]]
}


proc DescCC {cclist} {
    if {[lempty $cclist]} return ""
    return "Cc: [join $cclist ", "]\n"
}


proc GetBugText {id} {
    global bug
    catch {unset bug}
set query "
select
        bug_id,
        product,
        version,
        rep_platform,
        op_sys,
        bug_status,
        resolution,
        priority,
        bug_severity,
        area,
        assigned_to,
        reporter,
        bug_file_loc,
        short_desc,
        component
from bugs
where bug_id = $id";

    SendSQL $query

    set ret [FetchSQLData]
    
    if {$ret == ""} {
        return ""
    }
    set count 0
    foreach field { bug_id product version rep_platform op_sys bug_status
                  resolution priority bug_severity area assigned_to
                  reporter bug_file_loc short_desc
                  component } {
      set bug($field) [lindex $ret $count]
      incr count
    }

    set bug(assigned_to) [DBID_to_name $bug(assigned_to)]
    set bug(reporter) [DBID_to_name $bug(reporter)]

    set bug(long_desc) [GetLongDescription $id]

    set bug(cclist) [split [ShowCcList $id] ","]


    return "Bug\#: $id
Product: $bug(product)
Version: $bug(version)
Platform: $bug(rep_platform)
OS/Version: $bug(op_sys)
Status: $bug(bug_status)   
Resolution: $bug(resolution)
Severity: $bug(bug_severity)
Priority: $bug(priority)
Component: $bug(component)
Area: $bug(area)
AssignedTo: $bug(assigned_to)                            
ReportedBy: $bug(reporter)               
URL: $bug(bug_file_loc)
[DescCC $bug(cclist)]Summary: $bug(short_desc)

$bug(long_desc)"

}



proc fixaddresses {list} {
    global nomail
    set result {}
    foreach i [lrmdups $list] {
        if {![info exists nomail($i)]} {
            lappend result $i
        }
    }
    return [join $result ", "]
}


proc Log {str} {
    set lockfid [open "maillock" "w"]
    flock -write $lockfid
    set fid [open "maillog" "a"]
    puts $fid "[fmtclock [getclock] "%D %H:%M"] $str"
    close $fid
    close $lockfid
}
    

set COOKIE(Bugzilla_login) terry
set COOKIE(Bugzilla_password) terry

ConnectToDatabase



set template "From: bugzilla-daemon
To: %s
Cc: %s
Subject: \[Bug %s\] %s - %s

http://cvs-mirror.mozilla.org/webtools/bugzilla/show_bug.cgi?id=%s

%s"


set lockfid [open "maillock" "r"]
flock -read $lockfid

# foreach i [split [read_file -nonewline "okmail"] "\n"] {
#     set okmail($i) 1
# }

foreach i [split [read_file -nonewline "nomail"] "\n"] {
    if {[info exists okmail($i)]} {
        unset okmail($i)
    }
    set nomail($i) 1
}


close $lockfid


set regenerate 0
if {[cequal [lindex $argv 0] "regenerate"]} {
    set regenerate 1
    set last [lindex $argv 1]
    set argv ""
    loop i 1 [expr $last + 1] {
        lappend argv $i
    }
}

foreach i $argv {
    if {[lempty $i]} continue
    set old shadow/$i
    set new shadow/$i.tmp.[id process]
    set diffs shadow/$i.diffs.[id process]
    set verb "Changed"
    if {![file exists $old]} {
        close [open $old "w"]
        set verb "New"
    }
    set text [GetBugText $i]
    if {$text == ""} {
        if {$regenerate} {
            continue
        }
        error "Couldn't find bug $i."
    }
    set fid [open $new "w"]
    puts $fid $text
    close $fid
    if {[Different $old $new]} {
        catch {exec diff -c $old $new > $diffs}
        set tolist [fixaddresses [list $bug(assigned_to) $bug(reporter)]]
        set cclist [fixaddresses $bug(cclist)]
        set logstr "Bug $i changed"
        if {![lempty $tolist] || ![lempty $cclist]} {
            set msg [format $template $tolist $cclist $i $verb \
                         $bug(short_desc) $i [read_file $diffs]]
            if {!$regenerate || ![cequal $verb "New"]} {
                exec /usr/lib/sendmail -t << $msg
                set logstr "$logstr; mail sent to $tolist $cclist"
            }
        }
        unlink $diffs
        Log $logstr
    }
    frename $new $old
    catch {chmod 0666 $old}
    if {$regenerate} {
        puts -nonewline "$i "
    }
}

exit