Skip to content

Commit

Permalink
Validate Mailbox Status
Browse files Browse the repository at this point in the history
  • Loading branch information
VGR6479 committed Sep 2, 2024
1 parent 2fa410a commit 53271d2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 58 deletions.
104 changes: 59 additions & 45 deletions gerobug_dashboard/dashboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,28 @@ def ReportStatusView(request, id):
@login_required
def ReportUpdateStatus(request,id):
if request.method == "POST" and gerofilter.validate_id(id):
report = BugReport.objects.get(report_id=id)
max = ReportStatus.objects.count() - 2 # LIMITED TO "BOUNTY PROCESS"
if geroparser.check_mailbox_status():
report = BugReport.objects.get(report_id=id)
max = ReportStatus.objects.count() - 2 # LIMITED TO "BOUNTY PROCESS"

if report.report_status < max:
report.report_status += 1
report.save()
if report.report_status < max:
report.report_status += 1
report.save()

logging.getLogger("Gerologger").info("REPORT "+str(id)+" STATUS UPDATED ("+str(report.report_status)+") BY "+str(request.user.username))
messages.success(request,"Report Status is updated!")
logging.getLogger("Gerologger").info("REPORT "+str(id)+" STATUS UPDATED ("+str(report.report_status)+") BY "+str(request.user.username))
messages.success(request,"Report Status is updated!")

def trigger_geromailer(report):
payload = [report.report_id, report.report_title, report.report_status, "", report.report_severity]
destination = report.hunter_email
geromailer.notify(destination, payload) #TRIGGER GEROMAILER TO SEND UPDATE NOTIFICATION
def trigger_geromailer(report):
payload = [report.report_id, report.report_title, report.report_status, "", report.report_severity]
destination = report.hunter_email
geromailer.notify(destination, payload) #TRIGGER GEROMAILER TO SEND UPDATE NOTIFICATION

trigger = threading.Thread(target=trigger_geromailer, args=(report,))
trigger.start()
trigger = threading.Thread(target=trigger_geromailer, args=(report,))
trigger.start()

else:
logging.getLogger("Gerologger").error("Mailbox is not ready.")

return redirect('dashboard')


Expand Down Expand Up @@ -230,17 +234,22 @@ def FormHandler(request, id, complete):
elif status == "Bounty in Process" and complete == "1":
code = 704 #COMPLETE
logging.getLogger("Gerologger").info("REPORT "+str(id)+" STATUS UPDATED (COMPLETE) BY "+str(request.user.username))

# TRIGGER COMPANY ACTION WITH THREADING
def trigger_company_action(report):
geroparser.company_action(report.report_id, reasons, code)

if code != 0:
trigger = threading.Thread(target=trigger_company_action, args=(report,))
trigger.start()

messages.success(request,"Email is successfully being processed and sent to the bug hunter with your reason.")
return redirect('dashboard')
if geroparser.check_mailbox_status():
# TRIGGER COMPANY ACTION WITH THREADING
def trigger_company_action(report):
geroparser.company_action(report.report_id, reasons, code)

if code != 0:
trigger = threading.Thread(target=trigger_company_action, args=(report,))
trigger.start()
messages.success(request,"Email is successfully being processed and sent to the bug hunter with your reason.")

else:
logging.getLogger("Gerologger").error("CODE = 0")

else:
logging.getLogger("Gerologger").error("Mailbox is not ready.")

else:
logging.getLogger("Gerologger").error("Form invalid: "+str(request))
Expand All @@ -264,24 +273,29 @@ def InvalidHandler(request, id):
if request.method == "POST":
form = Invalidform(request.POST)
if form.is_valid():
reasons = form.cleaned_data.get('invalidreasons')
if geroparser.check_mailbox_status():
reasons = form.cleaned_data.get('invalidreasons')

# MARK AS INVALID
report.report_status = 0
report.save()

logging.getLogger("Gerologger").info("REPORT "+str(id)+" MARKED AS INVALID BY "+str(request.user.username))

def trigger_geromailer(report):
payload = [report.report_id, report.report_title, report.report_status, reasons, report.report_severity]
destination = report.hunter_email
geromailer.notify(destination, payload) # TRIGGER GEROMAILER TO SEND UPDATE NOTIFICATION

# SEND NOTIFICATION AND REASON WITH THREADING
trigger = threading.Thread(target=trigger_geromailer, args=(report,))
trigger.start()

# MARK AS INVALID
report.report_status = 0
report.save()

logging.getLogger("Gerologger").info("REPORT "+str(id)+" MARKED AS INVALID BY "+str(request.user.username))

def trigger_geromailer(report):
payload = [report.report_id, report.report_title, report.report_status, reasons, report.report_severity]
destination = report.hunter_email
geromailer.notify(destination, payload) # TRIGGER GEROMAILER TO SEND UPDATE NOTIFICATION

# SEND NOTIFICATION AND REASON WITH THREADING
trigger = threading.Thread(target=trigger_geromailer, args=(report,))
trigger.start()
messages.success(request,"Email is successfully being processed and sent to the bug hunter with your reason.")

else:
logging.getLogger("Gerologger").error("Mailbox is not ready.")

messages.success(request,"Email is successfully being processed and sent to the bug hunter with your reason.")
else:
messages.error(request,"Form invalid. Please report to the Admin for checking the logs.")
logging.getLogger("Gerologger").error("Form invalid: "+str(request))
Expand Down Expand Up @@ -381,8 +395,8 @@ def AdminSetting(request):
mailbox_account.mailbox_status = 0
mailbox_account.mailbox_type = mailbox.cleaned_data.get('mailbox_type')
mailbox_account.save()
logging.getLogger("Gerologger").info("Mailbox updated successfully")
messages.success(request,"Mailbox updated successfully!")
logging.getLogger("Gerologger").info("Mailbox updated successfully.")
messages.success(request,"Mailbox updated successfully.")
return redirect('setting')

account = AccountForm(request.POST)
Expand Down Expand Up @@ -536,16 +550,16 @@ def AdminSetting(request):
troubleshoot = TroubleshootForm(request.POST)
if troubleshoot.is_valid():
if troubleshoot.cleaned_data.get('troubleshoot_1') == True:
mailbox = MailBox.objects.get(mailbox_id=1)
if mailbox.email == "" or mailbox.password == "":
logging.getLogger("Gerologger").error("Mailbox Has Not Been Setup!")
else:
if geroparser.check_mailbox_status():
# FILE RECOVERY THREAD
def trigger_recovery(BUGREPORTS):
geroparser.recover_loss_file_handler(BUGREPORTS)

trigger = threading.Thread(target=trigger_recovery, args=(BugReport.objects.all(),))
trigger.start()

else:
logging.getLogger("Gerologger").error("Mailbox is not ready.")

return redirect("setting")

Expand Down
10 changes: 6 additions & 4 deletions gerobug_dashboard/geromail/geromailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def write_mail(code, payload, Destination):

if payload[3] == None:
payload[3] = "-"

# REPLACE WILD CARDS
subject = subject.replace("~ID~", payload[0]) #REPORT ID
body = body.replace("~ID~", payload[0]) #REPORT ID
Expand Down Expand Up @@ -95,25 +95,27 @@ def write_mail(code, payload, Destination):
SMTP_PORT = 465

if EMAIL == "" or PWD == "":
logging.getLogger("Gerologger").error("Mailbox is not ready.")
pass

else:
try:
connection = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
connection.login(EMAIL, PWD)
connection.sendmail(EMAIL, Destination, message.as_string())
connection.close()
logging.getLogger("Gerologger").info('Sent Email Successfully')

except Exception as e:
with smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT) as server:
server.starttls()
server.login(EMAIL, PWD)
server.sendmail(EMAIL, Destination, message.as_string())
server.close()
logging.getLogger("Gerologger").info('Sent Email Successfully')

logging.getLogger("Gerologger").info('Sent Email Successfully')

except Exception as e:
logging.getLogger("Gerologger").error(str(e))
logging.getLogger("Gerologger").error("Failed to Send Email = " + str(e))


# WRITE EMAIL NOTIFICATION / UPDATES
Expand Down
27 changes: 18 additions & 9 deletions gerobug_dashboard/geromail/geroparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

MAILBOX_READY = False
PARSER_RUNNING = False

ERROR_COUNT = 0

# GENERATE REPORT ID
def generate_id(email, ts):
Expand Down Expand Up @@ -112,6 +112,7 @@ def save_uan(type, id, report_id, date, summary, file):

# READ INBOX (UNSEEN) AND PARSE DATA
def read_mail():
global ERROR_COUNT
try:
# LOGIN TO IMAP / MAIL SERVER
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
Expand Down Expand Up @@ -439,7 +440,8 @@ def read_mail():
mail.logout()

except Exception as e:
logging.getLogger("Gerologger").error("Failed to Login = " + str(e))
logging.getLogger("Gerologger").error("Failed to Login = " + str(e) + "(" + str(ERROR_COUNT) + ")")
ERROR_COUNT+=1



Expand Down Expand Up @@ -486,25 +488,29 @@ def company_action(id, note, code):

geromailer.write_mail(code, payload, destination)

def check_mailbox_status():
global MAILBOX_READY

return MAILBOX_READY

# RUN GEROMAIL MODULES
def run():
global EMAIL, PWD, MAILBOX_READY, PARSER_RUNNING, IMAP_SERVER, IMAP_PORT
global EMAIL, PWD, MAILBOX_READY, PARSER_RUNNING, IMAP_SERVER, IMAP_PORT, ERROR_COUNT
MAILBOX_READY = False
error_count = 0
ERROR_COUNT = 0

if PARSER_RUNNING:
logging.getLogger("Gerologger").warning("Geroparser already started.")
return 0
else:
PARSER_RUNNING = True
logging.getLogger("Gerologger").debug("[LOG] Geroparser started.")

while PARSER_RUNNING:
# LIMIT ERRORS TO AVOID BLACKLISTED BY MAIL SERVER
if error_count >= 3:
if ERROR_COUNT >= 3:
logging.getLogger("Gerologger").warning("Error Limit Reached!")
error_count = 0
ERROR_COUNT = 0
mailbox = MailBox.objects.get(mailbox_id=1)
mailbox.email = ""
mailbox.password = ""
Expand All @@ -522,6 +528,9 @@ def run():

# ONLY RUN WHILE MAILBOX READY
while MAILBOX_READY:
if ERROR_COUNT >= 3:
MAILBOX_READY = False

mailbox = MailBox.objects.get(mailbox_id=1)
if mailbox.email == "" or mailbox.password == "":
MAILBOX_READY = False
Expand All @@ -545,8 +554,8 @@ def run():
mail.login(EMAIL,PWD)

except Exception as e:
error_count+=1
logging.getLogger("Gerologger").error("Credentials Failed = " + str(e) + "(" + str(error_count) + ")")
ERROR_COUNT+=1
logging.getLogger("Gerologger").error("Credentials Failed = " + str(e) + "(" + str(ERROR_COUNT) + ")")
MAILBOX_READY = False
time.sleep(5)
break
Expand Down

0 comments on commit 53271d2

Please sign in to comment.