Exemplo n.º 1
0
func (this *Script) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "code" {
		if msg["text"] == nil {
			return
		}

		str := msg["text"].(string)

		if !strings.HasPrefix(strings.ToLower(str), "exec ") {
			(*state)["code"] = (*state)["code"].(string) + "\n" + str

			if len((*state)["code"].(string)) >= 131070 {
				this.tg.SendMessage("Code too long. Maximum: 131KiB", msg.ChatId())
				utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			} else {
				this.tg.SendMessage("Code received. If you have finished, send 'exec script_type' to execute. Send /cancel to cancel.", msg.ChatId())
			}
		} else {
			code := (*state)["code"].(string)
			utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			this.Command(strings.ToLower(str[5:]), msg, []string{code})
		}

	} else if name == "brainfuck" {
		if msg["text"] != nil {
			text := strings.Trim(msg["text"].(string), " ")

			if text != "" {
				input := (*state)["chan"].(chan string)
				input <- strings.Replace(text, "\\n", "\n", -1)
				utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			}
		}
	}
}
Exemplo n.º 2
0
func (this *Misc) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "remind" {
		if (*state)["remind"] == nil {
			(*state)["remind"] = msg["text"].(string)
			this.tg.ReplyToMessage(msg.MessageId(), "How long after now should I remind you?", msg.ChatId())
		} else {
			duration, err := time.ParseDuration(msg["text"].(string))
			if err != nil {
				this.tg.ReplyToMessage(msg.MessageId(), "Invalid time. Supported format: BhCmDsEmsFnsGus", msg.ChatId())
			} else {
				text := (*state)["remind"].(string)

				this.tg.ReplyToMessage(msg.MessageId(), "Yes, sir!", msg.ChatId())

				utils.PostDelayed(func() {
					if !strings.HasPrefix(text, "\\/") {
						this.tg.SendMessage("@"+msg.From()["username"].(string)+" "+text, msg.ChatId())
					} else {
						msg["text"] = text[1:]
						utils.Handler()(msg)
					}
				}, duration)

				utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			}
		}
	}
}
Exemplo n.º 3
0
func (this *Scholar) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "google" {
		if (msg["text"] == nil) || (strings.ToLower(msg["text"].(string)) != "next") {
			return
		}

		start := (*state)["start"].(int)
		query := (*state)["query"].(string)

		this.tg.SendChatAction("typing", msg.ChatId())
		res, hasNext := Google(query, start, 5, this.ipv6)

		this.tg.SendMessageNoPreview(formatGoogle(res, hasNext), msg.ChatId())

		if hasNext {
			(*state)["start"] = start + len(res)
		} else {
			utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
		}
	}
}
Exemplo n.º 4
0
func (this *Misc) Command(name string, msg telegram.TObject, args []string) {
	switch name {
	case "echo":
		this.tg.SendMessage(args[0], msg.ChatId())
	case "parse":
		this.tg.ReplyToMessage(msg.MessageId(), strings.Join(args, "\n"), msg.ChatId())
	case "cancel":
		if utils.HasGrabber(msg.FromId(), msg.ChatId()) {
			utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			this.tg.SendMessage("Current session cancelled", msg.ChatId())
		}
	case "remind":
		this.tg.ReplyToMessage(msg.MessageId(), "What do you want me to remind you of?", msg.ChatId())
		utils.SetGrabber(types.Grabber{
			Name:      "remind",
			Uid:       msg.FromId(),
			Chat:      msg.ChatId(),
			Processor: this,
		})
	case "choose":
		this.choose(msg, args)
	}
}
Exemplo n.º 5
0
func (this *Barcode) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "barcode" {
		utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
		this.Decode(msg)
	}
}
Exemplo n.º 6
0
func (this *Script) Command(name string, msg telegram.TObject, args []string) {
	if name == "code" {
		this.tg.ReplyToMessage(msg.MessageId(), "Now send me some code. You can split it into multiple messages.\nAfter finishing, send 'exec script_type' to execute it as a script.", msg.ChatId())
		status := utils.SetGrabber(types.Grabber{
			Name:      "code",
			Uid:       msg.FromId(),
			Chat:      msg.ChatId(),
			Processor: this,
		})

		(*status)["code"] = ""
	} else if name == "brainfuck" {
		code := strings.Trim(args[0], " \n")

		if code == "" {
			this.tg.ReplyToMessage(msg.MessageId(), "Code is empty.", msg.ChatId())
		} else {
			end := time.Now().Add(time.Duration(int64(this.timeout)) * time.Second)
			res, err := brainfuck.New().SetInterrupter(func() bool {
				return time.Now().After(end)
			}).SetInput(func(out string) string {
				this.tg.ReplyToMessage(msg.MessageId(),
					fmt.Sprintf("Output: %s\nInput needed. Now send me the input data in 60 seconds. If nothing received, the interpreter will be interrupted.\nSend /cancel to force interrupt.", out),
					msg.ChatId())

				status := utils.SetGrabber(types.Grabber{
					Name:      "brainfuck",
					Uid:       msg.FromId(),
					Chat:      msg.ChatId(),
					Processor: this,
				})

				input := make(chan string)
				(*status)["chan"] = input

				now := time.Now()
				var dur time.Duration
				if now.Before(end) {
					dur = end.Sub(now)
				} else {
					// WTF??
					dur = time.Duration(0)
				}
				select {
				case result := <-input:
					end = time.Now().Add(dur)
					return result
				case <-time.After(60 * time.Second):
					utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
					this.tg.ReplyToMessage(msg.MessageId(), "Input timed out. Interrupting.", msg.ChatId())
					return string(0)
				}

				return string(0)
			}).Exec(code)

			if err != nil {
				this.tg.ReplyToMessage(msg.MessageId(), err.Error(), msg.ChatId())
			} else {
				res = strings.Trim(res, " \n")

				if res == "" {
					res = "Empty"
				}

				this.tg.ReplyToMessage(msg.MessageId(), res, msg.ChatId())
			}
		}
	}
}