WXUserReader.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 = ? or alias = ?";
  47. return con.Query<WXContact>(query, name, name);
  48. }
  49. return con.Query<WXContact>(query);
  50. }
  51. public List<WXMsg>? GetWXMsgs(string uid,string msg = "")
  52. {
  53. List<WXMsg> tmp = new List<WXMsg>();
  54. for (int i = 0; i <= 99; i++)
  55. {
  56. if(DBInfo.ContainsKey("MSG" + i.ToString()))
  57. {
  58. SQLiteConnection con = DBInfo["MSG" + i.ToString()];
  59. if (con == null)
  60. return tmp;
  61. List<WXMsg>? wXMsgs = null;
  62. if (msg == "")
  63. {
  64. string query = "select * from MSG where StrTalker=?";
  65. wXMsgs = con.Query<WXMsg>(query, uid);
  66. }
  67. else if(uid == "")
  68. {
  69. string query = "select * from MSG where StrContent like ?";
  70. wXMsgs = con.Query<WXMsg>(query, string.Format("%{0}%", msg));
  71. }
  72. else
  73. {
  74. string query = "select * from MSG where StrTalker=? and StrContent like ?";
  75. wXMsgs = con.Query<WXMsg>(query, uid, string.Format("%{0}%", msg));
  76. }
  77. foreach (WXMsg w in wXMsgs)
  78. {
  79. tmp.Add(w);
  80. }
  81. }
  82. }
  83. return tmp;
  84. }
  85. public WXSessionAttachInfo? GetWXMsgAtc(WXMsg msg)
  86. {
  87. SQLiteConnection con = DBInfo["MultiSearchChatMsg"];
  88. if (con == null)
  89. return null;
  90. string query = "select * from SessionAttachInfo where msgId = ? order by attachsize desc";
  91. List<WXSessionAttachInfo> list = con.Query<WXSessionAttachInfo>(query, msg.MsgSvrID);
  92. if (list.Count != 0)
  93. {
  94. //部分附件可能有md5校验,这里移除校验,给的是正确路径
  95. WXSessionAttachInfo acc = list[0];
  96. int index = acc.attachPath.IndexOf(".dat");
  97. int index2 = acc.attachPath.IndexOf(".dat");
  98. if (acc.attachPath.Length - index > 10 && index != -1)
  99. {
  100. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  101. }
  102. if (acc.attachPath.Length - index2 > 10 && index2 != -1)
  103. {
  104. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  105. }
  106. return acc;
  107. }
  108. else
  109. return null;
  110. }
  111. public WXMediaMsg? GetVoiceMsg(WXMsg msg)
  112. {
  113. for (int i = 0; i <= 99; i++)
  114. {
  115. if(DBInfo.ContainsKey("MediaMSG" + i.ToString()))
  116. {
  117. SQLiteConnection con = DBInfo["MediaMSG" + i.ToString()];
  118. if (con == null)
  119. continue;
  120. string query = "select * from Media where Reserved0=?";
  121. List<WXMediaMsg> wXMsgs = con.Query<WXMediaMsg>(query, msg.MsgSvrID);
  122. if (wXMsgs.Count != 0)
  123. return wXMsgs[0];
  124. }
  125. }
  126. return null;
  127. }
  128. public string? GetAttachment(WXMsgType type, WXMsg msg)
  129. {
  130. if (UserBakConfig == null)
  131. return null;
  132. string? tmpPath = Path.Combine(UserBakConfig.UserWorkspacePath, "Temp");
  133. if (!Directory.Exists(tmpPath))
  134. Directory.CreateDirectory(tmpPath);
  135. // 如果是图片和视频,从附件库中搜索
  136. string? path = null;
  137. if (type == WXMsgType.Image || type == WXMsgType.Video)
  138. {
  139. WXSessionAttachInfo? atcInfo = GetWXMsgAtc(msg);
  140. if (atcInfo == null)
  141. return null;
  142. path = atcInfo.attachPath;
  143. }
  144. // 如果是从语音,从媒体库查找
  145. else if (type == WXMsgType.Audio)
  146. {
  147. WXMediaMsg? voiceMsg = GetVoiceMsg(msg);
  148. if (voiceMsg == null)
  149. return null;
  150. if (voiceMsg.Buf == null)
  151. return null;
  152. // 从DB取音频文件到临时目录
  153. string tmp_file_path = Path.Combine(tmpPath, voiceMsg.Key + ".arm");
  154. using (FileStream stream = new FileStream(tmp_file_path, FileMode.OpenOrCreate))
  155. {
  156. stream.Write(voiceMsg.Buf, 0, voiceMsg.Buf.Length);
  157. }
  158. path = tmp_file_path;
  159. }
  160. if (path == null)
  161. return null;
  162. // 获取到原路径后,开始进行解密转移,只有图片和语音需要解密,解密后是直接归档目录
  163. if(type == WXMsgType.Image || type== WXMsgType.Audio)
  164. {
  165. path = DecryptAttachment(type, path);
  166. }
  167. else if (type == WXMsgType.Video)
  168. {
  169. string video_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Video");
  170. if(!Directory.Exists(video_dir))
  171. Directory.CreateDirectory(video_dir);
  172. FileInfo fileInfo = new FileInfo(path);
  173. string video_file_path = Path.Combine(video_dir, fileInfo.Name);
  174. // 视频的路径是相对路径,需要加上资源目录
  175. path = Path.Combine(UserBakConfig.UserResPath, path);
  176. if(!File.Exists(video_file_path))
  177. File.Copy(path, video_file_path);
  178. path = video_file_path;
  179. }
  180. if (path == null)
  181. return null;
  182. // 改相对路径
  183. path = path.Replace(UserBakConfig.UserWorkspacePath + "\\", "");
  184. return path;
  185. }
  186. public string? DecryptAttachment(WXMsgType type, string path)
  187. {
  188. if (UserBakConfig == null)
  189. return null;
  190. string? file_path = null;
  191. switch (type)
  192. {
  193. case WXMsgType.Image:
  194. string img_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Image");
  195. if (!Directory.Exists(img_dir))
  196. Directory.CreateDirectory(img_dir);
  197. // 图片的路径是相对路径,需要加上资源目录
  198. path = Path.Combine(UserBakConfig.UserResPath, path);
  199. byte[] decFileByte = DecryptionHelper.DecImage(path);
  200. string decFiletype = DecryptionHelper.CheckFileType(decFileByte);
  201. file_path = DecryptionHelper.SaveDecImage(decFileByte, path, img_dir, decFiletype);
  202. break;
  203. case WXMsgType.Audio:
  204. string audio_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Audio");
  205. if (!Directory.Exists(audio_dir))
  206. Directory.CreateDirectory(audio_dir);
  207. FileInfo fileInfo = new FileInfo(path);
  208. string audio_file_dir = Path.Combine(audio_dir, fileInfo.Name + ".mp3");
  209. ToolsHelper.DecodeVoice(path, path + ".pcm", audio_file_dir);
  210. file_path = audio_file_dir;
  211. break;
  212. }
  213. return file_path;
  214. }
  215. public List<WXMsgGroup> GetWXMsgGroup()
  216. {
  217. List<WXMsgGroup> g = new List<WXMsgGroup>();
  218. for (int i = 0; i <= 99; i++)
  219. {
  220. if (DBInfo.ContainsKey("MSG" + i.ToString()))
  221. {
  222. SQLiteConnection con = DBInfo["MSG" + i.ToString()];
  223. if (con == null)
  224. return g;
  225. string query = "select StrTalker,Count(localId) as MsgCount from MSG GROUP BY StrTalker";
  226. List<WXMsgGroup> wXMsgs = con.Query<WXMsgGroup>(query);
  227. foreach (WXMsgGroup w in wXMsgs)
  228. {
  229. WXMsgGroup? tmp = g.Find(x => x.UserName == w.UserName);
  230. if (tmp == null)
  231. g.Add(w);
  232. else
  233. tmp.MsgCount += g.Count;
  234. }
  235. }
  236. }
  237. return g;
  238. }
  239. }
  240. public enum WXMsgType
  241. {
  242. Image = 0,
  243. Video = 1,
  244. Audio = 2,
  245. File = 3,
  246. }
  247. }