diff --git a/transmission-telegram.go b/transmission-telegram.go index b1a3474..dac9989 100644 --- a/transmission-telegram.go +++ b/transmission-telegram.go @@ -154,6 +154,8 @@ func main() { case "add", "/add": // takes url to a torrent to add it + go add(&update, tokens[1:]) + case "search", "/search": // search for a torrent go search(&update, tokens[1:]) @@ -202,6 +204,10 @@ func main() { // prints a help message case "version", "/version": // print transmission and transmission-telegram versions + case "": + // might be a file received + go receiveTorrent(&update) + default: // no such command, try help @@ -337,7 +343,49 @@ func trackers(ud *tgbotapi.Update) { send(buf.String(), ud.Message.Chat.ID) } -// add +// add takes an URL to a .torrent file to add it to transmission +func add(ud *tgbotapi.Update, tokens []string) { + if len(tokens) == 0 { + send("add: needs atleast one URL", ud.Message.Chat.ID) + return + } + + // loop over the URL/s and add them + for _, url := range tokens { + cmd := transmission.NewAddCmdByURL(url) + + torrent, err := Client.ExecuteAddCommand(cmd) + if err != nil { + send("add: "+err.Error(), ud.Message.Chat.ID) + continue + } + + // check if torrent.Name is empty, then an error happened + if torrent.Name == "" { + send("add: error adding "+url, ud.Message.Chat.ID) + continue + } + send("Added: "+torrent.Name, ud.Message.Chat.ID) + } +} + +// receiveTorrent gets an update that potentially has a .torrent file to add +func receiveTorrent(ud *tgbotapi.Update) { + if ud.Message.Document.FileID == "" { + return // has no document + } + + // get the file ID and make the config + fconfig := tgbotapi.FileConfig{ud.Message.Document.FileID} + file, err := Bot.GetFile(fconfig) + if err != nil { + send("receiver: "+err.Error(), ud.Message.Chat.ID) + return + } + + // add by file URL + add(ud, []string{file.Link(BotToken)}) +} // search takes a query and returns torrents with match func search(ud *tgbotapi.Update, tokens []string) { @@ -600,20 +648,23 @@ func del(ud *tgbotapi.Update, tokens []string) { send("del: needs an ID", ud.Message.Chat.ID) return } - // try to read the id into an int - num, err := strconv.Atoi(tokens[0]) - if err != nil { - send(fmt.Sprintf("del: %s is not an ID", tokens[0]), ud.Message.Chat.ID) - return - } - name, err := Client.DeleteTorrent(num, false) - if err != nil { - send("del: "+err.Error(), ud.Message.Chat.ID) - return - } + // loop over tokens to read each potential id + for _, id := range tokens { + num, err := strconv.Atoi(id) + if err != nil { + send(fmt.Sprintf("del: %s is not an ID", id), ud.Message.Chat.ID) + return + } - send("Deleted: "+name, ud.Message.Chat.ID) + name, err := Client.DeleteTorrent(num, false) + if err != nil { + send("del: "+err.Error(), ud.Message.Chat.ID) + return + } + + send("Deleted: "+name, ud.Message.Chat.ID) + } } // deldata takes an id or more, and delete the corresponding torrent/s with their data @@ -623,20 +674,22 @@ func deldata(ud *tgbotapi.Update, tokens []string) { send("deldata: needs an ID", ud.Message.Chat.ID) return } - // try to read the id into an int - num, err := strconv.Atoi(tokens[0]) - if err != nil { - send(fmt.Sprintf("deldata: %s is not an ID", tokens[0]), ud.Message.Chat.ID) - return - } + // loop over tokens to read each potential id + for _, id := range tokens { + num, err := strconv.Atoi(id) + if err != nil { + send(fmt.Sprintf("deldata: %s is not an ID", id), ud.Message.Chat.ID) + return + } - name, err := Client.DeleteTorrent(num, true) - if err != nil { - send("deldata: "+err.Error(), ud.Message.Chat.ID) - return - } + name, err := Client.DeleteTorrent(num, true) + if err != nil { + send("deldata: "+err.Error(), ud.Message.Chat.ID) + return + } - send("Deleted with data: "+name, ud.Message.Chat.ID) + send("Deleted with data: "+name, ud.Message.Chat.ID) + } } // send takes a chat id and a message to send.