Skip to content

Commit

Permalink
Merge pull request #112 from ritsec/staging
Browse files Browse the repository at this point in the history
Add /dquery with option removed from /query and disabled good-food-enjoyers
  • Loading branch information
c0untingNumbers committed May 7, 2024
2 parents 1abf770 + 6e6ce95 commit 9f24b57
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 115 deletions.
3 changes: 2 additions & 1 deletion commands/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func populateSlashCommands(ctx ddtrace.SpanContext) {
SlashCommands["query"] = slash.Query
SlashCommands["scoreboard"] = slash.Scoreboard
SlashCommands["birthday"] = slash.Birthday
SlashCommands["dquery"] = slash.DQuery
}

// populateHandlers populates the Handlers map with all of the handlers
Expand Down Expand Up @@ -65,7 +66,7 @@ func populateScheduledEvents(ctx ddtrace.SpanContext) {
defer span.Finish()

// Populate the scheduled events
ScheduledEvents["goodfood"] = scheduled.GoodFood
// ScheduledEvents["goodfood"] = scheduled.GoodFood
ScheduledEvents["heartbeat"] = scheduled.Heartbeat
ScheduledEvents["status"] = scheduled.Status
ScheduledEvents["update"] = scheduled.Update
Expand Down
225 changes: 225 additions & 0 deletions commands/slash/dquery.go
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
}
}
}
Loading

0 comments on commit 9f24b57

Please sign in to comment.