diff --git a/bot.go b/bot.go index 1ef1249..3ffb076 100644 --- a/bot.go +++ b/bot.go @@ -162,6 +162,16 @@ func botMsgRescan(m *tb.Message) { if !m.Private() { return } + if _, ok := clientsKeepAlive[m.Chat.ID]; !ok && m.Chat.ID != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { p := JobPayloadRescanMsg{ @@ -202,6 +212,16 @@ func botMsgRescanAll(m *tb.Message) { if !m.Private() { return } + if _, ok := clientsKeepAlive[m.Chat.ID]; !ok && m.Chat.ID != cfg.Bot.Admin { + c := TGCommand{ + Type: commandReplyMsg, + Text: "Client not registered", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + return + } p := JobPayloadRescanMsg{ Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d ORDER BY id ASC;", objTypeMessage, objSubTypeMessageUnknown), MsgID64: int64(m.ID), @@ -231,103 +251,68 @@ func botMsgRescanAll(m *tb.Message) { return } -func botMsgExport(m *tb.Message) { +func botBackupExport(m *tb.Message) { if !m.Private() { return } - - c := TGCommand{ - Type: commandReplyMsg, - Text: `Starting messages export`, - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, - } - TGCmdQueue <- c - - b, err := zipMessages() - log.Printf("botMsgExportAll : Data returned.\n") - logOnError(err, "botMsgExportAll : exportMessages") - if err != nil { + if _, ok := clientsKeepAlive[m.Chat.ID]; !ok && m.Chat.ID != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, - Text: `Error exporting messages`, + Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c - return - } else { - text := fmt.Sprintf("Document size : %d bytes.", len(b)) - c := TGCommand{ - Type: commandReplyMsg, - Text: text, - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, - } - TGCmdQueue <- c } - d := tb.Document{} - d.File = tb.FromReader(bytes.NewReader(b)) - d.FileName = fmt.Sprintf("%s.backup.zip", time.Now().Format("20060102150405")) - d.Caption = d.FileName - d.MIME = `application/zip` - - c = TGCommand{ - Type: commandSendDocument, - Document: d, - ToChatID64: m.Chat.ID, + p := JobPayloadBackupExport{ + MsgID64: int64(m.ID), + ChatID64: m.Chat.ID, } - TGCmdQueue <- c + b, _ := json.Marshal(p) + _, err := createJob(objSubTypeJobBackupExport, objJobPriorityBackup, int64(m.Sender.ID), time.Now(), b) + logOnError(err, "botBackupExport : createJob(objSubTypeJobBackupExport)") return } -func botMsgLoad(m *tb.Message) { +func botBackupImport(m *tb.Message) { if !m.Private() { return } - r := regexp.MustCompile("^(http|https)://[a-z0-9./]+.zip$") // https://dump.siteop.biz/20190609163137.backup.zip - if r.MatchString(m.Payload) { - - resp, err := http.Get(m.Payload) - logOnError(err, "botMsgLoad : Get") - defer resp.Body.Close() - - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - + if _, ok := clientsKeepAlive[m.Chat.ID]; !ok && m.Chat.ID != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, - Text: "File downloaded.", + Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c - - err = UnzipMessages(buf.Bytes()) - logOnError(err, "botMsgLoad : UnzipMessages") - - c = TGCommand{ - Type: commandReplyMsg, - Text: "Messages injected.", - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, - } - TGCmdQueue <- c - - } else { + return + } + r := regexp.MustCompile(`^((http[s]?\:)\/\/)?([^\?\:\/#]+)(\:([0-9]+))?(\/[^\?\#]*)?(\?([^#]*))?(#.*)?.zip$`) + if !r.MatchString(m.Payload) { c := TGCommand{ Type: commandReplyMsg, - Text: "No file", + Text: "URL not valid.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c + return } + p := JobPayloadBackupExport{ + URL: m.Payload, + MsgID64: int64(m.ID), + ChatID64: m.Chat.ID, + } + b, _ := json.Marshal(p) + _, err := createJob(objSubTypeJobBackupImport, objJobPriorityBackup, int64(m.Sender.ID), time.Now(), b) + logOnError(err, "botBackupImport : createJob(objSubTypeJobBackupImport)") return + } func botMsgDump(m *tb.Message) { diff --git a/job.go b/job.go index 9532007..beb8b80 100644 --- a/job.go +++ b/job.go @@ -1,8 +1,11 @@ package main import ( + "archive/zip" + "bytes" "encoding/json" "fmt" + "io/ioutil" "strconv" "strings" "time" diff --git a/msg.go b/msg.go index 8a8b1c7..4d49ece 100644 --- a/msg.go +++ b/msg.go @@ -1,11 +1,7 @@ package main import ( - "archive/zip" - "bytes" - "encoding/json" "fmt" - "io/ioutil" "log" "regexp" "strconv"