Skip to content

Commit

Permalink
Merge pull request #1 from checkymander/Dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
checkymander committed May 25, 2021
2 parents 89f4b62 + d89d091 commit 8ee69de
Show file tree
Hide file tree
Showing 9 changed files with 949 additions and 369 deletions.
7 changes: 6 additions & 1 deletion Carbuncle/Carbuncle.csproj
Expand Up @@ -40,7 +40,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentParser.cs" />
<Compile Include="Helpers\ArgumentParser.cs" />
<Compile Include="Commands\AttachmentSearcher.cs" />
<Compile Include="Commands\MailMonitor.cs" />
<Compile Include="Commands\MailSender.cs" />
<Compile Include="Helpers\Common.cs" />
<Compile Include="Commands\MailSearcher.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
180 changes: 180 additions & 0 deletions Carbuncle/Commands/AttachmentSearcher.cs
@@ -0,0 +1,180 @@
using Carbuncle.Helpers;
using Microsoft.Office.Interop.Outlook;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Carbuncle.Commands
{
public class AttachmentSearcher
{
bool download { get; set; }
string downloadFolder { get; set; }
public AttachmentSearcher(bool download = false, string downloadFolder = "")
{
this.download = download;
this.downloadFolder = downloadFolder;
}
public void GetAttachmentsByID(string ID, OlDefaultFolders folder)
{
try
{
Application outlookApplication = new Application();
NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");
MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(folder);
var item = outlookNamespace.GetItemFromID(ID);
if (item is MailItem mailItem)
{
if (mailItem.Attachments.Count > 0)
{
if (download)
{
foreach (Attachment attachment in mailItem.Attachments)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
}
else
{
Common.DisplayMailItem(mailItem);
}
}
}
else if (item is MeetingItem meetingItem)
{
if (meetingItem.Attachments.Count > 0)
{
if (download)
{
foreach (Attachment attachment in meetingItem.Attachments)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
}
else
{
Common.DisplayMeetingItem(meetingItem);
}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
public void GetAttachmentsByKeyword(string keyword)
{
GetAttachmentsByRegex($"({keyword})");
}
public void GetAttachmentsByRegex(string regex)
{
MailSearcher ms = new MailSearcher();
Items mailItems = ms.GetInboxItems(OlDefaultFolders.olFolderInbox);
foreach (var item in ms.GetInboxItems(OlDefaultFolders.olFolderInbox))
{
try
{
if (item is MailItem mailItem)
{
if (mailItem.Attachments.Count > 0)
{
foreach (Attachment attachment in mailItem.Attachments)
{
if (Regex.Match(attachment.FileName, regex).Success)
{
if (download)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
else
{
Common.DisplayMailItem(mailItem);
}
}

}
}
}
else if (item is MeetingItem meetingItem)
{
if (meetingItem.Attachments.Count > 0)
{
foreach (Attachment attachment in meetingItem.Attachments)
{
if(Regex.Match(attachment.FileName, regex).Success)
{
if (download)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
else
{
Common.DisplayMeetingItem(meetingItem);
}
}

}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public void GetAllAttachments()
{
Console.WriteLine("Getting All Attachments\r\nDownload = " + download);
Console.WriteLine(downloadFolder);
MailSearcher ms = new MailSearcher();
//Items mailItems = ms.GetInboxItems(OlDefaultFolders.olFolderInbox);

foreach (var item in ms.GetInboxItems(OlDefaultFolders.olFolderInbox))
{
try
{
if (item is MailItem mailItem)
{
if (mailItem.Attachments.Count > 0)
{
if (download)
{
foreach (Attachment attachment in mailItem.Attachments)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
}
else
{
Common.DisplayMailItem(mailItem);
}
}
}
else if (item is MeetingItem meetingItem)
{
if (meetingItem.Attachments.Count > 0)
{
if (download)
{
foreach (Attachment attachment in meetingItem.Attachments)
{
attachment.SaveAsFile(downloadFolder.TrimEnd('\\') + "\\" + attachment.FileName);
}
}
else
{
Common.DisplayMeetingItem(meetingItem);
}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
54 changes: 54 additions & 0 deletions Carbuncle/Commands/MailMonitor.cs
@@ -0,0 +1,54 @@
using Microsoft.Office.Interop.Outlook;
using System;
using System.IO;
using Carbuncle.Helpers;

namespace Carbuncle.Commands
{
public class MailMonitor
{
public MailMonitor()
{

}
private void NewEmailEvent(object item)
{
if (item is MailItem mailItem)
{
Common.DisplayMailItem(mailItem);
}

if (item is MeetingItem meetingItem)
{
Common.DisplayMeetingItem(meetingItem);

}
}
public void Start()
{
MonitorEmail();
}
public void Start(string regex)
{
MonitorEmailRegex(regex);
}

private void MonitorEmail()
{
MailSearcher ms = new MailSearcher();
Console.WriteLine("[+] Starting e-mail monitoring...");
Items mailItems = ms.GetInboxItems(OlDefaultFolders.olFolderInbox);
mailItems.ItemAdd += new ItemsEvents_ItemAddEventHandler(NewEmailEvent);
Console.WriteLine("[+] Started, press Ctrl+Z to exit");
}
private void MonitorEmailRegex(string regex)
{
MailSearcher ms = new MailSearcher();
Console.WriteLine("[+] Starting e-mail monitoring...");
Items mailItems = ms.GetInboxItems(OlDefaultFolders.olFolderInbox);
mailItems.ItemAdd += new ItemsEvents_ItemAddEventHandler(NewEmailEvent);
Console.WriteLine("[+] Started, press Ctrl+Z to exit");
}
}

}

0 comments on commit 8ee69de

Please sign in to comment.