commit 825d1a28e8789f69ae1d47c8cafdad0130878c0a Author: Enrico Fasoli Date: Sat Aug 29 00:38:59 2015 +0000 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e9b861 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Telecommander + +The experimental full-featured command line client for Telegram is in development. + +It uses [enricostara](http://github.com/enricostara)'s [telegram.link](http://github.com/enricostara/telegram.link) to connect to Telegram and [chjj](http://github.com/chjj)'s [blessed](http://github.com/chjj/blessed) for the UI. + +At the moment you can use Telecommander to register or login into telegram. Yep, that's it for now, but stay tuned! + +### License + +The MIT License (MIT) + +Copyright (c) 2015 Enrico Fasoli (fazo96) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..bcb1cd4 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "telecommander", + "version": "0.1.0", + "description": "command line client for telegram", + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/fazo96/telecommander.git" + }, + "author": "Enrico Fasoli (fazo96)", + "license": "MIT", + "bugs": { + "url": "https://github.com/fazo96/telecommander/issues" + }, + "homepage": "https://github.com/fazo96/telecommander#readme", + "dependencies": { + "blessed": "^0.1.80", + "telegram.link": "^0.6.3" + } +} diff --git a/telecommander.js b/telecommander.js new file mode 100755 index 0000000..918992c --- /dev/null +++ b/telecommander.js @@ -0,0 +1,209 @@ +#!/usr/bin/env node + +var cfgDir = (process.env.HOME || process.env.USERPROFILE) + '/.config/telecommander/' +process.env.LOGGER_FILE = cfgDir+'log' + +var os = require('os') +var fs = require('fs') +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')() + +var screen = blessed.screen({ + smartCSR: true, + dockBorders: true +}) +screen.title = "Telecommander" + +var defaultStyle = { + fg: 'white', + border: { fg: 'gray' }, +} + +var chats = blessed.list({ + left: 0, + top:0, + height: screen.height-3, + width: '20%', + border: { type: 'line' }, + mouse: true, + style: defaultStyle, +}) +chats.style.selected = { bg: 'black' } + +var msgs = blessed.log({ + right: 0, + width: '80%', + height: screen.height-3, + border: { type: 'line' }, + style: defaultStyle +}) + +var cmdline = blessed.textbox({ + inputOnFocus: true, + bottom: 0, + left: 'center', + width: '100%', + height: 3, + border: { type: 'line' }, + style: defaultStyle +}) + +screen.append(chats); +screen.append(msgs); +screen.append(cmdline); + +var msgBox = { } +var contacts = { } + +var client,phone,code,phoneCodeHash,fullName,username,loginResult,uid,authKey +var registered = false + +function command(cmd){ + cmdname = cmd.split(' ')[0] + + if(cmdname === 'phone'){ + phone = cmd.split(' ')[1] + client.auth.sendCode(phone,5,'en',function(result){ + log('Errors:',result.error_code,result.error_message) + if(result.error_code) return + log('Res:',JSON.stringify(result)) + registered = result.phone_registered + phoneCodeHash = result.phone_code_hash + if(!registered){ + log("Your number is not registered. The client will register your account with the Telegram service") + log('Ready for phone code, use command: "code " to register') + } else { + log("Your number is already registered with telegram. The client will log in.") + log('Ready for phone code, use command: "code " to login') + } + }) + + } else if(cmdname === 'code'){ + cmdl = cmd.split(' ') + code = cmdl[1] + name = cmdl[2] + lastname = cmdl[3] + if(((!name || !lastname) && !registered) || !code) + return log('insufficient arguments:',cmd) + cb = function(result){ + loginResult = result + log('Result:',JSON.stringify(result)) + whenReady() + } + // Log in finally + if(registered) client.auth.signIn(phone,phoneCodeHash,code,cb) + else client.auth.signUp(phone,phoneCodeHash,code,name,lastname,cb) + + } else if(cmdname === 'init'){ + whenReady() + } +} + +cmdline.on('submit',function(value){ + msgs.add('[ECHO] '+value) + command(value) + cmdline.clearValue() + cmdline.focus() +}) + +cmdline.focus() +cmdline.key(['escape','C-c'], function(ch,key){ + if(client){ + log('Closing communications and shutting down...') + client.end(function(){ + process.exit(0) + }) + } else process.exit(0); +}); +screen.render(); + +function log(){ + args = Array.prototype.slice.call(arguments) + msg = args.join(' ') + msgs.add(msg) + logger.info(msg) +} + +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 + // Writes the new encrypted key to disk + fs.writeFile(keyPath,authKey,function(err){ + if(err) + log('Could not write key to disk:',err) + else + log('Done writing key to disk') + }) + log('Created key, writing it to disk') + log('ready for phone number, use command: phone ') + }) + } else { + log('Authkey loaded from disk. Should be ready to go.') + log('Authkey:',app.authKey) + whenReady() + } + }) + + client.once('dataCenter',function(dcs){ + log('Datacenters:',dcs.toPrintable()) + }) +} + +// Download Contacts, ecc +function whenReady(){ + log('READY!') + downloadData() +} + +function downloadData(){ + log('Downloading data...') + client.contacts.getContacts('',function(cont){ + contacts = cont + log('\nContacts:\n',JSON.stringify(cont)+'\n') + }) + client.messages.getDialogs(0,-1,10,function(dialogs){ + log('\nDialogs:\n',JSON.stringify(dialogs)+'\n') + }) +} + +var keyPath = cfgDir+'key' +// Try loading 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') + } + connect() + }) + } else { + log('Key not found') + connect() + } +})