Skip to content
85 changes: 53 additions & 32 deletions lib/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const events = require('events');
const path = require('path');
const notifier = require('node-notifier');
const readline = require('readline');
const { existsSync } = require('fs');

let messenger;

Expand Down Expand Up @@ -42,9 +43,9 @@ const rlInterface = readline.createInterface({

const colorList = [
safeColors.green,
safeColors.cyan,
safeColors.red,
safeColors.blue,
safeColors.cyan,
safeColors.magenta,
safeColors.yellow
];
Expand All @@ -59,27 +60,26 @@ function getDisplayName(author) {

if(!author) author = { name: 'unknown' };

if(currentConversation && currentConversation.isGroup && Settings.properties.groupColors) {
colorPosition = author.name.length % colorList.length;
if(currentConversation && Settings.properties.groupColors) {
if (currentConversation.isGroup)
colorPosition = author.name.length % colorList.length;
else if (author.id === messenger.userId)
colorPosition = 1;
}
let displayName;

let displayName;
if (!Settings.properties.useCustomNicknames) {
displayName = author.name;
} else displayName = author.custom_nickname || author.name;

if (author.id === messenger.userId)
return displayName;
else {
return colorList[colorPosition](displayName);
}
return colorList[colorPosition](displayName);
}

function renderMessage(author, message) {
let msg = `${getDisplayName(author)}: `;

if (Settings.properties.showTimestamps) {

const timeDifference = Date.now() - message.timestamp;
const daysAgo = Math.round(timeDifference / msInADay);

Expand Down Expand Up @@ -152,11 +152,11 @@ function renderMessage(author, message) {
const a = message.attachment;
let uri;
if (a.preview && a.preview.uri) {
uri = a.preview.uri;
uri = a.preview.uri;
} else if (a.preview_image && a.preview_image.uri) {
uri = a.preview_image.uri;
uri = a.preview_image.uri;
}

if (uri) {
atts[attsNo] = uri;
const x = `${attsNo}`;
Expand Down Expand Up @@ -420,17 +420,18 @@ InteractiveCli.prototype.handleCommands = function(command) {
case '/h':
case '/?':
case '/help':
console.log('/b /back /menu .... Get back to conversation selection'.cyan);
console.log('/q /exit /quit .... Quit the application'.cyan);
console.log('/logout ........... Exit and flush credentials'.cyan);
console.log('/s /switch [#] .... Quick switch to conversation number #'.cyan);
console.log('/search [query] ... Search your friends to chat'.cyan);
console.log('/v /view [#] ...... View the attachment by the number given after the type'.cyan);
console.log('/r /refresh ....... Refresh the current converation'.cyan);
console.log('/timestamp ........ Toggle timestamp for messages'.cyan);
console.log('/linelimit [#] .... Set max number of messages to display in a conversation'.cyan);
console.log('/nolimit .......... Unset a line limit, display all available messages'.cyan);
console.log('/help ............. Print this message'.cyan);
console.log('/b /back /menu ...... Get back to conversation selection'.cyan);
console.log('/q /exit /quit ...... Quit the application'.cyan);
console.log('/logout ............. Exit and flush credentials'.cyan);
console.log('/s /switch [#] ...... Quick switch to conversation number #'.cyan);
console.log('/a /attach [path] ... Attach file from path (if has space, must wrap with "")'.cyan);
console.log('/search [query] ..... Search your friends to chat'.cyan);
console.log('/v /view [#] ........ View the attachment by the number given after the type'.cyan);
console.log('/r /refresh ......... Refresh the current converation'.cyan);
console.log('/timestamp .......... Toggle timestamp for messages'.cyan);
console.log('/linelimit [#] ...... Set max number of messages to display in a conversation'.cyan);
console.log('/nolimit ............ Unset a line limit, display all available messages'.cyan);
console.log('/help ............... Print this message'.cyan);
rlInterface.prompt(true);
break;

Expand Down Expand Up @@ -486,6 +487,25 @@ InteractiveCli.prototype.handleCommands = function(command) {
interactive.handleCommands("/refresh");
}
break;
case '/a':
case '/attach':
rlInterface.prompt(true);
let filePath = options[1];
if (action === 1) {
let filePath = options[1];
if (filePath) {
if (existsSync(filePath)) {
emitter.emit('sendAttachment', filePath, recipientId);
} else {
console.log("File not found!".cyan);
}
} else {
console.log("Missing file to be attached!".cyan);
}
} else {
console.log("This command is only available in a conversation!".cyan);
}
break;

default:
console.log('Unknown command. Type /help for commands.'.cyan);
Expand Down Expand Up @@ -552,8 +572,9 @@ InteractiveCli.prototype.run = function(){
emitter.on('sendMessage', listeners.sendMessageListener.bind(listeners));
emitter.on('getThreadId', listeners.getThreadIdListener.bind(listeners));
emitter.on('startSearch', listeners.searchListener.bind(listeners));

console.log("Fetching conversations...".cyan);
emitter.on('sendAttachment', listeners.sendAttachmentListener.bind(listeners));

console.log("Fetching conversations...".cyan);
messenger.getFriends((err, friends) => {
if (err) {
console.log(`An error occured initially fetching friends list: ${err}`);
Expand All @@ -562,7 +583,7 @@ InteractiveCli.prototype.run = function(){
}

const entry = {
id: userId,
id: userId,
firstName: "Me",
name: "Me",
vanity: "unknown",
Expand All @@ -577,10 +598,10 @@ InteractiveCli.prototype.run = function(){
rlInterface.prompt(true);
});

// Set up the line reader
// Set up the line reader
rlInterface.on("line", interactive.handler);
rlInterface.on("close", interactive.exit);
rlInterface.prompt(true);
rlInterface.prompt(true);
});
});
};
Expand All @@ -599,7 +620,7 @@ InteractiveCli.prototype.handleExit = function(logout){
InteractiveCli.prototype.exit = function(){
console.log('Thanks for using fb-messenger-cli'.cyan);
console.log('Bye!'.cyan);

process.exit(0);
};

Expand Down
14 changes: 14 additions & 0 deletions lib/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ class Listeners {
}
}

sendAttachmentListener(filePath, recipientId) {
const errorHandler = (err) => {
if (err) {
console.log('Attachment did not send properly');
}
}
if (this.messenger.users[recipientId]) {
this.messenger.sendAttachment(this.messenger.users[recipientId].vanity, recipientId, filePath, errorHandler);
} else {
// This is a group and not a single user
this.messenger.sendGroupAttachment(recipientId, filePath, errorHandler);
}
}

searchListener(searchStr, callback) {
const parts = searchStr.split(' ');

Expand Down
Loading