Skip to content

模块系统

SirrChat 采用模块化架构,允许您根据需求定制和扩展功能。

模块类型

认证模块 (Auth)

处理用户身份验证。

go
type AuthModule interface {
    Name() string
    Authenticate(username, credential string) (bool, error)
    Supports(method string) bool
}

存储模块 (Storage)

管理邮件和数据存储。

go
type StorageModule interface {
    Save(mailbox string, message *Message) error
    Load(mailbox string, uid uint32) (*Message, error)
    Delete(mailbox string, uid uint32) error
}

过滤模块 (Filter)

邮件内容过滤和处理。

go
type FilterModule interface {
    Filter(ctx *Context, msg *Message) (Action, error)
}

内置模块

blockchain-auth

区块链钱包认证模块。

配置:

toml
[[modules.auth]]
name = "blockchain-auth"
enabled = true

[modules.auth.config]
networks = ["ethereum", "bsc"]

ldap-auth

LDAP 目录服务认证。

配置:

toml
[[modules.auth]]
name = "ldap-auth"
enabled = true

[modules.auth.config]
server = "ldap://ldap.example.com"

spam-filter

垃圾邮件过滤模块。

配置:

toml
[[modules.filter]]
name = "spam-filter"
enabled = true

[modules.filter.config]
threshold = 5.0
quarantine = true

dkim-signer

DKIM 邮件签名模块。

配置:

toml
[[modules.modify]]
name = "dkim-signer"
enabled = true

[modules.modify.config]
selector = "default"
private_key = "/etc/sirrchatd/dkim/private.key"

自定义模块

创建模块

go
package mymodule

import "github.com/mail-chat-chain/mailchatd/module"

type MyFilter struct {
    config Config
}

func (f *MyFilter) Name() string {
    return "my-filter"
}

func (f *MyFilter) Init(cfg map[string]interface{}) error {
    // 初始化配置
    return nil
}

func (f *MyFilter) Filter(ctx *module.Context, msg *module.Message) (module.Action, error) {
    // 实现过滤逻辑
    if someCondition(msg) {
        return module.ActionReject, nil
    }
    return module.ActionAccept, nil
}

注册模块

go
func init() {
    module.Register("my-filter", &MyFilter{})
}

配置模块

toml
[[modules.filter]]
name = "my-filter"
enabled = true

[modules.filter.config]
custom_option = "value"

模块管理

列出模块

bash
sirrchatd module list

输出:

Auth Modules:
  - blockchain-auth (enabled)
  - ldap-auth (disabled)
  - pam-auth (enabled)

Filter Modules:
  - spam-filter (enabled)
  - virus-scanner (enabled)

Storage Modules:
  - maildir (enabled)
  - s3 (disabled)

启用/禁用模块

bash
# 启用模块
sirrchatd module enable spam-filter

# 禁用模块
sirrchatd module disable spam-filter

重载模块

bash
sirrchatd module reload --name spam-filter

模块配置

全局配置

toml
[modules]
# 模块加载路径
load_path = "/usr/lib/sirrchatd/modules"

# 自动加载
auto_load = true

# 模块超时
timeout = "30s"

模块优先级

toml
[[modules.filter]]
name = "spam-filter"
priority = 100  # 数字越小优先级越高

[[modules.filter]]
name = "virus-scanner"
priority = 50

模块钩子

可用钩子

  • pre_auth: 认证前
  • post_auth: 认证后
  • pre_receive: 接收邮件前
  • post_receive: 接收邮件后
  • pre_send: 发送邮件前
  • post_send: 发送邮件后

钩子示例

go
func (m *MyModule) PreReceive(ctx *Context, msg *Message) error {
    // 在接收邮件前执行
    log.Printf("Receiving email from %s", msg.From)
    return nil
}

模块开发

开发环境设置

bash
# 克隆模块模板
git clone https://github.com/mail-chat-chain/module-template.git my-module
cd my-module

# 安装依赖
go mod download

# 构建模块
go build -buildmode=plugin -o my-module.so

测试模块

go
package mymodule_test

import (
    "testing"
    "github.com/mail-chat-chain/mailchatd/module"
)

func TestMyModule(t *testing.T) {
    m := &MyModule{}
    err := m.Init(map[string]interface{}{})
    if err != nil {
        t.Fatal(err)
    }

    // 测试逻辑
}

打包发布

bash
# 构建发布版本
go build -ldflags="-s -w" -buildmode=plugin

# 安装到系统
sudo cp my-module.so /usr/lib/sirrchatd/modules/

常用模块

rate-limiter

速率限制模块。

toml
[[modules.filter]]
name = "rate-limiter"

[modules.filter.config]
max_per_hour = 100
max_per_minute = 10

auto-reply

自动回复模块。

toml
[[modules.modify]]
name = "auto-reply"

[modules.modify.config]
enabled_users = ["user@example.com"]
message = "I'm out of office"

archive

邮件归档模块。

toml
[[modules.storage]]
name = "archive"

[modules.storage.config]
retention_days = 365
compress = true

模块性能

性能监控

bash
sirrchatd module stats

输出:

Module          Calls    Avg Time    Max Time
spam-filter     1,234    45ms        250ms
dkim-signer     1,234    12ms        45ms
virus-scanner   1,234    123ms       890ms

性能优化

toml
[modules.performance]
# 并发处理
parallel = true
max_goroutines = 10

# 缓存
enable_cache = true
cache_size_mb = 64

故障排除

调试模式

toml
[modules.debug]
enabled = true
log_level = "debug"
trace_calls = true

模块日志

bash
# 查看模块日志
sirrchatd module logs --name spam-filter

# 实时日志
sirrchatd module logs --name spam-filter --follow

相关文档:

Released under the GPL 3.0 License.