home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vbccmail / ccmail.bas next >
Encoding:
BASIC Source File  |  1998-02-18  |  45.0 KB  |  1,427 lines

  1. Attribute VB_Name = "CCmail_to_smtp"
  2. Option Explicit
  3.  
  4. #Const fullrun = True
  5.  
  6. Global GMToffset As String
  7. Global LocalPO As String
  8. Global GatewayPO As String
  9. Global CCMAILpassword As String
  10. Global CCMAILpodir As String
  11. Global CCMAILEXPORTFOLDER As String
  12. Global CCMAILEXPORTprognFOLDER As String
  13. Global CCMAILWORKINGFOLDER As String
  14. Global CCERRORDIR As String
  15.  
  16. Global CCMAILTEMPFILE As String
  17. Global inetDOMAIN  As String
  18. Global DEFAULTfromADDRESS  As String
  19. Global DEFAULTfromUSER As String
  20.  
  21. Global sendingmail    As Boolean
  22. '----------------------
  23. 'GPL (C) Alan Knowles 1998
  24.  
  25. Dim Globalsequence As Long
  26. Type ccADDRESS
  27.   Post_office As String
  28.   name As String
  29. End Type
  30.  
  31. Dim TextItems() As tyTextItem
  32. Dim FileItems() As String
  33.  
  34. Type tyTextItem
  35.   Title As String
  36.   Body As String
  37. End Type
  38.  
  39. Type ccMESSAGE
  40.   MAILfrom As ccADDRESS
  41.   Forwardedby As ccADDRESS
  42.   DateofMessage As Date
  43.   mailto() As ccADDRESS
  44.   mailcc() As ccADDRESS
  45.   mailbcc() As ccADDRESS
  46.   mailalsoto() As ccADDRESS
  47.   mailalsocc() As ccADDRESS
  48.   Priority As String
  49.   subject As String
  50.   RRQ As Boolean
  51.   RRT As Boolean
  52.   Notdeliverableto() As ccADDRESS
  53. End Type
  54.   
  55. Dim boundary As String
  56. Dim message As ccMESSAGE
  57. Dim ismime As Boolean
  58.  
  59.  
  60. Sub ccMail_Gateway_Main()
  61. ' revised version
  62. ' this one gets the mail then sends it!
  63.  
  64.  
  65.   Dim i As Long
  66.   Dim j As Long
  67.   Dim importfilename As String
  68.   Globalsequence = 1
  69.   Dim messageids() As String
  70.    'debug.print
  71.    Debug.Print ccexport_summary(messageids())
  72.   
  73.   For i = 1 To UBound(messageids)
  74.       Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCEXPORT]  Reading MSG#" & messageids(i), 0
  75.     ccexport_file messageids(i)
  76.     
  77.     readccfile
  78.     createsmtpfile ' USES THE SMTPSEND TO SEND A FILE!
  79. #If fullrun Then
  80.      Kill CCMAILTEMPFILE & "1"
  81. #Else
  82.    'debug.print "Killing "; CCMAILTEMPFILE
  83. #End If
  84.      
  85.      
  86.      For j = 0 To UBound(FileItems)
  87.             If Not FileItems(j) = "" Then
  88.   #If fullrun Then
  89.                   If Not Dir(CCMAILEXPORTFOLDER & FileItems(j)) = "" Then
  90.                     Kill CCMAILEXPORTFOLDER & FileItems(j)
  91.                  End If
  92. #End If
  93.                 'debug.print "killing attachment : "; FileItems(j)
  94.  
  95.             End If
  96.     Next
  97.  
  98.   Next
  99.   
  100.   
  101.   ' GRAB MESSAGES FROM CCSPOOL FOLDER & PROCESS!
  102.   
  103.  ' ReDim messageids(0)
  104. '  messageids(0) = Dir(CCMAILSPOOLFOLDER & "*.*")'
  105.   'Do While Not messageids(UBound(messageids)) = "" '
  106. '      ReDim Preserve messageids(UBound(messageids') + 1)
  107. '      messageids(UBound(messageids)) = Dir
  108. '  Loop
  109. '  For i = 0 To UBound(messageids) - 1
  110. '   'debug.print "parsing :"; messageids(i)
  111. '      parse_smtp messageids(i)
  112. '      importfilename = Create_ccimport_file()
  113. '      If Not ccimport_file(importfilename) Then
  114. '        FileCopy CCMAILSPOOLFOLDER & messageids(i), CCMAILEXPORTFOLDER & "probs\" & messageids(i)
  115. '     End If
  116. '
  117. '      If Not Dir(importfilename) = "" Then
  118. '        Kill importfilename
  119. '      End If
  120. '
  121. '      'debug.print "killing "; importfilename
  122. '
  123. '
  124. '      For j = 1 To UBound(FileItems)
  125. '        If Not FileItems(j) = "" Then
  126. '#If fullrun Then
  127. '          Kill FileItems(j)
  128. '#Else
  129. '          'debug.print "Killing attached file "; FileItems(j)
  130. '#End If
  131. '        End If
  132.         
  133.         
  134. '      Next
  135. '#If fullrun Then
  136. '          Kill CCMAILSPOOLFOLDER & messageids(i)
  137. '#End If
  138. '          'debug.print "Killing import file "; CCMAILSPOOLFOLDER & messageids(i)
  139.  
  140.       
  141.       
  142. '  Next
  143.  
  144. End Sub
  145.  
  146.  
  147. Sub Clearmessage()
  148.   ReDim TextItems(0)
  149.   ReDim FileItems(0)
  150.   With message
  151.  .MAILfrom.Post_office = ""
  152.  .MAILfrom.name = ""
  153.  
  154.     .Forwardedby.Post_office = ""
  155.     .Forwardedby.name = ""
  156.     .DateofMessage = Now
  157.     ReDim .mailto(0)
  158.     ReDim .mailcc(0)
  159.     ReDim .mailbcc(0)
  160.     ReDim .mailalsoto(0)
  161.     ReDim .mailalsocc(0)
  162.     .Priority = ""
  163.     .subject = "No Subject"
  164.     .RRQ = False
  165.     .RRT = False
  166.     ReDim .Notdeliverableto(0)
  167.   End With
  168.   TextItems(0).Body = ""
  169.   ismime = False
  170. End Sub
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. Sub readccfile()
  183.   Dim messagelength As Long
  184.   Dim contentlength As Long
  185.   Dim currentpartlength As Long
  186.   Dim isenvelope As Boolean
  187.   Dim buf As String
  188.   Dim firstword As String
  189.   Dim thefileFH As Integer
  190.   Dim tempstring As String
  191.   Dim textbodyflag As Boolean
  192.   Clearmessage
  193.   
  194.   thefileFH = FreeFile
  195.   Open CCMAILTEMPFILE & "1" For Input As #thefileFH
  196.   
  197.   isenvelope = True
  198.   Do While isenvelope
  199.     If EOF(thefileFH) Then
  200.       Exit Do
  201.     End If
  202.     Line Input #thefileFH, buf
  203.     If InStr(Trim(buf), " ") > 0 Then
  204.       firstword = Left(Trim(buf), InStr(Trim(buf), " ") - 1)
  205.     Else
  206.       firstword = Trim(buf)
  207.     End If
  208.     'debug.print UCase(firstword);
  209.     Select Case UCase(firstword)
  210.       Case "FROM:"
  211.         message.MAILfrom = addressparse(Trim(Mid(buf, 6)))
  212.       Case "FORWARDED" 'FORWARDED BY:'
  213.         message.Forwardedby = addressparse(Trim(Mid(buf, 14)))
  214.       Case "DATE:"
  215.         message.DateofMessage = dateparse(Trim(Mid(buf, 6)))
  216.       Case "TO:"
  217.         ReDim Preserve message.mailto(UBound(message.mailto) + 1)
  218.         message.mailto(UBound(message.mailto)) = addressparse(Trim(Mid(buf, 4)))
  219.         'debug.print "to - name"; message.mailto(UBound(message.mailto)).name
  220.         'debug.print "to - Post_office"; message.mailto(UBound(message.mailto)).Post_office
  221.       Case "CC:"
  222.         ReDim Preserve message.mailcc(UBound(message.mailcc) + 1)
  223.         message.mailcc(UBound(message.mailcc)) = addressparse(Trim(Mid(buf, 4)))
  224.       Case "BCC:"
  225.         ReDim Preserve message.mailbcc(UBound(message.mailbcc) + 1)
  226.         message.mailbcc(UBound(message.mailbcc)) = addressparse(Trim(Mid(buf, 5)))
  227.       Case "*TO:"
  228.         ReDim Preserve message.mailalsoto(UBound(message.mailalsoto) + 1)
  229.         message.mailalsoto(UBound(message.mailalsoto)) = addressparse(Trim(Mid(buf, 5)))
  230.       Case "*CC:"
  231.         ReDim Preserve message.mailalsocc(UBound(message.mailalsocc) + 1)
  232.         message.mailalsocc(UBound(message.mailalsocc)) = addressparse(Trim(Mid(buf, 5)))
  233. '        Case "PRIORITY:"
  234.         
  235.       Case "SUBJECT:"
  236.         message.subject = Trim(Mid(buf, 9))
  237.       Case "RRQ:"
  238.         message.RRQ = True
  239.       Case "RRT:"
  240.         message.RRQ = True
  241.       Case "CONTENTS:"
  242.  '       Line Input #thefileFH, buf ' this cound be the message!!
  243.         isenvelope = False
  244.     End Select
  245.   Loop
  246.   If isenvelope Then
  247.     Close #thefileFH
  248.     'debug.print "Found eof befoure contents"
  249.     tempstring = getnewfilename(CCERRORDIR)
  250.     FileCopy CCMAILTEMPFILE & "1", tempstring
  251.     Exit Sub
  252.   End If
  253.   
  254.   'debug.print "Found the body"
  255.   textbodyflag = False
  256.   ' now for the body
  257.     ' look for ' title = Text Item....
  258. readnextline:
  259.  If EOF(thefileFH) Then
  260.   ' a single line message!
  261.   'debug.print "found the EOF"
  262.   GoTo eofmpart
  263. End If
  264.     Line Input #thefileFH, buf
  265.     'debug.print "Testing ::"; buf
  266. interpret_start:
  267.    'debug.print "Checking : "; buf
  268.    
  269.    ' text item
  270.     If UCase(Left(buf, 10)) = "TEXT ITEM:" Then
  271.       'debug.print "Found Text Item"
  272.                 ReDim Preserve TextItems(UBound(TextItems) + 1)
  273.                 With TextItems(UBound(TextItems))
  274.                   .Title = "Title : " & Trim(Mid(buf, 11))
  275.                   .Body = ""
  276.                 End With
  277. readnexttextline:
  278.                 If EOF(thefileFH) Then
  279.                    GoTo eofmpart
  280.                 End If
  281.                 Line Input #thefileFH, buf
  282.                 'debug.print UCase(Mid(buf, 6, 4));
  283.                 If Not (UCase(Left(buf, 9)) = "FILE ITEM" Or UCase(Left(buf, 9)) = "TEXT ITEM") Then
  284.                     With TextItems(UBound(TextItems))
  285.                       If Trim(buf) = "." Then
  286.                       .Body = .Body & ".." & vbCrLf
  287.                       Else
  288.                       .Body = .Body & buf & vbCrLf
  289.                       End If
  290.                     End With
  291.                     If Not EOF(thefileFH) Then
  292.                       GoTo readnexttextline
  293.                     End If
  294.                 Else
  295.                   GoTo interpret_start
  296.                 End If
  297.       End If
  298.       
  299.      ' file item-------------------------
  300.       If UCase(Left(buf, 10)) = "FILE ITEM:" Then
  301.             'debug.print "found fileitem : "; Trim(Mid(buf, 11))
  302.            
  303.             ReDim Preserve FileItems(UBound(FileItems) + 1)
  304.             FileItems(UBound(FileItems)) = Trim(Mid(buf, 11))
  305.             If InStr(FileItems(UBound(FileItems)), " ") Then
  306.                FileItems(UBound(FileItems)) = Left(FileItems(UBound(FileItems)), InStr(FileItems(UBound(FileItems)), " ") - 1)
  307.             End If
  308.         '    If InStr(FileItems(UBound(FileItems)), ".") = 0 Then
  309.             '  If Not Dir(CCMAILEXPORTFOLDER & FileItems(UBound(FileItems))) = "" Then
  310.           '        Name CCMAILEXPORTFOLDER & FileItems(UBound(FileItems)) As CCMAILEXPORTFOLDER & FileItems(UBound(FileItems)) & ".doc"
  311.          '        FileItems(UBound(FileItems)) = FileItems(UBound(FileItems)) & ".doc"
  312.          '     End If  'try and fix a bit for mac->pc??
  313.         '    End If
  314.             
  315.             'debug.print "Added item: "; FileItems(UBound(FileItems))
  316.       
  317.                 ' what happens on mac files???
  318.             GoTo readnextline
  319.        End If
  320.        
  321.        'text on its own without item bit!
  322.         If Trim(buf) = "" Then
  323.           If textbodyflag Then
  324.             TextItems(0).Body = TextItems(0).Body & buf & vbCrLf
  325.           End If
  326.         Else
  327.           textbodyflag = True
  328.           If Trim(buf) = "." Then
  329.             TextItems(0).Body = TextItems(0).Body & ".." & vbCrLf
  330.           Else
  331.             TextItems(0).Body = TextItems(0).Body & buf & vbCrLf
  332.           End If
  333.         End If
  334.   
  335.        GoTo readnextline
  336. eofmpart:
  337.   Close #thefileFH
  338. End Sub
  339.  
  340.  
  341.  
  342.  
  343.  
  344. Sub createsmtpfile()
  345.   Dim outFH As Integer
  346.   Dim realfiles As Long
  347.   Dim test As Variant
  348.   Dim i As Long, j As Long
  349.   Dim thenumber As Long
  350.   Dim rmailstring As String
  351.   Dim addrstring As String
  352.   Dim extension As String
  353.   Dim contenttype As String
  354.   Dim basesixtyfourFH As Integer
  355.   Dim basesixtyfourstatus As Integer
  356.   Dim buf As String
  357.   Dim efforts As Integer
  358.   Dim temptime As Date
  359.   
  360.   '1st lets see if there are any files to make  up.
  361.   realfiles = 0
  362.   If UBound(FileItems) > 0 Then
  363.         For i = 1 To UBound(FileItems)
  364.               If Not Dir(CCMAILEXPORTFOLDER & FileItems(i)) = "" Then
  365.                     realfiles = realfiles + 1
  366.               Else
  367.                     'debug.print "Couldnt find "; CCMAILEXPORTFOLDER & FileItems(i)
  368.               End If
  369.         Next
  370.   End If
  371.   'debug.print "Found "; realfiles; " files"
  372.   ' try doing a dummy to get it to fail!
  373.   If realfiles > 0 Then
  374.     ' create the mime message!
  375.           
  376.           For i = 1 To UBound(FileItems)
  377.                  Call UUVBInit
  378.                   If Not Dir(CCMAILEXPORTFOLDER & FileItems(i)) = "" Then
  379.                          test = UUEncodeToFile(0, CCMAILEXPORTFOLDER & FileItems(i), _
  380.                          B64ENCODED, FileItems(i), CCMAILWORKINGFOLDER & "PARTa" & i, 0)
  381.                   Else
  382.                     'debug.print "failed to find file " & CCMAILEXPORTFOLDER & FileItems(i)
  383.                   End If
  384.                   Call UUVBShutdown
  385.           Next
  386.           
  387.           ' now I'm supposed to have the files in PART1 PART2....PARTx .001 !
  388.           
  389.   End If
  390.   ' get a number!
  391.  
  392.   'SMTP HEADER  - ASSUME THAT THE NEXT PROCESS WILL INTERPRET THIS INFORMATION!!
  393.   
  394.      ' from is reversed?
  395.   ReDim ADDRESSto(0)
  396.   With message
  397.     ADDRESSfrom = getinetaddr(.MAILfrom)
  398.     rmailstring = ""
  399.     For i = 1 To UBound(.mailto)
  400.       ReDim Preserve ADDRESSto(UBound(ADDRESSto) + 1)
  401.       ADDRESSto(UBound(ADDRESSto)) = getinetaddr(.mailto(i))
  402.     Next
  403.     For i = 1 To UBound(message.mailcc)
  404.     ReDim Preserve ADDRESSto(UBound(ADDRESSto) + 1)
  405.             ADDRESSto(UBound(ADDRESSto)) = getinetaddr(.mailcc(i))
  406.     Next
  407.     For i = 1 To UBound(message.mailbcc)
  408.     ReDim Preserve ADDRESSto(UBound(ADDRESSto) + 1)
  409.             ADDRESSto(UBound(ADDRESSto)) = getinetaddr(.mailbcc(i))
  410.     Next
  411.     MAXto = UBound(ADDRESSto)
  412.     
  413.    Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCSMTP] : Really to : " & rmailstring, 0
  414.    
  415.  
  416.   outFH = FreeFile
  417.   Open CCMAILWORKINGFOLDER & "SMTPOUT" & ".txt" For Output As #outFH
  418.   'RFC HEADER BIT!
  419.     Print #outFH, rfclineout("Date: " & rfcdate(.DateofMessage))
  420.     Print #outFH, rfclineout("From: " & getinetaddr(.MAILfrom))
  421.     Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCSMTP] : FROM : " & getinetaddr(.MAILfrom), 0
  422.     Print #outFH, rfclineout("Sender: " & getinetaddr(.MAILfrom))
  423.     Print #outFH, rfclineout("Reply-To: " & getinetaddr(.MAILfrom))
  424.     Print #outFH, rfclineout("Subject: " & .subject)
  425.     If UBound(.mailbcc) > 0 And UBound(.mailto) = 0 And UBound(.mailcc) = 0 Then
  426.       Print #outFH, rfclineout("To: undisclosed recipients:")
  427.     End If
  428.     addrstring = ""
  429.     For i = 1 To UBound(.mailto)
  430.       addrstring = addrstring & getinetaddr(.mailto(i)) & ", "
  431.     Next
  432.     For i = 1 To UBound(message.mailalsoto)
  433.       addrstring = addrstring & getinetaddr(.mailalsoto(i)) & ", "
  434.     Next
  435.     If Not addrstring = "" Then
  436.       addrstring = Left(addrstring, Len(addrstring) - 2) ' take away the '
  437.       Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCSMTP] : TO: " & addrstring, 0
  438.       Print #outFH, rfclineout("To: " & addrstring)
  439.     End If
  440.     
  441.     addrstring = ""
  442.     For i = 1 To UBound(message.mailcc)
  443.       addrstring = addrstring & getinetaddr(.mailcc(i)) & ", "
  444.     Next
  445.     For i = 1 To UBound(.mailalsocc)
  446.       addrstring = addrstring & getinetaddr(.mailalsocc(i)) & ", "
  447.     Next
  448.     If Not addrstring = "" Then
  449.       addrstring = Left(addrstring, Len(addrstring) - 2)
  450.             Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCSMTP] : CC: " & addrstring, 0
  451.       Print #outFH, rfclineout("cc: " & addrstring)
  452.     End If
  453.   End With
  454.   Print #outFH, "message-ID: " & rfcmakeID()
  455.   
  456.   boundary = "VBccmailgateway" & Format(Now, "yymmddhhnnss")
  457.      'and todays date time for diferenciating quoted msgs.
  458.   Print #outFH, "MIME-Version: 1.0"
  459.   Print #outFH, "Content-Type: multipart/mixed; boundary=" & boundary
  460.   Print #outFH, ""
  461.   Print #outFH, "This is a multi-part message in MIME format."
  462.   Print #outFH, ""
  463. 'Thats the header done, now for the message!!
  464.   'debug.print "textitems = " & UBound(TextItems)
  465.    Print #outFH, "--" & boundary
  466.       Print #outFH, "  Content-Type: text/plain;  charset = ""iso-8859-1"""
  467.      Print #outFH, "Content -Transfer - encoding: quoted -printable"
  468.       Print #outFH, ""
  469.   
  470.   
  471.   
  472.   For i = 0 To UBound(TextItems)
  473.     If Not TextItems(i).Body = "" Then
  474.       Print #outFH, ""
  475.       If Not TextItems(i).Title = "" Then
  476.         Print #outFH, TextItems(i).Title
  477.         Print #outFH, ""
  478.       End If
  479.       Print #outFH, TextItems(i).Body
  480.     End If
  481.   Next
  482.   'debug.print "looping 1 to "; UBound(FileItems); " looking for exported files!"
  483.   For i = 1 To UBound(FileItems)
  484.     'debug.print "going for file " & i
  485.     If Dir(CCMAILWORKINGFOLDER & "PARTa" & i & ".001") = "" Then
  486.       'debug.print "Couldnt find "; CCMAILWORKINGFOLDER & "PARTa" & i & ".001"
  487.     Else
  488.       ' create the mime header!
  489.       Print #outFH, ""
  490.       Print #outFH, "--" & boundary
  491.       
  492.       j = Len(FileItems(i))
  493.       Do While Not Mid(FileItems(i), j, 1) = "."
  494.         j = j - 1
  495.         If j = 0 Then
  496.           Exit Do
  497.         End If
  498.       Loop
  499.       If j > 0 Then
  500.         extension = Mid(FileItems(i), j - 1)
  501.       Else
  502.         extension = ""
  503.       End If
  504.       Select Case UCase(extension)
  505.         Case "JPG"
  506.           contenttype = "image/jpeg"
  507.       
  508. 'MIME TYPES GO IN HERE!!
  509.         Case Else
  510.           contenttype = "application/octet-stream"
  511.       End Select
  512.       
  513.       Print #outFH, "Content-transfer-encoding: base64"
  514.       Print #outFH, "Content-disposition: attachment; filename=""" & FileItems(i) & """"
  515.       Print #outFH, "Content-type: " & contenttype & "; name=""" & FileItems(i) & """"
  516.       Print #outFH, ""
  517.             Form1.List1.AddItem "[" & Format(Now, "hh:nn") & " CCSMTP] : FILES: " & FileItems(i), 0
  518.       basesixtyfourFH = FreeFile
  519.       Open CCMAILWORKINGFOLDER & "PARTa" & i & ".001" For Input As #basesixtyfourFH
  520.       basesixtyfourstatus = 0
  521.       Do While Not EOF(basesixtyfourFH)
  522.         Line Input #basesixtyfourFH, buf
  523.         Select Case basesixtyfourstatus
  524.           Case 0
  525.             If Not Trim(buf) = "" Then
  526.               basesixtyfourstatus = 1
  527.             End If
  528.           Case 1
  529.             If Trim(buf) = "" Then
  530.               basesixtyfourstatus = 2
  531.             End If
  532.           Case 2
  533.             If Not Trim(buf) = "" Then
  534.               Exit Do
  535.             End If
  536.         End Select
  537.       Loop
  538.       Print #outFH, buf
  539.       Do While Not EOF(basesixtyfourFH)
  540.         Line Input #basesixtyfourFH, buf
  541.         Print #outFH, buf
  542.       Loop
  543.       Close #basesixtyfourFH
  544.     End If
  545.     
  546.     
  547.     
  548.   Next i ' next file!!!
  549.   Print #outFH, ""
  550.   Print #outFH, "--" & boundary & "--"
  551.   Close #outFH
  552.   
  553.     For i = 1 To UBound(FileItems)
  554.           If Not Dir(CCMAILWORKINGFOLDER & "PARTa" & i) = "" Then
  555.                  Kill CCMAILWORKINGFOLDER & "PARTa" & i
  556.           End If
  557.   Next
  558.   sendingmail = True
  559.   smtpsend.domail
  560.   Do While sendingmail
  561.     DoEvents
  562.   Loop
  563. End Sub
  564.  
  565.  
  566.  
  567.  
  568. Function ccexport_summary(ByRef messageids() As String) As Long
  569. Dim thistime As Date
  570.   Dim ccexportcmdline As String
  571.   Dim buf As String
  572.   ReDim messageids(0)
  573.   ' create the string to do the export
  574.   Dim idshell As Long
  575.   Dim abc As Integer
  576.   If Dir(Left(CCMAILWORKINGFOLDER, Len(CCMAILWORKINGFOLDER) - 1), vbDirectory) = "" Then
  577.     MkDir Left(CCMAILWORKINGFOLDER, Len(CCMAILWORKINGFOLDER) - 1)
  578.   End If
  579.   
  580.   ccexportcmdline = CCMAILEXPORTprognFOLDER & "EXPORT " & GatewayPO & " " & CCMAILpassword & " " _
  581.     & CCMAILpodir & " @" & CCMAILTEMPFILE & " HEADINGS/ALL" & vbCrLf
  582.   #If fullrun Then
  583.   
  584.   abc = FreeFile
  585.   Open CCMAILWORKINGFOLDER & "export.bat" For Output As #abc
  586.   Print #abc, ccexportcmdline
  587.   'debug.print ccexportcmdline
  588.   Print #abc, "dir " & CCMAILWORKINGFOLDER & "export.bat > " & CCMAILWORKINGFOLDER & "9999.999"
  589.   Close #abc
  590.   If Not Dir(CCMAILWORKINGFOLDER & "9999.999") = "" Then
  591.     Kill CCMAILWORKINGFOLDER & "9999.999"
  592.   End If
  593.   idshell = Shell(CCMAILWORKINGFOLDER & "export.bat", 0)
  594.   #Else
  595.   'debug.print ccexportcmdline
  596.   #End If
  597.   thistime = Now + 0.002 ' 2 minutes
  598.   Do While Dir(CCMAILWORKINGFOLDER & "9999.999") = ""  'wait for it to finish
  599.     DoEvents
  600.     If Now > thistime Then
  601.         'debug.print "Nothing found"
  602.         ccexport_summary = 0
  603.         Exit Function
  604.     End If
  605.   Loop
  606.   If Dir(CCMAILTEMPFILE) = "" Then
  607.      'debug.print "Nothing found"
  608.      ccexport_summary = 0
  609.      Exit Function
  610.   End If
  611.   abc = FreeFile
  612.   Open CCMAILTEMPFILE For Input As #abc
  613.   Do While Not EOF(abc)
  614.     Line Input #abc, buf
  615.     'debug.print buf
  616.     
  617.     If UCase(Left(buf, 4)) = "MSG#" Then
  618.       ReDim Preserve messageids(UBound(messageids) + 1)
  619.       'debug.print "adding at "; UBound(messageids); " value "; Trim(Mid(buf, 6))
  620.       messageids(UBound(messageids)) = Trim(Mid(buf, 6))
  621.     End If
  622.   Loop
  623.   Close #abc
  624.   ' clean up - eg delete export.bat & ccmailtempfile!
  625.   Kill CCMAILTEMPFILE
  626.   
  627.   
  628.    ccexport_summary = UBound(messageids)
  629.    
  630. End Function
  631.  
  632.  
  633. Sub ccexport_file(messageid As String)
  634.   ' create the string to do the export
  635.   Dim idshell As Long
  636.   Dim abc As Integer
  637.   Dim ccexportcmdline As String
  638.   ccexportcmdline = CCMAILEXPORTprognFOLDER & "EXPORT " & GatewayPO & " " & CCMAILpassword _
  639.      & " " & CCMAILpodir & " @" & CCMAILTEMPFILE & "1" & _
  640.        " FILES/DETACH READ/" & messageid & " END/1" & vbCrLf
  641.   abc = FreeFile
  642.   #If fullrun Then
  643.   Open CCMAILWORKINGFOLDER & "export.bat" For Output As #abc
  644.   Print #abc, ccexportcmdline
  645.   Print #abc, "dir " & CCMAILWORKINGFOLDER & "export.bat > " & CCMAILWORKINGFOLDER & "9999.999"
  646.   Close #abc
  647.   #End If
  648.    'debug.print ccexportcmdline
  649.   #If fullrun Then
  650.  If Not Dir(CCMAILWORKINGFOLDER & "9999.999") = "" Then
  651.     Kill CCMAILWORKINGFOLDER & "9999.999"
  652.   End If
  653.  
  654.   idshell = Shell(CCMAILWORKINGFOLDER & "export.bat", 0)
  655.   #End If
  656.   Do While Dir(CCMAILWORKINGFOLDER & "9999.999") = ""  'wait for it to finish
  657.     DoEvents
  658.   Loop
  659.     
  660.  
  661.  
  662. End Sub
  663.  
  664. Function dateparse(adatestring As String) As Date
  665.   dateparse = Now ' until i can think of anything better!
  666. End Function
  667.  
  668.  
  669. Function addressparse(ccmailaddress As String) As ccADDRESS ' format xxxx at xxxx
  670.   Dim buf As String
  671.   buf = Trim(ccmailaddress)
  672.   With addressparse
  673.     If InStr(buf, " ") > 0 Then
  674.       .name = Left(buf, InStr(buf, " ") - 1)
  675.     Else
  676.       .name = buf
  677.     End If
  678.     If InStr(LCase(buf), " at ") > 0 Then
  679.       .Post_office = Trim(Mid(buf, InStr(LCase(buf), " at ") + 4))
  680.       If InStr(.Post_office, " ") > 0 Then
  681.         .Post_office = Left(.Post_office, InStr(.Post_office, " ") - 1)
  682.       End If
  683.     Else
  684.       .Post_office = LocalPO
  685.     End If
  686.   End With
  687. End Function
  688.  
  689.  
  690. Function getnewfilename(directoryname As String) As String
  691.   Dim testfilename As String
  692.   Dim testnumber As Integer
  693.   testnumber = 0
  694.   testfilename = directoryname & "Vbmail" & Format(Now, "yyyymmddhhnnss")
  695.   Do While Not Dir(testfilename & testnumber) = ""
  696.     testnumber = testnumber + 1
  697.   Loop
  698.   
  699.   getnewfilename = testfilename & testnumber
  700. End Function
  701.  
  702.  
  703. Function uucpgetinetaddr(theaddress As ccADDRESS) As String
  704. ' convert the PO to internet!
  705. '  abc@xxx.xxx at internet -> abc@xxx.xxx
  706.   'abc at cgcs -> abc.cgcs.ccmail@envision-design.com.hk
  707.   'abc at envhkpo -> abc@envision-design.com.hk
  708.   With theaddress
  709.     Select Case LCase(.Post_office)
  710.        Case LCase(GatewayPO)
  711.          If InStr(.name, "@") > 0 Then
  712.            uucpgetinetaddr = Mid(.name, InStr(.name, "@") + 1) & "!" & Left(.name, InStr(.name, "@") - 1)
  713.         Else
  714.           uucpgetinetaddr = inetDOMAIN & "!" & DEFAULTfromUSER
  715.         End If
  716.       Case LCase(LocalPO), ""
  717.          uucpgetinetaddr = inetDOMAIN & "!" & .name
  718.       Case Else
  719.          uucpgetinetaddr = inetDOMAIN & "!" & .name & "." & .Post_office & ".ccmail"
  720.       End Select
  721.   End With
  722. End Function
  723. Function getinetaddr(theaddress As ccADDRESS) As String
  724.   With theaddress
  725.     Select Case LCase(.Post_office)
  726.        Case LCase(GatewayPO)
  727.          If InStr(.name, "@") > 0 Then
  728.            getinetaddr = .name
  729.         Else
  730.            getinetaddr = DEFAULTfromADDRESS
  731.         End If
  732.       Case LCase(LocalPO), ""
  733.           getinetaddr = .name & "@" & inetDOMAIN
  734.       Case Else
  735.           getinetaddr = .name & "." & .Post_office & ".ccmail@" & inetDOMAIN
  736.       End Select
  737.   End With
  738. End Function
  739.  
  740. Function rfcdate(adate As Date) As String
  741.   rfcdate = Format(adate, "ddd, d mmm yyyy hh:nn:ss ") & GMToffset
  742. End Function
  743.  
  744.  
  745. Function rfclineout(buf As String) As String
  746.   Dim totallen As Long
  747.   Dim i As Long, j As Long
  748.   If Len(buf) < 79 Then
  749.     rfclineout = buf
  750.     Exit Function
  751.   End If
  752.   rfclineout = ""
  753.   totallen = Len(buf)
  754.   j = 1
  755.   i = 60
  756.   Do While Not i = j
  757.     If Mid(buf, i, 1) = " " Then
  758.       'cut here!
  759.       rfclineout = rfclineout & Mid(buf, j, (i - j)) & vbCrLf & "  "
  760.       j = i + 1
  761.       i = j + 60
  762.       If i > totallen Then
  763.         rfclineout = rfclineout & Mid(buf, j)
  764.         Exit Do
  765.       End If
  766.     Else
  767.       i = i - 1
  768.     End If
  769.   Loop
  770. End Function
  771.  
  772.  
  773. Function rfcmakeID()
  774.  rfcmakeID = "<" & Format(Now, "yymmddhhnnss") & Globalsequence & "@" & inetDOMAIN & ">"
  775.  Globalsequence = Globalsequence + 1
  776. End Function
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784. Sub parse_smtp(smtpfilename As String)
  785.   Dim returncode As Variant
  786.   Dim rc As Integer, fpath As String
  787.   Dim flags As Integer
  788.   Dim ptr As Long, nextptr As Long, filestatus As String
  789.   Dim uuret As Long, mvret As Long
  790.   Dim smtpFH As Integer
  791.   Dim buf As String
  792.   Dim parsestring As String
  793.   Dim extension As String
  794.   Dim thefilename As String
  795.   Dim thefilenumber As Long
  796.   Dim filen As String
  797.   Dim atest As Variant
  798.   
  799. Dim readfilestatus As Integer
  800. Dim hasgotamimebit As Boolean
  801. Dim statusline As Long
  802. statusline = 0
  803.   hasgotamimebit = False
  804.   Clearmessage
  805.   smtpFH = FreeFile
  806.   'debug.print "reading smtp file :"; CCMAILWORKINGFOLDER & smtpfilename
  807.    Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " SMTPIN] : Reading file " & CCMAILWORKINGFOLDER & smtpfilename, 0
  808.   Open CCMAILWORKINGFOLDER & smtpfilename For Input As #smtpFH
  809.   ' read SMTP header
  810.   Line Input #smtpFH, buf
  811.   Do While Not UCase(Trim(buf)) = "DATA"
  812.     'debug.print "Comparing :-: "; buf
  813.     Select Case Left(UCase(buf), 4)
  814.       Case "MAIL"
  815.         If InStr(buf, "<") > 0 Then
  816.           buf = Mid(buf, InStr(buf, "<") + 1)
  817.           If InStr(buf, ">") Then
  818.             buf = Left(buf, InStr(buf, ">") - 1)
  819.           End If
  820.           message.MAILfrom = smtpaddrparse(buf)
  821.           'debug.print "set  mailfrom"; message.MAILfrom.name, " -at-"; message.MAILfrom.Post_office
  822.            Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " SMTPIN] : From " & message.MAILfrom.name & " AT " & message.MAILfrom.Post_office, 0
  823.         End If
  824.       
  825.       Case "RCPT"
  826.         If InStr(buf, "<") > 0 Then
  827.           buf = Mid(buf, InStr(buf, "<") + 1)
  828.           If InStr(buf, ">") Then
  829.             buf = Left(buf, InStr(buf, ">") - 1)
  830.           End If
  831.           ReDim Preserve message.mailto(UBound(message.mailto) + 1)
  832.           message.mailto(UBound(message.mailto)) = smtpaddrparse(buf)
  833.           'debug.print "Added a recipient : "; message.mailto(UBound(message.mailto)).name; " -at- "; message.mailto(UBound(message.mailto)).Post_office
  834.            Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " SMTPIN] : TO :" & message.mailto(UBound(message.mailto)).name & " AT " & message.mailto(UBound(message.mailto)).Post_office, 0
  835.  
  836.         End If
  837.     End Select
  838.   Line Input #smtpFH, buf
  839.   Loop
  840.   ' cleared the smtp now read the envelope!!
  841.   ' loop basis -> read a line, get the next line, if it starts with white space then
  842.   ' it belongs to current line ->append!
  843.   ' otherwise process, reset string
  844.   boundary = ""
  845.   parsestring = ""
  846.   Do While Not buf = ""
  847.   ' starts with DATA!
  848.     If Left(buf, 1) = " " Or Left(buf, 1) = Chr(9) Then 'space or tab - continue!
  849.       parsestring = parsestring & buf
  850.       'debug.print "ADDED a line"
  851.     Else
  852.         parseprocess parsestring
  853.         parsestring = buf
  854.     End If
  855.     If EOF(smtpFH) Then
  856.       Exit Do 'eof error!
  857.     End If
  858.     Line Input #smtpFH, buf
  859.     'debug.print "buf=" & buf
  860.   Loop
  861.   parseprocess parsestring
  862.   
  863.   If EOF(smtpFH) Then
  864.     'error no message exists!!!
  865.     Close #smtpFH
  866.     Exit Sub
  867.   End If
  868.   
  869.   readfilestatus = 0
  870.    ' values 0 = im just reading
  871.    'value 1 = im reading a file
  872.    ' value 2 = im reading text header
  873.   ' value 3 = im reading text body
  874.   
  875.   On Error GoTo finishedreadingfile
  876.   
  877.   Do While Not EOF(smtpFH)
  878.     Select Case readfilestatus
  879.         Case 0 ' im just reading
  880.             If Left(LCase(buf), 13) = "content-trans" Then ' trans means int encoded
  881.               readfilestatus = 1
  882.               GoTo readnext
  883.             End If
  884.             If Left(LCase(buf), 19) = "content-disposition" Or Left(LCase(buf), 12) = "content-type" Then
  885.               readfilestatus = 2 ' could be file or text
  886.               GoTo readnext
  887.             End If
  888.               If buf = "--" & boundary & "--" Or buf = "--" & boundary Then
  889.                    GoTo readnext
  890.               End If
  891.             
  892.            If TextItems(0).Body = "" And buf = "" Then
  893.                 GoTo readnext
  894.            Else
  895.              TextItems(0).Body = TextItems(0).Body & buf & vbCrLf
  896.            End If
  897.         
  898.         
  899.         Case 1 ' im reading a file
  900.               If buf = "--" & boundary & "--" Or buf = "--" & boundary Then
  901.                    readfilestatus = 0
  902.               End If
  903.         Case 2 ' = im reading text header or file header
  904.             If Left(LCase(buf), 13) = "content-trans" Then ' trans means int encoded
  905.               readfilestatus = 1
  906.               hasgotamimebit = True
  907.               GoTo readnext
  908.             End If
  909.             If buf = "" Then ' got to the text bit!
  910.               readfilestatus = 3
  911.               GoTo readnext
  912.             End If
  913.         Case 3 ' = im reading text body
  914.         
  915.               If buf = "--" & boundary & "--" Or buf = "--" & boundary Then
  916.                    readfilestatus = 0
  917.                    
  918.                    GoTo readnext:
  919.               End If
  920.               TextItems(0).Body = TextItems(0).Body & buf & vbCrLf
  921.       End Select
  922. readnext:
  923.       Form1.txCountdown.Caption = "Reading " & statusline
  924.       statusline = statusline + 1
  925.     Line Input #smtpFH, buf
  926.     DoEvents
  927. Loop
  928.   GoTo rfinishedreadingfile
  929. finishedreadingfile:
  930. Resume rfinishedreadingfile
  931. rfinishedreadingfile:
  932.   On Error GoTo failedtoclose
  933.   Close #smtpFH
  934.   GoTo rfailedtoclose
  935. failedtoclose:
  936. Resume rfailedtoclose
  937. rfailedtoclose:
  938.   On Error GoTo 0
  939.   Call UUVBInit
  940.   returncode = UULoadFile(CCMAILWORKINGFOLDER & smtpfilename, CCMAILWORKINGFOLDER & smtpfilename, False)
  941.  
  942.   If UUVBListFirst(ptr) = False Then
  943.     'debug.print "It recons nothing there!!"
  944.      Call UUVBShutdown
  945.      GoTo showbody     ' No files found
  946.   End If
  947.   
  948.   Do While Not ptr = 0
  949.     Debug.Print "uuvblist returns "; UUVBListWalk(ptr, fpath, filestatus, flags, nextptr)
  950.     'debug.print "File:" & fpath & " -> " & filestatus
  951.     ' converting fpath into filename! ' should be able to use filestatus later
  952.     ' have to convert to 8.3 for godole ccmail dos
  953.     If InStr(fpath, ":") > 0 Then
  954.       fpath = Mid(fpath, InStr(fpath, ":") + 1)
  955.     End If
  956.     
  957.     
  958.     
  959.     If InStr(Right(fpath, 5), ".") > 0 Then
  960.       extension = Mid(Right(fpath, 5), InStr(Right(fpath, 5), ".") + 1, 3)
  961.       If InStr(Left(fpath, 8), ".") > 0 Then
  962.         thefilename = Left(fpath, InStr(Left(fpath, 8), ".") - 1)
  963.       Else
  964.         thefilename = Left(fpath, 8)
  965.       End If
  966.     Else
  967.       thefilename = Left(fpath, 8)
  968.       extension = "000"
  969.     End If
  970.     If thefilename = "" Then
  971.       thefilename = "file"
  972.     End If
  973.     If Not Dir(CCMAILWORKINGFOLDER & thefilename & "." & extension) = "" Then
  974.       thefilenumber = 0
  975.       Do While Not Dir(CCMAILWORKINGFOLDER & thefilename & "." & Format(thefilenumber, "000")) = ""
  976.         thefilenumber = thefilenumber + 1
  977.       Loop
  978.       If InStr(thefilename, " ") > 0 Then
  979.         filen = CCMAILWORKINGFOLDER & Left(thefilename, InStr(thefilename, " ") - 1) & "." & Format(thefilenumber, "000")
  980.       Else
  981.         filen = CCMAILWORKINGFOLDER & thefilename & "." & Format(thefilenumber, "000")
  982.       End If
  983.     Else
  984.       If InStr(thefilename, " ") > 0 Then
  985.        filen = CCMAILWORKINGFOLDER & Left(thefilename, InStr(thefilename, " ") - 1) & "." & extension
  986.       Else
  987.  
  988.        filen = CCMAILWORKINGFOLDER & thefilename & "." & extension
  989.       End If
  990.     End If
  991.   'debug.print "going to use " & filen
  992.     If Not UUVBDecode(ptr, flags, filen, uuret, mvret) Then
  993.        'if it failed to decode then just send the body!
  994.        Debug.Print "Failed somehow"
  995.       Call UUVBShutdown
  996.       GoTo trynext
  997.     End If
  998.     ReDim Preserve FileItems(UBound(FileItems) + 1)
  999.     'debug.print "added " & filen
  1000.     FileItems(UBound(FileItems)) = filen '(eg. with dir info?)
  1001. trynext:
  1002.     ptr = nextptr
  1003.   Loop
  1004.   Call UUVBShutdown
  1005.   If ismime Then
  1006.     Exit Sub
  1007.   End If
  1008. ' if the message was not a mime or it failed,
  1009. showbody:
  1010.   ' cop out area - if it all failed just attach the message as a file
  1011. '    If hasgotamimebit = True Then
  1012.       FileItems(0) = smtpfilename
  1013. '    End If
  1014.     
  1015.   
  1016.  
  1017. End Sub
  1018. Sub parseprocess(astring As String)
  1019.  
  1020.  'debug.print "Parsing Line "; astring
  1021.   'find location of ':'
  1022.   Dim colonlocation As Integer
  1023.   Dim itemis As String
  1024.   Dim datais As String
  1025.   Dim currenttestaddress  As String
  1026.   colonlocation = InStr(astring, ":")
  1027.   If colonlocation = 0 Then
  1028.     Exit Sub
  1029.   End If
  1030.   itemis = Trim(LCase(Left(astring, colonlocation - 1)))
  1031.   datais = Trim(Mid(astring, colonlocation + 1))
  1032.   'debug.print itemis; " -> "; datais
  1033.   Select Case LCase(itemis)
  1034. '    Case ">from" - got from from the SMTP header!!
  1035.     
  1036. '=     Case "bcc" lets ignore this, if it is bcc then it can be to!
  1037.     
  1038.     Case "bcc"
  1039.       Do While Not datais = ""
  1040.         If InStr(datais, ",") > 0 Then
  1041.           currenttestaddress = Trim(Left(datais, InStr(datais, ",") - 1))
  1042.           datais = Trim(Mid(datais, InStr(datais, ",") + 1))
  1043.         Else
  1044.           currenttestaddress = Trim(datais)
  1045.           datais = ""
  1046.         End If
  1047.         ReDim Preserve message.mailbcc(UBound(message.mailbcc) + 1)
  1048.         message.mailbcc(UBound(message.mailbcc)) = smtpaddrparse(currenttestaddress)
  1049.       Loop
  1050.     
  1051.     Case "cc"
  1052.       Do While Not datais = ""
  1053.         If InStr(datais, ",") > 0 Then
  1054.           currenttestaddress = Trim(Left(datais, InStr(datais, ",") - 1))
  1055.           datais = Trim(Mid(datais, InStr(datais, ",") + 1))
  1056.         Else
  1057.           currenttestaddress = Trim(datais)
  1058.           datais = ""
  1059.         End If
  1060.         ReDim Preserve message.mailalsocc(UBound(message.mailalsocc) + 1)
  1061.         message.mailalsocc(UBound(message.mailalsocc)) = smtpaddrparse(currenttestaddress)
  1062.       Loop
  1063.     Case "date"
  1064.       message.DateofMessage = rfcdataparse(datais)
  1065. '   Case "from"
  1066.  ' IGNORE THIS?? - use smtp bit!
  1067.    'Case "followup-to"
  1068.      
  1069.    'Case "in-reply-to"
  1070.    
  1071.    'Case "message-id"
  1072.    
  1073.     Case "mime-version"
  1074.       ismime = True ' if it aint then we append the text to the file!
  1075. '   Case "Path"
  1076.  '  Case "Reply-To"
  1077.  '  Case "References"
  1078.    Case "subject"
  1079.      message.subject = datais
  1080. '   Case "Sender"
  1081.    Case "to"
  1082.             'debug.print "got  astring in to ;"; astring
  1083.             Do While Not datais = ""
  1084.               If InStr(datais, ",") > 0 Then
  1085.                 currenttestaddress = Trim(Left(datais, InStr(datais, ",") - 1))
  1086.                 datais = Trim(Mid(datais, InStr(datais, ",") + 1))
  1087.               Else
  1088.                 currenttestaddress = Trim(datais)
  1089.                 datais = ""
  1090.               End If
  1091.               ReDim Preserve message.mailalsoto(UBound(message.mailalsoto) + 1)
  1092.               message.mailalsoto(UBound(message.mailalsoto)) = smtpaddrparse(currenttestaddress)
  1093.             Loop
  1094.    
  1095.    Case "content-type"
  1096.      'debug.print "found content-type"
  1097.               If InStr(LCase(datais), "boundary=") > 0 Then
  1098.       'debug.print "found boundary"
  1099.                     boundary = Mid(datais, InStr(LCase(datais), "boundary=") + 9)
  1100.                     If Left(boundary, 1) = """" Then
  1101.                           boundary = Mid(boundary, 2)
  1102.                     End If
  1103.                     If Right(boundary, 1) = """" Then
  1104.                           boundary = Left(boundary, Len(boundary) - 1)
  1105.                     End If
  1106.                     'debug.print "BOUNDARY IS '" & boundary & "'"
  1107.               End If
  1108.  
  1109.   End Select
  1110. End Sub
  1111.  
  1112.  
  1113.  
  1114. Function Create_ccimport_file() As String
  1115. Dim i As Long
  1116. Dim j As Long
  1117. Dim ccimportfile  As String
  1118. Dim ccimportFH As Integer
  1119. Dim readfilestatus As Integer
  1120. Dim thisfilename As String
  1121. ' before we start!!
  1122. ' check for duplicates in the to/cc/bcc & the also tos!
  1123.   
  1124. '-> how this works ..
  1125. ' all to address are stored in the mailto
  1126. ' to derive their true meaning need to use the
  1127. ' also tos
  1128.   
  1129.   
  1130.   For i = 1 To UBound(message.mailto)
  1131.     
  1132.     'to copy
  1133.     For j = 0 To UBound(message.mailalsoto)
  1134.       If LCase(ccoutaddr(message.mailto(i))) = LCase(ccoutaddr(message.mailalsoto(j))) Then
  1135.         message.mailalsoto(j).Post_office = ""
  1136.         message.mailalsoto(j).name = ""
  1137.       End If
  1138.     Next
  1139.  
  1140.     
  1141.     
  1142.     'cc copy
  1143.     For j = 0 To UBound(message.mailalsocc)
  1144.       
  1145.       If LCase(ccoutaddr(message.mailto(i))) = LCase(ccoutaddr(message.mailalsocc(j))) Then
  1146.         ReDim Preserve message.mailcc(UBound(message.mailcc) + 1)
  1147.         message.mailcc(UBound(message.mailcc)) = message.mailto(i)
  1148.         message.mailto(i).Post_office = ""
  1149.         message.mailto(i).name = ""
  1150.         message.mailalsocc(j).Post_office = ""
  1151.         message.mailalsocc(j).name = ""
  1152.       End If
  1153.     Next
  1154.     For j = 0 To UBound(message.mailbcc)
  1155.       
  1156.       If LCase(ccoutaddr(message.mailto(i))) = LCase(ccoutaddr(message.mailbcc(j))) Then
  1157.         message.mailto(i).Post_office = ""
  1158.         message.mailto(i).name = ""
  1159.       End If
  1160.     Next
  1161.   
  1162.   
  1163.   Next
  1164. changedaddress:
  1165.  
  1166.   ccimportfile = CCMAILTEMPFILE
  1167.   If Not Dir(ccimportfile) = "" Then
  1168.      Kill ccimportfile
  1169.   End If
  1170.   
  1171.   
  1172.   ccimportFH = FreeFile
  1173.   Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " CCOUT]  opening Output file", 0
  1174.   Open ccimportfile For Output As #ccimportFH
  1175.   
  1176.   Print #ccimportFH, "MESSAGE:"
  1177.   Print #ccimportFH, "FROM: " & ccoutaddr(message.MAILfrom)
  1178.   Print #ccimportFH, "DATE: " & ccoutdate(message.DateofMessage)
  1179.   For i = 1 To UBound(message.mailto)
  1180.     If Not message.mailto(i).name = "" Then
  1181.       Print #ccimportFH, "TO: " & ccoutaddr(message.mailto(i))
  1182. '      GoTo addedrecipient
  1183.     End If
  1184.   Next
  1185.   For i = 1 To UBound(message.mailcc)
  1186.     If Not message.mailcc(i).name = "" Then
  1187.       Print #ccimportFH, "CC: " & ccoutaddr(message.mailcc(i))
  1188. '      GoTo addedrecipient
  1189.     End If
  1190.   Next
  1191.   For i = 1 To UBound(message.mailbcc)
  1192.     If Not message.mailbcc(i).name = "" Then
  1193.       Print #ccimportFH, "BCC: " & ccoutaddr(message.mailbcc(i))
  1194. '      GoTo addedrecipient
  1195.     End If
  1196.   Next
  1197. addedrecipient:
  1198.   For i = 1 To UBound(message.mailalsoto)
  1199.     If Not message.mailalsoto(i).name = "" Then
  1200.       Print #ccimportFH, "*TO: " & ccoutaddr(message.mailalsoto(i))
  1201.     End If
  1202.   Next
  1203.   For i = 1 To UBound(message.mailalsocc)
  1204.     If Not message.mailalsocc(i).name = "" Then
  1205.       Print #ccimportFH, "*CC: " & ccoutaddr(message.mailalsocc(i))
  1206.     End If
  1207.   Next
  1208.   Print #ccimportFH, "SUBJECT: " & message.subject
  1209.   Print #ccimportFH, "CONTENTS:"
  1210.   Print #ccimportFH, ""
  1211.   'go through all the files and add the info!!
  1212.   'later we might import text files for bodys!
  1213.   
  1214.   
  1215.   
  1216.   For i = 1 To UBound(FileItems)
  1217.     If Not FileItems(i) = "" Then
  1218.              Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " CCIMPORT]: Adding file " & FileItems(i), 0
  1219.       Print #ccimportFH, "File Item: " & FileItems(i)
  1220.     End If
  1221.   Next
  1222.   If Not TextItems(0).Body = "" Then
  1223.    Print #ccimportFH, "Text item:"
  1224.     Print #ccimportFH, TextItems(0).Body
  1225.   End If
  1226.   
  1227.   If Not FileItems(0) = "" Then
  1228.     Dim tempFHreadoriginal As Integer
  1229.     Dim buf As String
  1230.     tempFHreadoriginal = FreeFile
  1231.     Open CCMAILWORKINGFOLDER & FileItems(0) For Input As #tempFHreadoriginal
  1232.     Line Input #tempFHreadoriginal, buf
  1233.     Do While Not buf = ""
  1234.       Line Input #tempFHreadoriginal, buf
  1235.     Loop
  1236.     Print #ccimportFH, "Text item:"
  1237.     On Error GoTo skipreading
  1238.     Do While Not EOF(tempFHreadoriginal)
  1239.       Line Input #tempFHreadoriginal, buf
  1240.       Print #ccimportFH, buf
  1241.     Loop
  1242.     GoTo rskipreading
  1243. skipreading:
  1244. Resume rskipreading
  1245. rskipreading:
  1246.    On Error GoTo skipclosing
  1247.     Close #tempFHreadoriginal
  1248.     GoTo rskipclosing
  1249. skipclosing:
  1250. Resume rskipclosing
  1251. rskipclosing:
  1252.   On Error GoTo 0
  1253.     ' try using the mime parts to guess what the attachments might be
  1254.     ' start at beginning,
  1255.     ' skip header
  1256.     tempFHreadoriginal = FreeFile
  1257.     Open CCMAILWORKINGFOLDER & FileItems(0) For Input As tempFHreadoriginal
  1258.     Line Input #tempFHreadoriginal, buf
  1259.     Do While Not buf = ""
  1260.       Line Input #tempFHreadoriginal, buf
  1261.     Loop
  1262.      readfilestatus = 0
  1263.      On Error GoTo finishedreadingfile
  1264.      Do While Not EOF(tempFHreadoriginal)
  1265.       Line Input #tempFHreadoriginal, buf
  1266.       If buf = "." Then
  1267.         GoTo rfinishedreadingfile
  1268.       End If
  1269.       If buf = "--" & boundary & "--" Or buf = "--" & boundary Then
  1270.               readfilestatus = 1
  1271.       End If
  1272.       Select Case readfilestatus
  1273.          Case 1
  1274.            If InStr(LCase(buf), "filename") > 0 Then
  1275.               thisfilename = Trim(Mid(buf, InStr(LCase(buf), "filename") + 8))
  1276.               If InStr(thisfilename, "=") > 0 Then
  1277.                 thisfilename = Trim(Mid(thisfilename, InStr(thisfilename, "=") + 1))
  1278.               End If
  1279.               ' enclosed in "
  1280.               If InStr(thisfilename, """") > 0 Then
  1281.                 thisfilename = Trim(Mid(thisfilename, InStr(thisfilename, """") + 1))
  1282.                 If InStr(thisfilename, """") > 0 Then
  1283.                   thisfilename = Trim(Left(thisfilename, InStr(thisfilename, """") - 1))
  1284.                 End If
  1285.               End If
  1286.               If InStr(thisfilename, " ") > 0 Then
  1287.                 thisfilename = Trim(Left(thisfilename, InStr(thisfilename, " ") - 1))
  1288.               End If
  1289.               readfilestatus = 2
  1290.            End If
  1291.           Case 2
  1292.             If buf = "" Then
  1293.                 readfilestatus = 3
  1294.                 Print #ccimportFH, "Text item: " & thisfilename
  1295.             End If
  1296.           Case 3
  1297.                 Print #ccimportFH, buf
  1298.       End Select
  1299.     Loop
  1300.     GoTo rfinishedreadingfile
  1301. finishedreadingfile:
  1302. Resume rfinishedreadingfile
  1303. rfinishedreadingfile:
  1304.   On Error GoTo finishedclosingfile
  1305.     Close #tempFHreadoriginal
  1306.     GoTo rfinishedclosingfile
  1307. finishedclosingfile:
  1308. Resume rfinishedclosingfile
  1309. rfinishedclosingfile:
  1310.   End If
  1311.     Close #ccimportFH
  1312.   Create_ccimport_file = ccimportfile
  1313.   
  1314.  
  1315. End Function
  1316.  
  1317. Function smtpaddrparse(astring As String) As ccADDRESS
  1318.   ' format !
  1319.   Dim temppo As String
  1320.   Dim tempname As String
  1321.   Dim tempccmailpo As String
  1322.   Dim i As Long
  1323.   ' xxx.cgsa.ccmail@envision-design.com.hk - > xxx at cgsa
  1324.   ' abc @ envision-design.com.hk -> xxx at envhkpo
  1325.   ' abc @ xyx.com -> abc@xyx.com at internet
  1326.   
  1327.   ' check for "<" & ">"
  1328.   
  1329.   
  1330.   If InStr(astring, "@") = 0 Then
  1331.        smtpaddrparse.Post_office = LocalPO
  1332.        smtpaddrparse.name = DEFAULTfromUSER
  1333.           'debug.print "address parse returns (NO @ SIGN) "; smtpaddrparse.name; " AT "; smtpaddrparse.Post_office; " from "; astring
  1334.  
  1335.        Exit Function
  1336.   End If
  1337.   temppo = Trim(Mid(astring, InStr(astring, "@") + 1))
  1338.   tempname = Trim(Left(astring, InStr(astring, "@") - 1))
  1339.   If InStr(tempname, "<") > 0 Then
  1340.     tempname = Mid(tempname, InStr(tempname, "<") + 1)
  1341.   End If
  1342.   If InStr(temppo, ">") > 0 Then
  1343.     temppo = Left(temppo, InStr(temppo, ">") - 1)
  1344.   End If
  1345.   
  1346.   
  1347.   
  1348.   If UCase(temppo) = UCase(inetDOMAIN) Then
  1349.         If UCase(Right(tempname, 7)) = ".CCMAIL" Then
  1350.              i = Len(tempname) - 7
  1351.              Do While Not Mid(tempname, i, 1) = "."
  1352.                     i = i - 1
  1353.                     If i = 0 Then
  1354.                        Exit Do
  1355.                    End If
  1356.               Loop
  1357.              If i = 0 Then
  1358.                    smtpaddrparse.Post_office = LocalPO
  1359.                    smtpaddrparse.name = DEFAULTfromUSER
  1360.                       'debug.print "address parse returns "; smtpaddrparse.name; "AT"; smtpaddrparse.Post_office; " from "; astring
  1361.  
  1362.                    Exit Function
  1363.             End If
  1364.             smtpaddrparse.Post_office = Mid(tempname, i + 1, Len(tempname) - 7 - i)
  1365.             smtpaddrparse.name = Left(tempname, i - 1)
  1366.         Else
  1367.               smtpaddrparse.Post_office = LocalPO
  1368.               smtpaddrparse.name = tempname
  1369.         End If
  1370.    Else
  1371.         smtpaddrparse.Post_office = GatewayPO
  1372.         smtpaddrparse.name = tempname & "@" & temppo
  1373.    End If
  1374.    'debug.print "address parse returns - " & smtpaddrparse.name & "-AT-" & smtpaddrparse.Post_office & "- from "; astring
  1375.   
  1376. End Function
  1377. Function ccimport_file(filetoimport As String) As Boolean
  1378. ccimport_file = True
  1379.   ' create the string to do the export
  1380.   Dim idshell As Long
  1381.   Dim abc As Integer
  1382.   Dim ccexportcmdline As String
  1383.   ccexportcmdline = CCMAILEXPORTprognFOLDER & "IMPORT " & LocalPO & " " & CCMAILpassword _
  1384.      & " " & CCMAILpodir & " @" & filetoimport & vbCrLf
  1385.   abc = FreeFile
  1386.     Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " CCIMPORT]  attempting import"
  1387.   #If fullrun Then
  1388.   Open CCMAILWORKINGFOLDER & "export.bat" For Output As #abc
  1389.   Print #abc, ccexportcmdline
  1390.   Print #abc, "dir " & CCMAILWORKINGFOLDER & " export.bat > " & CCMAILWORKINGFOLDER & "9999.999"
  1391.   Close #abc
  1392.   'debug.print ccexportcmdline
  1393.   #Else
  1394.     'debug.print ccexportcmdline
  1395.   #End If
  1396.   #If fullrun Then
  1397.     If Not Dir(CCMAILWORKINGFOLDER & "9999.999") = "" Then
  1398.       Kill CCMAILWORKINGFOLDER & "9999.999"
  1399.     End If
  1400.     If Not Dir(CCMAILEXPORTFOLDER & "*.UND") = "" Then
  1401.        Kill CCMAILEXPORTFOLDER & Dir(CCMAILEXPORTFOLDER & "*.UND")
  1402.     End If
  1403.     
  1404.     idshell = Shell(CCMAILWORKINGFOLDER & "export.bat", 0)
  1405.     Do While Dir(CCMAILWORKINGFOLDER & "9999.999") = ""
  1406.       DoEvents
  1407.     Loop
  1408.         Form1.List2.AddItem "[" & Format(Now, "hh:nn") & " CCIMPORT]  finished importing", 0
  1409.      If Not Dir(CCMAILEXPORTFOLDER & "*.UND") = "" Then
  1410.        FileCopy CCMAILEXPORTFOLDER & Dir(CCMAILEXPORTFOLDER & "*.UND"), CCMAILEXPORTFOLDER & "probs\" & "error.und"
  1411.        ccimport_file = False
  1412.     End If
  1413.   #End If
  1414.  
  1415.  
  1416. End Function
  1417. Function ccoutaddr(anaddress As ccADDRESS)
  1418.   'debug.print "printing address :-" & anaddress.name & " AT " & anaddress.Post_office
  1419.   ccoutaddr = anaddress.name & " AT " & anaddress.Post_office
  1420. End Function
  1421. Function ccoutdate(adate As Date)
  1422.  ccoutdate = Format(adate, "m/d/yy h:nnAM/PM")
  1423. End Function
  1424. Function rfcdataparse(astring As String) As Date
  1425.   rfcdataparse = Now
  1426. End Function
  1427.