This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
/
bzmail.js
217 lines (169 loc) · 6.19 KB
/
bzmail.js
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
var bug_info_cache = {};
var bz = "https://bugzilla.mozilla.org/";
function replacer(pattern, replacement) {
return function(html, obj) {
pattern = new RegExp(pattern.source, "gm");
return html.replace(pattern, replacement);
}
};
function bug_info(num) {
$.ajax({
url: 'https://api-dev.bugzilla.mozilla.org/latest/bug/' + num + '?include_fields=summary,status',
accepts: "application/json",
cache: true,
contents: "application/json",
crossDomain: true,
error: function() { console.log("api error"); },
success: function() { console.log("success"); },
});
};
function bug(num, str) {
if (!str) { str = num; }
return '<a href="' + bz + 'show_bug.cgi?id=' + num + '" target="_blank">' + str + '</a>';
};
function attachment(num, str) {
if (!str) { str = num; }
return '<a href="' + bz + 'attachment.cgi?id=' + num + '" target="_blank">' + str + '</a>';
};
var rblocks = replacer(/Blocks: (\d+)/,
'Blocks: ' + bug('$1'));
var rbug = replacer(/([bB]ug:?\s*)(\d+)/,
bug('$2', '$1$2'));
// Rewrite bugs in the final column of a table
var rtablebug1 = replacer(/\|(\d+)[^.-]\n/,
'|' + bug('$1') + '\n');
var rtablebug2 = replacer(/\|(\d+)[^.-], (\d+)[^.-]\n/,
'|' + bug('$1') + ', ' + bug('$2') + '\n');
var rtablebug3 = replacer(/\|(\d+)[^.-], (\d+)[^.-], (\d+)[^.-]([,\n])/,
'|' + bug('$1') + ', ' + bug('$2') + ', ' + bug('$3') + '$4');
// Rewrite bugs in the middle column of a table
var rtablebug4 = replacer(/\|(\d+)[^.-](\s+|)/,
'|' + bug('$1') + '$2');
var rtablebug5 = replacer(/\|(\d+)[^.-], (\d+)[^.-](\s+|)/,
'|' + bug('$1') + ', ' + bug('$2') + '$3');
var rtablebug6 = replacer(/\|(\d+)[^.-], (\d+)[^.-], (\d+)[^.-]([,\s]|)/,
'|' + bug('$1') + ', ' + bug('$2') + ', ' + bug('$3') + '$4');
var rtableattachment = replacer(/Attachment #(\d+)/, "Attachment #" + attachment('$1'));
function monospacer(html, obj) {
// Parse a bugzilla block and wrap pre tags around it. eg:
// What |Removed |Added
// ----------------------------------------------------------------------------
// Summary |Add trychooser syntax for |Add trychooser syntax for
// |SM builds |Spidermonkey builds
// The HTML gmail generates around this can be quite changable, so be
// robust. We match on HEADER+BARS+ROWS*. We stop when there is a newline
// with no ' 's (not too hard since '.' doesn't match newlines in JS).
var header1 = /( ){5} What( ){2}\|Old Value( ){9} \|New Value/;
var header2 = /( ){5} What( ){2}\|Removed( ){10} \|Added/;
var bars = /(-*<wbr>){2}-*/;
var row = /.*nbsp.*/;
// A simple line parser. When we see a header we open a PRE tag, and we
// close it again once we no longer match a row.
result = "";
var started = false;
var readd_div = false;
lines = html.split(/<br>\n/);
for each (var line in lines) {
if (line.match(header1) || line.match(header2)) {
// heading
started = true;
result += '<pre>' + line + '\n';
} else if (started) {
if (line.match(bars) || line.match(row)) {
// rows
// Special case: sometimes gmail closes a <div> in the middle of the
// table, which closes the pre. I don't like special-casing this, but
// the nicer way (wrapping each line in a <pre>) doesn't really fix
// the problem without a hack of it's own, and also leads to bad spacing.
if (line.match(/<\/div>/)) {
readd_div = true;
line = line.replace(/<\/div>/, '');
}
result += line + '\n';
// spacial case
} else {
// Finished
if (readd_div) {
result += '</pre></div>\n' + line;
} else {
result += '</pre>\n' + line;
}
started = false;
}
} else {
// Unrelated
result += line + '<br>\n';
}
}
return result;
}
function linkComments(html, data) {
if (!data)
return html;
var linkedComment = '<a href="' + bz + 'show_bug.cgi?id=' + data.bug_num + '#c'
+ data.comment_num + '" target="_blank">Comment #' + data.comment_num + '</a>';
var result = html.replace(/--- Comment #(\d+)/, '--- ' + linkedComment);
return result;
}
function comment_parser(html) {
// var comment_num = '--- Comment #(\\d+)';
// var author = ' from (.+?)';
// var username = '[(\\[](.+?)[)\\]]|()';
// var email = ' <.+?href="(.+?)".+?> ';
// var date = '(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\w+) ---';
// var anything = '[\\s\\S]*?';
var data = {};
data.bug_num = /show_bug.cgi\?id=(\d+)/.exec(html)[1];
var commentMatch = /--- Comment #(\d+)/.exec(html);
if (commentMatch)
data.comment_num = commentMatch[1];
return data;
}
var tweaker = {
parse: function(html) {
for each (var p in this.parsers) {
var obj = p(html);
if (obj) {
return obj;
}
}
return null;
},
replace: function(html, data) {
for each (var r in this.replacers) {
html = r(html, data);
}
return html;
},
replacers: [rblocks, rbug, monospacer, rtablebug1, rtablebug2, rtablebug3, rtablebug4,
rtablebug5, rtablebug6, linkComments, rtableattachment],
parsers: [comment_parser]
};
function tweak (msg) {
if (msg.getFromAddress().indexOf('bugzilla-daemon') === -1)
return;
msg = msg.getContentElement();
var html = msg.innerHTML;
bz = /(http.+?)show_bug\.cgi/.exec(html)[1];
var data = tweaker.parse(html);
var new_ = tweaker.replace(html, data);
if (html != new_) {
msg.innerHTML = new_;
}
if (tweaker.updateHtml)
tweaker.updateHtml(msg, data);
}
function loadGmonkey() {
if (unsafeWindow.gmonkey) {
unsafeWindow.gmonkey.load("2.0", function(gmail) {
var msg = new gmail.GmailMessage();
gmail.registerMessageViewChangeCallback(function (message) {
if (!message.isCollapsed() && !message.bugmail_tweaked) {
tweak(message);
message.bugmail_tweaked = true;
}
});
});
}
}
self.on('message', loadGmonkey);