@@ -17,7 +17,7 @@ public MailGrabber(string host, string user, string password)
17
17
_password = password ;
18
18
}
19
19
20
- internal List < Tuple < string , string > > FindUrl ( string sender , string regexPatternDownload , string regexPatternWorkspace )
20
+ internal async Task < List < Tuple < string , string > > > FindUrl ( string sender , string regexPatternDownload , string regexPatternWorkspace , string subjectFilter = "" )
21
21
{
22
22
using var client = new ImapClient ( ) ;
23
23
client . ServerCertificateValidationCallback = ( s , c , h , e ) => true ;
@@ -34,10 +34,16 @@ internal List<Tuple<string,string>> FindUrl(string sender, string regexPatternDo
34
34
35
35
if ( ( message . From [ 0 ] as MailboxAddress ) ? . Address != sender )
36
36
continue ;
37
-
37
+
38
+ if ( ! string . IsNullOrEmpty ( subjectFilter ) && ! message . Subject . Contains ( subjectFilter ) )
39
+ {
40
+ continue ;
41
+ }
42
+
38
43
string body = message . HtmlBody ;
44
+
39
45
40
- // Regex to extract URLs
46
+ // Regex to directly extract URLs
41
47
var regexDl = new Regex ( regexPatternDownload , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
42
48
var regexWorkspace = new Regex ( regexPatternWorkspace , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
43
49
@@ -60,9 +66,74 @@ internal List<Tuple<string,string>> FindUrl(string sender, string regexPatternDo
60
66
{
61
67
urls . Add ( new Tuple < string , string > ( dlUrl , workspaceUrl ) ) ;
62
68
}
69
+
70
+ // Extract mailgun URLs
71
+ // Regex pattern to find all links for the domain mg.mail.notion.so
72
+ string pattern = @"<a[^>]*href=""(?<url>https:\/\/mg\.mail\.notion\.so[^""]+)""[^>]*>[^<]*<\/a>" ;
73
+
74
+ List < string > links = new List < string > ( ) ;
75
+
76
+ // Extract all matching links
77
+ MatchCollection matches = Regex . Matches ( body , pattern ) ;
78
+ foreach ( Match match in matches )
79
+ {
80
+ links . Add ( match . Groups [ "url" ] . Value ) ;
81
+ }
82
+
83
+ // Perform GET requests and extract the Location header
84
+ // added here
85
+ HttpClientHandler httpClientHandler = new HttpClientHandler ( ) ;
86
+ httpClientHandler . AllowAutoRedirect = false ;
87
+ using HttpClient hc = new HttpClient ( httpClientHandler ) ;
88
+ string dlUrl2 = String . Empty ;
89
+ string workspaceUrl2 = String . Empty ;
90
+
91
+ foreach ( var link in links )
92
+ {
93
+ try
94
+ {
95
+ // Set the HttpClient to follow redirects
96
+ hc . DefaultRequestHeaders . Clear ( ) ;
97
+ var response = await hc . GetAsync ( link ) ;
98
+
99
+ // Check if the response is a redirect
100
+ if ( response . StatusCode == System . Net . HttpStatusCode . MovedPermanently ||
101
+ response . StatusCode == System . Net . HttpStatusCode . Found ||
102
+ response . StatusCode == System . Net . HttpStatusCode . SeeOther )
103
+ {
104
+ // Extract the Location header
105
+ if ( response . Headers . Location != null )
106
+ {
107
+ string actualLink = response . Headers . Location . ToString ( ) ;
108
+ if ( regexDl . Match ( actualLink ) . Success )
109
+ {
110
+ dlUrl2 = actualLink ;
111
+ }
112
+ if ( regexWorkspace . Match ( actualLink ) . Success )
113
+ {
114
+ workspaceUrl2 = actualLink ;
115
+ }
116
+
117
+
118
+ }
119
+ }
120
+ else
121
+ {
122
+ Console . WriteLine ( "No redirect for URL: " + link ) ;
123
+ }
124
+ }
125
+ catch ( Exception ex )
126
+ {
127
+ Console . WriteLine ( $ "Error fetching { link } : { ex . Message } ") ;
128
+ }
129
+ }
130
+ if ( ! string . IsNullOrEmpty ( dlUrl2 ) )
131
+ {
132
+ urls . Add ( new Tuple < string , string > ( dlUrl2 , workspaceUrl2 ) ) ;
133
+ }
63
134
}
64
135
65
- client . Disconnect ( true ) ;
136
+ await client . DisconnectAsync ( true ) ;
66
137
67
138
return urls ;
68
139
0 commit comments