Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 6 commits into from May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}
}

}