WXUserReader.cs 12 KB

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