-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from ritsec/staging
Add /dquery with option removed from /query and disabled good-food-enjoyers
- Loading branch information
Showing
4 changed files
with
338 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package slash | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/ritsec/ops-bot-iii/commands/slash/permission" | ||
"github.com/ritsec/ops-bot-iii/data" | ||
"github.com/ritsec/ops-bot-iii/ent/signin" | ||
"github.com/ritsec/ops-bot-iii/logging" | ||
"github.com/sirupsen/logrus" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
func DQuery() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) { | ||
return &discordgo.ApplicationCommand{ | ||
Name: "dquery", | ||
Description: "Query usernames by signins on specific date and outputs to CSV file", | ||
DefaultMemberPermissions: &permission.IGLead, | ||
Options: []*discordgo.ApplicationCommandOption{ | ||
{ | ||
Name: "type", | ||
Description: "The type of signin", | ||
Type: discordgo.ApplicationCommandOptionString, | ||
Required: true, | ||
Choices: []*discordgo.ApplicationCommandOptionChoice{ | ||
{ | ||
Name: "All", | ||
Value: "All", | ||
}, | ||
{ | ||
Name: "General Meeting", | ||
Value: "General Meeting", | ||
}, | ||
{ | ||
Name: "Contagion", | ||
Value: "Contagion", | ||
}, | ||
{ | ||
Name: "DFIR", | ||
Value: "DFIR", | ||
}, | ||
{ | ||
Name: "Ops", | ||
Value: "Ops", | ||
}, | ||
{ | ||
Name: "Ops IG", | ||
Value: "Ops IG", | ||
}, | ||
{ | ||
Name: "Red Team", | ||
Value: "Red Team", | ||
}, | ||
{ | ||
Name: "Red Team Recruiting", | ||
Value: "Red Team Recruiting", | ||
}, | ||
{ | ||
Name: "Reversing", | ||
Value: "Reversing", | ||
}, | ||
{ | ||
Name: "RVAPT", | ||
Value: "RVAPT", | ||
}, | ||
{ | ||
Name: "Physical", | ||
Value: "Physical", | ||
}, | ||
{ | ||
Name: "Vulnerability Research", | ||
Value: "Vulnerability Research", | ||
}, | ||
{ | ||
Name: "Wireless", | ||
Value: "Wireless", | ||
}, | ||
{ | ||
Name: "WiCyS", | ||
Value: "WiCyS", | ||
}, | ||
{ | ||
Name: "Other", | ||
Value: "Other", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "date", | ||
Description: "Specific date (YYYY-MM-DD) to query for", | ||
Type: discordgo.ApplicationCommandOptionString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
span := tracer.StartSpan( | ||
"commands.slash.signin:Signin", | ||
tracer.ResourceName("/signin"), | ||
) | ||
defer span.Finish() | ||
|
||
signinType := i.ApplicationCommandData().Options[0].StringValue() | ||
dateRequested := i.ApplicationCommandData().Options[1].StringValue() | ||
|
||
var entSigninType signin.Type | ||
switch signinType { | ||
case "General Meeting": | ||
entSigninType = signin.TypeGeneralMeeting | ||
case "Contagion": | ||
entSigninType = signin.TypeContagion | ||
case "DFIR": | ||
entSigninType = signin.TypeDFIR | ||
case "Ops": | ||
entSigninType = signin.TypeOps | ||
case "Ops IG": | ||
entSigninType = signin.TypeOpsIG | ||
case "Red Team": | ||
entSigninType = signin.TypeRedTeam | ||
case "Red Team Recruiting": | ||
entSigninType = signin.TypeRedTeamRecruiting | ||
case "RVAPT": | ||
entSigninType = signin.TypeRVAPT | ||
case "Reversing": | ||
entSigninType = signin.TypeReversing | ||
case "Physical": | ||
entSigninType = signin.TypePhysical | ||
case "Wireless": | ||
entSigninType = signin.TypeWireless | ||
case "WiCyS": | ||
entSigninType = signin.TypeWiCyS | ||
case "Vulnerability Research": | ||
entSigninType = signin.TypeVulnerabilityResearch | ||
case "Other": | ||
entSigninType = signin.TypeOther | ||
case "All": | ||
entSigninType = "All" | ||
} | ||
|
||
// Parsing the date as time.Time | ||
dateToQuery, err := time.Parse("2006-01-02", dateRequested) | ||
|
||
//Inform user that the date format is incorrect and exit | ||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Flags: discordgo.MessageFlagsEphemeral, | ||
Content: "Date format is invalid. It needs to be YYYY-MM-DD, e.g., 2003-05-12.", | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
return | ||
} | ||
return | ||
} | ||
|
||
signins, err := data.Signin.DQuery( | ||
dateToQuery, | ||
entSigninType, | ||
span.Context(), | ||
) | ||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
return | ||
} | ||
|
||
// Initial message | ||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Flags: discordgo.MessageFlagsEphemeral, | ||
Content: "OBIII is processing...", | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
} | ||
|
||
// Processing | ||
message := "" | ||
for x, signin := range signins { | ||
|
||
// Wait for 1 seconds after every 8 user's username is called | ||
if x > 0 && x%8 == 0 { | ||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
user, err := s.User(signin.Key) | ||
|
||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
return | ||
} | ||
|
||
if x == 0 { | ||
message += user.Username | ||
} else { | ||
message += fmt.Sprintf(",%s", user.Username) | ||
} | ||
} | ||
|
||
// Followup message | ||
followUpMessage := "Done" | ||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ | ||
Content: &followUpMessage, | ||
Files: []*discordgo.File{ | ||
{ | ||
Name: "query.csv", | ||
ContentType: "text/csv", | ||
Reader: strings.NewReader(message), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.