332172905e1085d7225df39987a4dbd9fb99b827
[nmhive.git] / nmbug.js
1 var nmbug_server = 'http://localhost:5000';
2
3 nmbug = {
4         show: function (message_id) {
5                 this._get_tags(message_id, this._edit_tags.bind(this));
6         },
7         _get_tags: function (message_id, callback) {
8                 var url = [
9                         nmbug_server,
10                         'mid',
11                         encodeURIComponent(message_id),
12                         ].join('/');
13                 console.log('nmbug: get tags from ' + url);
14                 var request = new XMLHttpRequest();
15                 request.onload = function () {
16                         if (this.status == 200) {
17                                 var tags = JSON.parse(this.response);
18                                 console.log('nmbug: got tags', tags);
19                                 callback(message_id, tags);
20                         } else {
21                                 throw 'Error fetching ' + url + ' (status ' + this.status + ')';
22                         }
23                 };
24                 request.open('get', url, true);
25                 request.send();
26         },
27         _edit_tags: function (message_id, tags) {
28                 alert('edit ' + tags + ' for ' + message_id);
29         },
30 };
31
32 var _gmane_handler = {
33         regexp: /gmane[.]org/,
34         handle: function (callback) {
35                 var article = this._get_article();
36                 this._get_message_id(article, callback);
37         },
38         _article_from_url: function (url) {
39                 var regexp = new RegExp('http://article.gmane.org/([^/]+)/([0-9]+)');
40                 var match = regexp.exec(url);
41                 console.log('nmbug: get article from ' + url, match);
42                 if (match) {
43                         return {'group': match[1], 'id': parseInt(match[2])};
44                 }
45         },
46         _get_article: function () {
47                 var article = this._article_from_url(document.URL);
48                 var i = 0;
49                 for (var i = 0; !article && i < window.frames.length; i++) {
50                         article = this._article_from_url(window.frames[i].document.URL);
51                 }
52                 if (!article) {
53                         throw "Cannot extract an article from Gmane's " + document.URL;
54                 }
55                 return article;
56         },
57         _get_message_id: function (article, callback) {
58                 var url = [
59                         nmbug_server,
60                         'gmane',
61                         article.group,
62                         article.id,
63                 ].join('/');
64                 console.log('nmbug: get Message-ID from ' + url);
65                 var request = new XMLHttpRequest();
66                 request.onload = function () {
67                         var message_id = this.responseText;
68                         callback(message_id);
69                 };
70                 request.open('get', url, true);
71                 request.send();
72         },
73 };
74
75 var handlers = [
76         _gmane_handler,
77 ];
78
79 function _check_handler(handler) {
80         var match = handler.regexp.test(document.URL);
81         console.log('nmbug: testing', handler, match);
82         if (match) {
83                 console.log('nmbug: matched', handler);
84                 handler.handle(nmbug.show.bind(nmbug));
85         }
86         return match;  /* break after the first match */
87 }
88
89 function run() {
90         var matched = handlers.some(_check_handler);
91         if (!matched) {
92                 throw 'No handler for ' + document.URL;
93         }
94 }