Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

imessagemodule

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

imessagemodule - npm Package Compare versions

Comparing version 0.2.3 to 0.3.0

applescript/send.scpt

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 @@

6

package.json
{
"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"
}
}

@@ -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");
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc