@@ -56,57 +56,9 @@ Module modMail
5656 End If
5757 End Function
5858
59- Sub CheckMail()
60- If My.Settings.Mail_Enable = True Then
61- If modGlobal.IsOnline = True AndAlso My.Settings.Mail_IMAPMode = False Then
62- Try
63- If pClient.Connected = True Then
64- CloseServer()
65- pClient = New TcpClient(My.Settings.Mail_POPHost, My.Settings.Mail_POPPort)
66- ret_Val = 0
67- Exit Sub
68- Else
69- pClient = New TcpClient(My.Settings.Mail_POPHost, My.Settings.Mail_POPPort)
70-
71- NetworkS_tream = pClient.GetStream 'Read the stream
72- m_sslStream = New SslStream(NetworkS_tream) 'Read SSL stream
73- m_sslStream.AuthenticateAsClient(My.Settings.Mail_POPHost) 'Auth
74- Read_Stream = New StreamReader(m_sslStream) 'Read the stream
75- StatResp = Read_Stream.ReadLine()
76-
77- StatResp = Login(m_sslStream, "USER " & My.Settings.Mail_Username)
78- My.Application.Log.WriteEntry( "POP3: " & StatResp)
79- StatResp = Login(m_sslStream, "PASS " & My.Settings.Mail_Password)
80- My.Application.Log.WriteEntry( "POP3: " & StatResp)
81- StatResp = Login(m_sslStream, "STAT " )
82- My.Application.Log.WriteEntry( "POP3: " & StatResp)
83-
84- 'Get Messages count
85- server_Stat = StatResp.Split( " "c )
86- My.Application.Log.WriteEntry( "POP3 Message count: " & server_Stat( 1 ))
87- ret_Val = 1
88- End If
89-
90- If IsNumeric(server_Stat( 1 )) = True Then 'Apparently POP3 returned 'bad' during a "Temporary system problem" and 'Command' when an invalid password is used, which I want to mitigate.
91- GetMessages(server_Stat( 1 ))
92- Else
93- My.Application.Log.WriteEntry( "Mail server returned a bad message count" , TraceEventType.Warning)
94- End If
95- CloseServer()
96- Catch SocketEx As System.Net.Sockets.SocketException
97- My.Application.Log.WriteException(SocketEx, TraceEventType.Warning, "usually caused by a mail connection timeout" )
98- Catch NullRefEx As System.NullReferenceException
99- My.Application.Log.WriteException(NullRefEx, TraceEventType.Warning, "usually caused by an empty POP3 response" )
100- End Try
101- End If
102- Else
103- My.Application.Log.WriteEntry( "Mail module is disabled" )
104- End If
105- End Sub
106-
10759 Sub CheckMailImap()
10860 If My.Settings.Mail_Enable = True Then
109- If modGlobal.IsOnline = True AndAlso My.Settings.Mail_IMAPMode = True Then
61+ If modGlobal.IsOnline = True Then
11062 Try
11163 pClient = New TcpClient(My.Settings.Mail_IMAPHost, My.Settings.Mail_IMAPPort)
11264 m_sslStream = New SslStream(pClient.GetStream())
@@ -133,11 +85,7 @@ Module modMail
13385 End Sub
13486
13587 Sub CheckMailHandler(source As Object , e As System.Timers.ElapsedEventArgs)
136- If My.Settings.Mail_IMAPMode = True Then
137- CheckMailImap()
138- Else
139- CheckMail()
140- End If
88+ CheckMailImap()
14189 End Sub
14290
14391 Function CheckPurelyMailBalance( ByVal strPurelyMailAPIKey As String ) As String
@@ -169,13 +117,6 @@ Module modMail
169117 End Try
170118 End Function
171119
172- Sub CloseServer()
173- StatResp = Login(m_sslStream, "QUIT " )
174- My.Application.Log.WriteEntry( "POP3: " & StatResp)
175- pClient.Close()
176- ret_Val = 0
177- End Sub
178-
179120 Sub CreateMailkeysDb()
180121 modDatabase.Execute( "CREATE TABLE IF NOT EXISTS MAILKEYS(Id INTEGER PRIMARY KEY, Nickname TEXT UNIQUE, CmdAllowlist TEXT, CmdKey TEXT)" )
181122 modDatabase.Execute( "ALTER TABLE MAILKEYS RENAME COLUMN CmdWhitelist TO CmdAllowlist" )
@@ -239,96 +180,16 @@ Module modMail
239180 Return result
240181 End Function
241182
242- Sub GetEmails( ByVal Server_Command As String )
243- Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
244- Dim stream_Reader As StreamReader
245- Dim TxtLine, CmdRec, ReFrom, ReSubj As String
246- Dim CmdTo As String = "" , CmdFrom As String = "" , CmdSubj As String = "" , CmdID As String = "" , CmdKeyLookup As String = "" , CmdNickLookup As String = ""
247- Try
248- m_sslStream.Write(m_buffer, 0 , m_buffer.Length)
249- stream_Reader = New StreamReader(m_sslStream)
250- Do While stream_Reader.Peek() <> - 1
251- TxtLine = stream_Reader.ReadLine()
252-
253- If TxtLine.StartsWith( "Received: " ) Then
254- CmdRec = String .Copy(TxtLine)
255- My.Application.Log.WriteEntry( "Command " & CmdRec)
256- End If
257- If TxtLine.StartsWith( "Message-ID: " ) Then
258- CmdID = String .Copy(TxtLine)
259- My.Application.Log.WriteEntry( "Command " & CmdID)
260- End If
261- If TxtLine.StartsWith( "Subject: " ) Then
262- CmdSubj = String .Copy(TxtLine)
263- My.Application.Log.WriteEntry( "Command " & CmdSubj)
264- End If
265- If TxtLine.StartsWith( "From: " ) Then
266- CmdFrom = String .Copy(TxtLine)
267- My.Application.Log.WriteEntry( "Command " & CmdFrom)
268- End If
269- If TxtLine.StartsWith( "To: " ) Then
270- CmdTo = String .Copy(TxtLine)
271- My.Application.Log.WriteEntry( "Command " & CmdTo)
272- End If
273-
274- If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" AndAlso CmdID <> "" Then
275- Exit Do
276- End If
277- Loop
278-
279- If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" AndAlso CmdID <> "" Then
280- ReFrom = CmdFrom.Replace( "From: " , "" )
281- CmdKeyLookup = GetCmdKeyFromAllowlist(ReFrom)
282- If CmdKeyLookup <> "" Then
283- If CmdTo = "To: " & CmdKeyLookup & " <" & My.Settings.Mail_From & ">" Then
284- CmdNickLookup = GetNicknameFromKey(ReFrom, CmdKeyLookup)
285- My.Application.Log.WriteEntry( "Received email from " + CmdNickLookup + ", command key validated" )
286- ReSubj = CmdSubj.Replace( "Subject: " , "" )
287- modConverse.Interpret(ReSubj, True , False , CmdNickLookup)
288- Else
289- My.Application.Log.WriteEntry( "Received email from authorized user, but command key was not valid" )
290- End If
291- Else
292- My.Application.Log.WriteEntry( "Received email from unauthorized user, ignoring" )
293- End If
294- End If
295- Catch ex As Exception
296- My.Application.Log.WriteException(ex)
297- End Try
298- End Sub
299-
300- Sub GetMessages( ByVal Num_Emails As Integer )
301- Dim List_Resp As String
302- Dim StrRetr As String
303- Dim I As Integer
304- For I = 1 To Num_Emails
305- List_Resp = Login(m_sslStream, "LIST " & I.ToString)
306- My.Application.Log.WriteEntry( "POP3: " & List_Resp)
307-
308- StrRetr = ( "RETR " & I & vbCrLf)
309- GetEmails(StrRetr)
310- Next I
311- End Sub
312-
313183 Function Load() As String
314184 My.Application.Log.WriteEntry( "Loading mail module" )
315185 If My.Settings.Mail_Enable = True Then
316- If My.Settings.Mail_IMAPMode = False AndAlso My.Settings.Mail_POPHost = "" Then
317- My.Application.Log.WriteEntry( "No mail POP host set, asking for it" )
318- My.Settings.Mail_POPHost = InputBox( "Enter mail POP host." , "Mail POP Host" )
319- End If
320186
321- If My.Settings.Mail_IMAPMode = False AndAlso My.Settings.Mail_POPPort = "" Then
322- My.Application.Log.WriteEntry( "No mail POP port set, asking for it" )
323- My.Settings.Mail_POPPort = InputBox( "Enter mail POP port." , "Mail POP Port" , "995" )
324- End If
325-
326- If My.Settings.Mail_IMAPMode = True AndAlso My.Settings.Mail_IMAPHost = "" Then
187+ If My.Settings.Mail_IMAPHost = "" Then
327188 My.Application.Log.WriteEntry( "No mail IMAP host set, asking for it" )
328189 My.Settings.Mail_IMAPHost = InputBox( "Enter mail IMAP host." , "Mail IMAP Host" )
329190 End If
330191
331- If My.Settings.Mail_IMAPMode = True AndAlso My.Settings. Mail_IMAPPort = "" Then
192+ If My.Settings.Mail_IMAPPort = "" Then
332193 My.Application.Log.WriteEntry( "No mail IMAP port set, asking for it" )
333194 My.Settings.Mail_IMAPPort = InputBox( "Enter mail IMAP port." , "Mail IMAP Port" , "993" )
334195 End If
@@ -562,28 +423,6 @@ Module modMail
562423 End Select
563424 End Function
564425
565- Function Login( ByVal SslStrem As SslStream, ByVal Server_Command As String ) As String
566- Dim justExit As Boolean = False
567- Dim Read_Stream2 = New StreamReader(SslStrem)
568- Server_Command = Server_Command + vbCrLf
569- m_buffer = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
570- Try
571- m_sslStream.Write(m_buffer, 0 , m_buffer.Length)
572- Catch IOExcep As System.IO.IOException
573- My.Application.Log.WriteException(IOExcep)
574- modMail.Send( "Mail crash averted" , "Mail crash averted" ) ' Remove this line later if this retry method actually works
575- justExit = True
576- Threading.Thread.Sleep( 600000 )
577- End Try
578- If justExit = False Then
579- Dim Server_Response As String
580- Server_Response = Read_Stream2.ReadLine()
581- Return Server_Response
582- Else
583- Return "Dumped"
584- End If
585- End Function
586-
587426 ''' <summary>
588427 ''' Asks for an updated mail password.
589428 ''' </summary>
@@ -598,7 +437,6 @@ Module modMail
598437 If tmrMailCheckTimer IsNot Nothing Then
599438 tmrMailCheckTimer.Enabled = False
600439 RemoveHandler tmrMailCheckTimer.Elapsed, AddressOf CheckMailHandler
601- ' CloseServer() - Wasn't used before, causes errors, commenting out this line.
602440 End If
603441 Return "Mail module unloaded"
604442 End Function
0 commit comments