imessagemodule
Advanced tools
Comparing version 0.2.3 to 0.3.0
166
index.js
@@ -1,18 +0,108 @@ | ||
var Queue = require('./queue.js').Queue; | ||
const Queue = require('queue-fifo') | ||
const shell = require('shelljs') | ||
const fs = require('fs') | ||
const SQLiteMessagesDB = `${process.env.HOME}/Library/Messages/chat.db` | ||
const sqlite3 = require('sqlite3').verbose() | ||
// sending queue, since we can only send one message at a time | ||
var sendingQueue = []; | ||
if (!fs.existsSync(SQLiteMessagesDB)) { | ||
return | ||
} | ||
const db = new sqlite3.Database(SQLiteMessagesDB) | ||
let didCancelSend = false | ||
// isSending is set true when a message is actively being sent by applescript | ||
var isSending = false; | ||
var isSending = false | ||
// instantiate a queue to store messages in | ||
var messageQueue = new Queue(); | ||
var messageQueue = new Queue() | ||
const _automatorSendMessageToChat = (chatTitle, chatMessage, callback) => { | ||
const debugging = true | ||
return shell.exec(`automator -i "${chatTitle}\n${chatMessage.replace(/(?:\\[rn]|[\r\n]+)+/g, '\r')}" ${__dirname}/automations/Messages-sendMessage.workflow`, { | ||
const log = (message) => { | ||
if (!debugging) { | ||
return | ||
} | ||
console.log(message) | ||
} | ||
const getChatGUIDFromSQLiteDB = async (chatTitle) => { | ||
if (!chatTitle || String(chatTitle).length === 0) { | ||
log(`no chat title given, returning false`) | ||
return false | ||
} | ||
const sql = `SELECT guid FROM chat WHERE chat.display_name = '${chatTitle}' OR chat.chat_identifier = '${chatTitle}'`; | ||
const result = await new Promise((resolve) => { | ||
db.serialize(function() { | ||
db.all(sql, function(err, rows) { | ||
log(`got results from SQL query:`) | ||
log(err) | ||
log(rows) | ||
if (!rows || !rows.length) { | ||
return resolve(false) | ||
} | ||
return resolve(rows[rows.length - 1].guid) | ||
}) | ||
}) | ||
}) | ||
return result | ||
} | ||
const _osascriptSendMessageToChat = async (chatTitle, chatMessage, callback) => { | ||
const finishedWithSend = () => { | ||
isSending = false | ||
callback() | ||
if (didCancelSend) { | ||
log(`due to previously cancelled send, sending more messages from queue`) | ||
didCancelSend = false | ||
sendMessagesFromQueue() | ||
} | ||
} | ||
// need to retrieve chatGUID from chatTitle | ||
let chatGUID = await getChatGUIDFromSQLiteDB(chatTitle) | ||
if (!chatGUID) { | ||
log(`unable to look up chatGUID for ${chatTitle}, using automator to start a new chat`) | ||
const automatorCommand = `automator -i "${chatTitle}\n${chatMessage.replace(/(?:\\[rn]|[\r\n]+)+/g, '\r')}" ${__dirname}/automations/Messages-sendMessage.workflow` | ||
log(`exec automator command:\n${automatorCommand}`) | ||
return shell.exec(automatorCommand, { | ||
silent: true | ||
}, finishedWithSend) | ||
} | ||
const appleScriptCommand = `osascript ${__dirname}/applescript/send.scpt "${chatMessage}" "${chatGUID}"` | ||
log(`exec osascript command:\n${appleScriptCommand}`) | ||
return shell.exec(appleScriptCommand, { | ||
silent: true | ||
}, callback) | ||
}, finishedWithSend) | ||
} | ||
@@ -22,10 +112,10 @@ | ||
var Message = function(chatTitle, chatMessage, callback) { | ||
this.chatTitle = chatTitle; | ||
this.chatMessage = chatMessage; | ||
this.messageCallback = callback; | ||
this.chatTitle = chatTitle | ||
this.chatMessage = chatMessage | ||
this.messageCallback = callback | ||
if (!callback) { | ||
this.messageCallback = function() {}; | ||
this.messageCallback = function() {} | ||
} | ||
}; | ||
} | ||
@@ -36,22 +126,38 @@ /** | ||
*/ | ||
function sendMessagesFromQueue() { | ||
async function sendMessagesFromQueue () { | ||
if (!messageQueue.hasNext() || isSending) { | ||
log(`sendMessagesFromQueue`) | ||
return; | ||
if (isSending) { | ||
log(`already sending another message, cancelling this send`) | ||
didCancelSend = true | ||
return | ||
} | ||
isSending = true; | ||
if (messageQueue.isEmpty()) { | ||
log(`no messages to send`) | ||
return | ||
} | ||
log(`sending message!`) | ||
isSending = true | ||
// select the first messages that we need to send | ||
var message = messageQueue.dequeue(); | ||
var message = messageQueue.dequeue() | ||
// remove the first message from the queue | ||
sendingQueue = sendingQueue.slice(1, sendingQueue.length); | ||
log(message) | ||
var chatTitle = message.chatTitle; | ||
var chatMessage = message.chatMessage; | ||
var messageCallback = message.messageCallback; | ||
var chatTitle = message.chatTitle | ||
var chatMessage = message.chatMessage | ||
var messageCallback = message.messageCallback | ||
_automatorSendMessageToChat(chatTitle, chatMessage, messageCallback); | ||
log(`hand off to osascript`) | ||
await _osascriptSendMessageToChat(chatTitle, chatMessage, messageCallback) | ||
} | ||
@@ -67,7 +173,13 @@ | ||
exports.sendMessage = function(chatTitle, chatMessage, callback) { | ||
log(`enqueue new message ${chatTitle} ${chatMessage}`) | ||
// add new message to the messagequeue | ||
messageQueue.enqueue(new Message(chatTitle, chatMessage, callback)); | ||
messageQueue.enqueue(new Message(chatTitle, chatMessage, callback)) | ||
log(`now attempt to send`) | ||
// attempt to send messages | ||
sendMessagesFromQueue(); | ||
}; | ||
sendMessagesFromQueue() | ||
} | ||
@@ -74,0 +186,0 @@ |
{ | ||
"name": "imessagemodule", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "A fast and reliable module for easily sending iMessages from your Mac", | ||
@@ -26,4 +26,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"shelljs": "^0.8.3" | ||
"queue-fifo": "^0.2.6", | ||
"shelljs": "^0.8.3", | ||
"sqlite3": "^4.1.1" | ||
} | ||
} |
12
test.js
@@ -5,7 +5,11 @@ | ||
imessage.sendMessage("", "test 1", function() { | ||
console.log("should be fine."); | ||
console.log("should be fine 1"); | ||
}); | ||
imessage.sendMessage("paul come back 🤷♀️", "imessage module 2 unit test - can send message with emojis🤷♀️\nand new lines", function() { | ||
console.log("should be fine."); | ||
}); | ||
imessage.sendMessage("to group chat", "imessage module unit test - can send message with emojis🤷♀️\nand new lines", function() { | ||
console.log("should be fine 2"); | ||
}); | ||
imessage.sendMessage("direct to an email", "test 3", function() { | ||
console.log("should be fine 3"); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407777
128
3
2
1
+ Addedqueue-fifo@^0.2.6
+ Addedsqlite3@^4.1.1
+ Addedabbrev@1.1.1(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedaproba@1.2.0(transitive)
+ Addedare-we-there-yet@1.1.7(transitive)
+ Addedchownr@1.1.4(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedconsole-control-strings@1.1.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddbly-linked-list@0.3.4(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addeddetect-libc@1.0.3(transitive)
+ Addedfs-minipass@1.2.7(transitive)
+ Addedgauge@2.7.4(transitive)
+ Addedhas-unicode@2.0.1(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedignore-walk@3.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedlodash.isequal@4.5.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedminipass@2.9.0(transitive)
+ Addedminizlib@1.3.3(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedms@2.1.3(transitive)
+ Addednan@2.22.0(transitive)
+ Addedneedle@2.9.1(transitive)
+ Addednode-pre-gyp@0.11.0(transitive)
+ Addednopt@4.0.3(transitive)
+ Addednpm-bundled@1.1.2(transitive)
+ Addednpm-normalize-package-bin@1.0.1(transitive)
+ Addednpm-packlist@1.4.8(transitive)
+ Addednpmlog@4.1.2(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedosenv@0.1.5(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedqueue-fifo@0.2.6(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsax@1.4.1(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsqlite3@4.2.0(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedtar@4.4.19(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwide-align@1.1.5(transitive)
+ Addedyallist@3.1.1(transitive)