From 9b4a09677a352d9938e4505bfc2d0c8d304567ac Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 22 Oct 2019 19:36:54 +0300 Subject: [PATCH] Hello world XMPP echo component --- .gitignore | 2 ++ Makefile | 2 ++ config.yml.example | 23 +++++++++++++++++ config/config.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 8 ++++++ go.sum | 8 ++++++ telegabber.go | 18 +++++++++++++ xmpp/component.go | 30 ++++++++++++++++++++++ xmpp/handlers.go | 21 +++++++++++++++ 9 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 config.yml.example create mode 100644 config/config.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 telegabber.go create mode 100644 xmpp/component.go create mode 100644 xmpp/handlers.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bda8001 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.yml +telegabber diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d401472 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + go build -o telegabber diff --git a/config.yml.example b/config.yml.example new file mode 100644 index 0000000..82945b4 --- /dev/null +++ b/config.yml.example @@ -0,0 +1,23 @@ +:telegram: + :loglevel: :warn + :content: + :path: '/var/www/telegabber/content' # webserver workdir + :link: 'http://tlgrm.localhost/content' # webserver public address + :upload: 'https:///xmppfiles.localhost' # xmpp http upload address + :tdlib_verbosity: 1 + :tdlib: + :lib_path: 'lib/' + :client: + :api_id: '17349' + :api_hash: '344583e45741c457fe1862106095a5eb' + :device_model: 'telegabber' + :application_version: '2.0' + :use_chat_info_database: false + +:xmpp: + :loglevel: :warn + :jid: 'tlgrm.localhost' + :host: '127.0.0.1' + :port: 8899 + :password: 'password' + :db: 'sessions.dat' diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..be05cb5 --- /dev/null +++ b/config/config.go @@ -0,0 +1,64 @@ +package config + +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +type Config struct { + Telegram TelegramConfig `yaml:":telegram"` + Xmpp XmppConfig `yaml:":xmpp"` +} + +type XmppConfig struct { + Loglevel string `yaml:":loglevel"` + Jid string `yaml:":jid"` + Host string `yaml:":host"` + Port string `yaml:":port"` + Password string `yaml:":password"` + Db string `yaml:":db"` +} + +type TelegramConfig struct { + Loglevel string `yaml:":loglevel"` + Content TelegramContentConfig `yaml:":content"` + Verbosity uint8 `yaml:":tdlib_verbosity"` + Tdlib TelegramTdlibConfig `yaml:":tdlib"` +} + +type TelegramContentConfig struct { + Path string `yaml:":path"` + Link string `yaml:":link"` + Upload string `yaml:":upload"` +} + +type TelegramTdlibConfig struct { + Path string `yaml:":lib_path"` + Client TelegramTdlibClientConfig `yaml:":client"` +} + +type TelegramTdlibClientConfig struct { + ApiId string `yaml:":api_id"` + ApiHash string `yaml:":api_hash"` + DeviceModel string `yaml:":device_model"` + ApplicationVersion string `yaml:":application_version"` + UseChatInfoDatabase bool `yaml:":use_chat_info_database"` +} + +func ReadConfig(path string) Config { + var config Config + + file, err := ioutil.ReadFile(path) + if err != nil { + log.Fatalf("Can't open config file: %v", err) + } + + err = yaml.Unmarshal(file, &config) + if err != nil { + log.Fatalf("Error parsing config: %v", err) + } + + return config +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0af1591 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module dev.narayana.im/narayana/telegabber + +go 1.13 + +require ( + gopkg.in/yaml.v2 v2.2.4 + gosrc.io/xmpp v0.1.3 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e3d167a --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gosrc.io/xmpp v0.1.3 h1:VYP1bA35irlQ1ZAJqNhJOz8NSsSTkzQRhREfmuG1H80= +gosrc.io/xmpp v0.1.3/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY= diff --git a/telegabber.go b/telegabber.go new file mode 100644 index 0000000..dd1e187 --- /dev/null +++ b/telegabber.go @@ -0,0 +1,18 @@ +package main + +import ( + "log" + + "dev.narayana.im/narayana/telegabber/config" + "dev.narayana.im/narayana/telegabber/xmpp" +) + +const CONFIG_PATH string = "config.yml" + +func main() { + config := config.ReadConfig(CONFIG_PATH) + cm := xmpp.NewComponent(config.Xmpp) + + // reconnect automatically + log.Fatal(cm.Run()) +} diff --git a/xmpp/component.go b/xmpp/component.go new file mode 100644 index 0000000..e484e99 --- /dev/null +++ b/xmpp/component.go @@ -0,0 +1,30 @@ +package xmpp + +import ( + "log" + + "dev.narayana.im/narayana/telegabber/config" + + "gosrc.io/xmpp" +) + +func NewComponent(conf config.XmppConfig) *xmpp.StreamManager { + options := xmpp.ComponentOptions{ + Address: conf.Host + ":" + conf.Port, + Domain: conf.Jid, + Secret: conf.Password, + Name: "telegabber", + } + + router := xmpp.NewRouter() + router.HandleFunc("message", HandleMessage) + + component, err := xmpp.NewComponent(options, router) + if err != nil { + log.Fatalf("%+v", err) + } + + cm := xmpp.NewStreamManager(component, nil) + + return cm +} diff --git a/xmpp/handlers.go b/xmpp/handlers.go new file mode 100644 index 0000000..38bbe7a --- /dev/null +++ b/xmpp/handlers.go @@ -0,0 +1,21 @@ +package xmpp + +import ( + "fmt" + "os" + + "gosrc.io/xmpp" + "gosrc.io/xmpp/stanza" +) + +func HandleMessage(s xmpp.Sender, p stanza.Packet) { + msg, ok := p.(stanza.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return + } + + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) +}