WXUserReader.cs 11 KB

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