-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
158 lines (137 loc) · 4.25 KB
/
index.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
var path = require('path');
var sqlite3 = require('sqlite3').verbose();
var Q = require('q');
var HOME = getUserHome();
var identities = [];
function getUserHome() {
var envVar = (process.platform == 'win32') ? 'USERPROFILE' : 'HOME';
return process.env[envVar];
}
module.exports = iMessage;
function iMessage(opts) {
opts = opts || {};
this.path = opts.path || iMessage.DB_PATH;
this.db = this.connect();
}
iMessage.OSX_EPOCH = 978307200;
iMessage.DB_PATH = path.join(HOME, '/Library/Messages/chat.db');
iMessage.prototype.connect = function() {
var deferred = Q.defer();
var db = new sqlite3.Database(
this.path,
sqlite3.OPEN_READONLY,
function(err, res) {
if (err) return deferred.reject(err);
return deferred.resolve(db);
});
return deferred.promise;
};
iMessage.prototype.getDb = function(cb) {
var args = arguments;
// nodeify?
this.db
.then(function(db) {
cb(null, db);
}, function(err) {
cb(err);
});
};
iMessage.prototype.getRecipients = function(string, cb) {
if (typeof string == 'function') {
cb = string;
string = false;
}
this.db.done(function(db) {
var where = "";
// Maybe dangerous, check SQLlite doc
if (string && string != "") where = " WHERE id LIKE '%"+string+"%'";
db.all("SELECT * FROM `handle`" + where, cb);
});
};
iMessage.prototype.getRecipientById = function(id, details, cb) {
if (typeof details == 'function') {
cb = details;
details = false;
}
this.db.done(function(db) {
db.get("SELECT * FROM `handle` WHERE ROWID = $id", { $id: id }, function(err, recipient) {
if (!details) return cb(err, recipient);
if (err) return cb(err);
db.all("SELECT * FROM `message` WHERE handle_id = $id", {$id: id}, function(err, messages) {
if (err) return cb(err);
recipient.messages = messages;
cb(err, recipient);
});
});
});
};
iMessage.prototype.getMessages = function(string, details, cb) {
if (typeof string == 'function') {
cb = string;
string = false;
}
if (typeof details == 'function') {
cb = details;
details = false;
}
this.db.done(function(db) {
var where = "";
var join = "";
// Maybe dangerous, check SQLlite doc
if (string && string != "") where = " WHERE `message`.text LIKE '%"+string+"%'";
if (details) join = " JOIN `handle` ON `handle`.ROWID = `message`.handle_id";
db.all("SELECT * FROM `message`" + join + where, cb);
});
};
iMessage.prototype.getMessagesFromId = function(id, string, cb) {
if (typeof string == 'function') {
cb = string;
string = false;
}
this.db.done(function(db) {
var where = "";
// Maybe dangerous, check SQLlite doc
if (string && string != "") where = " AND text LIKE '%"+string+"%'";
db.all("SELECT * FROM `message` WHERE handle_id = $id"+where, {$id: id}, function(err, messages) {
cb(err, messages);
});
});
};
iMessage.prototype.getAttachmentsFromId = function(id, cb) {
this.db.done(function(db) {
db.all("SELECT * FROM `message` \
INNER JOIN `message_attachment_join` \
ON `message`.ROWID = `message_attachment_join`.message_id \
INNER JOIN `attachment` \
ON `attachment`.ROWID = `message_attachment_join`.attachment_id \
WHERE `message`.handle_id = $id", {$id: id}, function(err, messages) {
cb(err, messages);
});
});
};
iMessage.prototype.getAttachmentById = function(id, cb) {
this.db.done(function(db) {
db.get("SELECT * FROM `message` \
INNER JOIN `message_attachment_join` \
ON `message`.ROWID = `message_attachment_join`.message_id \
INNER JOIN `attachment` \
ON `attachment`.ROWID = `message_attachment_join`.attachment_id \
WHERE `message_attachment_join`.attachment_id = $id", {$id: id}, function(err, messages) {
cb(err, messages);
});
});
};
iMessage.prototype.getAttachments = function(cb) {
this.db.done(function(db) {
db.all("SELECT * FROM `message_attachment_join` \
INNER JOIN `message` \
ON `message`.ROWID = `message_attachment_join`.message_id \
INNER JOIN `attachment` \
ON `attachment`.ROWID = `message_attachment_join`.attachment_id", cb);
});
};
iMessage.prototype.disconnect = function() {
this.db.done(function(db) {
db.close();
});
};