1 Commits

Author SHA1 Message Date
Shen Junzheng
07a15117f3 docs 2025-04-16 18:18:30 +08:00
32 changed files with 760 additions and 2058 deletions

View File

@@ -149,29 +149,17 @@ GET /api/v1/chatlog?time=2023-01-01&talker=wxid_xxx
参数说明:
- `time`: 时间范围,格式为 `YYYY-MM-DD` 或 `YYYY-MM-DD~YYYY-MM-DD`
- `talker`: 聊天对象标识(支持 wxid、群聊 ID、备注名、昵称等
- `limit`: 返回记录数量
- `offset`: 分页偏移量
- `format`: 输出格式,支持 `json`、`csv` 或纯文本
- `limit`: 返回记录数量(默认 100
- `offset`: 分页偏移量(默认 0
- `format`: 输出格式,支持 `json`、`csv` 或纯文本(默认 纯文本)
### 其他 API 接口
- **联系人列表**`GET /api/v1/contact`
- **群聊列表**`GET /api/v1/chatroom`
- **会话列表**`GET /api/v1/session`
- **多媒体内容**`GET /api/v1/media?msgid=xxx`
### 多媒体内容
聊天记录中的多媒体内容会通过 HTTP 服务进行提供,可通过以下路径访问:
- **图片内容**`GET /image/<id>`
- **视频内容**`GET /video/<id>`
- **文件内容**`GET /file/<id>`
- **语音内容**`GET /voice/<id>`
- **多媒体内容**`GET /data/<data dir relative path>`
当请求图片、视频、文件内容时,将返回 302 跳转到多媒体内容 URL。
当请求语音内容时,将直接返回语音内容,并对原始 SILK 语音做了实时转码 MP3 处理。
多媒体内容 URL 地址为基于`数据目录`的相对地址,请求多媒体内容将直接返回对应文件,并针对加密图片做了实时解密处理。
## MCP 集成

View File

@@ -1,43 +0,0 @@
package chatlog
import (
"runtime"
"github.com/sjzar/chatlog/internal/chatlog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(serverCmd)
serverCmd.Flags().StringVarP(&serverAddr, "addr", "a", "127.0.0.1:5030", "server address")
serverCmd.Flags().StringVarP(&serverDataDir, "data-dir", "d", "", "data dir")
serverCmd.Flags().StringVarP(&serverWorkDir, "work-dir", "w", "", "work dir")
serverCmd.Flags().StringVarP(&serverPlatform, "platform", "p", runtime.GOOS, "platform")
serverCmd.Flags().IntVarP(&serverVer, "version", "v", 3, "version")
}
var (
serverAddr string
serverDataDir string
serverWorkDir string
serverPlatform string
serverVer int
)
var serverCmd = &cobra.Command{
Use: "server",
Short: "Start HTTP server",
Run: func(cmd *cobra.Command, args []string) {
m, err := chatlog.New("")
if err != nil {
log.Err(err).Msg("failed to create chatlog instance")
return
}
if err := m.CommandHTTPServer(serverAddr, serverDataDir, serverWorkDir, serverPlatform, serverVer); err != nil {
log.Err(err).Msg("failed to start server")
return
}
},
}

View File

@@ -40,8 +40,8 @@ func (s *Service) GetDB() *wechatdb.DB {
return s.db
}
func (s *Service) GetMessages(start, end time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
return s.db.GetMessages(start, end, talker, sender, keyword, limit, offset)
func (s *Service) GetMessages(start, end time.Time, talker string, limit, offset int) ([]*model.Message, error) {
return s.db.GetMessages(start, end, talker, limit, offset)
}
func (s *Service) GetContacts(key string, limit, offset int) (*wechatdb.GetContactsResp, error) {

View File

@@ -32,10 +32,10 @@ func (s *Service) initRouter() {
router.StaticFileFS("/", "./index.htm", http.FS(staticDir))
// Media
router.GET("/image/*key", s.GetImage)
router.GET("/video/*key", s.GetVideo)
router.GET("/file/*key", s.GetFile)
router.GET("/voice/*key", s.GetVoice)
router.GET("/image/:key", s.GetImage)
router.GET("/video/:key", s.GetVideo)
router.GET("/file/:key", s.GetFile)
router.GET("/voice/:key", s.GetVoice)
router.GET("/data/*path", s.GetMediaData)
// MCP Server
@@ -75,13 +75,11 @@ func (s *Service) NoRoute(c *gin.Context) {
func (s *Service) GetChatlog(c *gin.Context) {
q := struct {
Time string `form:"time"`
Talker string `form:"talker"`
Sender string `form:"sender"`
Keyword string `form:"keyword"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
Time string `form:"time"`
Talker string `form:"talker"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
}{}
if err := c.BindQuery(&q); err != nil {
@@ -102,7 +100,7 @@ func (s *Service) GetChatlog(c *gin.Context) {
q.Offset = 0
}
messages, err := s.db.GetMessages(start, end, q.Talker, q.Sender, q.Keyword, q.Limit, q.Offset)
messages, err := s.db.GetMessages(start, end, q.Talker, q.Limit, q.Offset)
if err != nil {
errors.Err(c, err)
return
@@ -121,7 +119,7 @@ func (s *Service) GetChatlog(c *gin.Context) {
c.Writer.Flush()
for _, m := range messages {
c.Writer.WriteString(m.PlainText(strings.Contains(q.Talker, ","), util.PerfectTimeFormat(start, end), c.Request.Host))
c.Writer.WriteString(m.PlainText(len(q.Talker) == 0, c.Request.Host))
c.Writer.WriteString("\n")
c.Writer.Flush()
}
@@ -131,10 +129,10 @@ func (s *Service) GetChatlog(c *gin.Context) {
func (s *Service) GetContacts(c *gin.Context) {
q := struct {
Keyword string `form:"keyword"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
Key string `form:"key"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
}{}
if err := c.BindQuery(&q); err != nil {
@@ -142,7 +140,7 @@ func (s *Service) GetContacts(c *gin.Context) {
return
}
list, err := s.db.GetContacts(q.Keyword, q.Limit, q.Offset)
list, err := s.db.GetContacts(q.Key, q.Limit, q.Offset)
if err != nil {
errors.Err(c, err)
return
@@ -176,10 +174,10 @@ func (s *Service) GetContacts(c *gin.Context) {
func (s *Service) GetChatRooms(c *gin.Context) {
q := struct {
Keyword string `form:"keyword"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
Key string `form:"key"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
}{}
if err := c.BindQuery(&q); err != nil {
@@ -187,7 +185,7 @@ func (s *Service) GetChatRooms(c *gin.Context) {
return
}
list, err := s.db.GetChatRooms(q.Keyword, q.Limit, q.Offset)
list, err := s.db.GetChatRooms(q.Key, q.Limit, q.Offset)
if err != nil {
errors.Err(c, err)
return
@@ -220,10 +218,10 @@ func (s *Service) GetChatRooms(c *gin.Context) {
func (s *Service) GetSessions(c *gin.Context) {
q := struct {
Keyword string `form:"keyword"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
Key string `form:"key"`
Limit int `form:"limit"`
Offset int `form:"offset"`
Format string `form:"format"`
}{}
if err := c.BindQuery(&q); err != nil {
@@ -231,7 +229,7 @@ func (s *Service) GetSessions(c *gin.Context) {
return
}
sessions, err := s.db.GetSessions(q.Keyword, q.Limit, q.Offset)
sessions, err := s.db.GetSessions(q.Key, q.Limit, q.Offset)
if err != nil {
errors.Err(c, err)
return
@@ -281,51 +279,30 @@ func (s *Service) GetVoice(c *gin.Context) {
}
func (s *Service) GetMedia(c *gin.Context, _type string) {
key := strings.TrimPrefix(c.Param("key"), "/")
key := c.Param("key")
if key == "" {
errors.Err(c, errors.InvalidArg(key))
return
}
keys := util.Str2List(key, ",")
if len(keys) == 0 {
errors.Err(c, errors.InvalidArg(key))
media, err := s.db.GetMedia(_type, key)
if err != nil {
errors.Err(c, err)
return
}
var _err error
for _, k := range keys {
if len(k) != 32 {
absolutePath := filepath.Join(s.ctx.DataDir, k)
if _, err := os.Stat(absolutePath); os.IsNotExist(err) {
continue
}
c.Redirect(http.StatusFound, "/data/"+k)
return
}
media, err := s.db.GetMedia(_type, k)
if err != nil {
_err = err
continue
}
if c.Query("info") != "" {
c.JSON(http.StatusOK, media)
return
}
switch media.Type {
case "voice":
s.HandleVoice(c, media.Data)
return
default:
c.Redirect(http.StatusFound, "/data/"+media.Path)
return
}
}
if _err != nil {
errors.Err(c, _err)
if c.Query("info") != "" {
c.JSON(http.StatusOK, media)
return
}
switch media.Type {
case "voice":
s.HandleVoice(c, media.Data)
default:
c.Redirect(http.StatusFound, "/data/"+media.Path)
}
}
func (s *Service) GetMediaData(c *gin.Context) {
@@ -374,8 +351,7 @@ func (s *Service) HandleDatFile(c *gin.Context, path string) {
case "bmp":
c.Data(http.StatusOK, "image/bmp", out)
default:
c.Data(http.StatusOK, "image/jpg", out)
// c.File(path)
c.File(path)
}
}

View File

@@ -77,21 +77,6 @@ func (s *Service) Start() error {
return nil
}
func (s *Service) ListenAndServe() error {
if s.ctx.HTTPAddr == "" {
s.ctx.HTTPAddr = DefalutHTTPAddr
}
s.server = &http.Server{
Addr: s.ctx.HTTPAddr,
Handler: s.router,
}
log.Info().Msg("Starting HTTP server on " + s.ctx.HTTPAddr)
return s.server.ListenAndServe()
}
func (s *Service) Stop() error {
if s.server == nil {

View File

@@ -1,757 +1,156 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatlog</title>
<style>
:root {
--primary-color: #3498db;
--primary-dark: #2980b9;
--success-color: #2ecc71;
--success-dark: #27ae60;
--error-color: #e74c3c;
--bg-light: #f5f5f5;
--bg-white: #ffffff;
--text-color: #333333;
--border-color: #dddddd;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
line-height: 1.6;
color: var(--text-color);
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #fafafa;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.welcome-text {
text-align: center;
margin-bottom: 30px;
}
.api-section {
background-color: var(--bg-light);
border-radius: 10px;
padding: 25px;
width: 100%;
max-width: 850px;
margin-bottom: 30px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
h1 {
color: #2c3e50;
margin-bottom: 15px;
}
h2 {
color: var(--primary-color);
margin-top: 20px;
border-bottom: 2px solid var(--primary-color);
padding-bottom: 8px;
display: inline-block;
}
h3 {
margin-top: 20px;
color: #34495e;
}
.docs-link {
color: var(--primary-color);
text-decoration: none;
font-weight: bold;
transition: all 0.2s ease;
}
.docs-link:hover {
text-decoration: underline;
color: var(--primary-dark);
}
.api-tester {
background-color: var(--bg-white);
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 25px;
margin-top: 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}
.form-group {
margin-bottom: 18px;
}
label {
display: block;
margin-bottom: 6px;
font-weight: 600;
color: #34495e;
}
input,
select,
textarea {
width: 100%;
padding: 10px 12px;
border: 1px solid #ddd;
border-radius: 6px;
box-sizing: border-box;
font-size: 14px;
transition: all 0.3s;
}
input:focus,
select:focus,
textarea:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}
input::placeholder,
textarea::placeholder {
color: #aaa;
}
button {
background-color: var(--primary-color);
color: white;
border: none;
padding: 12px 18px;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
font-weight: 500;
transition: all 0.3s;
display: inline-flex;
align-items: center;
justify-content: center;
}
button:hover {
background-color: var(--primary-dark);
transform: translateY(-1px);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
button:active {
transform: translateY(0);
}
.result-container {
margin-top: 20px;
border: 1px solid var(--border-color);
border-radius: 6px;
padding: 15px;
background-color: #f9f9f9;
max-height: 400px;
overflow-y: auto;
white-space: pre-wrap;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo,
monospace;
font-size: 14px;
line-height: 1.5;
position: relative;
}
.request-url {
background-color: #f0f0f0;
padding: 10px;
border-radius: 6px;
margin-bottom: 10px;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo,
monospace;
font-size: 14px;
word-break: break-all;
border: 1px dashed #ccc;
display: flex;
justify-content: space-between;
align-items: center;
}
.url-text {
flex-grow: 1;
margin-right: 10px;
}
.copy-url-button {
background-color: #9b59b6;
padding: 6px 12px;
font-size: 12px;
white-space: nowrap;
}
.loading {
text-align: center;
padding: 20px;
color: #666;
}
.loading::after {
content: "...";
animation: dots 1.5s steps(5, end) infinite;
}
@keyframes dots {
0%,
20% {
content: ".";
.random-paragraph {
display: none;
}
40% {
content: "..";
}
60% {
content: "...";
}
80%,
100% {
content: "";
}
}
.tab-container {
display: flex;
margin-bottom: 20px;
border-bottom: 1px solid #e0e0e0;
}
.tab {
padding: 12px 20px;
cursor: pointer;
margin-right: 5px;
border-radius: 6px 6px 0 0;
font-weight: 500;
transition: all 0.2s;
border: 1px solid transparent;
border-bottom: none;
position: relative;
bottom: -1px;
}
.tab:hover {
background-color: #f0f8ff;
}
.tab.active {
background-color: var(--bg-white);
border-color: #e0e0e0;
color: var(--primary-color);
border-bottom: 1px solid white;
}
.tab-content {
display: none;
padding: 20px 0;
}
.tab-content.active {
display: block;
animation: fadeIn 0.3s;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.button-group {
display: flex;
justify-content: flex-end;
margin-top: 20px;
}
.copy-button {
background-color: var(--success-color);
padding: 8px 15px;
font-size: 14px;
margin-left: 10px;
}
.copy-button:hover {
background-color: var(--success-dark);
}
.error-message {
color: var(--error-color);
font-weight: bold;
margin-top: 10px;
padding: 10px;
border-radius: 4px;
background-color: rgba(231, 76, 60, 0.1);
border-left: 4px solid var(--error-color);
display: none;
}
.api-description {
margin-bottom: 15px;
color: #555;
}
.badge {
display: inline-block;
padding: 3px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: 600;
margin-left: 8px;
background-color: rgba(52, 152, 219, 0.1);
color: var(--primary-color);
}
.optional-param {
font-size: 12px;
color: #777;
margin-left: 5px;
font-style: italic;
}
.required-field {
color: var(--error-color);
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="welcome-text">
<h1>🎉 恭喜Chatlog 服务已成功启动</h1>
<p>
Chatlog 是一个帮助你轻松使用自己聊天数据的工具,现在你可以通过 HTTP
API 访问你的聊天记录、联系人和群聊信息。
</p>
</div>
</head>
<body>
<div id="paragraphContainer">
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
('-. .-. ('-. .-') _
( OO ) / ( OO ).-. ( OO) )
.-----. ,--. ,--. / . --. / / '._ ,--. .-'),-----. ,----.
' .--./ | | | | | \-. \ |'--...__) | |.-') ( OO' .-. ' ' .-./-')
| |('-. | .| | .-'-' | | '--. .--' | | OO ) / | | | | | |_( O- )
/_) |OO ) | | \| |_.' | | | | |`-' | \_) | |\| | | | .--, \
|| |`-'| | .-. | | .-. | | | (| '---.' \ | | | |(| | '. (_/
(_' '--'\ | | | | | | | | | | | | `' '-' ' | '--' |
`-----' `--' `--' `--' `--' `--' `------' `-----' `------'
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
_____ _ _ _
/ __ \| | | | | |
| / \/| |__ __ _ | |_ | | ___ __ _
| | | '_ \ / _` || __|| | / _ \ / _` |
| \__/\| | | || (_| || |_ | || (_) || (_| |
\____/|_| |_| \__,_| \__||_| \___/ \__, |
__/ |
|___/
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
<div class="api-section">
<h2>🔍 API 接口与调试</h2>
,-----. ,--. ,--. ,--.
' .--./ | ,---. ,--,--. ,-' '-. | | ,---. ,---.
| | | .-. | ' ,-. | '-. .-' | | | .-. | | .-. |
' '--'\ | | | | \ '-' | | | | | ' '-' ' ' '-' '
`-----' `--' `--' `--`--' `--' `--' `---' .`- /
`---'
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
____ _ _ _
/ ___| | |__ __ _ | |_ | | ___ __ _
| | | '_ \ / _` | | __| | | / _ \ / _` |
| |___ | | | | | (_| | | |_ | | | (_) | | (_| |
\____| |_| |_| \__,_| \__| |_| \___/ \__, |
|___/
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
_____ _____ _____ _____ _____ _______ _____
/\ \ /\ \ /\ \ /\ \ /\ \ /::\ \ /\ \
/::\ \ /::\____\ /::\ \ /::\ \ /::\____\ /::::\ \ /::\ \
/::::\ \ /:::/ / /::::\ \ \:::\ \ /:::/ / /::::::\ \ /::::\ \
/::::::\ \ /:::/ / /::::::\ \ \:::\ \ /:::/ / /::::::::\ \ /::::::\ \
/:::/\:::\ \ /:::/ / /:::/\:::\ \ \:::\ \ /:::/ / /:::/~~\:::\ \ /:::/\:::\ \
/:::/ \:::\ \ /:::/____/ /:::/__\:::\ \ \:::\ \ /:::/ / /:::/ \:::\ \ /:::/ \:::\ \
/:::/ \:::\ \ /::::\ \ /::::\ \:::\ \ /::::\ \ /:::/ / /:::/ / \:::\ \ /:::/ \:::\ \
/:::/ / \:::\ \ /::::::\ \ _____ /::::::\ \:::\ \ /::::::\ \ /:::/ / /:::/____/ \:::\____\ /:::/ / \:::\ \
/:::/ / \:::\ \ /:::/\:::\ \ /\ \ /:::/\:::\ \:::\ \ /:::/\:::\ \ /:::/ / |:::| | |:::| | /:::/ / \:::\ ___\
/:::/____/ \:::\____\/:::/ \:::\ /::\____\/:::/ \:::\ \:::\____\ /:::/ \:::\____\/:::/____/ |:::|____| |:::| |/:::/____/ ___\:::| |
\:::\ \ \::/ /\::/ \:::\ /:::/ /\::/ \:::\ /:::/ / /:::/ \::/ /\:::\ \ \:::\ \ /:::/ / \:::\ \ /\ /:::|____|
\:::\ \ \/____/ \/____/ \:::\/:::/ / \/____/ \:::\/:::/ / /:::/ / \/____/ \:::\ \ \:::\ \ /:::/ / \:::\ /::\ \::/ /
\:::\ \ \::::::/ / \::::::/ / /:::/ / \:::\ \ \:::\ /:::/ / \:::\ \:::\ \/____/
\:::\ \ \::::/ / \::::/ / /:::/ / \:::\ \ \:::\__/:::/ / \:::\ \:::\____\
\:::\ \ /:::/ / /:::/ / \::/ / \:::\ \ \::::::::/ / \:::\ /:::/ /
\:::\ \ /:::/ / /:::/ / \/____/ \:::\ \ \::::::/ / \:::\/:::/ /
\:::\ \ /:::/ / /:::/ / \:::\ \ \::::/ / \::::::/ /
\:::\____\ /:::/ / /:::/ / \:::\____\ \::/____/ \::::/ /
\::/ / \::/ / \::/ / \::/ / ~~ \::/____/
\/____/ \/____/ \/____/ \/____/
<div class="api-tester">
<div class="tab-container">
<div class="tab active" data-tab="session">最近会话</div>
<div class="tab" data-tab="chatroom">群聊</div>
<div class="tab" data-tab="contact">联系人</div>
<div class="tab" data-tab="chatlog">聊天记录</div>
</div>
<!-- 会话查询表单 -->
<div class="tab-content active" id="session-tab">
<div class="api-description">
<p>
查询最近会话列表。<span class="badge">GET /api/v1/session</span>
</p>
</div>
<div class="form-group">
<label for="session-format"
>输出格式:<span class="optional-param">可选</span></label
>
<select id="session-format">
<option value="">默认</option>
<option value="json">JSON</option>
<option value="text">纯文本</option>
</select>
</div>
</div>
<!-- 群聊查询表单 -->
<div class="tab-content" id="chatroom-tab">
<div class="api-description">
<p>
查询群聊列表,可选择性地按关键词搜索。<span class="badge"
>GET /api/v1/chatroom</span
>
</p>
</div>
<div class="form-group">
<label for="chatroom-keyword"
>搜索群聊:<span class="optional-param">可选</span></label
>
<input
type="text"
id="chatroom-keyword"
placeholder="输入关键词搜索群聊"
/>
</div>
<div class="form-group">
<label for="chatroom-format"
>输出格式:<span class="optional-param">可选</span></label
>
<select id="chatroom-format">
<option value="">默认</option>
<option value="json">JSON</option>
<option value="text">纯文本</option>
</select>
</div>
</div>
<!-- 联系人查询表单 -->
<div class="tab-content" id="contact-tab">
<div class="api-description">
<p>
查询联系人列表,可选择性地按关键词搜索。<span class="badge"
>GET /api/v1/contact</span
>
</p>
</div>
<div class="form-group">
<label for="contact-keyword"
>搜索联系人:<span class="optional-param">可选</span></label
>
<input
type="text"
id="contact-keyword"
placeholder="输入关键词搜索联系人"
/>
</div>
<div class="form-group">
<label for="contact-format"
>输出格式:<span class="optional-param">可选</span></label
>
<select id="contact-format">
<option value="">默认</option>
<option value="json">JSON</option>
<option value="text">纯文本</option>
</select>
</div>
</div>
<!-- 聊天记录查询表单 -->
<div class="tab-content" id="chatlog-tab">
<div class="api-description">
<p>
查询指定时间范围内与特定联系人或群聊的聊天记录。<span
class="badge"
>GET /api/v1/chatlog</span
>
</p>
</div>
<div class="form-group">
<label for="time"
>时间范围:<span class="required-field">*</span></label
>
<input
type="text"
id="time"
placeholder="例如2023-01-01 或 2023-01-01~2023-01-31"
/>
</div>
<div class="form-group">
<label for="talker"
>聊天对象:<span class="required-field">*</span></label
>
<input
type="text"
id="talker"
placeholder="wxid、群ID、备注名或昵称"
/>
</div>
<div class="form-group">
<label for="sender"
>发送者:<span class="optional-param">可选</span></label
>
<input
type="text"
id="sender"
placeholder="指定消息发送者"
/>
</div>
<div class="form-group">
<label for="keyword"
>关键词:<span class="optional-param">可选</span></label
>
<input
type="text"
id="keyword"
placeholder="搜索消息内容中的关键词"
/>
</div>
<div class="form-group">
<label for="limit"
>返回数量:<span class="optional-param">可选</span></label
>
<input type="number" id="limit" placeholder="默认不做限制" />
</div>
<div class="form-group">
<label for="offset"
>偏移量:<span class="optional-param">可选</span></label
>
<input type="number" id="offset" placeholder="默认 0" />
</div>
<div class="form-group">
<label for="format"
>输出格式:<span class="optional-param">可选</span></label
>
<select id="format">
<option value="">默认</option>
<option value="text">纯文本</option>
<option value="json">JSON</option>
<option value="csv">CSV</option>
</select>
</div>
</div>
<button id="test-api">执行查询</button>
<div id="result-wrapper" style="display: none; margin-top: 20px">
<div class="request-url" id="request-url-container">
<span class="url-text" id="request-url"></span>
<button class="copy-button copy-url-button" id="copy-url">
复制请求URL
</button>
</div>
<div class="result-container" id="api-result">
<p>查询结果将显示在这里...</p>
</div>
<div class="button-group">
<button class="copy-button" id="copy-result">复制结果</button>
</div>
</div>
<div class="error-message" id="error-message"></div>
</div>
</div>
<div class="api-section">
<h2>🤖 MCP 集成</h2>
<p>
Chatlog 支持 MCP (Model Context Protocol) SSE 协议,可与支持 MCP 的 AI
助手无缝集成。
</p>
<p>SSE 端点:<strong>/sse</strong></p>
<p>
详细集成指南请参考
<a
href="https://github.com/sjzar/chatlog/blob/main/docs/mcp.md"
class="docs-link"
target="_blank"
>MCP 集成指南</a
>
</p>
</div>
<div class="api-section">
<h2>📚 更多资源</h2>
<p>
查看
<a
href="https://github.com/sjzar/chatlog"
class="docs-link"
target="_blank"
>GitHub 项目</a
>
获取完整文档和使用指南。
</p>
<p>
如果你有任何问题或建议,欢迎通过
<a
href="https://github.com/sjzar/chatlog/discussions"
class="docs-link"
target="_blank"
>Discussions</a
>
进行交流。
</p>
</div>
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
___ _ _ __ ____ __ _____ ___
/ __)( )_( ) /__\ (_ _)( ) ( _ ) / __)
( (__ ) _ ( /(__)\ )( )(__ )(_)( ( (_-.
\___)(_) (_)(__)(__) (__) (____)(_____) \___/
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
________ ___ ___ ________ _________ ___ ________ ________
|\ ____\ |\ \|\ \ |\ __ \ |\___ ___\ |\ \ |\ __ \ |\ ____\
\ \ \___| \ \ \\\ \ \ \ \|\ \ \|___ \ \_| \ \ \ \ \ \|\ \ \ \ \___|
\ \ \ \ \ __ \ \ \ __ \ \ \ \ \ \ \ \ \ \\\ \ \ \ \ ___
\ \ \____ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \____ \ \ \\\ \ \ \ \|\ \
\ \_______\ \ \__\ \__\ \ \__\ \__\ \ \__\ \ \_______\ \ \_______\ \ \_______\
\|_______| \|__|\|__| \|__|\|__| \|__| \|_______| \|_______| \|_______|
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
╔═╗┬ ┬┌─┐┌┬┐┬ ┌─┐┌─┐
├─┤├─┤ │ │ │ ││ ┬
╚═╝┴ ┴┴ ┴ ┴ ┴─┘└─┘└─┘
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
▄▄· ▄ .▄ ▄▄▄· ▄▄▄▄▄▄▄▌ ▄▄ •
▐█ ▌▪██▪▐█▐█ ▀█ •██ ██• ▪ ▐█ ▀ ▪
██ ▄▄██▀▐█▄█▀▀█ ▐█.▪██▪ ▄█▀▄ ▄█ ▀█▄
▐███▌██▌▐▀▐█ ▪▐▌ ▐█▌·▐█▌▐▌▐█▌.▐▌▐█▄▪▐█
·▀▀▀ ▀▀▀ · ▀ ▀ ▀▀▀ .▀▀▀ ▀█▄▀▪·▀▀▀▀
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
,. - ., .·¨'`; ,.·´¨;\ ,., ' , . ., ° ,. ' , ·. ,.-·~·., ,.-·^*ª'` ·,
,·'´ ,. - , ';\ '; ;'\ '; ;::\ ;´ '· ., ;'´ , ., _';\' / ';\ / ·'´,.-·-., `,' .·´ ,·'´:¯'`·, '\
,·´ .'´\:::::;' ;:'\ ' ; ;::'\ ,' ;::'; .´ .-, ';\ \:´¨¯:;' `;::'\:'\ ,' ,'::'\ / .'´\:::::::'\ '\ ° ,´ ,'\:::::::::\,.·\'
/ ,'´::::'\;:-/ ,' ::; ' ; ;::_';,. ,.' ;:::';° / /:\:'; ;:'\' \::::; ,'::_'\;' ,' ;:::';' ,·' ,'::::\:;:-·-:'; ';\ / /:::\;·'´¯'`·;\:::\°
,' ;':::::;'´ '; /\::;' ' .' ,. -·~-·, ;:::'; ' ,' ,'::::'\'; ;::'; ,' ,'::;' '; ,':::;' ;. ';:::;´ ,' ,':'\ ; ;:::;' '\;:·´
; ;:::::; '\*'´\::\' ° '; ;'\::::::::; '/::::; ,.-·' '·~^*'´¨, ';::; ; ;:::; ° ; ,':::;' ' '; ;::; ,'´ .'´\::'; '; ;::/ ,·´¯'; °
'; ';::::'; '\::'\/.' ; ';:;\;::-··; ;::::; ':, ,·:²*´¨¯'`; ;::'; ; ;::;' ,' ,'::;' '; ':;: ,.·´,.·´::::\;'° '; '·;' ,.·´, ;'\
\ '·:;:'_ ,. -·'´.·´\ ':,.·´\;' ;' ,' :::/ ' ,' / \::::::::'; ;::'; ; ;::;' ; ';_:,.-·´';\ \·, `*´,.·'´::::::;·´ \'·. `'´,.·:´'; ;::\'
'\:` · .,. -·:´::::::\' \:::::\ \·.'::::; ,' ,'::::\·²*'´¨¯':,'\:; ',.'\::;' ', _,.-·'´:\:\ \\:¯::\:::::::;:·´ '\::\¯::::::::'; ;::';
\:::::::\:::::::;:·'´' \;:·´ \:\::'; \`¨\:::/ \::\' \::\:;' \¨:::::::::::\'; `\:::::\;::·'´ ° `·:\:::;:·´';.·´\::;'
`· :;::\;::-·´ `·\;' '\::\;' '\;' ' \;:' '\;::_;:-·'´ ¯ ¯ \::::\;'
' `¨' ° '¨ '\:·´'
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
▄████▄ ██░ ██ ▄▄▄ ▄▄▄█████▓ ██▓ ▒█████ ▄████
▒██▀ ▀█ ▓██░ ██▒▒████▄ ▓ ██▒ ▓▒▓██▒ ▒██▒ ██▒ ██▒ ▀█▒
▒▓█ ▄ ▒██▀▀██░▒██ ▀█▄ ▒ ▓██░ ▒░▒██░ ▒██░ ██▒▒██░▄▄▄░
▒▓▓▄ ▄██▒░▓█ ░██ ░██▄▄▄▄██ ░ ▓██▓ ░ ▒██░ ▒██ ██░░▓█ ██▓
▒ ▓███▀ ░░▓█▒░██▓ ▓█ ▓██▒ ▒██▒ ░ ░██████▒░ ████▓▒░░▒▓███▀▒
░ ░▒ ▒ ░ ▒ ░░▒░▒ ▒▒ ▓▒█░ ▒ ░░ ░ ▒░▓ ░░ ▒░▒░▒░ ░▒ ▒
░ ▒ ▒ ░▒░ ░ ▒ ▒▒ ░ ░ ░ ░ ▒ ░ ░ ▒ ▒░ ░ ░
░ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
</pre>
<pre style="float:left;white-space:pre-wrap;" class="random-paragraph">
▄█▄ ▄ █ ██ ▄▄▄▄▀ █ ████▄ ▄▀
█▀ ▀▄ █ █ █ █ ▀▀▀ █ █ █ █ ▄▀
█ ▀ ██▀▀█ █▄▄█ █ █ █ █ █ ▀▄
█▄ ▄▀ █ █ █ █ █ ███▄ ▀████ █ █
▀███▀ █ █ ▀ ▀ ███
▀ █
</pre>
</div>
<script>
// 标签切换功能
document.querySelectorAll(".tab").forEach((tab) => {
tab.addEventListener("click", function () {
// 移除所有标签的活动状态
document
.querySelectorAll(".tab")
.forEach((t) => t.classList.remove("active"));
// 设置当前标签为活动状态
this.classList.add("active");
// 隐藏所有内容区域
document.querySelectorAll(".tab-content").forEach((content) => {
content.classList.remove("active");
});
// 显示当前标签对应的内容
const tabId = this.getAttribute("data-tab") + "-tab";
document.getElementById(tabId).classList.add("active");
// 清空结果区域
document.getElementById("result-wrapper").style.display = "none";
document.getElementById("api-result").innerHTML =
"<p>查询结果将显示在这里...</p>";
document.getElementById("request-url").textContent = "";
document.getElementById("error-message").style.display = "none";
document.getElementById("error-message").textContent = "";
});
});
// API 测试功能
document
.getElementById("test-api")
.addEventListener("click", async function () {
const resultContainer = document.getElementById("api-result");
const requestUrlContainer = document.getElementById("request-url");
const errorMessage = document.getElementById("error-message");
const resultWrapper = document.getElementById("result-wrapper");
errorMessage.style.display = "none";
errorMessage.textContent = "";
try {
// 获取当前活动的标签
const activeTab = document
.querySelector(".tab.active")
.getAttribute("data-tab");
let url = "/api/v1/";
let params = new URLSearchParams();
// 根据不同的标签构建不同的请求
switch (activeTab) {
case "chatlog":
url += "chatlog";
const time = document.getElementById("time").value;
const talker = document.getElementById("talker").value;
const sender = document.getElementById("sender").value;
const keyword = document.getElementById("keyword").value;
const limit = document.getElementById("limit").value;
const offset = document.getElementById("offset").value;
const format = document.getElementById("format").value;
// 验证必填项
if (!time || !talker) {
errorMessage.textContent =
"错误: 时间范围和聊天对象为必填项!";
errorMessage.style.display = "block";
return;
}
if (time) params.append("time", time);
if (talker) params.append("talker", talker);
if (sender) params.append("sender", sender);
if (keyword) params.append("keyword", keyword);
if (limit) params.append("limit", limit);
if (offset) params.append("offset", offset);
if (format) params.append("format", format);
break;
case "contact":
url += "contact";
const contactKeyword =
document.getElementById("contact-keyword").value;
const contactFormat =
document.getElementById("contact-format").value;
if (contactKeyword) params.append("keyword", contactKeyword);
if (contactFormat) params.append("format", contactFormat);
break;
case "chatroom":
url += "chatroom";
const chatroomKeyword =
document.getElementById("chatroom-keyword").value;
const chatroomFormat =
document.getElementById("chatroom-format").value;
if (chatroomKeyword) params.append("keyword", chatroomKeyword);
if (chatroomFormat) params.append("format", chatroomFormat);
break;
case "session":
url += "session";
const sessionFormat =
document.getElementById("session-format").value;
if (sessionFormat) params.append("format", sessionFormat);
break;
window.onload = function() {
showRandomParagraph();
};
function showRandomParagraph() {
const paragraphs = document.getElementsByClassName("random-paragraph");
for (let i = 0; i < paragraphs.length; i++) {
paragraphs[i].style.display = "none";
}
// 添加参数到URL
const apiUrl = params.toString()
? `${url}?${params.toString()}`
: url;
// 获取完整URL包含域名部分
const fullUrl = window.location.origin + apiUrl;
// 显示完整请求URL
requestUrlContainer.textContent = fullUrl;
resultWrapper.style.display = "block";
// 显示加载中
resultContainer.innerHTML = '<div class="loading">加载中</div>';
// 发送请求
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// 获取响应内容类型
const contentType = response.headers.get("content-type");
let result;
if (contentType && contentType.includes("application/json")) {
// 如果是JSON格式化显示
result = await response.json();
resultContainer.innerHTML = JSON.stringify(result, null, 2);
} else {
// 其他格式直接显示文本
result = await response.text();
resultContainer.innerHTML = result;
}
} catch (error) {
resultContainer.innerHTML = "";
errorMessage.textContent = `查询出错: ${error.message}`;
errorMessage.style.display = "block";
console.error("API查询出错:", error);
}
});
// 复制结果功能
document
.getElementById("copy-result")
.addEventListener("click", function () {
const resultText = document.getElementById("api-result").innerText;
copyToClipboard(resultText, this, "已复制结果!");
});
// 复制URL功能
document
.getElementById("copy-url")
.addEventListener("click", function () {
// 获取完整URL包含域名部分
const urlText = document.getElementById("request-url").innerText;
copyToClipboard(urlText, this, "已复制URL!");
});
// 通用复制功能
function copyToClipboard(text, button, successMessage) {
navigator.clipboard
.writeText(text)
.then(() => {
const originalText = button.textContent;
button.textContent = successMessage;
setTimeout(() => {
button.textContent = originalText;
}, 2000);
})
.catch((err) => {
console.error("复制失败:", err);
});
}
const randomIndex = Math.floor(Math.random() * paragraphs.length);
paragraphs[randomIndex].style.display = "block";
}
</script>
</body>
</body>
</html>

View File

@@ -306,44 +306,3 @@ func (m *Manager) CommandDecrypt(dataDir string, workDir string, key string, pla
return nil
}
func (m *Manager) CommandHTTPServer(addr string, dataDir string, workDir string, platform string, version int) error {
if addr == "" {
addr = "127.0.0.1:5030"
}
if workDir == "" {
return fmt.Errorf("workDir is required")
}
if platform == "" {
return fmt.Errorf("platform is required")
}
if version == 0 {
return fmt.Errorf("version is required")
}
m.ctx.HTTPAddr = addr
m.ctx.DataDir = dataDir
m.ctx.WorkDir = workDir
m.ctx.Platform = platform
m.ctx.Version = version
// 如果是 4.0 版本,更新下 xorkey
if m.ctx.Version == 4 && m.ctx.DataDir != "" {
go dat2img.ScanAndSetXorKey(m.ctx.DataDir)
}
// 按依赖顺序启动服务
if err := m.db.Start(); err != nil {
return err
}
if err := m.mcp.Start(); err != nil {
return err
}
return m.http.ListenAndServe()
}

View File

@@ -21,12 +21,12 @@ var (
InputSchema: mcp.ToolSchema{
Type: "object",
Properties: mcp.M{
"keyword": mcp.M{
"query": mcp.M{
"type": "string",
"description": "联系人的搜索关键词可以是姓名、备注名或ID。",
},
},
Required: []string{"keyword"},
Required: []string{"query"},
},
}
@@ -36,12 +36,12 @@ var (
InputSchema: mcp.ToolSchema{
Type: "object",
Properties: mcp.M{
"keyword": mcp.M{
"query": mcp.M{
"type": "string",
"description": "群聊的搜索关键词可以是群名称、群ID或相关描述",
},
},
Required: []string{"keyword"},
Required: []string{"query"},
},
}
@@ -55,136 +55,24 @@ var (
}
ToolChatLog = mcp.Tool{
Name: "chatlog",
Description: `检索历史聊天记录,可根据时间、对话方、发送者和关键词等条件进行精确查询。当用户需要查找特定信息或想了解与某人/某群的历史交流时使用此工具。
【强制多步查询流程!】
当查询特定话题或特定发送者发言时,必须严格按照以下流程使用,任何偏离都会导致错误的结果:
步骤1: 初步定位相关消息
- 使用keyword参数查找特定话题
- 使用sender参数查找特定发送者的消息
- 使用较宽时间范围初步查询
步骤2: 【必须执行】针对每个关键结果点分别获取上下文
- 必须对步骤1返回的每个时间点T1, T2, T3...分别执行独立查询(时间范围接近的消息可以合并为一个查询)
- 每次独立查询必须移除keyword参数
- 每次独立查询必须移除sender参数
- 每次独立查询使用"Tn前后15-30分钟"的窄范围
- 每次独立查询仅保留talker参数
步骤3: 【必须执行】综合分析所有上下文
- 必须等待所有步骤2的查询结果返回后再进行分析
- 必须综合考虑所有上下文信息后再回答用户
【严格执行规则!】
- 禁止仅凭步骤1的结果直接回答用户
- 禁止在步骤2使用过大的时间范围一次性查询所有上下文
- 禁止跳过步骤2或步骤3
- 必须对每个关键结果点分别执行独立的上下文查询
【执行示例】
正确流程示例:
1. 步骤1: chatlog(time="2023-04-01~2023-04-30", talker="工作群", keyword="项目进度")
返回结果: 4月5日、4月12日、4月20日有相关消息
2. 步骤2:
- 查询1: chatlog(time="2023-04-05/09:30~2023-04-05/10:30", talker="工作群") // 注意没有keyword
- 查询2: chatlog(time="2023-04-12/14:00~2023-04-12/15:00", talker="工作群") // 注意没有keyword
- 查询3: chatlog(time="2023-04-20/16:00~2023-04-20/17:00", talker="工作群") // 注意没有keyword
3. 步骤3: 综合分析所有上下文后回答用户
错误流程示例:
- 仅执行步骤1后直接回答
- 步骤2使用time="2023-04-01~2023-04-30"一次性查询
- 步骤2仍然保留keyword或sender参数
【自我检查】回答用户前必须自问:
- 我是否对每个关键时间点都执行了独立的上下文查询?
- 我是否在上下文查询中移除了keyword和sender参数?
- 我是否分析了所有上下文后再回答?
- 如果上述任一问题答案为"否",则必须纠正流程
返回格式:"昵称(ID) 时间\n消息内容\n昵称(ID) 时间\n消息内容"
当查询多个Talker时返回格式为"昵称(ID)\n[TalkerName(Talker)] 时间\n消息内容"
重要提示:
1. 当用户询问特定时间段内的聊天记录时,必须使用正确的时间格式,特别是包含小时和分钟的查询
2. 对于"今天下午4点到5点聊了啥"这类查询,正确的时间参数格式应为"2023-04-18/16:00~2023-04-18/17:00"
3. 当用户询问具体群聊中某人的聊天记录时,使用"sender"参数
4. 当用户询问包含特定关键词的聊天记录时,使用"keyword"参数`,
Name: "chatlog",
Description: "查询特定时间或时间段内与特定联系人或群组的聊天记录。当用户需要回顾过去的对话内容、查找特定信息或想了解与某人/某群的历史交流时使用此工具。",
InputSchema: mcp.ToolSchema{
Type: "object",
Properties: mcp.M{
"time": mcp.M{
"type": "string",
"description": `指定查询的时间点或时间范围,格式必须严格遵循以下规则:
【单一时间点格式】
- 精确到日:"2023-04-18"或"20230418"
- 精确到分钟(必须包含斜杠和冒号):"2023-04-18/14:30"或"20230418/14:30"表示2023年4月18日14点30分
【时间范围格式】(使用"~"分隔起止时间)
- 日期范围:"2023-04-01~2023-04-18"
- 同一天的时间段:"2023-04-18/14:30~2023-04-18/15:45"
* 表示2023年4月18日14点30分到15点45分之间
【重要提示】包含小时分钟的格式必须使用斜杠和冒号:"/"和":"
正确示例:"2023-04-18/16:30"4月18日下午4点30分
错误示例:"2023-04-18 16:30"、"2023-04-18T16:30"
【其他支持的格式】
- 年份:"2023"
- 月份:"2023-04"或"202304"`,
"type": "string",
"description": "查询的时间点或时间段。可以是具体时间,例如 YYYY-MM-DD也可以是时间段例如 YYYY-MM-DD~YYYY-MM-DD时间段之间用\"~\"分隔。",
},
"talker": mcp.M{
"type": "string",
"description": `指定对话方(联系人或群组)
- 可使用ID、昵称或备注名
- 多个对话方用","分隔,如:"张三,李四,工作群"
- 【重要】这是多步查询中唯一应保留的参数`,
},
"sender": mcp.M{
"type": "string",
"description": `指定群聊中的发送者
- 仅在查询群聊记录时有效
- 多个发送者用","分隔,如:"张三,李四"
- 可使用ID、昵称或备注名
【重要】查询特定发送者的消息时:
1. 第一步使用sender参数初步定位多个相关消息时间点
2. 后续步骤必须移除sender参数分别查询每个时间点前后的完整对话
3. 错误示例:对所有找到的消息一次性查询大范围上下文
4. 正确示例对每个时间点T分别执行查询"T前后15-30分钟"不带sender`,
},
"keyword": mcp.M{
"type": "string",
"description": `搜索内容中的关键词
- 支持正则表达式匹配
- 【重要】查询特定话题时:
1. 第一步使用keyword参数初步定位多个相关消息时间点
2. 后续步骤必须移除keyword参数分别查询每个时间点前后的完整对话
3. 错误示例:对所有找到的关键词消息一次性查询大范围上下文
4. 正确示例对每个时间点T分别执行查询"T前后15-30分钟"不带keyword`,
"type": "string",
"description": "交谈对象可以是联系人或群聊。支持使用ID、昵称、备注名等进行查询。",
},
},
Required: []string{"time", "talker"},
},
}
ToolCurrentTime = mcp.Tool{
Name: "current_time",
Description: `获取当前系统时间返回RFC3339格式的时间字符串包含用户本地时区信息
使用场景:
- 当用户询问"总结今日聊天记录"、"本周都聊了啥"等当前时间问题
- 当用户提及"昨天"、"上周"、"本月"等相对时间概念,需要确定基准时间点
- 需要执行依赖当前时间的计算(如"上个月5号我们有开会吗"
返回示例2025-04-18T21:29:00+08:00
注意:此工具不需要任何输入参数,直接调用即可获取当前时间。`,
InputSchema: mcp.ToolSchema{
Type: "object",
Properties: mcp.M{},
},
}
ResourceRecentChat = mcp.Resource{
Name: "最近会话",
URI: "session://recent",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"net/url"
"strings"
"time"
"github.com/sjzar/chatlog/internal/chatlog/ctx"
"github.com/sjzar/chatlog/internal/chatlog/database"
@@ -84,7 +83,6 @@ func (s *Service) processMCP(session *mcp.Session, req *mcp.Request) {
ToolChatRoom,
ToolRecentChat,
ToolChatLog,
ToolCurrentTime,
}})
case mcp.MethodToolsCall:
err = s.toolsCall(session, req)
@@ -132,13 +130,13 @@ func (s *Service) toolsCall(session *mcp.Session, req *mcp.Request) error {
buf := &bytes.Buffer{}
switch callReq.Name {
case "query_contact":
keyword := ""
if v, ok := callReq.Arguments["keyword"]; ok {
keyword = v.(string)
query := ""
if v, ok := callReq.Arguments["query"]; ok {
query = v.(string)
}
limit := util.MustAnyToInt(callReq.Arguments["limit"])
offset := util.MustAnyToInt(callReq.Arguments["offset"])
list, err := s.db.GetContacts(keyword, limit, offset)
list, err := s.db.GetContacts(query, limit, offset)
if err != nil {
return fmt.Errorf("无法获取联系人列表: %v", err)
}
@@ -147,13 +145,13 @@ func (s *Service) toolsCall(session *mcp.Session, req *mcp.Request) error {
buf.WriteString(fmt.Sprintf("%s,%s,%s,%s\n", contact.UserName, contact.Alias, contact.Remark, contact.NickName))
}
case "query_chat_room":
keyword := ""
if v, ok := callReq.Arguments["keyword"]; ok {
keyword = v.(string)
query := ""
if v, ok := callReq.Arguments["query"]; ok {
query = v.(string)
}
limit := util.MustAnyToInt(callReq.Arguments["limit"])
offset := util.MustAnyToInt(callReq.Arguments["offset"])
list, err := s.db.GetChatRooms(keyword, limit, offset)
list, err := s.db.GetChatRooms(query, limit, offset)
if err != nil {
return fmt.Errorf("无法获取群聊列表: %v", err)
}
@@ -162,13 +160,13 @@ func (s *Service) toolsCall(session *mcp.Session, req *mcp.Request) error {
buf.WriteString(fmt.Sprintf("%s,%s,%s,%s,%d\n", chatRoom.Name, chatRoom.Remark, chatRoom.NickName, chatRoom.Owner, len(chatRoom.Users)))
}
case "query_recent_chat":
keyword := ""
if v, ok := callReq.Arguments["keyword"]; ok {
keyword = v.(string)
query := ""
if v, ok := callReq.Arguments["query"]; ok {
query = v.(string)
}
limit := util.MustAnyToInt(callReq.Arguments["limit"])
offset := util.MustAnyToInt(callReq.Arguments["offset"])
data, err := s.db.GetSessions(keyword, limit, offset)
data, err := s.db.GetSessions(query, limit, offset)
if err != nil {
return fmt.Errorf("无法获取会话列表: %v", err)
}
@@ -192,29 +190,16 @@ func (s *Service) toolsCall(session *mcp.Session, req *mcp.Request) error {
if v, ok := callReq.Arguments["talker"]; ok {
talker = v.(string)
}
sender := ""
if v, ok := callReq.Arguments["sender"]; ok {
sender = v.(string)
}
keyword := ""
if v, ok := callReq.Arguments["keyword"]; ok {
keyword = v.(string)
}
limit := util.MustAnyToInt(callReq.Arguments["limit"])
offset := util.MustAnyToInt(callReq.Arguments["offset"])
messages, err := s.db.GetMessages(start, end, talker, sender, keyword, limit, offset)
messages, err := s.db.GetMessages(start, end, talker, limit, offset)
if err != nil {
return fmt.Errorf("无法获取聊天记录: %v", err)
}
if len(messages) == 0 {
buf.WriteString("未找到符合查询条件的聊天记录")
}
for _, m := range messages {
buf.WriteString(m.PlainText(strings.Contains(talker, ","), util.PerfectTimeFormat(start, end), ""))
buf.WriteString(m.PlainText(len(talker) == 0, ""))
buf.WriteString("\n")
}
case "current_time":
buf.WriteString(time.Now().Local().Format(time.RFC3339))
default:
return fmt.Errorf("未支持的工具: %s", callReq.Name)
}
@@ -243,6 +228,7 @@ func (s *Service) resourcesRead(session *mcp.Session, req *mcp.Request) error {
buf := &bytes.Buffer{}
switch u.Scheme {
case "contact":
list, err := s.db.GetContacts(u.Host, 0, 0)
if err != nil {
return fmt.Errorf("无法获取联系人列表: %v", err)
@@ -276,15 +262,12 @@ func (s *Service) resourcesRead(session *mcp.Session, req *mcp.Request) error {
}
limit := util.MustAnyToInt(u.Query().Get("limit"))
offset := util.MustAnyToInt(u.Query().Get("offset"))
messages, err := s.db.GetMessages(start, end, u.Host, "", "", limit, offset)
messages, err := s.db.GetMessages(start, end, u.Host, limit, offset)
if err != nil {
return fmt.Errorf("无法获取聊天记录: %v", err)
}
if len(messages) == 0 {
buf.WriteString("未找到符合查询条件的聊天记录")
}
for _, m := range messages {
buf.WriteString(m.PlainText(strings.Contains(u.Host, ","), util.PerfectTimeFormat(start, end), ""))
buf.WriteString(m.PlainText(len(u.Host) == 0, ""))
buf.WriteString("\n")
}
default:

View File

@@ -65,7 +65,7 @@ func Newf(cause error, code int, format string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(format, args...),
Cause: cause,
Code: code,
Code: http.StatusInternalServerError,
}
}

View File

@@ -2,7 +2,6 @@ package errors
import (
"net/http"
"runtime/debug"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
@@ -51,7 +50,7 @@ func RecoveryMiddleware() gin.HandlerFunc {
}
// 记录错误日志
log.Err(err).Msgf("PANIC RECOVERED\n%s", string(debug.Stack()))
log.Err(err).Msg("PANIC RECOVERED")
// 返回 500 错误
c.JSON(http.StatusInternalServerError, err)

View File

@@ -35,7 +35,6 @@ type Image struct {
}
type Video struct {
Md5 string `xml:"md5,attr"`
RawMd5 string `xml:"rawmd5,attr"`
// Length string `xml:"length,attr"`
// PlayLength string `xml:"playlength,attr"`

View File

@@ -79,12 +79,7 @@ func (m *Message) ParseMediaInfo(data string) error {
case 3:
m.Contents["md5"] = msg.Image.MD5
case 43:
if msg.Video.Md5 != "" {
m.Contents["md5"] = msg.Video.Md5
}
if msg.Video.RawMd5 != "" {
m.Contents["rawmd5"] = msg.Video.RawMd5
}
m.Contents["md5"] = msg.Video.RawMd5
case 49:
m.SubType = int64(msg.App.Type)
switch m.SubType {
@@ -188,11 +183,7 @@ func (m *Message) SetContent(key string, value interface{}) {
m.Contents[key] = value
}
func (m *Message) PlainText(showChatRoom bool, timeFormat string, host string) string {
if timeFormat == "" {
timeFormat = "01-02 15:04:05"
}
func (m *Message) PlainText(showChatRoom bool, host string) string {
m.SetContent("host", host)
@@ -225,7 +216,7 @@ func (m *Message) PlainText(showChatRoom bool, timeFormat string, host string) s
buf.WriteString("] ")
}
buf.WriteString(m.Time.Format(timeFormat))
buf.WriteString(m.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("\n")
buf.WriteString(m.PlainTextContent())
@@ -239,23 +230,7 @@ func (m *Message) PlainTextContent() string {
case 1:
return m.Content
case 3:
keylist := make([]string, 0)
if m.Contents["md5"] != nil {
if md5, ok := m.Contents["md5"].(string); ok {
keylist = append(keylist, md5)
}
}
if m.Contents["imgfile"] != nil {
if imgfile, ok := m.Contents["imgfile"].(string); ok {
keylist = append(keylist, imgfile)
}
}
if m.Contents["thumb"] != nil {
if thumb, ok := m.Contents["thumb"].(string); ok {
keylist = append(keylist, thumb)
}
}
return fmt.Sprintf("![图片](http://%s/image/%s)", m.Contents["host"], strings.Join(keylist, ","))
return fmt.Sprintf("![图片](http://%s/image/%s)", m.Contents["host"], m.Contents["md5"])
case 34:
if voice, ok := m.Contents["voice"]; ok {
return fmt.Sprintf("[语音](http://%s/voice/%s)", m.Contents["host"], voice)
@@ -264,28 +239,10 @@ func (m *Message) PlainTextContent() string {
case 42:
return "[名片]"
case 43:
keylist := make([]string, 0)
if m.Contents["md5"] != nil {
if md5, ok := m.Contents["md5"].(string); ok {
keylist = append(keylist, md5)
}
if path, ok := m.Contents["path"]; ok {
return fmt.Sprintf("![视频](http://%s/data/%s)", m.Contents["host"], path)
}
if m.Contents["rawmd5"] != nil {
if rawmd5, ok := m.Contents["rawmd5"].(string); ok {
keylist = append(keylist, rawmd5)
}
}
if m.Contents["videofile"] != nil {
if videofile, ok := m.Contents["videofile"].(string); ok {
keylist = append(keylist, videofile)
}
}
if m.Contents["thumb"] != nil {
if thumb, ok := m.Contents["thumb"].(string); ok {
keylist = append(keylist, thumb)
}
}
return fmt.Sprintf("![视频](http://%s/video/%s)", m.Contents["host"], strings.Join(keylist, ","))
return fmt.Sprintf("![视频](http://%s/video/%s)", m.Contents["host"], m.Contents["md5"])
case 47:
return "[动画表情]"
case 49:
@@ -305,11 +262,7 @@ func (m *Message) PlainTextContent() string {
if !ok {
return "[合并转发]"
}
host := ""
if m.Contents["host"] != nil {
host = m.Contents["host"].(string)
}
return recordInfo.String("", host)
return recordInfo.String("", m.Contents["host"].(string))
case 33, 36:
if m.Contents["title"] == "" {
return "[小程序]"
@@ -337,11 +290,7 @@ func (m *Message) PlainTextContent() string {
return "> [引用]\n" + m.Content
}
buf := strings.Builder{}
host := ""
if m.Contents["host"] != nil {
host = m.Contents["host"].(string)
}
referContent := refer.PlainText(false, "", host)
referContent := refer.PlainText(false, m.Contents["host"].(string))
for _, line := range strings.Split(referContent, "\n") {
if line == "" {
continue

View File

@@ -96,7 +96,7 @@ func (m *MessageV3) Wrap() *Message {
if len(parts) > 1 {
path = strings.Join(parts[1:], "/")
}
_m.Contents["videofile"] = path
_m.Contents["path"] = path
}
}
}

View File

@@ -2,10 +2,7 @@ package model
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"path/filepath"
"strings"
"time"
@@ -88,14 +85,10 @@ func (m *MessageV4) Wrap(talker string) *Message {
if packedInfo := ParsePackedInfo(m.PackedInfoData); packedInfo != nil {
// FIXME 尝试解决 v4 版本 xml 数据无法匹配到 hardlink 记录的问题
if _m.Type == 3 && packedInfo.Image != nil {
_talkerMd5Bytes := md5.Sum([]byte(talker))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
_m.Contents["imgfile"] = filepath.Join("msg", "attach", talkerMd5, _m.Time.Format("2006-01"), "Img", fmt.Sprintf("%s.dat", packedInfo.Image.Md5))
_m.Contents["thumb"] = filepath.Join("msg", "attach", talkerMd5, _m.Time.Format("2006-01"), "Img", fmt.Sprintf("%s_t.dat", packedInfo.Image.Md5))
_m.Contents["md5"] = packedInfo.Image.Md5
}
if _m.Type == 43 && packedInfo.Video != nil {
_m.Contents["videofile"] = filepath.Join("msg", "video", _m.Time.Format("2006-01"), fmt.Sprintf("%s.mp4", packedInfo.Video.Md5))
_m.Contents["thumb"] = filepath.Join("msg", "video", _m.Time.Format("2006-01"), fmt.Sprintf("%s_thumb.jpg", packedInfo.Video.Md5))
_m.Contents["md5"] = packedInfo.Video.Md5
}
}
}

View File

@@ -22,7 +22,7 @@ const (
var V3KeyPatterns = []KeyPatternInfo{
{
Pattern: []byte{0x72, 0x74, 0x72, 0x65, 0x65, 0x5f, 0x69, 0x33, 0x32},
Offsets: []int{24},
Offset: 24,
},
}
@@ -122,73 +122,16 @@ func (e *V3Extractor) findMemory(ctx context.Context, pid uint32, memoryChannel
return err
}
totalSize := len(memory)
log.Debug().Msgf("Read memory region, size: %d bytes", totalSize)
log.Debug().Msgf("Read memory region, size: %d bytes", len(memory))
// If memory is small enough, process it as a single chunk
if totalSize <= MinChunkSize {
select {
case memoryChannel <- memory:
log.Debug().Msg("Memory sent as a single chunk for analysis")
case <-ctx.Done():
return ctx.Err()
}
return nil
// Send memory data to channel for processing
select {
case memoryChannel <- memory:
log.Debug().Msg("Memory region sent for analysis")
case <-ctx.Done():
return ctx.Err()
}
chunkCount := MaxWorkers * ChunkMultiplier
// Calculate chunk size based on fixed chunk count
chunkSize := totalSize / chunkCount
if chunkSize < MinChunkSize {
// Reduce number of chunks if each would be too small
chunkCount = totalSize / MinChunkSize
if chunkCount == 0 {
chunkCount = 1
}
chunkSize = totalSize / chunkCount
}
// Process memory in chunks from end to beginning
for i := chunkCount - 1; i >= 0; i-- {
select {
case <-ctx.Done():
return ctx.Err()
default:
// Calculate start and end positions for this chunk
start := i * chunkSize
end := (i + 1) * chunkSize
// Ensure the last chunk includes all remaining memory
if i == chunkCount-1 {
end = totalSize
}
// Add overlap area to catch patterns at chunk boundaries
if i > 0 {
start -= ChunkOverlapBytes
if start < 0 {
start = 0
}
}
chunk := memory[start:end]
log.Debug().
Int("chunk_index", i+1).
Int("total_chunks", chunkCount).
Int("chunk_size", len(chunk)).
Int("start_offset", start).
Int("end_offset", end).
Msg("Processing memory chunk")
select {
case memoryChannel <- chunk:
case <-ctx.Done():
return ctx.Err()
}
}
}
return nil
}
@@ -230,26 +173,24 @@ func (e *V3Extractor) SearchKey(ctx context.Context, memory []byte) (string, boo
break // No more matches found
}
// Try each offset for this pattern
for _, offset := range keyPattern.Offsets {
// Check if we have enough space for the key
keyOffset := index + offset
if keyOffset < 0 || keyOffset+32 > len(memory) {
continue
}
// Check if we have enough space for the key
keyOffset := index + keyPattern.Offset
if keyOffset < 0 || keyOffset+32 > len(memory) {
index -= 1
continue
}
// Extract the key data, which is at the offset position and 32 bytes long
keyData := memory[keyOffset : keyOffset+32]
// Extract the key data, which is 32 bytes long
keyData := memory[keyOffset : keyOffset+32]
// Validate key against database header
if e.validator.Validate(keyData) {
log.Debug().
Str("pattern", hex.EncodeToString(keyPattern.Pattern)).
Int("offset", offset).
Str("key", hex.EncodeToString(keyData)).
Msg("Key found")
return hex.EncodeToString(keyData), true
}
// Validate key against database header
if e.validator.Validate(keyData) {
log.Debug().
Str("pattern", hex.EncodeToString(keyPattern.Pattern)).
Int("offset", keyPattern.Offset).
Str("key", hex.EncodeToString(keyData)).
Msg("Key found")
return hex.EncodeToString(keyData), true
}
index -= 1

View File

@@ -16,16 +16,17 @@ import (
)
const (
MaxWorkers = 8
MinChunkSize = 1 * 1024 * 1024 // 1MB
ChunkOverlapBytes = 1024 // Greater than all offsets
ChunkMultiplier = 2 // Number of chunks = MaxWorkers * ChunkMultiplier
MaxWorkers = 8
)
var V4KeyPatterns = []KeyPatternInfo{
{
Pattern: []byte{0x20, 0x66, 0x74, 0x73, 0x35, 0x28, 0x25, 0x00},
Offsets: []int{16, -80, 64},
Offset: 16,
},
{
Pattern: []byte{0x20, 0x66, 0x74, 0x73, 0x35, 0x28, 0x25, 0x00},
Offset: -80,
},
}
@@ -125,72 +126,14 @@ func (e *V4Extractor) findMemory(ctx context.Context, pid uint32, memoryChannel
return err
}
totalSize := len(memory)
log.Debug().Msgf("Read memory region, size: %d bytes", totalSize)
log.Debug().Msgf("Read memory region, size: %d bytes", len(memory))
// If memory is small enough, process it as a single chunk
if totalSize <= MinChunkSize {
select {
case memoryChannel <- memory:
log.Debug().Msg("Memory sent as a single chunk for analysis")
case <-ctx.Done():
return ctx.Err()
}
return nil
}
chunkCount := MaxWorkers * ChunkMultiplier
// Calculate chunk size based on fixed chunk count
chunkSize := totalSize / chunkCount
if chunkSize < MinChunkSize {
// Reduce number of chunks if each would be too small
chunkCount = totalSize / MinChunkSize
if chunkCount == 0 {
chunkCount = 1
}
chunkSize = totalSize / chunkCount
}
// Process memory in chunks from end to beginning
for i := chunkCount - 1; i >= 0; i-- {
select {
case <-ctx.Done():
return ctx.Err()
default:
// Calculate start and end positions for this chunk
start := i * chunkSize
end := (i + 1) * chunkSize
// Ensure the last chunk includes all remaining memory
if i == chunkCount-1 {
end = totalSize
}
// Add overlap area to catch patterns at chunk boundaries
if i > 0 {
start -= ChunkOverlapBytes
if start < 0 {
start = 0
}
}
chunk := memory[start:end]
log.Debug().
Int("chunk_index", i+1).
Int("total_chunks", chunkCount).
Int("chunk_size", len(chunk)).
Int("start_offset", start).
Int("end_offset", end).
Msg("Processing memory chunk")
select {
case memoryChannel <- chunk:
case <-ctx.Done():
return ctx.Err()
}
}
// Send memory data to channel for processing
select {
case memoryChannel <- memory:
log.Debug().Msg("Memory region sent for analysis")
case <-ctx.Done():
return ctx.Err()
}
return nil
@@ -234,26 +177,24 @@ func (e *V4Extractor) SearchKey(ctx context.Context, memory []byte) (string, boo
break // No more matches found
}
// Try each offset for this pattern
for _, offset := range keyPattern.Offsets {
// Check if we have enough space for the key
keyOffset := index + offset
if keyOffset < 0 || keyOffset+32 > len(memory) {
continue
}
// Check if we have enough space for the key
keyOffset := index + keyPattern.Offset
if keyOffset < 0 || keyOffset+32 > len(memory) {
index -= 1
continue
}
// Extract the key data, which is at the offset position and 32 bytes long
keyData := memory[keyOffset : keyOffset+32]
// Extract the key data, which is 16 bytes after the pattern and 32 bytes long
keyData := memory[keyOffset : keyOffset+32]
// Validate key against database header
if keyData, ok := e.validate(ctx, keyData); ok {
log.Debug().
Str("pattern", hex.EncodeToString(keyPattern.Pattern)).
Int("offset", offset).
Str("key", hex.EncodeToString(keyData)).
Msg("Key found")
return hex.EncodeToString(keyData), true
}
// Validate key against database header
if e.validator.Validate(keyData) {
log.Debug().
Str("pattern", hex.EncodeToString(keyPattern.Pattern)).
Int("offset", keyPattern.Offset).
Str("key", hex.EncodeToString(keyData)).
Msg("Key found")
return hex.EncodeToString(keyData), true
}
index -= 1
@@ -263,19 +204,11 @@ func (e *V4Extractor) SearchKey(ctx context.Context, memory []byte) (string, boo
return "", false
}
func (e *V4Extractor) validate(ctx context.Context, keyDate []byte) ([]byte, bool) {
if e.validator.Validate(keyDate) {
return keyDate, true
}
// Try to find a valid key by ***
return nil, false
}
func (e *V4Extractor) SetValidate(validator *decrypt.Validator) {
e.validator = validator
}
type KeyPatternInfo struct {
Pattern []byte
Offsets []int
Offset int
}

View File

@@ -13,8 +13,8 @@ import (
const (
V3ProcessName = "WeChat"
V4ProcessName = "Weixin"
V3DBFile = `Msg\Misc.db`
V4DBFile = `db_storage\session\session.db`
V3DBFile = "Msg\\Misc.db"
V4DBFile = "db_storage\\message\\message_0.db"
)
// Detector 实现 Windows 平台的进程检测器

View File

@@ -5,8 +5,6 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"regexp"
"sort"
"strings"
"time"
@@ -17,7 +15,6 @@ import (
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
"github.com/sjzar/chatlog/internal/wechatdb/datasource/dbm"
"github.com/sjzar/chatlog/pkg/util"
)
const (
@@ -28,7 +25,7 @@ const (
Media = "media"
)
var Groups = []*dbm.Group{
var Groups = []dbm.Group{
{
Name: Message,
Pattern: `^msg_([0-9]?[0-9])?\.db$`,
@@ -117,10 +114,6 @@ func (ds *DataSource) initMessageDbs() error {
dbPaths, err := ds.dbm.GetDBPath(Message)
if err != nil {
if strings.Contains(err.Error(), "db file not found") {
ds.talkerDBMap = make(map[string]string)
return nil
}
return err
}
// 处理每个数据库文件
@@ -162,10 +155,6 @@ func (ds *DataSource) initMessageDbs() error {
func (ds *DataSource) initChatRoomDb() error {
db, err := ds.dbm.GetDB(ChatRoom)
if err != nil {
if strings.Contains(err.Error(), "db file not found") {
ds.user2DisplayName = make(map[string]string)
return nil
}
return err
}
@@ -191,162 +180,70 @@ func (ds *DataSource) initChatRoomDb() error {
return nil
}
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
// GetMessages 实现获取消息的方法
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
// 在 darwinv3 中,每个联系人/群聊的消息存储在单独的表中,表名为 Chat_md5(talker)
// 首先需要找到对应的表名
if talker == "" {
return nil, errors.ErrTalkerEmpty
}
// 解析talker参数支持多个talker以英文逗号分隔
talkers := util.Str2List(talker, ",")
if len(talkers) == 0 {
return nil, errors.ErrTalkerEmpty
_talkerMd5Bytes := md5.Sum([]byte(talker))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
dbPath, ok := ds.talkerDBMap[talkerMd5]
if !ok {
return nil, errors.TalkerNotFound(talker)
}
// 解析sender参数支持多个发送者以英文逗号分隔
senders := util.Str2List(sender, ",")
// 预编译正则表达式如果有keyword
var regex *regexp.Regexp
if keyword != "" {
var err error
regex, err = regexp.Compile(keyword)
if err != nil {
return nil, errors.QueryFailed("invalid regex pattern", err)
}
db, err := ds.dbm.OpenDB(dbPath)
if err != nil {
return nil, err
}
tableName := fmt.Sprintf("Chat_%s", talkerMd5)
// 从每个相关数据库中查询消息,并在读取时进行过滤
filteredMessages := []*model.Message{}
// 构建查询条件
query := fmt.Sprintf(`
SELECT msgCreateTime, msgContent, messageType, mesDes
FROM %s
WHERE msgCreateTime >= ? AND msgCreateTime <= ?
ORDER BY msgCreateTime ASC
`, tableName)
// 对每个talker进行查询
for _, talkerItem := range talkers {
// 检查上下文是否已取消
if err := ctx.Err(); err != nil {
return nil, err
}
// 在 darwinv3 中,需要先找到对应的数据库
_talkerMd5Bytes := md5.Sum([]byte(talkerItem))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
dbPath, ok := ds.talkerDBMap[talkerMd5]
if !ok {
// 如果找不到对应的数据库跳过此talker
continue
}
db, err := ds.dbm.OpenDB(dbPath)
if err != nil {
log.Error().Msgf("数据库 %s 未打开", dbPath)
continue
}
tableName := fmt.Sprintf("Chat_%s", talkerMd5)
// 构建查询条件
query := fmt.Sprintf(`
SELECT msgCreateTime, msgContent, messageType, mesDes
FROM %s
WHERE msgCreateTime >= ? AND msgCreateTime <= ?
ORDER BY msgCreateTime ASC
`, tableName)
// 执行查询
rows, err := db.QueryContext(ctx, query, startTime.Unix(), endTime.Unix())
if err != nil {
// 如果表不存在跳过此talker
if strings.Contains(err.Error(), "no such table") {
continue
}
log.Err(err).Msgf("从数据库 %s 查询消息失败", dbPath)
continue
}
// 处理查询结果,在读取时进行过滤
for rows.Next() {
var msg model.MessageDarwinV3
err := rows.Scan(
&msg.MsgCreateTime,
&msg.MsgContent,
&msg.MessageType,
&msg.MesDes,
)
if err != nil {
rows.Close()
log.Err(err).Msgf("扫描消息行失败")
continue
}
// 将消息包装为通用模型
message := msg.Wrap(talkerItem)
// 应用sender过滤
if len(senders) > 0 {
senderMatch := false
for _, s := range senders {
if message.Sender == s {
senderMatch = true
break
}
}
if !senderMatch {
continue // 不匹配sender跳过此消息
}
}
// 应用keyword过滤
if regex != nil {
plainText := message.PlainTextContent()
if !regex.MatchString(plainText) {
continue // 不匹配keyword跳过此消息
}
}
// 通过所有过滤条件,保留此消息
filteredMessages = append(filteredMessages, message)
// 检查是否已经满足分页处理数量
if limit > 0 && len(filteredMessages) >= offset+limit {
// 已经获取了足够的消息,可以提前返回
rows.Close()
// 对所有消息按时间排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Seq < filteredMessages[j].Seq
})
// 处理分页
if offset >= len(filteredMessages) {
return []*model.Message{}, nil
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
}
return filteredMessages[offset:end], nil
}
}
rows.Close()
}
// 对所有消息按时间排序
// FIXME 不同 talker 需要使用 Time 排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Time.Before(filteredMessages[j].Time)
})
// 处理分页
if limit > 0 {
if offset >= len(filteredMessages) {
return []*model.Message{}, nil
query += fmt.Sprintf(" LIMIT %d", limit)
if offset > 0 {
query += fmt.Sprintf(" OFFSET %d", offset)
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
}
return filteredMessages[offset:end], nil
}
return filteredMessages, nil
// 执行查询
rows, err := db.QueryContext(ctx, query, startTime.Unix(), endTime.Unix())
if err != nil {
return nil, errors.QueryFailed(query, err)
}
defer rows.Close()
// 处理查询结果
messages := []*model.Message{}
for rows.Next() {
var msg model.MessageDarwinV3
err := rows.Scan(
&msg.MsgCreateTime,
&msg.MsgContent,
&msg.MessageType,
&msg.MesDes,
)
if err != nil {
log.Err(err).Msgf("扫描消息行失败")
continue
}
// 将消息包装为通用模型
message := msg.Wrap(talker)
messages = append(messages, message)
}
return messages, nil
}
// 从表名中提取 talker

View File

@@ -16,7 +16,7 @@ import (
type DataSource interface {
// 消息
GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error)
GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error)
// 联系人
GetContacts(ctx context.Context, key string, limit, offset int) ([]*model.Contact, error)

View File

@@ -34,7 +34,7 @@ func NewDBManager(path string) *DBManager {
}
}
func (d *DBManager) AddGroup(g *Group) error {
func (d *DBManager) AddGroup(g Group) error {
fg, err := filemonitor.NewFileGroup(g.Name, d.path, g.Pattern, g.BlackList)
if err != nil {
return err

View File

@@ -9,7 +9,7 @@ import (
func TestXxx(t *testing.T) {
path := "/Users/sarv/Documents/chatlog/bigjun_9e7a"
g := &Group{
g := Group{
Name: "session",
Pattern: `session\.db$`,
BlackList: []string{},

View File

@@ -6,7 +6,6 @@ import (
"database/sql"
"encoding/hex"
"fmt"
"regexp"
"sort"
"strings"
"time"
@@ -18,7 +17,6 @@ import (
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
"github.com/sjzar/chatlog/internal/wechatdb/datasource/dbm"
"github.com/sjzar/chatlog/pkg/util"
)
const (
@@ -29,7 +27,7 @@ const (
Voice = "voice"
)
var Groups = []*dbm.Group{
var Groups = []dbm.Group{
{
Name: Message,
Pattern: `^message_([0-9]?[0-9])?\.db$`,
@@ -115,10 +113,6 @@ func (ds *DataSource) SetCallback(name string, callback func(event fsnotify.Even
func (ds *DataSource) initMessageDbs() error {
dbPaths, err := ds.dbm.GetDBPath(Message)
if err != nil {
if strings.Contains(err.Error(), "db file not found") {
ds.messageInfos = make([]MessageDBInfo, 0)
return nil
}
return err
}
@@ -177,16 +171,11 @@ func (ds *DataSource) getDBInfosForTimeRange(startTime, endTime time.Time) []Mes
return dbs
}
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
if talker == "" {
return nil, errors.ErrTalkerEmpty
}
// 解析talker参数支持多个talker以英文逗号分隔
talkers := util.Str2List(talker, ",")
if len(talkers) == 0 {
return nil, errors.ErrTalkerEmpty
}
log.Debug().Msg(talker)
// 找到时间范围内的数据库文件
dbInfos := ds.getDBInfosForTimeRange(startTime, endTime)
@@ -194,21 +183,13 @@ func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.T
return nil, errors.TimeRangeNotFound(startTime, endTime)
}
// 解析sender参数支持多个发送者以英文逗号分隔
senders := util.Str2List(sender, ",")
// 预编译正则表达式如果有keyword
var regex *regexp.Regexp
if keyword != "" {
var err error
regex, err = regexp.Compile(keyword)
if err != nil {
return nil, errors.QueryFailed("invalid regex pattern", err)
}
if len(dbInfos) == 1 {
// LIMIT 和 OFFSET 逻辑在单文件情况下可以直接在 SQL 里处理
return ds.getMessagesSingleFile(ctx, dbInfos[0], startTime, endTime, talker, limit, offset)
}
// 从每个相关数据库中查询消息,并在读取时进行过滤
filteredMessages := []*model.Message{}
// 从每个相关数据库中查询消息
totalMessages := []*model.Message{}
for _, dbInfo := range dbInfos {
// 检查上下文是否已取消
@@ -222,141 +203,183 @@ func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.T
continue
}
// 对每个talker进行查询
for _, talkerItem := range talkers {
// 构建表名
_talkerMd5Bytes := md5.Sum([]byte(talkerItem))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
tableName := "Msg_" + talkerMd5
messages, err := ds.getMessagesFromDB(ctx, db, startTime, endTime, talker)
if err != nil {
log.Err(err).Msgf("从数据库 %s 获取消息失败", dbInfo.FilePath)
continue
}
// 检查表是否存在
var exists bool
err = db.QueryRowContext(ctx,
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?",
tableName).Scan(&exists)
totalMessages = append(totalMessages, messages...)
if err != nil {
if err == sql.ErrNoRows {
// 表不存在继续下一个talker
continue
}
return nil, errors.QueryFailed("", err)
}
// 构建查询条件
conditions := []string{"create_time >= ? AND create_time <= ?"}
args := []interface{}{startTime.Unix(), endTime.Unix()}
log.Debug().Msgf("Table name: %s", tableName)
log.Debug().Msgf("Start time: %d, End time: %d", startTime.Unix(), endTime.Unix())
query := fmt.Sprintf(`
SELECT m.sort_seq, m.server_id, m.local_type, n.user_name, m.create_time, m.message_content, m.packed_info_data, m.status
FROM %s m
LEFT JOIN Name2Id n ON m.real_sender_id = n.rowid
WHERE %s
ORDER BY m.sort_seq ASC
`, tableName, strings.Join(conditions, " AND "))
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
// 如果表不存在SQLite 会返回错误
if strings.Contains(err.Error(), "no such table") {
continue
}
log.Err(err).Msgf("从数据库 %s 查询消息失败", dbInfo.FilePath)
continue
}
// 处理查询结果,在读取时进行过滤
for rows.Next() {
var msg model.MessageV4
err := rows.Scan(
&msg.SortSeq,
&msg.ServerID,
&msg.LocalType,
&msg.UserName,
&msg.CreateTime,
&msg.MessageContent,
&msg.PackedInfoData,
&msg.Status,
)
if err != nil {
rows.Close()
return nil, errors.ScanRowFailed(err)
}
// 将消息转换为标准格式
message := msg.Wrap(talkerItem)
// 应用sender过滤
if len(senders) > 0 {
senderMatch := false
for _, s := range senders {
if message.Sender == s {
senderMatch = true
break
}
}
if !senderMatch {
continue // 不匹配sender跳过此消息
}
}
// 应用keyword过滤
if regex != nil {
plainText := message.PlainTextContent()
if !regex.MatchString(plainText) {
continue // 不匹配keyword跳过此消息
}
}
// 通过所有过滤条件,保留此消息
filteredMessages = append(filteredMessages, message)
// 检查是否已经满足分页处理数量
if limit > 0 && len(filteredMessages) >= offset+limit {
// 已经获取了足够的消息,可以提前返回
rows.Close()
// 对所有消息按时间排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Seq < filteredMessages[j].Seq
})
// 处理分页
if offset >= len(filteredMessages) {
return []*model.Message{}, nil
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
}
return filteredMessages[offset:end], nil
}
}
rows.Close()
if limit+offset > 0 && len(totalMessages) >= limit+offset {
break
}
}
// 对所有消息按时间排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Seq < filteredMessages[j].Seq
sort.Slice(totalMessages, func(i, j int) bool {
return totalMessages[i].Seq < totalMessages[j].Seq
})
// 处理分页
if limit > 0 {
if offset >= len(filteredMessages) {
if offset >= len(totalMessages) {
return []*model.Message{}, nil
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
if end > len(totalMessages) {
end = len(totalMessages)
}
return filteredMessages[offset:end], nil
return totalMessages[offset:end], nil
}
return filteredMessages, nil
return totalMessages, nil
}
// getMessagesSingleFile 从单个数据库文件获取消息
func (ds *DataSource) getMessagesSingleFile(ctx context.Context, dbInfo MessageDBInfo, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
db, err := ds.dbm.OpenDB(dbInfo.FilePath)
if err != nil {
return nil, errors.DBConnectFailed(dbInfo.FilePath, nil)
}
// 构建表名
_talkerMd5Bytes := md5.Sum([]byte(talker))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
tableName := "Msg_" + talkerMd5
// 检查表是否存在
var exists bool
err = db.QueryRowContext(ctx,
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?",
tableName).Scan(&exists)
if err != nil {
if err == sql.ErrNoRows {
// 表不存在,返回空结果
return []*model.Message{}, nil
}
return nil, errors.QueryFailed("", err)
}
// 构建查询条件
conditions := []string{"create_time >= ? AND create_time <= ?"}
args := []interface{}{startTime.Unix(), endTime.Unix()}
query := fmt.Sprintf(`
SELECT m.sort_seq, m.server_id, m.local_type, n.user_name, m.create_time, m.message_content, m.packed_info_data, m.status
FROM %s m
LEFT JOIN Name2Id n ON m.real_sender_id = n.rowid
WHERE %s
ORDER BY m.sort_seq ASC
`, tableName, strings.Join(conditions, " AND "))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
if offset > 0 {
query += fmt.Sprintf(" OFFSET %d", offset)
}
}
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return nil, errors.QueryFailed(query, err)
}
defer rows.Close()
// 处理查询结果
messages := []*model.Message{}
for rows.Next() {
var msg model.MessageV4
err := rows.Scan(
&msg.SortSeq,
&msg.ServerID,
&msg.LocalType,
&msg.UserName,
&msg.CreateTime,
&msg.MessageContent,
&msg.PackedInfoData,
&msg.Status,
)
if err != nil {
return nil, errors.ScanRowFailed(err)
}
messages = append(messages, msg.Wrap(talker))
}
return messages, nil
}
// getMessagesFromDB 从数据库获取消息
func (ds *DataSource) getMessagesFromDB(ctx context.Context, db *sql.DB, startTime, endTime time.Time, talker string) ([]*model.Message, error) {
// 构建表名
_talkerMd5Bytes := md5.Sum([]byte(talker))
talkerMd5 := hex.EncodeToString(_talkerMd5Bytes[:])
tableName := "Msg_" + talkerMd5
// 检查表是否存在
var exists bool
err := db.QueryRowContext(ctx,
"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?",
tableName).Scan(&exists)
if err != nil {
if err == sql.ErrNoRows {
// 表不存在,返回空结果
return []*model.Message{}, nil
}
return nil, errors.QueryFailed("", err)
}
// 构建查询条件
conditions := []string{"create_time >= ? AND create_time <= ?"}
args := []interface{}{startTime.Unix(), endTime.Unix()}
query := fmt.Sprintf(`
SELECT m.sort_seq, m.server_id, m.local_type, n.user_name, m.create_time, m.message_content, m.packed_info_data, m.status
FROM %s m
LEFT JOIN Name2Id n ON m.real_sender_id = n.rowid
WHERE %s
ORDER BY m.sort_seq ASC
`, tableName, strings.Join(conditions, " AND "))
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
// 如果表不存在SQLite 会返回错误
if strings.Contains(err.Error(), "no such table") {
return []*model.Message{}, nil
}
return nil, errors.QueryFailed(query, err)
}
defer rows.Close()
// 处理查询结果
messages := []*model.Message{}
for rows.Next() {
var msg model.MessageV4
err := rows.Scan(
&msg.SortSeq,
&msg.ServerID,
&msg.LocalType,
&msg.UserName,
&msg.CreateTime,
&msg.MessageContent,
&msg.PackedInfoData,
&msg.Status,
)
if err != nil {
return nil, errors.ScanRowFailed(err)
}
messages = append(messages, msg.Wrap(talker))
}
return messages, nil
}
// 联系人

View File

@@ -2,9 +2,9 @@ package windowsv3
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"regexp"
"sort"
"strings"
"time"
@@ -16,7 +16,6 @@ import (
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
"github.com/sjzar/chatlog/internal/wechatdb/datasource/dbm"
"github.com/sjzar/chatlog/pkg/util"
)
const (
@@ -28,7 +27,7 @@ const (
Voice = "voice"
)
var Groups = []*dbm.Group{
var Groups = []dbm.Group{
{
Name: Message,
Pattern: `^MSG([0-9]?[0-9])?\.db$`,
@@ -36,7 +35,7 @@ var Groups = []*dbm.Group{
},
{
Name: Contact,
Pattern: `^MicroMsg\.db$`,
Pattern: `^MicroMsg.db$`,
BlackList: []string{},
},
{
@@ -123,10 +122,6 @@ func (ds *DataSource) initMessageDbs() error {
// 获取所有消息数据库文件路径
dbPaths, err := ds.dbm.GetDBPath(Message)
if err != nil {
if strings.Contains(err.Error(), "db file not found") {
ds.messageInfos = make([]MessageDBInfo, 0)
return nil
}
return err
}
@@ -222,38 +217,21 @@ func (ds *DataSource) getDBInfosForTimeRange(startTime, endTime time.Time) []Mes
return dbs
}
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
if talker == "" {
return nil, errors.ErrTalkerEmpty
}
// 解析talker参数支持多个talker以英文逗号分隔
talkers := util.Str2List(talker, ",")
if len(talkers) == 0 {
return nil, errors.ErrTalkerEmpty
}
// GetMessages 实现 DataSource 接口的 GetMessages 方法
func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
// 找到时间范围内的数据库文件
dbInfos := ds.getDBInfosForTimeRange(startTime, endTime)
if len(dbInfos) == 0 {
return nil, errors.TimeRangeNotFound(startTime, endTime)
}
// 解析sender参数支持多个发送者以英文逗号分隔
senders := util.Str2List(sender, ",")
// 预编译正则表达式如果有keyword
var regex *regexp.Regexp
if keyword != "" {
var err error
regex, err = regexp.Compile(keyword)
if err != nil {
return nil, errors.QueryFailed("invalid regex pattern", err)
}
if len(dbInfos) == 1 {
// LIMIT 和 OFFSET 逻辑在单文件情况下可以直接在 SQL 里处理
return ds.getMessagesSingleFile(ctx, dbInfos[0], startTime, endTime, talker, limit, offset)
}
// 从每个相关数据库中查询消息
filteredMessages := []*model.Message{}
totalMessages := []*model.Message{}
for _, dbInfo := range dbInfos {
// 检查上下文是否已取消
@@ -267,137 +245,172 @@ func (ds *DataSource) GetMessages(ctx context.Context, startTime, endTime time.T
continue
}
// 对每个talker进行查询
for _, talkerItem := range talkers {
// 构建查询条件
conditions := []string{"Sequence >= ? AND Sequence <= ?"}
args := []interface{}{startTime.Unix() * 1000, endTime.Unix() * 1000}
messages, err := ds.getMessagesFromDB(ctx, db, dbInfo, startTime, endTime, talker)
if err != nil {
log.Err(err).Msgf("从数据库 %s 获取消息失败", dbInfo.FilePath)
continue
}
// 添加talker条件
talkerID, ok := dbInfo.TalkerMap[talkerItem]
if ok {
conditions = append(conditions, "TalkerId = ?")
args = append(args, talkerID)
} else {
conditions = append(conditions, "StrTalker = ?")
args = append(args, talkerItem)
}
totalMessages = append(totalMessages, messages...)
query := fmt.Sprintf(`
SELECT MsgSvrID, Sequence, CreateTime, StrTalker, IsSender,
Type, SubType, StrContent, CompressContent, BytesExtra
FROM MSG
WHERE %s
ORDER BY Sequence ASC
`, strings.Join(conditions, " AND "))
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
// 如果表不存在跳过此talker
if strings.Contains(err.Error(), "no such table") {
continue
}
log.Err(err).Msgf("从数据库 %s 查询消息失败", dbInfo.FilePath)
continue
}
// 处理查询结果,在读取时进行过滤
for rows.Next() {
var msg model.MessageV3
var compressContent []byte
var bytesExtra []byte
err := rows.Scan(
&msg.MsgSvrID,
&msg.Sequence,
&msg.CreateTime,
&msg.StrTalker,
&msg.IsSender,
&msg.Type,
&msg.SubType,
&msg.StrContent,
&compressContent,
&bytesExtra,
)
if err != nil {
rows.Close()
return nil, errors.ScanRowFailed(err)
}
msg.CompressContent = compressContent
msg.BytesExtra = bytesExtra
// 将消息转换为标准格式
message := msg.Wrap()
// 应用sender过滤
if len(senders) > 0 {
senderMatch := false
for _, s := range senders {
if message.Sender == s {
senderMatch = true
break
}
}
if !senderMatch {
continue // 不匹配sender跳过此消息
}
}
// 应用keyword过滤
if regex != nil {
plainText := message.PlainTextContent()
if !regex.MatchString(plainText) {
continue // 不匹配keyword跳过此消息
}
}
// 通过所有过滤条件,保留此消息
filteredMessages = append(filteredMessages, message)
// 检查是否已经满足分页处理数量
if limit > 0 && len(filteredMessages) >= offset+limit {
// 已经获取了足够的消息,可以提前返回
rows.Close()
// 对所有消息按时间排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Seq < filteredMessages[j].Seq
})
// 处理分页
if offset >= len(filteredMessages) {
return []*model.Message{}, nil
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
}
return filteredMessages[offset:end], nil
}
}
rows.Close()
if limit+offset > 0 && len(totalMessages) >= limit+offset {
break
}
}
// 对所有消息按时间排序
sort.Slice(filteredMessages, func(i, j int) bool {
return filteredMessages[i].Seq < filteredMessages[j].Seq
sort.Slice(totalMessages, func(i, j int) bool {
return totalMessages[i].Seq < totalMessages[j].Seq
})
// 处理分页
if limit > 0 {
if offset >= len(filteredMessages) {
if offset >= len(totalMessages) {
return []*model.Message{}, nil
}
end := offset + limit
if end > len(filteredMessages) {
end = len(filteredMessages)
if end > len(totalMessages) {
end = len(totalMessages)
}
return filteredMessages[offset:end], nil
return totalMessages[offset:end], nil
}
return filteredMessages, nil
return totalMessages, nil
}
// getMessagesSingleFile 从单个数据库文件获取消息
func (ds *DataSource) getMessagesSingleFile(ctx context.Context, dbInfo MessageDBInfo, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
db, err := ds.dbm.OpenDB(dbInfo.FilePath)
if err != nil {
return nil, errors.DBConnectFailed(dbInfo.FilePath, nil)
}
// 构建查询条件
conditions := []string{"Sequence >= ? AND Sequence <= ?"}
args := []interface{}{startTime.Unix() * 1000, endTime.Unix() * 1000}
if len(talker) > 0 {
// TalkerId 有索引,优先使用
talkerID, ok := dbInfo.TalkerMap[talker]
if ok {
conditions = append(conditions, "TalkerId = ?")
args = append(args, talkerID)
} else {
conditions = append(conditions, "StrTalker = ?")
args = append(args, talker)
}
}
query := fmt.Sprintf(`
SELECT MsgSvrID, Sequence, CreateTime, StrTalker, IsSender,
Type, SubType, StrContent, CompressContent, BytesExtra
FROM MSG
WHERE %s
ORDER BY Sequence ASC
`, strings.Join(conditions, " AND "))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
if offset > 0 {
query += fmt.Sprintf(" OFFSET %d", offset)
}
}
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return nil, errors.QueryFailed(query, err)
}
defer rows.Close()
// 处理查询结果
totalMessages := []*model.Message{}
for rows.Next() {
var msg model.MessageV3
var compressContent []byte
var bytesExtra []byte
err := rows.Scan(
&msg.MsgSvrID,
&msg.Sequence,
&msg.CreateTime,
&msg.StrTalker,
&msg.IsSender,
&msg.Type,
&msg.SubType,
&msg.StrContent,
&compressContent,
&bytesExtra,
)
if err != nil {
return nil, errors.ScanRowFailed(err)
}
msg.CompressContent = compressContent
msg.BytesExtra = bytesExtra
totalMessages = append(totalMessages, msg.Wrap())
}
return totalMessages, nil
}
// getMessagesFromDB 从数据库获取消息
func (ds *DataSource) getMessagesFromDB(ctx context.Context, db *sql.DB, dbInfo MessageDBInfo, startTime, endTime time.Time, talker string) ([]*model.Message, error) {
// 构建查询条件
conditions := []string{"Sequence >= ? AND Sequence <= ?"}
args := []interface{}{startTime.Unix() * 1000, endTime.Unix() * 1000}
if len(talker) > 0 {
talkerID, ok := dbInfo.TalkerMap[talker]
if ok {
conditions = append(conditions, "TalkerId = ?")
args = append(args, talkerID)
} else {
conditions = append(conditions, "StrTalker = ?")
args = append(args, talker)
}
}
query := fmt.Sprintf(`
SELECT MsgSvrID, Sequence, CreateTime, StrTalker, IsSender,
Type, SubType, StrContent, CompressContent, BytesExtra
FROM MSG
WHERE %s
ORDER BY Sequence ASC
`, strings.Join(conditions, " AND "))
// 执行查询
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return nil, errors.QueryFailed(query, err)
}
defer rows.Close()
// 处理查询结果
messages := []*model.Message{}
for rows.Next() {
var msg model.MessageV3
var compressContent []byte
var bytesExtra []byte
err := rows.Scan(
&msg.MsgSvrID,
&msg.Sequence,
&msg.CreateTime,
&msg.StrTalker,
&msg.IsSender,
&msg.Type,
&msg.SubType,
&msg.StrContent,
&compressContent,
&bytesExtra,
)
if err != nil {
return nil, errors.ScanRowFailed(err)
}
msg.CompressContent = compressContent
msg.BytesExtra = bytesExtra
messages = append(messages, msg.Wrap())
}
return messages, nil
}
// GetContacts 实现获取联系人信息的方法

View File

@@ -18,8 +18,8 @@ func (r *Repository) initChatRoomCache(ctx context.Context) error {
}
chatRoomMap := make(map[string]*model.ChatRoom)
remarkToChatRoom := make(map[string][]*model.ChatRoom)
nickNameToChatRoom := make(map[string][]*model.ChatRoom)
remarkToChatRoom := make(map[string]*model.ChatRoom)
nickNameToChatRoom := make(map[string]*model.ChatRoom)
chatRoomList := make([]string, 0)
chatRoomRemark := make([]string, 0)
chatRoomNickName := make([]string, 0)
@@ -30,21 +30,11 @@ func (r *Repository) initChatRoomCache(ctx context.Context) error {
chatRoomMap[chatRoom.Name] = chatRoom
chatRoomList = append(chatRoomList, chatRoom.Name)
if chatRoom.Remark != "" {
remark, ok := remarkToChatRoom[chatRoom.Remark]
if !ok {
remark = make([]*model.ChatRoom, 0)
}
remark = append(remark, chatRoom)
remarkToChatRoom[chatRoom.Remark] = remark
remarkToChatRoom[chatRoom.Remark] = chatRoom
chatRoomRemark = append(chatRoomRemark, chatRoom.Remark)
}
if chatRoom.NickName != "" {
nickName, ok := nickNameToChatRoom[chatRoom.NickName]
if !ok {
nickName = make([]*model.ChatRoom, 0)
}
nickName = append(nickName, chatRoom)
nickNameToChatRoom[chatRoom.NickName] = nickName
nickNameToChatRoom[chatRoom.NickName] = chatRoom
chatRoomNickName = append(chatRoomNickName, chatRoom.NickName)
}
}
@@ -59,21 +49,11 @@ func (r *Repository) initChatRoomCache(ctx context.Context) error {
chatRoomMap[contact.UserName] = chatRoom
chatRoomList = append(chatRoomList, contact.UserName)
if contact.Remark != "" {
remark, ok := remarkToChatRoom[chatRoom.Remark]
if !ok {
remark = make([]*model.ChatRoom, 0)
}
remark = append(remark, chatRoom)
remarkToChatRoom[chatRoom.Remark] = remark
remarkToChatRoom[contact.Remark] = chatRoom
chatRoomRemark = append(chatRoomRemark, contact.Remark)
}
if contact.NickName != "" {
nickName, ok := nickNameToChatRoom[chatRoom.NickName]
if !ok {
nickName = make([]*model.ChatRoom, 0)
}
nickName = append(nickName, chatRoom)
nickNameToChatRoom[chatRoom.NickName] = nickName
nickNameToChatRoom[contact.NickName] = chatRoom
chatRoomNickName = append(chatRoomNickName, contact.NickName)
}
}
@@ -83,12 +63,9 @@ func (r *Repository) initChatRoomCache(ctx context.Context) error {
sort.Strings(chatRoomNickName)
r.chatRoomCache = chatRoomMap
r.chatRoomList = chatRoomList
r.remarkToChatRoom = remarkToChatRoom
r.nickNameToChatRoom = nickNameToChatRoom
r.chatRoomList = chatRoomList
r.chatRoomRemark = chatRoomRemark
r.chatRoomNickName = chatRoomNickName
return nil
}
@@ -98,7 +75,7 @@ func (r *Repository) GetChatRooms(ctx context.Context, key string, limit, offset
if key != "" {
ret = r.findChatRooms(key)
if len(ret) == 0 {
return []*model.ChatRoom{}, nil
return nil, errors.ChatRoomNotFound(key)
}
if limit > 0 {
@@ -152,21 +129,21 @@ func (r *Repository) findChatRoom(key string) *model.ChatRoom {
return chatRoom
}
if chatRoom, ok := r.remarkToChatRoom[key]; ok {
return chatRoom[0]
return chatRoom
}
if chatRoom, ok := r.nickNameToChatRoom[key]; ok {
return chatRoom[0]
return chatRoom
}
// Contain
for _, remark := range r.chatRoomRemark {
if strings.Contains(remark, key) {
return r.remarkToChatRoom[remark][0]
return r.remarkToChatRoom[remark]
}
}
for _, nickName := range r.chatRoomNickName {
if strings.Contains(nickName, key) {
return r.nickNameToChatRoom[nickName][0]
return r.nickNameToChatRoom[nickName]
}
}
@@ -180,42 +157,26 @@ func (r *Repository) findChatRooms(key string) []*model.ChatRoom {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
if chatRooms, ok := r.remarkToChatRoom[key]; ok {
for _, chatRoom := range chatRooms {
if !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
}
if chatRoom, ok := r.remarkToChatRoom[key]; ok && !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
if chatRooms, ok := r.nickNameToChatRoom[key]; ok {
for _, chatRoom := range chatRooms {
if !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
}
if chatRoom, ok := r.nickNameToChatRoom[key]; ok && !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
// Contain
for _, remark := range r.chatRoomRemark {
if strings.Contains(remark, key) {
for _, chatRoom := range r.remarkToChatRoom[remark] {
if !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
}
if strings.Contains(remark, key) && !distinct[r.remarkToChatRoom[remark].Name] {
ret = append(ret, r.remarkToChatRoom[remark])
distinct[r.remarkToChatRoom[remark].Name] = true
}
}
for _, nickName := range r.chatRoomNickName {
if strings.Contains(nickName, key) {
for _, chatRoom := range r.nickNameToChatRoom[nickName] {
if !distinct[chatRoom.Name] {
ret = append(ret, chatRoom)
distinct[chatRoom.Name] = true
}
}
if strings.Contains(nickName, key) && !distinct[r.nickNameToChatRoom[nickName].Name] {
ret = append(ret, r.nickNameToChatRoom[nickName])
distinct[r.nickNameToChatRoom[nickName].Name] = true
}
}

View File

@@ -18,9 +18,9 @@ func (r *Repository) initContactCache(ctx context.Context) error {
}
contactMap := make(map[string]*model.Contact)
aliasMap := make(map[string][]*model.Contact)
remarkMap := make(map[string][]*model.Contact)
nickNameMap := make(map[string][]*model.Contact)
aliasMap := make(map[string]*model.Contact)
remarkMap := make(map[string]*model.Contact)
nickNameMap := make(map[string]*model.Contact)
chatRoomUserMap := make(map[string]*model.Contact)
chatRoomInContactMap := make(map[string]*model.Contact)
contactList := make([]string, 0)
@@ -34,30 +34,15 @@ func (r *Repository) initContactCache(ctx context.Context) error {
// 建立快速查找索引
if contact.Alias != "" {
alias, ok := aliasMap[contact.Alias]
if !ok {
alias = make([]*model.Contact, 0)
}
alias = append(alias, contact)
aliasMap[contact.Alias] = alias
aliasMap[contact.Alias] = contact
aliasList = append(aliasList, contact.Alias)
}
if contact.Remark != "" {
remark, ok := remarkMap[contact.Remark]
if !ok {
remark = make([]*model.Contact, 0)
}
remark = append(remark, contact)
remarkMap[contact.Remark] = remark
remarkMap[contact.Remark] = contact
remarkList = append(remarkList, contact.Remark)
}
if contact.NickName != "" {
nickName, ok := nickNameMap[contact.NickName]
if !ok {
nickName = make([]*model.Contact, 0)
}
nickName = append(nickName, contact)
nickNameMap[contact.NickName] = nickName
nickNameMap[contact.NickName] = contact
nickNameList = append(nickNameList, contact.NickName)
}
@@ -103,7 +88,7 @@ func (r *Repository) GetContacts(ctx context.Context, key string, limit, offset
if key != "" {
ret = r.findContacts(key)
if len(ret) == 0 {
return []*model.Contact{}, nil
return nil, errors.ContactNotFound(key)
}
if limit > 0 {
end := offset + limit
@@ -139,29 +124,29 @@ func (r *Repository) findContact(key string) *model.Contact {
return contact
}
if contact, ok := r.aliasToContact[key]; ok {
return contact[0]
return contact
}
if contact, ok := r.remarkToContact[key]; ok {
return contact[0]
return contact
}
if contact, ok := r.nickNameToContact[key]; ok {
return contact[0]
return contact
}
// Contain
for _, alias := range r.aliasList {
if strings.Contains(alias, key) {
return r.aliasToContact[alias][0]
return r.aliasToContact[alias]
}
}
for _, remark := range r.remarkList {
if strings.Contains(remark, key) {
return r.remarkToContact[remark][0]
return r.remarkToContact[remark]
}
}
for _, nickName := range r.nickNameList {
if strings.Contains(nickName, key) {
return r.nickNameToContact[nickName][0]
return r.nickNameToContact[nickName]
}
}
return nil
@@ -174,62 +159,37 @@ func (r *Repository) findContacts(key string) []*model.Contact {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
if contacts, ok := r.aliasToContact[key]; ok {
for _, contact := range contacts {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if contact, ok := r.aliasToContact[key]; ok && !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
if contacts, ok := r.remarkToContact[key]; ok {
for _, contact := range contacts {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if contact, ok := r.remarkToContact[key]; ok && !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
if contacts, ok := r.nickNameToContact[key]; ok {
for _, contact := range contacts {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if contact, ok := r.nickNameToContact[key]; ok && !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
// Contain
for _, alias := range r.aliasList {
if strings.Contains(alias, key) {
for _, contact := range r.aliasToContact[alias] {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if strings.Contains(alias, key) && !distinct[r.aliasToContact[alias].UserName] {
ret = append(ret, r.aliasToContact[alias])
distinct[r.aliasToContact[alias].UserName] = true
}
}
for _, remark := range r.remarkList {
if strings.Contains(remark, key) {
for _, contact := range r.remarkToContact[remark] {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if strings.Contains(remark, key) && !distinct[r.remarkToContact[remark].UserName] {
ret = append(ret, r.remarkToContact[remark])
distinct[r.remarkToContact[remark].UserName] = true
}
}
for _, nickName := range r.nickNameList {
if strings.Contains(nickName, key) {
for _, contact := range r.nickNameToContact[nickName] {
if !distinct[contact.UserName] {
ret = append(ret, contact)
distinct[contact.UserName] = true
}
}
if strings.Contains(nickName, key) && !distinct[r.nickNameToContact[nickName].UserName] {
ret = append(ret, r.nickNameToContact[nickName])
distinct[r.nickNameToContact[nickName].UserName] = true
}
}
return ret
}

View File

@@ -2,20 +2,23 @@ package repository
import (
"context"
"strings"
"time"
"github.com/sjzar/chatlog/internal/model"
"github.com/sjzar/chatlog/pkg/util"
"github.com/rs/zerolog/log"
)
// GetMessages 实现 Repository 接口的 GetMessages 方法
func (r *Repository) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
func (r *Repository) GetMessages(ctx context.Context, startTime, endTime time.Time, talker string, limit, offset int) ([]*model.Message, error) {
talker, sender = r.parseTalkerAndSender(ctx, talker, sender)
messages, err := r.ds.GetMessages(ctx, startTime, endTime, talker, sender, keyword, limit, offset)
if contact, _ := r.GetContact(ctx, talker); contact != nil {
talker = contact.UserName
} else if chatRoom, _ := r.GetChatRoom(ctx, talker); chatRoom != nil {
talker = chatRoom.Name
}
messages, err := r.ds.GetMessages(ctx, startTime, endTime, talker, limit, offset)
if err != nil {
return nil, err
}
@@ -59,53 +62,3 @@ func (r *Repository) enrichMessage(msg *model.Message) {
}
}
}
func (r *Repository) parseTalkerAndSender(ctx context.Context, talker, sender string) (string, string) {
displayName2User := make(map[string]string)
users := make(map[string]bool)
talkers := util.Str2List(talker, ",")
if len(talkers) > 0 {
for i := 0; i < len(talkers); i++ {
if contact, _ := r.GetContact(ctx, talkers[i]); contact != nil {
talkers[i] = contact.UserName
} else if chatRoom, _ := r.GetChatRoom(ctx, talker); chatRoom != nil {
talkers[i] = chatRoom.Name
}
}
// 获取群聊的用户列表
for i := 0; i < len(talkers); i++ {
if chatRoom, _ := r.GetChatRoom(ctx, talkers[i]); chatRoom != nil {
for user, displayName := range chatRoom.User2DisplayName {
displayName2User[displayName] = user
}
for _, user := range chatRoom.Users {
users[user.UserName] = true
}
}
}
talker = strings.Join(talkers, ",")
}
senders := util.Str2List(sender, ",")
if len(senders) > 0 {
for i := 0; i < len(senders); i++ {
if user, ok := displayName2User[senders[i]]; ok {
senders[i] = user
} else {
// FIXME 大量群聊用户名称重复,无法直接通过 GetContact 获取 ID后续再优化
for user := range users {
if contact := r.getFullContact(user); contact != nil {
if contact.DisplayName() == senders[i] {
senders[i] = user
break
}
}
}
}
}
sender = strings.Join(senders, ",")
}
return talker, sender
}

View File

@@ -17,9 +17,9 @@ type Repository struct {
// Cache for contact
contactCache map[string]*model.Contact
aliasToContact map[string][]*model.Contact
remarkToContact map[string][]*model.Contact
nickNameToContact map[string][]*model.Contact
aliasToContact map[string]*model.Contact
remarkToContact map[string]*model.Contact
nickNameToContact map[string]*model.Contact
chatRoomInContact map[string]*model.Contact
contactList []string
aliasList []string
@@ -28,8 +28,8 @@ type Repository struct {
// Cache for chat room
chatRoomCache map[string]*model.ChatRoom
remarkToChatRoom map[string][]*model.ChatRoom
nickNameToChatRoom map[string][]*model.ChatRoom
remarkToChatRoom map[string]*model.ChatRoom
nickNameToChatRoom map[string]*model.ChatRoom
chatRoomList []string
chatRoomRemark []string
chatRoomNickName []string
@@ -43,17 +43,17 @@ func New(ds datasource.DataSource) (*Repository, error) {
r := &Repository{
ds: ds,
contactCache: make(map[string]*model.Contact),
aliasToContact: make(map[string][]*model.Contact),
remarkToContact: make(map[string][]*model.Contact),
nickNameToContact: make(map[string][]*model.Contact),
aliasToContact: make(map[string]*model.Contact),
remarkToContact: make(map[string]*model.Contact),
nickNameToContact: make(map[string]*model.Contact),
chatRoomUserToInfo: make(map[string]*model.Contact),
contactList: make([]string, 0),
aliasList: make([]string, 0),
remarkList: make([]string, 0),
nickNameList: make([]string, 0),
chatRoomCache: make(map[string]*model.ChatRoom),
remarkToChatRoom: make(map[string][]*model.ChatRoom),
nickNameToChatRoom: make(map[string][]*model.ChatRoom),
remarkToChatRoom: make(map[string]*model.ChatRoom),
nickNameToChatRoom: make(map[string]*model.ChatRoom),
chatRoomList: make([]string, 0),
chatRoomRemark: make([]string, 0),
chatRoomNickName: make([]string, 0),

View File

@@ -57,11 +57,11 @@ func (w *DB) Initialize() error {
return nil
}
func (w *DB) GetMessages(start, end time.Time, talker string, sender string, keyword string, limit, offset int) ([]*model.Message, error) {
func (w *DB) GetMessages(start, end time.Time, talker string, limit, offset int) ([]*model.Message, error) {
ctx := context.Background()
// 使用 repository 获取消息
messages, err := w.repo.GetMessages(ctx, start, end, talker, sender, keyword, limit, offset)
messages, err := w.repo.GetMessages(ctx, start, end, talker, limit, offset)
if err != nil {
return nil, err
}

View File

@@ -17,7 +17,6 @@ import (
// Format defines the header and extension for different image types
type Format struct {
Header []byte
AesKey []byte
Ext string
}
@@ -30,13 +29,10 @@ var (
BMP = Format{Header: []byte{0x42, 0x4D}, Ext: "bmp"}
Formats = []Format{JPG, PNG, GIF, TIFF, BMP}
V4Format1 = Format{Header: []byte{0x07, 0x08, 0x56, 0x31}, AesKey: []byte("cfcd208495d565ef")}
V4Format2 = Format{Header: []byte{0x07, 0x08, 0x56, 0x32}, AesKey: []byte("0000000000000000")} // FIXME
V4Formats = []Format{V4Format1, V4Format2}
// WeChat v4 related constants
V4XorKey byte = 0x37 // Default XOR key for WeChat v4 dat files
JpgTail = []byte{0xFF, 0xD9} // JPG file tail marker
V4XorKey byte = 0x37 // Default XOR key for WeChat v4 dat files
V4DatHeader = []byte{0x07, 0x08, 0x56, 0x31} // WeChat v4 dat file header
JpgTail = []byte{0xFF, 0xD9} // JPG file tail marker
)
// Dat2Image converts WeChat dat file data to image data
@@ -47,12 +43,8 @@ func Dat2Image(data []byte) ([]byte, string, error) {
}
// Check if this is a WeChat v4 dat file
if len(data) >= 6 {
for _, format := range V4Formats {
if bytes.Equal(data[:4], format.Header) {
return Dat2ImageV4(data, format.AesKey)
}
}
if len(data) >= 6 && bytes.Equal(data[:4], V4DatHeader) {
return Dat2ImageV4(data)
}
// For older WeChat versions, use XOR decryption
@@ -142,7 +134,7 @@ func ScanAndSetXorKey(dirPath string) (byte, error) {
}
// Check if it's a WeChat v4 dat file
if len(data) < 6 || (!bytes.Equal(data[:4], V4Format1.Header) && !bytes.Equal(data[:4], V4Format2.Header)) {
if len(data) < 6 || !bytes.Equal(data[:4], V4DatHeader) {
return nil
}
@@ -187,13 +179,13 @@ func ScanAndSetXorKey(dirPath string) (byte, error) {
// Dat2ImageV4 processes WeChat v4 dat image files
// WeChat v4 uses a combination of AES-ECB and XOR encryption
func Dat2ImageV4(data []byte, aeskey []byte) ([]byte, string, error) {
func Dat2ImageV4(data []byte) ([]byte, string, error) {
if len(data) < 15 {
return nil, "", fmt.Errorf("data length is too short for WeChat v4 format: %d", len(data))
}
// Parse dat file header:
// - 6 bytes: 0x07085631 or 0x07085632 (dat file identifier)
// - 6 bytes: 0x07085631 (dat file identifier)
// - 4 bytes: int (little-endian) AES-ECB128 encryption length
// - 4 bytes: int (little-endian) XOR encryption length
// - 1 byte: 0x01 (unknown)
@@ -214,7 +206,7 @@ func Dat2ImageV4(data []byte, aeskey []byte) ([]byte, string, error) {
}
// Decrypt AES part
aesDecryptedData, err := decryptAESECB(fileData[:aesEncryptLen0], aeskey)
aesDecryptedData, err := decryptAESECB(fileData[:aesEncryptLen0], []byte("cfcd208495d565ef"))
if err != nil {
return nil, "", fmt.Errorf("AES decrypt error: %v", err)
}

View File

@@ -3,7 +3,6 @@ package util
import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
@@ -46,26 +45,3 @@ func IsNumeric(s string) bool {
func SplitInt64ToTwoInt32(input int64) (int64, int64) {
return input & 0xFFFFFFFF, input >> 32
}
func Str2List(str string, sep string) []string {
list := make([]string, 0)
if str == "" {
return list
}
listMap := make(map[string]bool)
for _, elem := range strings.Split(str, sep) {
elem = strings.TrimSpace(elem)
if len(elem) == 0 {
continue
}
if _, ok := listMap[elem]; ok {
continue
}
listMap[elem] = true
list = append(list, elem)
}
return list
}

View File

@@ -582,8 +582,8 @@ func adjustStartTime(t time.Time, g TimeGranularity) time.Time {
func adjustEndTime(t time.Time, g TimeGranularity) time.Time {
switch g {
case GranularitySecond, GranularityMinute, GranularityHour:
// 对于精确到秒/分钟/小时的时间,保持原样
return t
// 对于精确到秒/分钟/小时的时间,设置为当天结束
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, t.Location())
case GranularityDay:
// 精确到天,设置为当天结束
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, t.Location())
@@ -634,25 +634,3 @@ func isValidDate(year, month, day int) bool {
return day <= daysInMonth
}
func PerfectTimeFormat(start time.Time, end time.Time) string {
endTime := end
// 如果结束时间是某一天的 0 点整,将其减去 1 秒,视为前一天的结束
if endTime.Hour() == 0 && endTime.Minute() == 0 && endTime.Second() == 0 && endTime.Nanosecond() == 0 {
endTime = endTime.Add(-time.Second) // 减去 1 秒
}
// 判断是否跨年
if start.Year() != endTime.Year() {
return "2006-01-02 15:04:05" // 完整格式,包含年月日时分秒
}
// 判断是否跨天(但在同一年内)
if start.YearDay() != endTime.YearDay() {
return "01-02 15:04:05" // 月日时分秒格式
}
// 在同一天内
return "15:04:05" // 只显示时分秒
}