mirror of
https://github.com/fazo96/telecommander.git
synced 2025-01-10 11:34:20 +01:00
basic message download implemented
This commit is contained in:
parent
378fe7f68d
commit
c769aa28a1
149
telecommander.js
149
telecommander.js
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
// Prepare deps and fs
|
||||||
var cfgDir = (process.env.HOME || process.env.USERPROFILE) + '/.config/telecommander/'
|
var cfgDir = (process.env.HOME || process.env.USERPROFILE) + '/.config/telecommander/'
|
||||||
process.env.LOGGER_FILE = cfgDir+'log'
|
process.env.LOGGER_FILE = cfgDir+'log'
|
||||||
|
|
||||||
@ -15,6 +16,8 @@ var logger = getLogger('main')
|
|||||||
|
|
||||||
var telegramLink = require('telegram.link')()
|
var telegramLink = require('telegram.link')()
|
||||||
|
|
||||||
|
// Prepare blessed UI
|
||||||
|
|
||||||
var screen = blessed.screen({
|
var screen = blessed.screen({
|
||||||
smartCSR: true,
|
smartCSR: true,
|
||||||
dockBorders: true
|
dockBorders: true
|
||||||
@ -30,6 +33,7 @@ var defaultStyle = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to create a log box
|
||||||
function mkBox(){
|
function mkBox(){
|
||||||
return blessed.log({
|
return blessed.log({
|
||||||
right: 0,
|
right: 0,
|
||||||
@ -41,6 +45,7 @@ function mkBox(){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contact list window
|
||||||
var chats = blessed.list({
|
var chats = blessed.list({
|
||||||
left: 0,
|
left: 0,
|
||||||
top:0,
|
top:0,
|
||||||
@ -52,6 +57,7 @@ var chats = blessed.list({
|
|||||||
})
|
})
|
||||||
chats.style.selected = { bg: 'blue' }
|
chats.style.selected = { bg: 'blue' }
|
||||||
|
|
||||||
|
// Command line prompt
|
||||||
var cmdline = blessed.textbox({
|
var cmdline = blessed.textbox({
|
||||||
inputOnFocus: true,
|
inputOnFocus: true,
|
||||||
bottom: 0,
|
bottom: 0,
|
||||||
@ -62,23 +68,43 @@ var cmdline = blessed.textbox({
|
|||||||
style: defaultStyle
|
style: defaultStyle
|
||||||
})
|
})
|
||||||
|
|
||||||
var msgBox = { "Status": mkBox() }
|
var statusWindow = "Status"
|
||||||
|
|
||||||
|
// mgsBox holds the chat boxes for every list entry
|
||||||
|
var msgBox = { }
|
||||||
|
msgBox[statusWindow] = mkBox()
|
||||||
|
|
||||||
|
// Add stuff to the screen
|
||||||
screen.append(chats);
|
screen.append(chats);
|
||||||
screen.append(cmdline);
|
screen.append(cmdline);
|
||||||
screen.append(msgBox["Status"]);
|
screen.append(msgBox[statusWindow]);
|
||||||
|
|
||||||
|
// Contacts holds all the data about downloaded contacts
|
||||||
var contacts = { }
|
var contacts = { }
|
||||||
|
// nameToUid is used to match a name to its user id (for the contact list)
|
||||||
|
var nameToUid = { }
|
||||||
|
|
||||||
var client,phone,code,phoneCodeHash,fullName,username,loginResult,uid,authKey
|
var client // used to talk with telegram
|
||||||
var registered = false
|
var phone // our phone number
|
||||||
var selectedUser = "Status"
|
var code // our phone code
|
||||||
|
var phoneCodeHash // our phone code thingy that telegram wants
|
||||||
|
var fullName
|
||||||
|
var username
|
||||||
|
var loginResult // Store here the server answer to the login
|
||||||
|
var uid // our user id
|
||||||
|
var authKey // our authorization key to access telegram
|
||||||
|
var registered = false // keep track of wether the phone number is registered
|
||||||
|
var connected = false // keep track of wether we are good to go and logged in
|
||||||
|
var selectedWindow = statusWindow // the currently selected window
|
||||||
|
|
||||||
function command(cmd){
|
function command(cmd){
|
||||||
cmdl = cmd.split(' ')
|
cmdl = cmd.split(' ')
|
||||||
cmdname = cmdl[0]
|
cmdname = cmdl[0]
|
||||||
|
|
||||||
if(cmdname === 'phone'){
|
if(cmdname === 'phone'){ // So the user can provide his phone numbah
|
||||||
|
if(connected){
|
||||||
|
return log("Silly user, you're already connected! We don't need that phone number")
|
||||||
|
}
|
||||||
phone = cmd.split(' ')[1]
|
phone = cmd.split(' ')[1]
|
||||||
client.auth.sendCode(phone,5,'en',function(result){
|
client.auth.sendCode(phone,5,'en',function(result){
|
||||||
log('Errors:',result.error_code,result.error_message)
|
log('Errors:',result.error_code,result.error_message)
|
||||||
@ -95,7 +121,10 @@ function command(cmd){
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
} else if(cmdname === 'code'){
|
} else if(cmdname === 'code'){ // So the user can provide his phone code
|
||||||
|
if(connected){
|
||||||
|
return log("Silly user, you're already connected! We don't need that phone code")
|
||||||
|
}
|
||||||
code = cmdl[1]
|
code = cmdl[1]
|
||||||
name = cmdl[2]
|
name = cmdl[2]
|
||||||
lastname = cmdl[3]
|
lastname = cmdl[3]
|
||||||
@ -110,64 +139,76 @@ function command(cmd){
|
|||||||
if(registered) client.auth.signIn(phone,phoneCodeHash,code,cb)
|
if(registered) client.auth.signIn(phone,phoneCodeHash,code,cb)
|
||||||
else client.auth.signUp(phone,phoneCodeHash,code,name,lastname,cb)
|
else client.auth.signUp(phone,phoneCodeHash,code,name,lastname,cb)
|
||||||
|
|
||||||
} else if(cmdname === 'init'){
|
} else if(cmdname === 'msg'){ // Send a message
|
||||||
whenReady()
|
msg(cmdl[1],cmdl[2])
|
||||||
} else if(cmdname === 'msg'){
|
|
||||||
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: selectedUser} })
|
|
||||||
var randid = parseInt(Math.random() * 1000000000)
|
|
||||||
client.messages.sendMessage(peer,cmdl[1],randid,function(sent){
|
|
||||||
log('Send message:',cmdl[1],'to:',selectedUser+':',sent.toPrintable())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chats.addItem('Status')
|
chats.addItem(msgBox[statusWindow])
|
||||||
|
screen.render();
|
||||||
|
|
||||||
|
// What happens when a different window is selected
|
||||||
chats.on('select',function(selected){
|
chats.on('select',function(selected){
|
||||||
log('SELECT:',selected.content)
|
log('SELECT:',selected.content)
|
||||||
msgBox[selectedUser].hide()
|
msgBox[selectedWindow].hide()
|
||||||
selectedUser = selected.content;
|
selectedWindow = selected.content;
|
||||||
//if(!isNaN(selectedUser)){ // Actual user, not utility window
|
if(!msgBox[selectedWindow]){
|
||||||
if(!msgBox[selectedUser]){
|
msgBox[selectedWindow] = mkBox()
|
||||||
msgBox[selectedUser] = mkBox()
|
screen.append(msgBox[selectedWindow])
|
||||||
screen.append(msgBox[selectedUser])
|
|
||||||
}
|
}
|
||||||
msgBox[selectedUser].show()
|
var uid = nameToUid[selectedWindow]
|
||||||
|
if(uid != undefined){
|
||||||
|
// Is a real user: download messages and stuff
|
||||||
|
getMessages(uid,msgBox[selectedWindow])
|
||||||
|
}
|
||||||
|
msgBox[selectedWindow].show()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// What happens when the user submits a command in the prompt
|
||||||
cmdline.on('submit',function(value){
|
cmdline.on('submit',function(value){
|
||||||
msgBox[selectedUser].add('[ECHO] '+value)
|
msgBox[selectedWindow].add('< '+value)
|
||||||
if(selectedUser == "Status"){
|
if(nameToUid[selectedWindow] == undefined){
|
||||||
command(value)
|
command(value)
|
||||||
} else {
|
} else {
|
||||||
// Send Message
|
// Send Message
|
||||||
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: selectedUser} })
|
msg(nameToUid[selectedWindow],value)
|
||||||
var randid = parseInt(Math.random() * 1000000000)
|
|
||||||
client.messages.sendMessage(peer,value,randid,function(sent){
|
|
||||||
log('Send message:',value,'to:',selectedUser+':',sent.toPrintable())
|
|
||||||
msgBox[selectedUser].add('You: '+value)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
cmdline.clearValue()
|
cmdline.clearValue()
|
||||||
cmdline.focus()
|
cmdline.focus()
|
||||||
})
|
})
|
||||||
cmdline.focus()
|
|
||||||
|
cmdline.focus() // make sure prompt is focused
|
||||||
|
|
||||||
|
// Catch ctrl-c or escape event and close program
|
||||||
cmdline.key(['escape','C-c'], function(ch,key){
|
cmdline.key(['escape','C-c'], function(ch,key){
|
||||||
if(client){
|
if(connected || client != undefined){
|
||||||
log('Closing communications and shutting down...')
|
log('Closing communications and shutting down...')
|
||||||
client.end(function(){
|
client.end(function(){
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
})
|
})
|
||||||
} else process.exit(0);
|
} else process.exit(0);
|
||||||
});
|
});
|
||||||
//cmdline.focus()
|
|
||||||
screen.render();
|
|
||||||
|
|
||||||
|
// Send a message
|
||||||
|
function msg(uid,str){
|
||||||
|
if(!connected){
|
||||||
|
return log('Error: not ready to send messages')
|
||||||
|
}
|
||||||
|
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: ''+uid } })
|
||||||
|
var randid = parseInt(Math.random() * 1000000000)
|
||||||
|
client.messages.sendMessage(peer,str,randid,function(sent){
|
||||||
|
log('Send message:','"'+str+'"','to:',selectedWindow+':',sent.toPrintable())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write something in the Status box
|
||||||
function log(){
|
function log(){
|
||||||
args = Array.prototype.slice.call(arguments)
|
args = Array.prototype.slice.call(arguments)
|
||||||
msg = args.join(' ')
|
var msg = args.join(' ')
|
||||||
msgBox["Status"].add(msg)
|
msgBox[statusWindow].add(msg)
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare data to feed to telegramLink
|
||||||
var app = {
|
var app = {
|
||||||
id: '42419',
|
id: '42419',
|
||||||
hash: '90a3c2cdbf9b391d9ed72c0639dc0786',
|
hash: '90a3c2cdbf9b391d9ed72c0639dc0786',
|
||||||
@ -206,33 +247,51 @@ function connect(){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download Contacts, ecc
|
// Executed when connected and logged in
|
||||||
function whenReady(){
|
function whenReady(){
|
||||||
log('READY!')
|
log('READY!')
|
||||||
|
connected = true
|
||||||
downloadData()
|
downloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Downloads stuff
|
||||||
function downloadData(){
|
function downloadData(){
|
||||||
log('Downloading data...')
|
log('Downloading data...')
|
||||||
client.contacts.getContacts('',function(cont){
|
client.contacts.getContacts('',function(cont){
|
||||||
//log('\nContacts:\n',JSON.stringify(cont)+'\n')
|
//log('\nContacts:\n',JSON.stringify(cont)+'\n')
|
||||||
chats.clearItems()
|
chats.clearItems()
|
||||||
chats.add("Status")
|
chats.add(statusWindow)
|
||||||
cont.users.list.forEach(function(user,index){
|
cont.users.list.forEach(function(user,index){
|
||||||
contacts[user.id] = user
|
contacts[user.id] = user
|
||||||
chats.addItem(''+user.id)
|
var name = user.first_name + ' ' + user.last_name + (user.username?' ('+user.username+')':'')
|
||||||
|
nameToUid[name] = user.id
|
||||||
|
chats.addItem(''+name)
|
||||||
log('Added user: '+user.id)
|
log('Added user: '+user.id)
|
||||||
})
|
})
|
||||||
fs.writeFile(cfgDir+'contacts.json',JSON.stringify(cont.users.list[0]))
|
|
||||||
})
|
})
|
||||||
client.messages.getDialogs(0,-1,10,function(dialogs){
|
client.messages.getDialogs(0,0,10,function(dialogs){
|
||||||
//log('\nDialogs:\n',JSON.stringify(dialogs)+'\n')
|
log('\nDialogs:\n',dialogs.toPrintable()+'\n')
|
||||||
fs.writeFile(cfgDir+'dialogs.json',JSON.stringify(dialogs))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get message history with given id
|
||||||
|
function getMessages(uid,window){
|
||||||
|
if(!connected){
|
||||||
|
return log('Uh cant get messages cuz not connected.....')
|
||||||
|
}
|
||||||
|
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: uid } })
|
||||||
|
client.messages.getHistory(peer,0,-1,20,function(res){
|
||||||
|
window.add('\nMessage History with',uid+':\n',res.toPrintable())
|
||||||
|
res.messages.list.reverse()
|
||||||
|
res.messages.list.forEach(function(msg){
|
||||||
|
if(!msg.message) return;
|
||||||
|
window.add('> '+msg.message)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to load key from disk
|
||||||
var keyPath = cfgDir+'key'
|
var keyPath = cfgDir+'key'
|
||||||
// Try loading key
|
|
||||||
log('Checking disk for key...')
|
log('Checking disk for key...')
|
||||||
fs.exists(keyPath,function(exists){
|
fs.exists(keyPath,function(exists){
|
||||||
if(exists){
|
if(exists){
|
||||||
|
Loading…
Reference in New Issue
Block a user