Skip to content

Commit

Permalink
some code reformattings.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Sep 7, 2024
1 parent 9af16d2 commit 7c4d158
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 76 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/tlgen/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ func (*Generator) generateFile(f func(file *jen.File, d bool), filename string,
return err
}

return os.WriteFile(filename, buf.Bytes(), 0644)
return os.WriteFile(filename, buf.Bytes(), 0600)
}
4 changes: 3 additions & 1 deletion internal/cmd/tlgen/gen/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gen

import (
"fmt"
"strings"
"unicode"

Expand Down Expand Up @@ -103,7 +104,8 @@ func (g *Generator) typeIdFromSchemaType(t string) *jen.Statement {
break
}
// pp.Fprintln(os.Stderr, g.schema)
panic("'" + t + "'")
//panic("'" + t + "'")
fmt.Println("panic: ", t)
}

return item
Expand Down
50 changes: 44 additions & 6 deletions internal/cmd/tlgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const (
)

var (
API_SOURCES = []string{"https://raw.githubusercontent.com/null-nick/TL-Schema/main/api.tl",
API_SOURCES = []string{
"https://raw.githubusercontent.com/TGScheme/Schema/main/main_api.tl", "https://raw.githubusercontent.com/null-nick/TL-Schema/main/api.tl",
"https://raw.githubusercontent.com/telegramdesktop/tdesktop/dev/Telegram/SourceFiles/mtproto/scheme/api.tl",
"https://raw.githubusercontent.com/TGScheme/Schema/main/main_api.tl",
}
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {

remoteAPIVersion = cleanComments(remoteAPIVersion)

file, err := os.OpenFile(tlLOC, os.O_RDWR|os.O_CREATE, 0644)
file, err := os.OpenFile(tlLOC, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -243,12 +243,14 @@ func minorFixes(outdir, layer string) {
ID: id,
})`)

replaceWithRegexp(filepath.Join(execWorkDir, "methods_gen.go"), `(?m)^\s*Chatlist\s*$`, " Chatlist InputChatlistDialogFilter")

replace(filepath.Join(execWorkDir, "enums_gen.go"), `Null Null`, `NullCrc Null`)

replace(filepath.Join(execWorkDir, "init_gen.go"), `Null,`, `NullCrc,`)
if layer != "0" {
// replace eg: ApiVersion = 181
file, err := os.OpenFile(filepath.Join(execWorkDir, "const.go"), os.O_RDWR|os.O_CREATE, 0644)
file, err := os.OpenFile(filepath.Join(execWorkDir, "const.go"), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
Expand All @@ -274,7 +276,7 @@ func minorFixes(outdir, layer string) {

// ALSO UPDATE README.md with ApiVersion

rdfile, err := os.OpenFile(filepath.Join(execWorkDir, "../README.md"), os.O_RDWR|os.O_CREATE, 0644)
rdfile, err := os.OpenFile(filepath.Join(execWorkDir, "../README.md"), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
Expand All @@ -298,7 +300,7 @@ func minorFixes(outdir, layer string) {
}

func replace(filename, old, new string) {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -330,6 +332,42 @@ func replace(filename, old, new string) {
}
}

func replaceWithRegexp(filename, old, new string) {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}

content, err := io.ReadAll(f)
if err != nil {
panic(err)
}

// fmt.Println("Replacing", old, "with", new, "in", filename)

// regexp.MustCompile(`(?m)^Chatlist\s*$`).ReplaceAllString(string(content), "Chatlist InputChatlistDialogFilter")

str := string(content)
re := regexp.MustCompile(old)
str = re.ReplaceAllString(str, new)

// truncate the file before writing
err = f.Truncate(0)
if err != nil {
panic(err)
}

_, err = f.Seek(0, 0)
if err != nil {
panic(err)
}

_, err = f.WriteString(str)
if err != nil {
panic(err)
}
}

func cleanComments(b []byte) []byte {
re := regexp.MustCompile(`---types---\n\n(.+?)\n\n---types---`)
b = re.ReplaceAll(b, []byte("$1\n\n---types---"))
Expand Down
76 changes: 47 additions & 29 deletions telegram/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package telegram

import (
"bytes"
"strings"

"github.com/pkg/errors"
)

// Button is a helper struct for creating buttons for messages.
type Button struct{}

func (Button) Force(placeHolder string) *ReplyKeyboardForceReply {
Expand Down Expand Up @@ -74,41 +76,62 @@ func (Button) Clear() *ReplyKeyboardHide {
return &ReplyKeyboardHide{}
}

// message.Click() is a function that clicks a button in a message.
//
// It takes one optional argument, which can be either:
// - the text of the button to click
// - the data of the button to click
// - the coordinates of the button to click ([x, y])
type ClickOptions struct {
Game bool
Password string
}

// Click clicks a button in a message.
//
// If no argument is given, the first button will be clicked.
//
// If an argument is provided, it can be one of the following:
// - The text of the button to click.
// - The data of the button to click.
// - The coordinates of the button to click as a slice of integers [x, y].
func (m *NewMessage) Click(options ...any) (*MessagesBotCallbackAnswer, error) {
requestParams := &MessagesGetBotCallbackAnswerParams{
Peer: m.Peer,
MsgID: m.ID,
Game: false,
}

if len(options) > 0 {
if opt, ok := options[0].(*ClickOptions); ok {
requestParams.Game = opt.Game
if opt.Password != "" {
accountPasswordSrp, err := m.Client.AccountGetPassword()
if err != nil {
return nil, err
}

password, err := GetInputCheckPassword(opt.Password, accountPasswordSrp)
if err != nil {
return nil, err
}

requestParams.Password = password
}
}
}

if m.ReplyMarkup() == nil {
return nil, errors.New("message has no buttons")
return nil, errors.New("replyMarkup: message has no buttons")
}

switch messageButtons := (*m.ReplyMarkup()).(type) {
case *ReplyInlineMarkup:
if messageButtons, ok := (*m.ReplyMarkup()).(*ReplyInlineMarkup); ok {
if len(messageButtons.Rows) == 0 {
return nil, errors.New("message has no buttons")
return nil, errors.New("replyMarkup: rows are empty")
}

switch len(options) {
case 0:
if len(messageButtons.Rows[0].Buttons) == 0 {
return nil, errors.New("message has no buttons")
return nil, errors.New("replyMarkup: row(0) has no buttons")
}

switch button := messageButtons.Rows[0].Buttons[0].(type) {
case *KeyboardButtonCallback:
if button, ok := messageButtons.Rows[0].Buttons[0].(*KeyboardButtonCallback); ok {
requestParams.Data = button.Data
//case *KeyboardButtonSimpleWebView:
}

case 1:
Expand All @@ -118,36 +141,31 @@ func (m *NewMessage) Click(options ...any) (*MessagesBotCallbackAnswer, error) {
for _, button := range row.Buttons {
switch opt := options[0].(type) {
case string:
switch button := button.(type) {
case *KeyboardButtonCallback:
if button.Text == opt {
requestParams.Data = button.Data
}
if button, ok := button.(*KeyboardButtonCallback); ok && strings.EqualFold(button.Text, opt) {
requestParams.Data = button.Data
}
case []byte:
switch button := button.(type) {
case *KeyboardButtonCallback:
if bytes.Equal(button.Data, opt) {
requestParams.Data = button.Data
}
if button, ok := button.(*KeyboardButtonCallback); ok && bytes.Equal(button.Data, opt) {
requestParams.Data = button.Data
}
case int, int32, int64:
if optInt, ok := opt.(int); ok && optInt == currentX {
switch button := button.(type) {
case *KeyboardButtonCallback:
if button, ok := button.(*KeyboardButtonCallback); ok {
requestParams.Data = button.Data
}
}

case []int, []int32, []int64:
if optInts, ok := opt.([]int); ok && len(optInts) == 2 {
if optInts[0] == currentX && optInts[1] == currentY {
switch button := button.(type) {
case *KeyboardButtonCallback:
if button, ok := button.(*KeyboardButtonCallback); ok {
requestParams.Data = button.Data
}
}
}

default:
return nil, errors.New("replyMarkup: invalid argument type (expected string, []byte, int, or []int)")
}
currentY++
}
Expand All @@ -158,7 +176,7 @@ func (m *NewMessage) Click(options ...any) (*MessagesBotCallbackAnswer, error) {
}

if requestParams.Data == nil {
return nil, errors.New("button with given text/data/(x,y) not found")
return nil, errors.New("replyMarkup: button with given (text, data, or coordinates) not found")
}

return m.Client.MessagesGetBotCallbackAnswer(requestParams)
Expand Down
2 changes: 1 addition & 1 deletion telegram/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewCache(logLevel string, fileN string) *CACHE {

// --------- Cache file Functions ---------
func (c *CACHE) WriteFile() {
file, err := os.OpenFile(c.fileN, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
file, err := os.OpenFile(c.fileN, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
c.logger.Error("error opening cache file: ", err)
return
Expand Down
Loading

0 comments on commit 7c4d158

Please sign in to comment.