Commit 331e331217695409295527ce72d874eece326e9f
1 parent
7e2cb53435
Exists in
master
ready to http
Showing 1 changed file with 46 additions and 12 deletions Side-by-side Diff
index.js
... | ... | @@ -6,6 +6,25 @@ var fs = require('fs'); |
6 | 6 | var ini = require('ini'); |
7 | 7 | var config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); |
8 | 8 | |
9 | +var last_message_hash; | |
10 | + | |
11 | +var logger = new (winston.Logger)({ | |
12 | + transports: [ | |
13 | + new (winston.transports.Console)({ | |
14 | + timestamp: function() { | |
15 | + return strftime('%F %T', new Date()); | |
16 | + } | |
17 | + }), | |
18 | + new (winston.transports.DailyRotateFile)({ | |
19 | + filename: __dirname + '/log', | |
20 | + timestamp: function() { | |
21 | + return strftime('%F %T', new Date()); | |
22 | + } | |
23 | + }) | |
24 | + ] | |
25 | +}); | |
26 | + | |
27 | + | |
9 | 28 | var ym = require('yahoomessenger'); |
10 | 29 | ym.newInstance(); |
11 | 30 | |
... | ... | @@ -14,33 +33,48 @@ function onReady(){ |
14 | 33 | } |
15 | 34 | |
16 | 35 | function onLoginSuccessful(data) { |
17 | - console.log('Login successful as ' + data.firstname + ' ' + data.lastname + ' (' + data.username + ')'); | |
18 | - console.log(data); | |
36 | + logger.info('Login successful as ' + data.firstname + ' ' + data.lastname + ' (' + data.username + ')', {data: data}); | |
37 | +} | |
38 | + | |
39 | +function sendIgnoreResponse(destination, message) { | |
40 | + ym.sendPM(destination, "Pesan anda diabaikan, silahkan diulang beberapa saat lagi jika diperlukan: " + message); | |
19 | 41 | } |
20 | 42 | |
21 | 43 | function onPm(data) { |
22 | - console.log('onPM()'); | |
23 | - console.log(data); | |
44 | + logger.info('onPM()', {data: data}); | |
45 | + | |
46 | + var new_message_hash = data.sender + ': ' + data.message; | |
47 | + | |
48 | + if (last_message_hash == new_message_hash) { | |
49 | + return; | |
50 | + } | |
51 | + | |
52 | + last_message_hash = new_message_hash; | |
24 | 53 | ym.sendPM(data.sender, "Pesan anda telah diterima dan akan segera diproses: " + data.message); |
25 | 54 | } |
26 | 55 | |
27 | 56 | function onOfflinePM(data) { |
28 | - console.log('onOfflinePM()'); | |
29 | - console.log(data); | |
57 | + logger.info('onOfflinePM()', {data: data}); | |
58 | + sendIgnoreResponse(data.sender, data.message); | |
30 | 59 | } |
31 | 60 | |
32 | 61 | function onBuddyAddRequest(data) { |
33 | - console.log('onBuddyAddRequest()'); | |
34 | - console.log(data); | |
62 | + logger.info('onBuddyAddRequest()', {data: data}); | |
35 | 63 | ym.acceptAddBuddy(data.username); |
64 | + logger.info('Accept buddy add request: ' + data.username, {data: data}); | |
65 | +} | |
66 | + | |
67 | +function onHttpIncomingMessage(request, response) { | |
68 | + var qs = url.parse(request.url, true).query; | |
69 | + logger.info("onHttpIncomingMessage()", {qs: qs}); | |
36 | 70 | } |
37 | 71 | |
38 | 72 | function createHttpServer() { |
39 | - console.log('createHttpServer()'); | |
73 | + logger.verbose('createHttpServer()'); | |
40 | 74 | |
41 | - var httpServer = http.createServer(function(request,response) { | |
42 | - var qs = url.parse(request.url, true).query; | |
43 | - | |
75 | + var httpServer = http.createServer(onHttpIncomingMessage); | |
76 | + httpServer.listen(config.globals.listen_port, function(){ | |
77 | + logger.info("HTTP server listening on " + config.globals.listen_port); | |
44 | 78 | }); |
45 | 79 | } |
46 | 80 |