2015-08-29 02:38:59 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Prepare deps and fs
|
2015-08-29 02:38:59 +02:00
|
|
|
var cfgDir = (process.env.HOME || process.env.USERPROFILE) + '/.config/telecommander/'
|
|
|
|
process.env.LOGGER_FILE = cfgDir+'log'
|
|
|
|
|
|
|
|
var os = require('os')
|
|
|
|
var fs = require('fs')
|
2015-08-30 01:49:03 +02:00
|
|
|
var moment = require('moment')
|
2015-08-29 02:38:59 +02:00
|
|
|
var blessed = require('blessed')
|
|
|
|
|
|
|
|
try { fs.makeDirSync(cfgDir,'0770') } catch (e) { }
|
|
|
|
|
|
|
|
var getLogger = require('get-log')
|
|
|
|
getLogger.PROJECT_NAME = 'telecommander'
|
|
|
|
var logger = getLogger('main')
|
|
|
|
|
|
|
|
var telegramLink = require('telegram.link')()
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Prepare blessed UI
|
|
|
|
|
2015-08-29 02:38:59 +02:00
|
|
|
var screen = blessed.screen({
|
|
|
|
smartCSR: true,
|
|
|
|
dockBorders: true
|
|
|
|
})
|
|
|
|
screen.title = "Telecommander"
|
|
|
|
|
|
|
|
var defaultStyle = {
|
|
|
|
fg: 'white',
|
|
|
|
border: { fg: 'gray' },
|
2015-08-29 04:09:03 +02:00
|
|
|
scrollbar: {
|
|
|
|
bg: 'blue',
|
|
|
|
fg: 'red'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Function to create a log box
|
2015-08-29 04:09:03 +02:00
|
|
|
function mkBox(){
|
|
|
|
return blessed.log({
|
|
|
|
right: 0,
|
|
|
|
width: '80%',
|
|
|
|
height: screen.height-3,
|
|
|
|
border: { type: 'line' },
|
|
|
|
scrollable: true,
|
2015-08-30 12:16:41 +02:00
|
|
|
draggable: true,
|
2015-08-29 04:09:03 +02:00
|
|
|
style: defaultStyle
|
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Contact list window
|
2015-08-29 02:38:59 +02:00
|
|
|
var chats = blessed.list({
|
|
|
|
left: 0,
|
|
|
|
top:0,
|
|
|
|
height: screen.height-3,
|
|
|
|
width: '20%',
|
|
|
|
border: { type: 'line' },
|
|
|
|
mouse: true,
|
|
|
|
style: defaultStyle,
|
|
|
|
})
|
2015-08-29 04:09:03 +02:00
|
|
|
chats.style.selected = { bg: 'blue' }
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Command line prompt
|
2015-08-29 02:38:59 +02:00
|
|
|
var cmdline = blessed.textbox({
|
|
|
|
inputOnFocus: true,
|
|
|
|
bottom: 0,
|
|
|
|
left: 'center',
|
|
|
|
width: '100%',
|
|
|
|
height: 3,
|
|
|
|
border: { type: 'line' },
|
|
|
|
style: defaultStyle
|
|
|
|
})
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
var statusWindow = "Status"
|
|
|
|
|
|
|
|
// mgsBox holds the chat boxes for every list entry
|
|
|
|
var msgBox = { }
|
|
|
|
msgBox[statusWindow] = mkBox()
|
2015-08-29 04:09:03 +02:00
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Add stuff to the screen
|
2015-08-29 02:38:59 +02:00
|
|
|
screen.append(chats);
|
|
|
|
screen.append(cmdline);
|
2015-08-29 13:51:14 +02:00
|
|
|
screen.append(msgBox[statusWindow]);
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-30 01:49:03 +02:00
|
|
|
// Contacts holds all the contacts data
|
2015-08-29 02:38:59 +02:00
|
|
|
var contacts = { }
|
2015-08-30 01:49:03 +02:00
|
|
|
// Groups hold all the data about groups
|
|
|
|
var groups = { }
|
2015-08-29 13:51:14 +02:00
|
|
|
// nameToUid is used to match a name to its user id (for the contact list)
|
|
|
|
var nameToUid = { }
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-30 12:16:41 +02:00
|
|
|
var state = { } // keeps track of the telegram update state
|
2015-08-29 13:51:14 +02:00
|
|
|
var client // used to talk with telegram
|
2015-08-30 12:16:41 +02:00
|
|
|
var user = { } // holds data about current user
|
2015-08-29 13:51:14 +02:00
|
|
|
var authKey // our authorization key to access telegram
|
|
|
|
var connected = false // keep track of wether we are good to go and logged in
|
|
|
|
var selectedWindow = statusWindow // the currently selected window
|
2015-08-29 02:38:59 +02:00
|
|
|
|
|
|
|
function command(cmd){
|
2015-08-29 04:09:03 +02:00
|
|
|
cmdl = cmd.split(' ')
|
|
|
|
cmdname = cmdl[0]
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
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")
|
|
|
|
}
|
2015-08-30 12:16:41 +02:00
|
|
|
user.phone = cmd.split(' ')[1]
|
|
|
|
var mindate = moment()
|
|
|
|
client.auth.sendCode(user.phone,5,'en',function(result){
|
|
|
|
if(result.err_code){
|
|
|
|
return log('Errors:',result.error_code,result.error_message)
|
|
|
|
}
|
|
|
|
//log('Res:',JSON.stringify(result))
|
|
|
|
user.registered = result.phone_registered
|
|
|
|
user.phoneCodeHash = result.phone_code_hash
|
|
|
|
function gmd(){
|
|
|
|
var m = moment()
|
|
|
|
m = m.subtract(m.diff(mindate))
|
|
|
|
return 'Please use a telegram code not older than '+m.fromNow(true)
|
|
|
|
}
|
|
|
|
if(!user.registered){
|
2015-08-29 02:38:59 +02:00
|
|
|
log("Your number is not registered. The client will register your account with the Telegram service")
|
2015-08-30 12:16:41 +02:00
|
|
|
log(gmd())
|
2015-08-29 02:38:59 +02:00
|
|
|
log('Ready for phone code, use command: "code <code> <name> <lastname>" to register')
|
|
|
|
} else {
|
|
|
|
log("Your number is already registered with telegram. The client will log in.")
|
2015-08-30 12:16:41 +02:00
|
|
|
log(gmd())
|
2015-08-29 02:38:59 +02:00
|
|
|
log('Ready for phone code, use command: "code <code>" to login')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
} 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")
|
|
|
|
}
|
2015-08-29 02:38:59 +02:00
|
|
|
code = cmdl[1]
|
|
|
|
name = cmdl[2]
|
|
|
|
lastname = cmdl[3]
|
2015-08-30 12:16:41 +02:00
|
|
|
if(((!name || !lastname) && !user.registered) || !code)
|
2015-08-29 02:38:59 +02:00
|
|
|
return log('insufficient arguments:',cmd)
|
|
|
|
cb = function(result){
|
2015-08-30 12:16:41 +02:00
|
|
|
user.id = ''+result.user.id
|
|
|
|
user.phone = result.user.phone
|
|
|
|
user.phoneCodeHash = result.phone_code_hash
|
|
|
|
user.username = result.user.username
|
|
|
|
user.first_name = result.user.first_name
|
|
|
|
user.last_name = result.user.last_name
|
|
|
|
// Done, write user data and key to disk
|
|
|
|
log('Writing key to disk...')
|
|
|
|
fs.writeFile(cfgDir+'key',authKey,function(err){
|
|
|
|
if(err)
|
|
|
|
log('Could not write key to disk:',err)
|
|
|
|
else
|
|
|
|
log('key saved to disk')
|
|
|
|
})
|
|
|
|
log('Writing user info to disk...')
|
|
|
|
fs.writeFile(cfgDir+'user_data.json',JSON.stringify(user),function(err){
|
|
|
|
if(err)
|
|
|
|
log("ERROR: couldn't write user_data.json:",err)
|
|
|
|
else
|
|
|
|
log('user_data.json saved to disk with data:',JSON.stringify(user))
|
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
whenReady()
|
|
|
|
}
|
|
|
|
// Log in finally
|
2015-08-30 12:16:41 +02:00
|
|
|
if(user.registered) client.auth.signIn(user.phone,user.phoneCodeHash,code,cb)
|
|
|
|
else client.auth.signUp(user.phone,user.phoneCodeHash,code,name,lastname,cb)
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
} else if(cmdname === 'msg'){ // Send a message
|
2015-08-30 12:16:41 +02:00
|
|
|
sendMsg(cmdl[1],cmdl[2])
|
2015-08-29 02:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 13:51:14 +02:00
|
|
|
chats.addItem(msgBox[statusWindow])
|
|
|
|
screen.render();
|
|
|
|
|
|
|
|
// What happens when a different window is selected
|
2015-08-29 04:09:03 +02:00
|
|
|
chats.on('select',function(selected){
|
|
|
|
log('SELECT:',selected.content)
|
2015-08-29 13:51:14 +02:00
|
|
|
msgBox[selectedWindow].hide()
|
|
|
|
selectedWindow = selected.content;
|
2015-08-30 01:49:03 +02:00
|
|
|
var newb = getMsgBox(selectedWindow)
|
|
|
|
newb.show()
|
2015-08-29 04:09:03 +02:00
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
|
2015-08-30 01:49:03 +02:00
|
|
|
// Get msgBox for given chat, create if not exists
|
|
|
|
function getMsgBox(chat){
|
|
|
|
// Automatically convert ids to names
|
2015-08-30 12:16:41 +02:00
|
|
|
//if(contacts[chat]) chat = contacts[chat].user.id
|
|
|
|
//if(groups[chat]); // To be implemented
|
|
|
|
if(chat === undefined){
|
|
|
|
log('ERROR: asked for box for "undefined"!!')
|
|
|
|
return msgBox[statusWindow]
|
|
|
|
}
|
2015-08-30 01:49:03 +02:00
|
|
|
if(!msgBox[chat]){
|
|
|
|
log('Generating window: "'+chat+'"')
|
|
|
|
msgBox[chat] = mkBox()
|
|
|
|
screen.append(msgBox[chat])
|
|
|
|
var uid = nameToUid[chat]
|
|
|
|
if(uid != undefined){
|
|
|
|
// Is a real user: download messages and stuff
|
|
|
|
getMessages(uid,msgBox[chat])
|
|
|
|
}
|
2015-08-30 12:16:41 +02:00
|
|
|
} else log('Getting window','"'+chat+'"')
|
2015-08-30 01:49:03 +02:00
|
|
|
return msgBox[chat]
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// What happens when the user submits a command in the prompt
|
2015-08-29 02:38:59 +02:00
|
|
|
cmdline.on('submit',function(value){
|
2015-08-30 01:49:03 +02:00
|
|
|
msgBox[statusWindow].add('< '+value)
|
2015-08-29 13:51:14 +02:00
|
|
|
if(nameToUid[selectedWindow] == undefined){
|
2015-08-29 04:09:03 +02:00
|
|
|
command(value)
|
|
|
|
} else {
|
|
|
|
// Send Message
|
2015-08-30 12:16:41 +02:00
|
|
|
sendMsg(nameToUid[selectedWindow],value)
|
2015-08-29 04:09:03 +02:00
|
|
|
}
|
2015-08-29 02:38:59 +02:00
|
|
|
cmdline.clearValue()
|
|
|
|
cmdline.focus()
|
|
|
|
})
|
2015-08-29 13:51:14 +02:00
|
|
|
|
|
|
|
cmdline.focus() // make sure prompt is focused
|
|
|
|
|
|
|
|
// Catch ctrl-c or escape event and close program
|
2015-08-29 02:38:59 +02:00
|
|
|
cmdline.key(['escape','C-c'], function(ch,key){
|
2015-08-29 13:51:14 +02:00
|
|
|
if(connected || client != undefined){
|
2015-08-29 02:38:59 +02:00
|
|
|
log('Closing communications and shutting down...')
|
|
|
|
client.end(function(){
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
} else process.exit(0);
|
|
|
|
});
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Send a message
|
2015-08-30 12:16:41 +02:00
|
|
|
function sendMsg(uid,str){
|
2015-08-29 13:51:14 +02:00
|
|
|
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
|
2015-08-29 02:38:59 +02:00
|
|
|
function log(){
|
|
|
|
args = Array.prototype.slice.call(arguments)
|
2015-08-29 13:51:14 +02:00
|
|
|
var msg = args.join(' ')
|
|
|
|
msgBox[statusWindow].add(msg)
|
2015-08-29 02:38:59 +02:00
|
|
|
logger.info(msg)
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Prepare data to feed to telegramLink
|
2015-08-29 02:38:59 +02:00
|
|
|
var app = {
|
|
|
|
id: '42419',
|
|
|
|
hash: '90a3c2cdbf9b391d9ed72c0639dc0786',
|
|
|
|
version: require('./package.json').version,
|
|
|
|
lang: 'en',
|
|
|
|
deviceModel: os.type(),
|
|
|
|
systemVersion: os.platform()+'/'+os.release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connects to telegram
|
|
|
|
function connect(){
|
|
|
|
client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){
|
|
|
|
if(!app.authKey){
|
|
|
|
log('Creating authkey...')
|
|
|
|
client.createAuthKey(function(auth){
|
|
|
|
authKey = auth.key.encrypt('password') // I know sorry, but I'm testing. Will add security later, I promise
|
2015-08-30 12:16:41 +02:00
|
|
|
log('Created key')
|
2015-08-29 02:38:59 +02:00
|
|
|
// Writes the new encrypted key to disk
|
|
|
|
log('ready for phone number, use command: phone <number>')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log('Authkey loaded from disk. Should be ready to go.')
|
|
|
|
whenReady()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
client.once('dataCenter',function(dcs){
|
|
|
|
log('Datacenters:',dcs.toPrintable())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Executed when connected and logged in
|
2015-08-29 02:38:59 +02:00
|
|
|
function whenReady(){
|
|
|
|
log('READY!')
|
2015-08-29 13:51:14 +02:00
|
|
|
connected = true
|
2015-08-29 02:38:59 +02:00
|
|
|
downloadData()
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
// Downloads stuff
|
2015-08-29 02:38:59 +02:00
|
|
|
function downloadData(){
|
|
|
|
log('Downloading data...')
|
2015-08-30 01:49:03 +02:00
|
|
|
|
2015-08-29 02:38:59 +02:00
|
|
|
client.contacts.getContacts('',function(cont){
|
2015-08-29 04:09:03 +02:00
|
|
|
chats.clearItems()
|
2015-08-29 13:51:14 +02:00
|
|
|
chats.add(statusWindow)
|
2015-08-30 12:16:41 +02:00
|
|
|
cont.users.list.forEach(function(u,index){
|
|
|
|
if(!contacts[u.id]) contacts[u.id] = {}
|
|
|
|
contacts[u.id].user = u
|
|
|
|
var name = getName(u.id)
|
|
|
|
nameToUid[name] = u.id
|
2015-08-30 01:49:03 +02:00
|
|
|
chats.addItem(name)
|
2015-08-30 12:16:41 +02:00
|
|
|
//log('Added user:',u.id,'-',name)
|
2015-08-29 04:09:03 +02:00
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
})
|
2015-08-30 01:49:03 +02:00
|
|
|
|
2015-08-29 13:51:14 +02:00
|
|
|
client.messages.getDialogs(0,0,10,function(dialogs){
|
2015-08-30 01:49:03 +02:00
|
|
|
dialogs.dialogs.list.forEach(function(item){
|
|
|
|
if(item.peer.chat_id){ // is a group
|
|
|
|
groups[item.peer.chat_id] = item
|
2015-08-30 12:16:41 +02:00
|
|
|
//log('Added group:',item.peer.chat_id)
|
2015-08-30 01:49:03 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
client.updates.getState(function(astate){
|
2015-08-30 12:16:41 +02:00
|
|
|
updateState(astate)
|
|
|
|
log(state.unreadCount,'unread messages')
|
|
|
|
log('Started receiving updates')
|
|
|
|
// Can't use registerOnUpdates because it's apparently broken
|
|
|
|
//client.registerOnUpdates(onUpdate)
|
|
|
|
setTimeout(downloadUpdates,1000)
|
2015-08-29 13:51:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-30 12:16:41 +02:00
|
|
|
// Updates the current state
|
|
|
|
function updateState(newstate){
|
|
|
|
state.pts = newstate.pts
|
|
|
|
state.qts = newstate.qts
|
|
|
|
state.date = newstate.date
|
|
|
|
state.sqp = newstate.seq
|
|
|
|
state.unreadCount = newstate.unread_count
|
|
|
|
}
|
|
|
|
|
|
|
|
// process an update
|
2015-08-30 01:49:03 +02:00
|
|
|
function onUpdate(upd){
|
|
|
|
log('Got Update:',upd.toPrintable())
|
|
|
|
// Process update
|
|
|
|
if(update.message){
|
|
|
|
// It's a chat message
|
|
|
|
log('Writing chat message to ',update.from_id)
|
|
|
|
//appendMsg(update,getName(update.from_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 12:16:41 +02:00
|
|
|
function downloadUpdates(){
|
|
|
|
client.updates.getDifference(state.pts,state.date,state.qts,function(res){
|
|
|
|
log('Got Diff: ',res.toPrintable())
|
|
|
|
if(res.state){
|
|
|
|
updateState(res.state)
|
|
|
|
}
|
|
|
|
if(res.new_messages){
|
|
|
|
res.new_messages.list.forEach(function(msg){
|
|
|
|
if(!msg.message) return log('Empty message!',msg)
|
|
|
|
//log('Scheduling message: '+msg.message)
|
|
|
|
appendMsg(msg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setTimeout(downloadUpdates,1000)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-30 01:49:03 +02:00
|
|
|
function getName(uid){
|
2015-08-30 12:16:41 +02:00
|
|
|
var u
|
2015-08-30 01:49:03 +02:00
|
|
|
if(!contacts[uid])
|
2015-08-30 12:16:41 +02:00
|
|
|
if(groups[uid])
|
|
|
|
return uid
|
|
|
|
else{
|
|
|
|
log('Failed to find name for:',uid)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
else u = contacts[uid].user
|
|
|
|
return u.first_name + ' ' + u.last_name + (u.username?' ('+u.username+')':'')
|
2015-08-30 01:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get message history with given id in the given box
|
|
|
|
function getMessages(uid,box){
|
2015-08-29 13:51:14 +02:00
|
|
|
if(!connected){
|
|
|
|
return log('Uh cant get messages cuz not connected.....')
|
|
|
|
}
|
2015-08-30 01:49:03 +02:00
|
|
|
box.add('Downloading message history for '+getName(uid))
|
2015-08-29 13:51:14 +02:00
|
|
|
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: uid } })
|
|
|
|
client.messages.getHistory(peer,0,-1,20,function(res){
|
2015-08-30 01:49:03 +02:00
|
|
|
//log(res.toPrintable())
|
2015-08-30 12:16:41 +02:00
|
|
|
log('Got history for: '+getName(peer.user_id))
|
2015-08-30 01:49:03 +02:00
|
|
|
res.messages.list.sort(function(msg1,msg2){
|
|
|
|
return msg1.date - msg2.date
|
|
|
|
})
|
|
|
|
if(res.messages.list.length === 0)
|
2015-08-30 12:16:41 +02:00
|
|
|
return appendToUserBox('No messages.',res)
|
2015-08-29 13:51:14 +02:00
|
|
|
res.messages.list.forEach(function(msg){
|
2015-08-30 12:16:41 +02:00
|
|
|
if(!msg.message) return log('Empty message!',msg)
|
|
|
|
//log('Scheduling message: '+msg.message)
|
|
|
|
appendMsg(msg)
|
2015-08-29 13:51:14 +02:00
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-30 12:16:41 +02:00
|
|
|
function appendToUserBox(msg,context){
|
|
|
|
var goesto
|
|
|
|
if(context.messages.list.length > 0){
|
|
|
|
if(context.messages.list[0].to_id.chat_id){
|
|
|
|
// Group message
|
|
|
|
log('Chose',getName(context.messages.list[0].to_id.chat_id))
|
|
|
|
goesto = getMsgBox(getName(context.messages.list[0].to_id.chat_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(goesto === undefined){
|
|
|
|
if(context.users.list[0].user_id == user.id){
|
|
|
|
goesto = getMsgBox(getName(context.users.list[1].id))
|
|
|
|
} else{
|
|
|
|
goesto = getMsgBox(getName(context.users.list[0].id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendMsg(msg,goesto,true)
|
|
|
|
}
|
|
|
|
|
2015-08-30 01:49:03 +02:00
|
|
|
// Writes given telegram.link "message" object to given boxId
|
2015-08-30 12:16:41 +02:00
|
|
|
function appendMsg(msg,toBoxId,bare){
|
|
|
|
var box
|
|
|
|
if(toBoxId != undefined){
|
|
|
|
box = toBoxId
|
|
|
|
} else {
|
|
|
|
if(msg.from_id === msg.to_id.user_id || msg.from_id != user.id){
|
|
|
|
box = getMsgBox(getName(msg.from_id))
|
|
|
|
} else if(msg.to_id != user.id) {
|
|
|
|
// don't forget dat .user_id! don't need it in from_id...
|
|
|
|
box = getMsgBox(getName(msg.to_id.user_id))
|
|
|
|
} else {
|
|
|
|
// Wtf ? maybe a group
|
|
|
|
log('Unknown message: from',msg.from_id,'to',msg.to_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(bare)
|
|
|
|
box.add(msg)
|
|
|
|
else {
|
|
|
|
var from = msg.from_id
|
|
|
|
var date = moment.unix(msg.date).fromNow()
|
|
|
|
name = getName(from)
|
|
|
|
box.add(date+' | '+(name || from)+' > '+msg.message)
|
|
|
|
}
|
2015-08-30 01:49:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 12:16:41 +02:00
|
|
|
// - Entry Point -
|
|
|
|
// Load authKey and userdata from disk, then act depending on outcome
|
2015-08-29 02:38:59 +02:00
|
|
|
var keyPath = cfgDir+'key'
|
|
|
|
log('Checking disk for key...')
|
|
|
|
fs.exists(keyPath,function(exists){
|
|
|
|
if(exists){
|
|
|
|
log('Key found')
|
|
|
|
fs.readFile(keyPath,function(err,content){
|
|
|
|
if(err)
|
|
|
|
log('Error while reading key:',err)
|
|
|
|
else {
|
|
|
|
authKey = telegramLink.retrieveAuthKey(content,'password') // yeah sorry just testing
|
|
|
|
app.authKey = authKey
|
|
|
|
log('Key loaded')
|
2015-08-30 12:16:41 +02:00
|
|
|
fs.readFile(cfgDir+'user_data.json',function(err,data){
|
|
|
|
if(err)
|
|
|
|
log("FATAL: couldn't read user_data.json")
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
log("Got User Data from disk: ",data)
|
|
|
|
user = JSON.parse(data)
|
|
|
|
connect()
|
|
|
|
} catch (e) {
|
|
|
|
log("FATAL: user data corrupted:",e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-08-29 02:38:59 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log('Key not found')
|
|
|
|
connect()
|
|
|
|
}
|
|
|
|
})
|