1
0
mirror of https://github.com/fazo96/telecommander.git synced 2025-01-10 11:34:20 +01:00

improve messages in the sign in/up phase

This commit is contained in:
Enrico Fasoli 2015-08-30 22:47:36 +00:00
parent a3aeca5cf3
commit 92f37bf061

View File

@ -143,6 +143,7 @@ function command(cmd){
} }
user.phone = cmd.split(' ')[1] user.phone = cmd.split(' ')[1]
var mindate = moment() var mindate = moment()
log('Checking your phone number with Telegram...')
client.auth.sendCode(user.phone,5,'en',function(result){ client.auth.sendCode(user.phone,5,'en',function(result){
if(result.err_code){ if(result.err_code){
return log('Errors:',result.error_code,result.error_message) return log('Errors:',result.error_code,result.error_message)
@ -156,13 +157,14 @@ function command(cmd){
return 'Please use a telegram code not older than '+m.fromNow(true) return 'Please use a telegram code not older than '+m.fromNow(true)
} }
if(!user.registered){ if(!user.registered){
log("Your number is not registered. The client will register your account with the Telegram service") log("Your number is not registered. Telecommander will register your account with the Telegram service")
log(gmd()) log(gmd())
log('Ready for phone code, use command: "code <code> <name> <lastname>" to register') log('Ready for phone code, use command: "code <code> <name> <lastname>" to register')
log("If you don't want to sign up, just don't enter the code and press ESC to exit. No data was saved to the file system")
} else { } else {
log("Your number is already registered with telegram. The client will log in.") log("Your number is already assigned to a Telegram account. Telecommander will log you in.")
log(gmd()) log(gmd())
log('Ready for phone code, use command: "code <code>" to login') log("If you don't want to sign in, just don't enter the code and press ESC to exit. No data was saved to the file system")
} }
}) })
@ -183,19 +185,12 @@ function command(cmd){
user.first_name = result.user.first_name user.first_name = result.user.first_name
user.last_name = result.user.last_name user.last_name = result.user.last_name
// Done, write user data and key to disk // Done, write user data and key to disk
log('Writing key to disk...') log('Writing Log In token and user data to',cfgDir)
fs.writeFile(cfgDir+'key',authKey,function(err){ fs.writeFile(cfgDir+'key',authKey,function(err){
if(err) if(err) log('FATAL: Could not write key to disk:',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){ fs.writeFile(cfgDir+'user_data.json',JSON.stringify(user),function(err){
if(err) if(err) log("FATAL: couldn't write user_data.json:",err)
log("ERROR: couldn't write user_data.json:",err)
else
log('user_data.json saved to disk with data:',JSON.stringify(user))
}) })
whenReady() whenReady()
} }
@ -306,15 +301,13 @@ function sendMsg(name,str){
function connect(){ function connect(){
client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){ client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){
if(!app.authKey){ if(!app.authKey){
log('Creating authkey...') log('Downloading Authorization Key...')
client.createAuthKey(function(auth){ client.createAuthKey(function(auth){
authKey = auth.key.encrypt('password') // I know sorry, but I'm testing. Will add security later, I promise authKey = auth.key.encrypt('password') // I know sorry, but I'm testing. Will add security later, I promise
log('Created key')
// Writes the new encrypted key to disk // Writes the new encrypted key to disk
log('ready for phone number, use command: phone <number>') log('Ready for phone number, use command: phone <number>')
}) })
} else { } else {
log('Authkey loaded from disk. Should be ready to go.')
whenReady() whenReady()
} }
}) })
@ -341,7 +334,8 @@ function downloadData(){
}) })
client.messages.getDialogs(0,0,10,function(dialogs){ client.messages.getDialogs(0,0,10,function(dialogs){
dialogs.chats.list.forEach(addGroup) if(dialogs && dialogs.chats && dialogs.chats.list)
dialogs.chats.list.forEach(addGroup)
}) })
client.updates.getState(function(astate){ client.updates.getState(function(astate){
@ -416,14 +410,18 @@ function downloadUpdates(){
}) })
} }
function nameForUser(u){
return u.first_name + ' ' + u.last_name + (u.username?' (@'+u.username+')':'')
}
function getName(id,type){ function getName(id,type){
if(type === undefined) throw new Error('no type') if(id === user.id) return nameForUser(user)
if(type === 'group' && groups[id]) else if(type === undefined) throw new Error('no type')
else if(type === 'group' && groups[id])
return groups[id].title return groups[id].title
else if(type === 'user' && contacts[id]){ else if(type === 'user' && contacts[id])
var u = contacts[id].user return nameForUser(contacts[id].user)
return u.first_name + ' ' + u.last_name + (u.username?' (@'+u.username+')':'') else log('Failed to find name for:',id)
} else log('Failed to find name for:',id)
} }
// Get message history with given name in the given box // Get message history with given name in the given box
@ -513,24 +511,24 @@ function appendMsg(msg,toBoxId,bare,smartmode){
// - Entry Point - // - Entry Point -
// Load authKey and userdata from disk, then act depending on outcome // Load authKey and userdata from disk, then act depending on outcome
var keyPath = cfgDir+'key' var keyPath = cfgDir+'key'
log('Checking disk for key...') log('Loading files...')
fs.exists(keyPath,function(exists){ fs.exists(keyPath,function(exists){
if(exists){ if(exists){
log('Key found') //log('Authorization Key found')
fs.readFile(keyPath,function(err,content){ fs.readFile(keyPath,function(err,content){
if(err) if(err)
log('Error while reading key:',err) log('Error while reading key:',err)
else { else {
authKey = telegramLink.retrieveAuthKey(content,'password') // yeah sorry just testing authKey = telegramLink.retrieveAuthKey(content,'password') // yeah sorry just testing
app.authKey = authKey app.authKey = authKey
log('Key loaded') log('Authorization Key found')
fs.readFile(cfgDir+'user_data.json',function(err,data){ fs.readFile(cfgDir+'user_data.json',function(err,data){
if(err) if(err)
log("FATAL: couldn't read user_data.json") log("FATAL: couldn't read user_data.json")
else { else {
try { try {
log("Got User Data from disk: ",data)
user = JSON.parse(data) user = JSON.parse(data)
log('Welcome',getName(user.id,'user'))
connect() connect()
} catch (e) { } catch (e) {
log("FATAL: user data corrupted:",e) log("FATAL: user data corrupted:",e)
@ -540,7 +538,6 @@ fs.exists(keyPath,function(exists){
} }
}) })
} else { } else {
log('Key not found')
connect() connect()
} }
}) })