# d99kris/nchat architecture

> Architecture diagram of the GitHub repository d99kris/nchat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/d99kris/nchat
- Repository: https://github.com/d99kris/nchat

## Overview

##### Project Type and Purpose
- nchat is a terminal-based, full-stack chat client supporting Telegram and WhatsApp.
- It’s implemented primarily in C++ (UI, core engine, protocol wrappers) with parts in Go (WhatsApp via whatsmeow).
- Key purpose: provide a lightweight ncurses UI, handle message serialization, protocol abstraction (Telegram via tdlib, WhatsApp via whatsmeow), local caching, configuration and theming.

##### File Structure
- Top level:
- CMakeLists.txt, make.sh, build scripts, workflows (.github)
- src/ – main.cpp and dozens of u*.cpp/h for ncurses UI (views, controllers, dialogs).
- lib/ – protocol modules and common utilities:
– lib/common + lib/duchat + lib/ncutil: shared data structures, serialization, caching, logging, file utilities, thread primitives.
– lib/tgchat: C++ wrapper around tdlib (ext/td) for Telegram API.
– lib/wmchat: Go wrapper around whatsmeow (ext/whatsmeow) for WhatsApp API.
- ext/ – embedded third-party libs: apathy, cereal, clip, sqlite_modern_cpp, tdlib source, whatsmeow source.
- themes/, utils/, dev/, doc/: configuration templates, update scripts, documentation.
- Architectural pattern: MVC/MVP—UI views/controllers in src/ talk to core “Model” in lib/, which interfaces with protocol engines.

##### README for Insights
- Describes UI commands, configuration files, themes, build/dependencies.
- Lists third-party libraries and update scripts.
- Mentions cache (SQLite) and serialization (cereal).
- Confirms two protocol pipelines: Telegram and WhatsApp.

##### Main Components and Their Interactions
a. Components
- UI Layer (ncurses) [src/]: screens, dialogs, input handling, theming.
- Controller/Model Layer [src/uicontroller.cpp, lib/*]: translates UI actions into chat operations.
- Protocol Adapters:
- Telegram Module [lib/tgchat + ext/tdlib]: authentication, message send/receive via tdlib.
- WhatsApp Module [lib/wmchat + ext/whatsmeow]: similar role in Go.
- Utility Library [lib/ncutil, lib/common]: configuration, file I/O, caching, logging, synchronization.
- Data Layer (cache) [sqlite_modern_cpp]: local message history, settings, profiles.
- Serialization [ext/cereal]: encode/decode messages, settings.
- Clipboard Integration [ext/clip].
- Build & Deployment (CMake, make.sh).
b. Relationships / Data Flow
- UI → Controller → Protocol Adapter → External Library → Network (Telegram/WhatsApp servers).
- Incoming network events → Library → Adapter → Model → UI update.
- Model ↔ Data Layer (SQLite cache via sqlite_modern_cpp).
- Model → Serialization (cereal) for config, history export.
- UI → Clipboard (clip) for copy/paste.
c. Patterns / Principles
- MVC/MVP: separation of UI (views/controllers) from business logic (models, protocol adapters).
- Layered architecture: UI, Core, Protocol, Data, External.
- Plugin-like protocol adapters for extensibility.
- Dependency injection via shared lib/ncutil utilities.
- CMake-driven modular build.
d. Technologies / Frameworks
- ncurses (UI)
- C++17, Go
- CMake, make.sh, help2man
- SQLite (sqlite_modern_cpp)
- third-party: cereal (serialization), clip (clipboard), apathy (logging), tdlib, whatsmeow

##### Detailed Capture
- Update scripts (utils/tdlib-update, whatsmeow-update) linked to ext/tdlib and ext/whatsmeow.
- Build flow: CMakeLists → compiles modules → links into nchat binary (and Go binary for WhatsApp).

## Diagram (Mermaid)

```mermaid
graph TB
  subgraph "nchat Application"
    subgraph "UI Layer"
      UI_UILayer["UI (ncurses)"]:::ui
    end
    subgraph "Controllers & Model"
      UIController["UI Controllers"]:::core
      UIModel["UI Models"]:::core
    end
    subgraph "Protocol Layer"
      subgraph Telegram
        TelegramAdapter["Telegram Adapter"]:::adapter
        Tdlib["tdlib"]:::extlib
      end
      subgraph WhatsApp
        WhatsAppAdapter["WhatsApp Adapter"]:::adapter
        Whatsmeow["whatsmeow"]:::extlib
      end
    end
    subgraph "Utility Layer"
      NCUtil["lib/ncutil"]:::util
      Common["lib/common"]:::util
    end
    Cache["SQLite Cache"]:::data
    Cereal["Serialization (cereal)"]:::extlib
    Clip["Clipboard Integration"]:::extlib
    Themes["Themes & Config"]:::conf
    Build["Build & Deployment"]:::build
  end

  ExternalServiceT["Telegram Server"]:::external
  ExternalServiceW["WhatsApp Server"]:::external

  UI_UILayer --> UIController
  UIController --> TelegramAdapter
  UIController --> WhatsAppAdapter
  UIModel --> TelegramAdapter
  UIModel --> WhatsAppAdapter
  TelegramAdapter --> Tdlib
  WhatsAppAdapter --> Whatsmeow
  Tdlib --> ExternalServiceT
  ExternalServiceT --> Tdlib
  Whatsmeow --> ExternalServiceW
  ExternalServiceW --> Whatsmeow
  UIController <--> UIModel
  UIController <--> Cache
  UIModel <--> Cache
  UIController --> Cereal
  UIController --> Clip
  UI_UILayer --> Clip
  Themes --> UI_UILayer
  Themes --> UIController
  NCUtil --> UI_UILayer
  NCUtil --> UIController
  NCUtil --> TelegramAdapter
  NCUtil --> WhatsAppAdapter
  Common --> UI_UILayer
  Common --> UIController
  Common --> TelegramAdapter
  Common --> WhatsAppAdapter
  Build --> UI_UILayer
  Build --> UIController
  Build --> TelegramAdapter
  Build --> WhatsAppAdapter
  Build --> NCUtil
  Build --> Common

  click UI_UILayer "https://github.com/d99kris/nchat/tree/master/src/"
  click UIController "https://github.com/d99kris/nchat/blob/master/src/uicontroller.cpp"
  click UIModel "https://github.com/d99kris/nchat/blob/master/src/uimodel.cpp"
  click TelegramAdapter "https://github.com/d99kris/nchat/blob/master/lib/tgchat/src/tgchat.cpp"
  click WhatsAppAdapter "https://github.com/d99kris/nchat/blob/master/lib/wmchat/src/wmchat.cpp"
  click NCUtil "https://github.com/d99kris/nchat/tree/master/lib/ncutil/"
  click Common "https://github.com/d99kris/nchat/tree/master/lib/common/"
  click Cache "https://github.com/d99kris/nchat/tree/master/ext/sqlite_modern_cpp/"
  click Cereal "https://github.com/d99kris/nchat/tree/master/ext/cereal/"
  click Clip "https://github.com/d99kris/nchat/tree/master/ext/clip/"
  click Themes "https://github.com/d99kris/nchat/tree/master/themes/"
  click Build "https://github.com/d99kris/nchat/blob/master/CMakeLists.txt"

  classDef ui fill:#D0E8FF,stroke:#036,stroke-width:1px
  classDef core fill:#D0FFD8,stroke:#070,stroke-width:1px
  classDef adapter fill:#E0FFD8,stroke:#070,stroke-width:1px
  classDef util fill:#F0E0FF,stroke:#606,stroke-width:1px
  classDef data fill:#FFE0B2,stroke:#A65,stroke-width:1px
  classDef extlib fill:#F8F8F8,stroke:#888,stroke-dasharray:2 2
  classDef conf fill:#FFE0F0,stroke:#C05,stroke-width:1px
  classDef build fill:#FFF3BF,stroke:#B50,stroke-width:1px
  classDef external fill:#FFFBCC,stroke:#CC0,stroke-dasharray:5 5
```
