From af89daae67961761c59b8f476529cae936b16f63 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 3 Apr 2015 00:29:32 +0200 Subject: [PATCH] Added signature detection. --- .../app/controllers/ticket_zoom.js.coffee | 14 ++- .../app/lib/app_post/utils.js.coffee | 90 ++++++++++++++ .../views/ticket_zoom/article_view.jst.eco | 2 +- public/assets/tests/html-utils.js | 110 ++++++++++++++++++ 4 files changed, 210 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index b9f5cca52..db0a3b692 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -484,13 +484,17 @@ class App.TicketZoom extends App.Controller ) # set see more options - previewHeight = 270 + maxHeight = 450 @$('.textBubble-content').each( (index) -> - bubble = $( @ ) - heigth = bubble.height() - if heigth > (previewHeight + 30) + bubble = $( @ ) + offsetTop = bubble.find('.js-signatureMarker').position() + heigth = bubble.height() + if offsetTop bubble.attr('data-height', heigth) - bubble.css('height', "#{previewHeight}px") + bubble.css('height', "#{offsetTop.top + 42}px") + else if heigth > maxHeight + bubble.attr('data-height', heigth) + bubble.css('height', "#{maxHeight}px") else bubble.parent().find('.textBubble-overflowContainer').addClass('hide') ) diff --git a/app/assets/javascripts/app/lib/app_post/utils.js.coffee b/app/assets/javascripts/app/lib/app_post/utils.js.coffee index 876d2bb51..acb53d3c6 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -16,6 +16,26 @@ class App.Utils ascii = '
' + ascii.replace(/\n/g, '
') + '
' ascii.replace(/
<\/div>/g, '

') + # rawText = App.Utils.html2text( html ) + @html2text: ( html ) -> + html = $('
' + html + '
') + + # insert new lines + html.find('div, p, pre, code, center, blockquote, form, textarea, address, table, tr').replaceWith( -> + content = $(@).html() + "\n" + content + .replace(/
/g, "\n") + .replace(//g, "\n") + ) + + # replace
as string, is/was not possible throuh replaceWith + htmlTmp = html.html().replace(/
/g, "\n") + + # trim and cleanup + $('
' + htmlTmp + '
').text().trim() + .replace(/(\r\n|\n\r)/g, "\n") # cleanup + .replace(/\r/g, "\n") # cleanup + # htmlEscapedAndLinkified = App.Utils.linkify( rawText ) @linkify: (ascii) -> window.linkify( ascii ) @@ -166,6 +186,76 @@ class App.Utils else true + # messageWithMarker = App.Utils.signatureIdentify( message, sender ) + @signatureIdentify: (message, sender = false) -> + textToSearch = @html2text( message ) + + # count lines, if we do have lower the 10, ignore this + textToSearchInLines = textToSearch.split("\n") + return message if textToSearchInLines.length < 10 + + quote = (str) -> + (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + + # search for signature seperator "--\n" + searchForSeperator = (textToSearchInLines) -> + for line in textToSearchInLines + if line && line.match( /^\s{0,10}--\s{0,10}$/ ) + return line + false + marker = searchForSeperator(textToSearchInLines) + + # search for Apple Mail + # On 01/04/15 10:55, Bob Smith wrote: + searchForAppleMail = (textToSearchInLines) -> + for line in textToSearchInLines + if line && line.match( /^(On|Am)\s.+?\s(wrote|schrieb):/ ) + return line + false + if !marker + marker = searchForAppleMail(textToSearchInLines) + + # search for otrs + # 25.02.2015 10:26 - edv hotline schrieb: + searchForOtrs = (textToSearchInLines) -> + for line in textToSearchInLines + if line && line.match( /^.+?\s.+?\s-\s.+?\s(wrote|schrieb):/ ) + return line + false + if !marker + marker = searchForOtrs(textToSearchInLines) + + # search for Ms + # From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc] + # Send: Donnerstag, 2. April 2015 10:00 + searchForMs = (textToSearchInLines) -> + fromFound = undefined + for line in textToSearchInLines + if !marker + + # find Sent + if fromFound + if line && line.match( /^(Sent|Gesendet):\s.+?/) + return fromFound + else + fromFound = undefined + + # find From + else + if line && line.match( /^(From|Von):\s.+?/) + fromFound = line + false + if !marker + marker = searchForMs(textToSearchInLines) + + # if no marker is found, return + return message if !marker + + # insert marker + markerTemplate = '' + regex = new RegExp( "\>(\s{0,10}#{quote(marker)})\s{0,10}\<" ) + message.replace( regex, ">#{markerTemplate}\$1<" ) + # textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } ) @replaceTags: (template, objects) -> template = template.replace( /#\{\s{0,2}(.+?)\s{0,2}\}/g, ( index, key ) -> diff --git a/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco b/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco index 9ce99c92b..eb6b32746 100644 --- a/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco +++ b/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco @@ -44,7 +44,7 @@
- <%- @article.html %> + <%- App.Utils.signatureIdentify( @article.html ) %>
<%- @T('See more') %>
diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 6879cdc64..a30c6ef7a 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -66,6 +66,41 @@ test( "text2html", function() { }); +// html2text +test( "html2text", function() { + + var source = "
Some
Value

Test
" + var should = "Some\nValue\n\n\nTest" + var result = App.Utils.html2text( source ) + equal( result, should, source ) + + source = "
Some
Value
" + should = "Some\nValue" + result = App.Utils.html2text( source ) + equal( result, should, source ) + + source = "
Some
Value
" + should = "Some\nValue" + result = App.Utils.html2text( source ) + equal( result, should, source ) + + source = "
Some
<b>Value</b>
" + should = "Some\nValue" + result = App.Utils.html2text( source ) + equal( result, should, source ) + + source = "
> Welcome!
>
> Thank you for installing Zammad.
>
> You will find ...
" + should = "> Welcome!\n>\n> Thank you for installing Zammad.\n>\n> You will find ..." + result = App.Utils.html2text( source ) + equal( result, should, source ) + + source = "
test 123










--
Bob Smith
" + should = "test 123 \n\n\n\n\n\n\n\n\n\n\n--\nBob Smith" + result = App.Utils.html2text( source ) + equal( result, should, source ) + +}); + // linkify test( "linkify", function() { @@ -451,6 +486,81 @@ test( "check signature", function() { }); +// identify signature +test( "identify signature", function() { + + var message = "
test 123
" + var should = '
test 123
' + var result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123
--
Bob Smith
" + should = '
test 123
--
Bob Smith
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123










--
Bob Smith
" + should = '
test 123










--
Bob Smith
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--
--
Bob Smith
" + should = '
test 123









--no not match--
--
Bob Smith
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--
--
Bob Smith
" + should = '
test 123









--no not match--
--
Bob Smith
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123










--
Bob Smith




--
Bob Smith
" + should = '
test 123










--
Bob Smith




--
Bob Smith
' + //should = '
test 123










--
Bob Smith




--
Bob Smith
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123
test 123
test 123
test 123
test 123
test 123
test 123
test 123
--
Bob Smith
" + should = "
test 123
test 123
test 123
test 123
test 123
test 123
test 123
test 123
--
Bob Smith
" + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "

test 123

test 123

test 123

test 123

test 123

test 123

test 123

test 123

--

Bob Smith

" + should = "

test 123

test 123

test 123

test 123

test 123

test 123

test 123

test 123

--

Bob Smith

" + result = App.Utils.signatureIdentify( message ) + + message = "
test 123









--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala
" + should = '
test 123









--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
" + should = '
test 123









--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
" + should = '
test 123









--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
" + should = '
test 123









--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
" + should = '
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + + message = "
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
" + should = '
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
' + result = App.Utils.signatureIdentify( message ) + equal( result, should ) + +}); + // replace tags test( "check replace tags", function() {