2
0

WXUserReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using SQLite;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Interop;
  11. using System.Xml.Linq;
  12. using WechatPCMsgBakTool.Helpers;
  13. using WechatPCMsgBakTool.Model;
  14. namespace WechatPCMsgBakTool
  15. {
  16. public class WXUserReader
  17. {
  18. private Dictionary<string, SQLiteConnection> DBInfo = new Dictionary<string, SQLiteConnection>();
  19. private UserBakConfig? UserBakConfig = null;
  20. public WXUserReader(UserBakConfig userBakConfig) {
  21. string path = Path.Combine(userBakConfig.UserWorkspacePath, "DecDB");
  22. UserBakConfig = userBakConfig;
  23. LoadDB(path);
  24. }
  25. public void LoadDB(string path)
  26. {
  27. string[] dbFileList = Directory.GetFiles(path);
  28. foreach (var item in dbFileList)
  29. {
  30. FileInfo fileInfo = new FileInfo(item);
  31. if (fileInfo.Extension != ".db")
  32. continue;
  33. SQLiteConnection con = new SQLiteConnection(item);
  34. string dbName = fileInfo.Name.Split('.')[0];
  35. DBInfo.Add(dbName, con);
  36. }
  37. }
  38. public List<WXContact>? GetWXContacts(string? name = null)
  39. {
  40. SQLiteConnection con = DBInfo["MicroMsg"];
  41. if (con == null)
  42. return null;
  43. string query = "select * from contact";
  44. if (name != null)
  45. {
  46. query = "select * from contact where username like ? or alias like ? or nickname like ? or remark like ?";
  47. return con.Query<WXContact>(query, $"%{name}%", $"%{name}%", $"%{name}%", $"%{name}%");
  48. }
  49. return con.Query<WXContact>(query);
  50. }
  51. public List<WXChatRoom>? GetWXChatRooms()
  52. {
  53. SQLiteConnection con = DBInfo["MicroMsg"];
  54. if (con == null)
  55. return null;
  56. string query = "select * from ChatRoom";
  57. return con.Query<WXChatRoom>(query);
  58. }
  59. public List<WXMsg>? GetWXMsgs(string uid,string msg = "")
  60. {
  61. List<WXMsg> tmp = new List<WXMsg>();
  62. for (int i = 0; i <= 99; i++)
  63. {
  64. if(DBInfo.ContainsKey("MSG" + i.ToString()))
  65. {
  66. SQLiteConnection con = DBInfo["MSG" + i.ToString()];
  67. if (con == null)
  68. return tmp;
  69. List<WXMsg>? wXMsgs = null;
  70. if (msg == "")
  71. {
  72. string query = "select * from MSG where StrTalker=?";
  73. wXMsgs = con.Query<WXMsg>(query, uid);
  74. }
  75. else if(uid == "")
  76. {
  77. string query = "select * from MSG where StrContent like ?";
  78. wXMsgs = con.Query<WXMsg>(query, string.Format("%{0}%", msg));
  79. }
  80. else
  81. {
  82. string query = "select * from MSG where StrTalker=? and StrContent like ?";
  83. wXMsgs = con.Query<WXMsg>(query, uid, string.Format("%{0}%", msg));
  84. }
  85. foreach (WXMsg w in wXMsgs)
  86. {
  87. tmp.Add(w);
  88. }
  89. }
  90. }
  91. return tmp;
  92. }
  93. public List<WXSessionAttachInfo>? GetWXMsgAtc()
  94. {
  95. SQLiteConnection con = DBInfo["MultiSearchChatMsg"];
  96. if (con == null)
  97. return null;
  98. string query = "select * from SessionAttachInfo";
  99. List<WXSessionAttachInfo> list = con.Query<WXSessionAttachInfo>(query);
  100. if (list.Count != 0)
  101. {
  102. return list;
  103. }
  104. else
  105. return null;
  106. }
  107. public WXSessionAttachInfo? GetWXMsgAtc(WXMsg msg)
  108. {
  109. SQLiteConnection con = DBInfo["MultiSearchChatMsg"];
  110. if (con == null)
  111. return null;
  112. string query = "select * from SessionAttachInfo where msgId = ? order by attachsize desc";
  113. List<WXSessionAttachInfo> list = con.Query<WXSessionAttachInfo>(query, msg.MsgSvrID);
  114. if (list.Count != 0)
  115. {
  116. //部分附件可能有md5校验,这里移除校验,给的是正确路径
  117. WXSessionAttachInfo acc = list[0];
  118. int index = acc.attachPath.IndexOf(".dat");
  119. int index2 = acc.attachPath.IndexOf(".dat");
  120. if (acc.attachPath.Length - index > 10 && index != -1)
  121. {
  122. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  123. }
  124. if (acc.attachPath.Length - index2 > 10 && index2 != -1)
  125. {
  126. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  127. }
  128. return acc;
  129. }
  130. else
  131. return null;
  132. }
  133. public WXMediaMsg? GetVoiceMsg(WXMsg msg)
  134. {
  135. for (int i = 0; i <= 99; i++)
  136. {
  137. if(DBInfo.ContainsKey("MediaMSG" + i.ToString()))
  138. {
  139. SQLiteConnection con = DBInfo["MediaMSG" + i.ToString()];
  140. if (con == null)
  141. continue;
  142. string query = "select * from Media where Reserved0=?";
  143. List<WXMediaMsg> wXMsgs = con.Query<WXMediaMsg>(query, msg.MsgSvrID);
  144. if (wXMsgs.Count != 0)
  145. return wXMsgs[0];
  146. }
  147. }
  148. return null;
  149. }
  150. public string? GetAttachment(WXMsgType type, WXMsg msg)
  151. {
  152. if (UserBakConfig == null)
  153. return null;
  154. string? tmpPath = Path.Combine(UserBakConfig.UserWorkspacePath, "Temp");
  155. if (!Directory.Exists(tmpPath))
  156. Directory.CreateDirectory(tmpPath);
  157. // 如果是图片和视频,从附件库中搜索
  158. string? path = null;
  159. if (type == WXMsgType.Image || type == WXMsgType.Video)
  160. {
  161. WXSessionAttachInfo? atcInfo = GetWXMsgAtc(msg);
  162. if (atcInfo == null)
  163. return null;
  164. path = atcInfo.attachPath;
  165. }
  166. // 如果是从语音,从媒体库查找
  167. else if (type == WXMsgType.Audio)
  168. {
  169. WXMediaMsg? voiceMsg = GetVoiceMsg(msg);
  170. if (voiceMsg == null)
  171. return null;
  172. if (voiceMsg.Buf == null)
  173. return null;
  174. // 从DB取音频文件到临时目录
  175. string tmp_file_path = Path.Combine(tmpPath, voiceMsg.Key + ".arm");
  176. using (FileStream stream = new FileStream(tmp_file_path, FileMode.OpenOrCreate))
  177. {
  178. stream.Write(voiceMsg.Buf, 0, voiceMsg.Buf.Length);
  179. }
  180. path = tmp_file_path;
  181. }
  182. if (path == null)
  183. return null;
  184. // 获取到原路径后,开始进行解密转移,只有图片和语音需要解密,解密后是直接归档目录
  185. if(type == WXMsgType.Image || type== WXMsgType.Audio)
  186. {
  187. path = DecryptAttachment(type, path);
  188. }
  189. else if (type == WXMsgType.Video)
  190. {
  191. string video_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Video");
  192. if(!Directory.Exists(video_dir))
  193. Directory.CreateDirectory(video_dir);
  194. FileInfo fileInfo = new FileInfo(path);
  195. string video_file_path = Path.Combine(video_dir, fileInfo.Name);
  196. // 视频的路径是相对路径,需要加上资源目录
  197. path = Path.Combine(UserBakConfig.UserResPath, path);
  198. if(!File.Exists(video_file_path))
  199. File.Copy(path, video_file_path);
  200. path = video_file_path;
  201. }
  202. if (path == null)
  203. return null;
  204. // 改相对路径
  205. path = path.Replace(UserBakConfig.UserWorkspacePath + "\\", "");
  206. return path;
  207. }
  208. public string? DecryptAttachment(WXMsgType type, string path)
  209. {
  210. if (UserBakConfig == null)
  211. return null;
  212. string? file_path = null;
  213. switch (type)
  214. {
  215. case WXMsgType.Image:
  216. string img_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Image");
  217. if (!Directory.Exists(img_dir))
  218. Directory.CreateDirectory(img_dir);
  219. // 图片的路径是相对路径,需要加上资源目录
  220. path = Path.Combine(UserBakConfig.UserResPath, path);
  221. byte[] decFileByte = DecryptionHelper.DecImage(path);
  222. string decFiletype = DecryptionHelper.CheckFileType(decFileByte);
  223. file_path = DecryptionHelper.SaveDecImage(decFileByte, path, img_dir, decFiletype);
  224. break;
  225. case WXMsgType.Audio:
  226. string audio_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Audio");
  227. if (!Directory.Exists(audio_dir))
  228. Directory.CreateDirectory(audio_dir);
  229. FileInfo fileInfo = new FileInfo(path);
  230. string audio_file_dir = Path.Combine(audio_dir, fileInfo.Name + ".mp3");
  231. ToolsHelper.DecodeVoice(path, path + ".pcm", audio_file_dir);
  232. file_path = audio_file_dir;
  233. break;
  234. }
  235. return file_path;
  236. }
  237. public List<WXMsgGroup> GetWXMsgGroup()
  238. {
  239. List<WXMsgGroup> g = new List<WXMsgGroup>();
  240. for (int i = 0; i <= 99; i++)
  241. {
  242. if (DBInfo.ContainsKey("MSG" + i.ToString()))
  243. {
  244. SQLiteConnection con = DBInfo["MSG" + i.ToString()];
  245. if (con == null)
  246. return g;
  247. string query = "select StrTalker,Count(localId) as MsgCount from MSG GROUP BY StrTalker";
  248. List<WXMsgGroup> wXMsgs = con.Query<WXMsgGroup>(query);
  249. foreach (WXMsgGroup w in wXMsgs)
  250. {
  251. WXMsgGroup? tmp = g.Find(x => x.UserName == w.UserName);
  252. if (tmp == null)
  253. g.Add(w);
  254. else
  255. tmp.MsgCount += g.Count;
  256. }
  257. }
  258. }
  259. return g;
  260. }
  261. }
  262. public enum WXMsgType
  263. {
  264. Image = 0,
  265. Video = 1,
  266. Audio = 2,
  267. File = 3,
  268. }
  269. }