1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

Merge branch 'master' into feature-json

This commit is contained in:
Fabian Schlenz 2017-02-23 06:16:45 +01:00
commit 0cbdaed639
8 changed files with 66 additions and 27 deletions

View File

@ -30,6 +30,8 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.Vector; import java.util.Vector;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -38,7 +40,6 @@ public class CommandLineController {
private static Logger logger = LoggerFactory.getLogger(CommandLineController.class); private static Logger logger = LoggerFactory.getLogger(CommandLineController.class);
private ApiStorage storage; private ApiStorage storage;
public TelegramApp app; public TelegramApp app;
public UserManager user = null;
public CommandLineController() { public CommandLineController() {
logger.info("CommandLineController started. App version {}", Config.APP_APPVER); logger.info("CommandLineController started. App version {}", Config.APP_APPVER);
@ -77,8 +78,11 @@ public class CommandLineController {
TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler); TelegramClient client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler);
try { try {
logger.info("Creating UserManager"); logger.info("Initializing UserManager");
user = new UserManager(client); UserManager.init(client);
Database.init(client);
UserManager user = UserManager.getInstance();
if (!CommandLineOptions.cmd_login && !user.isLoggedIn()) { if (!CommandLineOptions.cmd_login && !user.isLoggedIn()) {
System.out.println("Your authorization data is invalid or missing. You will have to login with Telegram again."); System.out.println("Your authorization data is invalid or missing. You will have to login with Telegram again.");
@ -91,12 +95,15 @@ public class CommandLineController {
} }
} }
if (CommandLineOptions.cmd_stats) {
cmd_stats();
System.exit(0);
}
logger.debug("CommandLineOptions.val_export: {}", CommandLineOptions.val_export); logger.debug("CommandLineOptions.val_export: {}", CommandLineOptions.val_export);
if (CommandLineOptions.val_export != null) { if (CommandLineOptions.val_export != null) {
if (CommandLineOptions.val_export.toLowerCase().equals("html")) { if (CommandLineOptions.val_export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user); (new HTMLExporter()).export();
System.exit(0); System.exit(0);
} else { } else {
show_error("Unknown export format."); show_error("Unknown export format.");
@ -117,7 +124,6 @@ public class CommandLineController {
} }
logger.info("Initializing Download Manager"); logger.info("Initializing Download Manager");
DownloadManager d = new DownloadManager(user, client, new CommandLineDownloadProgress());
if (CommandLineOptions.val_test != null) { if (CommandLineOptions.val_test != null) {
if (CommandLineOptions.val_test == 1) { if (CommandLineOptions.val_test == 1) {
@ -132,6 +138,7 @@ public class CommandLineController {
System.exit(1); System.exit(1);
} }
DownloadManager d = new DownloadManager(client, new CommandLineDownloadProgress());
logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages); logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages);
d.downloadMessages(CommandLineOptions.val_limit_messages); d.downloadMessages(CommandLineOptions.val_limit_messages);
@ -147,7 +154,7 @@ public class CommandLineController {
logger.error("Exception caught!", e); logger.error("Exception caught!", e);
} finally { } finally {
if (CommandLineOptions.cmd_daemon) { if (CommandLineOptions.cmd_daemon) {
handler.setUser(user, client); handler.activate();
System.out.println("DAEMON mode requested - keeping running."); System.out.println("DAEMON mode requested - keeping running.");
} else { } else {
client.close(); client.close();
@ -159,7 +166,7 @@ public class CommandLineController {
} }
private void printHeader() { private void printHeader() {
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz"); System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016, 2017 Fabian Schlenz");
System.out.println(); System.out.println();
System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are"); System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are");
System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details."); System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details.");
@ -214,7 +221,20 @@ public class CommandLineController {
return account; return account;
} }
private void cmd_stats() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("count.accounts", Utils.getAccounts().size());
map.put("count.messages", Database.getInstance().getMessageCount());
map.put("messages.top_id", Database.getInstance().getTopMessageID());
for(Map.Entry<String, Integer> pair : Database.getInstance().getMessageMediaTypesWithCount().entrySet()) {
map.put(pair.getKey(), pair.getValue());
}
System.out.println(map.toString());
}
private void cmd_login(String phone) throws RpcErrorException, IOException { private void cmd_login(String phone) throws RpcErrorException, IOException {
UserManager user = UserManager.getInstance();
if (phone==null) { if (phone==null) {
System.out.println("Please enter your phone number in international format."); System.out.println("Please enter your phone number in international format.");
System.out.println("Example: +4917077651234"); System.out.println("Example: +4917077651234");
@ -270,6 +290,7 @@ public class CommandLineController {
System.out.println(" --license Displays the license of this program."); System.out.println(" --license Displays the license of this program.");
System.out.println(" -d, --daemon Keep running and automatically save new messages."); System.out.println(" -d, --daemon Keep running and automatically save new messages.");
System.out.println(" --anonymize (Try to) Remove all sensitive information from output. Useful for requesting support."); System.out.println(" --anonymize (Try to) Remove all sensitive information from output. Useful for requesting support.");
System.out.println(" --stats Print some usage statistics.");
} }
private void list_accounts() { private void list_accounts() {

View File

@ -29,6 +29,7 @@ class CommandLineOptions {
public static boolean cmd_daemon = false; public static boolean cmd_daemon = false;
public static boolean cmd_no_media = false; public static boolean cmd_no_media = false;
public static boolean cmd_anonymize = false; public static boolean cmd_anonymize = false;
public static boolean cmd_stats = false;
public static String val_account = null; public static String val_account = null;
public static Integer val_limit_messages = null; public static Integer val_limit_messages = null;
@ -113,6 +114,9 @@ class CommandLineOptions {
case "--anonymize": case "--anonymize":
cmd_anonymize = true; break; cmd_anonymize = true; break;
case "--stats":
cmd_stats = true; break;
default: default:
throw new RuntimeException("Unknown command " + arg); throw new RuntimeException("Unknown command " + arg);
} }

View File

@ -54,13 +54,10 @@ public class Database {
public UserManager user_manager; public UserManager user_manager;
public TelegramClient client; public TelegramClient client;
private final static Logger logger = LoggerFactory.getLogger(Database.class); private final static Logger logger = LoggerFactory.getLogger(Database.class);
private static Database instance = null;
public Database(UserManager user_manager, TelegramClient client) { private Database(TelegramClient client) {
this(user_manager, client, true); this.user_manager = UserManager.getInstance();
}
public Database(UserManager user_manager, TelegramClient client, boolean update_db) {
this.user_manager = user_manager;
this.client = client; this.client = client;
System.out.println("Opening database..."); System.out.println("Opening database...");
try { try {
@ -80,14 +77,20 @@ public class Database {
CommandLineController.show_error("Could not connect to SQLITE database."); CommandLineController.show_error("Could not connect to SQLITE database.");
} }
this.init(update_db); // Run updates
DatabaseUpdates updates = new DatabaseUpdates(conn, this);
updates.doUpdates();
System.out.println("Database is ready."); System.out.println("Database is ready.");
} }
private void init(boolean update_db) { public static void init(TelegramClient c) {
if (!update_db) return; instance = new Database(c);
DatabaseUpdates updates = new DatabaseUpdates(conn, this); }
updates.doUpdates();
public static Database getInstance() {
if (instance == null) throw new RuntimeException("Database is not initialized but getInstance() was called.");
return instance;
} }
public void backupDatabase(int currentVersion) { public void backupDatabase(int currentVersion) {

View File

@ -62,11 +62,11 @@ public class DownloadManager {
static boolean last_download_succeeded = true; static boolean last_download_succeeded = true;
static final Logger logger = LoggerFactory.getLogger(DownloadManager.class); static final Logger logger = LoggerFactory.getLogger(DownloadManager.class);
public DownloadManager(UserManager u, TelegramClient c, DownloadProgressInterface p) { public DownloadManager(TelegramClient c, DownloadProgressInterface p) {
this.user = u; this.user = UserManager.getInstance();
this.client = c; this.client = c;
this.prog = p; this.prog = p;
this.db = new Database(u, c); this.db = Database.getInstance();
} }
public void downloadMessages(Integer limit) throws RpcErrorException, IOException { public void downloadMessages(Integer limit) throws RpcErrorException, IOException {

View File

@ -36,7 +36,7 @@ class TelegramUpdateHandler implements UpdateCallback {
public boolean debug = false; public boolean debug = false;
private Gson gson = Utils.getGson(); private Gson gson = Utils.getGson();
public void setUser(UserManager user, TelegramClient client) { this.user = user; this.db = new Database(user, client, false);} public void activate() { this.user = UserManager.getInstance(); this.db = Database.getInstance();}
public void onUpdates(TelegramClient c, TLUpdates u) { public void onUpdates(TelegramClient c, TLUpdates u) {
if (db==null) return; if (db==null) return;

View File

@ -58,7 +58,7 @@ class TestFeatures {
// Prints system.encoding and default charset // Prints system.encoding and default charset
System.out.println("Default Charset: " + Charset.defaultCharset()); System.out.println("Default Charset: " + Charset.defaultCharset());
System.out.println("file.encoding: " + System.getProperty("file.encoding")); System.out.println("file.encoding: " + System.getProperty("file.encoding"));
Database db = new Database(user, client, false); Database db = Database.getInstance();
System.out.println("Database encoding: " + db.getEncoding()); System.out.println("Database encoding: " + db.getEncoding());
} }
} }

View File

@ -43,8 +43,13 @@ public class UserManager {
private TLAuthorization auth = null; private TLAuthorization auth = null;
private boolean password_needed = false; private boolean password_needed = false;
private static Logger logger = LoggerFactory.getLogger(UserManager.class); private static Logger logger = LoggerFactory.getLogger(UserManager.class);
private static UserManager instance = null;
public UserManager(TelegramClient c) throws IOException { public static void init(TelegramClient c) throws IOException {
instance = new UserManager(c);
}
private UserManager(TelegramClient c) throws IOException {
this.client = c; this.client = c;
logger.debug("Calling getFullUser"); logger.debug("Calling getFullUser");
try { try {
@ -56,6 +61,11 @@ public class UserManager {
} }
} }
public static UserManager getInstance() {
if (instance==null) throw new RuntimeException("UserManager is not yet initialized.");
return instance;
}
public boolean isLoggedIn() { return user!=null; } public boolean isLoggedIn() { return user!=null; }
public void sendCodeToPhoneNumber(String number) throws RpcErrorException, IOException { public void sendCodeToPhoneNumber(String number) throws RpcErrorException, IOException {

View File

@ -45,9 +45,10 @@ import org.slf4j.LoggerFactory;
public class HTMLExporter { public class HTMLExporter {
private static Logger logger = LoggerFactory.getLogger(HTMLExporter.class); private static Logger logger = LoggerFactory.getLogger(HTMLExporter.class);
public void export(UserManager user) throws IOException { public void export() throws IOException {
try { try {
Database db = new Database(user, null); UserManager user = UserManager.getInstance();
Database db = Database.getInstance();
// Create base dir // Create base dir
logger.debug("Creating base dir"); logger.debug("Creating base dir");