Skip to content

Commit 1a44321

Browse files
committed
allow chat to use up to 3 different webhooks with --1, --2 and --3
fixes: #170
1 parent 23479d4 commit 1a44321

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,20 @@ flags are stored in the `.ret/flag.json` file
159159
### 📢 <u>cha</u>t
160160

161161
```
162-
$ ret chat [-] [message1 message2 message3...]
162+
$ ret chat [--1] [--2] [--3] [-] [message1 message2 message3...]
163163
```
164164

165165
chat via a discord webhook with ret
166166

167167
use - to read from stdin
168168

169-
requires that `"chatwebhookurl"` from `~/.config/ret` is a valid webhook
169+
requires a valid webhook this is typically `"chatwebhookurl"` from `~/.config/ret` is a valid webhook
170+
171+
however the command supports up to 3 webhooks using `$ ret --1 chat`, `$ ret --2 chat` and `$ ret --3 chat`
172+
173+
if no numerical override is specified the `"chatwebhookurl"` webhook is used by default
174+
175+
webhooks 2 and 3 are set with `"chatwebhookurl2"` and `"chatwebhookurl3"` respectively
170176

171177
requires that `"username"` from `~/.config/ret` is set to valid string
172178

@@ -573,6 +579,8 @@ if a file called `writeup.md` already exists the command will abort
573579
"wizardpostcommand": "",
574580
"username": "",
575581
"chatwebhookurl": "",
582+
"chatwebhookurl2": "",
583+
"chatwebhookurl3": "",
576584
"gisttoken": "",
577585
"openaikey": "",
578586
"openaimodel": "",

commands/chat.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ func init() {
2222
Help: ChatHelp,
2323
Url: "https://github.com/rerrorctf/ret/blob/main/commands/chat.go",
2424
Arguments: []Argument{
25+
{
26+
Name: "--1",
27+
Optional: true,
28+
List: false,
29+
Override: true,
30+
},
31+
{
32+
Name: "--2",
33+
Optional: true,
34+
List: false,
35+
Override: true,
36+
},
37+
{
38+
Name: "--3",
39+
Optional: true,
40+
List: false,
41+
Override: true,
42+
},
2543
{
2644
Name: "-",
2745
Optional: true,
@@ -38,20 +56,23 @@ func init() {
3856
func ChatHelp() string {
3957
return "chat via a discord webhook with ret\n\n" +
4058
"use - to read from stdin\n\n" +
41-
"requires that " + theme.ColorYellow + "`\"chatwebhookurl\"`" + theme.ColorReset + " from " + theme.ColorCyan + "`~/.config/ret`" + theme.ColorReset + " is a valid webhook\n\n" +
59+
"requires a valid webhook this is typically " + theme.ColorYellow + "`\"chatwebhookurl\"`" + theme.ColorReset + " from " + theme.ColorCyan + "`~/.config/ret`" + theme.ColorReset + " is a valid webhook\n\n" +
60+
"however the command supports up to 3 webhooks using " + theme.ColorGray + "`$ ret --1 chat`" + theme.ColorReset + ", " + theme.ColorGray + "`$ ret --2 chat` " + theme.ColorReset + "and " + theme.ColorGray + "`$ ret --3 chat`" + theme.ColorReset + "\n\n" +
61+
"if no numerical override is specified the " + theme.ColorYellow + "`\"chatwebhookurl\"`" + theme.ColorReset + " webhook is used by default\n\n" +
62+
"webhooks 2 and 3 are set with " + theme.ColorYellow + "`\"chatwebhookurl2\"`" + theme.ColorReset + " and " + theme.ColorYellow + "`\"chatwebhookurl3\"`" + theme.ColorReset + " respectively\n\n" +
4263
"requires that " + theme.ColorYellow + "`\"username\"`" + theme.ColorReset + " from " + theme.ColorCyan + "`~/.config/ret`" + theme.ColorReset + " is set to valid string\n\n" +
4364
"when data is read from stdin, due to the use of the - argument, it will be sent as an embed with an accurate timestamp and a random color\n\n" +
4465
"color codes, such as the ones used by this tool, are stripped by this code prior to sending\n\n" +
4566
"for more information please see " + theme.ColorPurple + "https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks\n" + theme.ColorReset
4667
}
4768

48-
func sendMessage(message map[string]interface{}) {
69+
func sendMessage(message map[string]interface{}, webhook string) {
4970
body, err := json.Marshal(message)
5071
if err != nil {
5172
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": %v\n", err)
5273
}
5374

54-
req, err := http.NewRequest("POST", config.ChatWebhookUrl, bytes.NewBuffer(body))
75+
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(body))
5576
if err != nil {
5677
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": %v\n", err)
5778
}
@@ -66,18 +87,18 @@ func sendMessage(message map[string]interface{}) {
6687
resp.Body.Close()
6788
}
6889

69-
func sendChat(message string) {
90+
func sendChat(message string, webhook string) {
7091
message = theme.RemoveColors(message)
7192

7293
body := map[string]interface{}{
7394
"username": config.Username,
7495
"content": message,
7596
}
7697

77-
sendMessage(body)
98+
sendMessage(body, webhook)
7899
}
79100

80-
func sendEmbed(message string) {
101+
func sendEmbed(message string, webhook string) {
81102
message = theme.RemoveColors(message)
82103

83104
embed := map[string]interface{}{
@@ -97,38 +118,46 @@ func sendEmbed(message string) {
97118
"embeds": []interface{}{embed},
98119
}
99120

100-
sendMessage(body)
121+
sendMessage(body, webhook)
101122
}
102123

103124
func Chat(args []string) {
104125
if len(args) == 0 {
105126
log.Fatalf("💥 " + theme.ColorRed + " error" + theme.ColorReset + ": expected 1 or more arguments\n")
106127
}
107128

108-
if config.ChatWebhookUrl == "" {
109-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": no chat webhook url found in %s\n", config.UserConfig)
110-
}
111-
112129
if config.Username == "" {
113130
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": no username found in %s\n", config.UserConfig)
114131
}
115132

133+
webhook := config.ChatWebhookUrl
134+
135+
if CommandsChoice2 {
136+
webhook = config.ChatWebhookUrl2
137+
} else if CommandsChoice3 {
138+
webhook = config.ChatWebhookUrl3
139+
}
140+
141+
if webhook == "" {
142+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": no chat webhook url found in %s\n", config.UserConfig)
143+
}
144+
116145
if len(args) > 0 {
117146
if strings.Compare("-", args[0]) == 0 {
118147
var buffer bytes.Buffer
119148
_, err := io.Copy(&buffer, os.Stdin)
120149
if err != nil {
121150
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": %v\n", err)
122151
}
123-
sendEmbed(buffer.String())
152+
sendEmbed(buffer.String(), webhook)
124153

125154
if len(args) > 1 {
126-
sendChat(strings.Join(args[1:], " "))
155+
sendChat(strings.Join(args[1:], " "), webhook)
127156
}
128157

129158
return
130159
}
131160
}
132161

133-
sendChat(strings.Join(args, " "))
162+
sendChat(strings.Join(args, " "), webhook)
134163
}

config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var (
3232
WizardPostCommand = ""
3333
Username = ""
3434
ChatWebhookUrl = ""
35+
ChatWebhookUrl2 = ""
36+
ChatWebhookUrl3 = ""
3537
GistToken = ""
3638
OpenAIKey = ""
3739
OpenAIModel = "gpt-4o"
@@ -54,6 +56,8 @@ type Config struct {
5456
WizardPostCommand string `json:"wizardpostcommand"`
5557
Username string `json:"username"`
5658
ChatWebhookUrl string `json:"chatwebhookurl"`
59+
ChatWebhookUrl2 string `json:"chatwebhookurl2"`
60+
ChatWebhookUrl3 string `json:"chatwebhookurl3"`
5761
GistToken string `json:"gisttoken"`
5862
OpenAIKey string `json:"openaikey"`
5963
OpenAIModel string `json:"openaimodel"`
@@ -129,6 +133,14 @@ func ParseUserConfig() {
129133
ChatWebhookUrl = userConfig.ChatWebhookUrl
130134
}
131135

136+
if len(userConfig.ChatWebhookUrl2) > 0 {
137+
ChatWebhookUrl2 = userConfig.ChatWebhookUrl2
138+
}
139+
140+
if len(userConfig.ChatWebhookUrl3) > 0 {
141+
ChatWebhookUrl3 = userConfig.ChatWebhookUrl3
142+
}
143+
132144
if len(userConfig.GistToken) > 0 {
133145
GistToken = userConfig.GistToken
134146
}
@@ -191,6 +203,8 @@ func WriteUserConfig() {
191203
userConfig.WizardPostCommand = WizardPostCommand
192204
userConfig.Username = Username
193205
userConfig.ChatWebhookUrl = ChatWebhookUrl
206+
userConfig.ChatWebhookUrl2 = ChatWebhookUrl2
207+
userConfig.ChatWebhookUrl3 = ChatWebhookUrl3
194208
userConfig.GistToken = GistToken
195209
userConfig.OpenAIKey = OpenAIKey
196210
userConfig.OpenAIModel = OpenAIModel

0 commit comments

Comments
 (0)