-protected
-
- ## do any specialized parsing
- ## returns nil and flashes error message if parsing failed
- def parse_user_query_string s
- extraopts = {}
-
- subs = s.gsub(/\b(to|from):(\S+)\b/) do
- field, name = $1, $2
- if(p = ContactManager.contact_for(name))
- [field, p.email]
- elsif name == "me"
- [field, "(" + AccountManager.user_emails.join("||") + ")"]
- else
- [field, name]
- end.join(":")
- end
-
- ## if we see a label:deleted or a label:spam term anywhere in the query
- ## string, we set the extra load_spam or load_deleted options to true.
- ## bizarre? well, because the query allows arbitrary parenthesized boolean
- ## expressions, without fully parsing the query, we can't tell whether
- ## the user is explicitly directing us to search spam messages or not.
- ## e.g. if the string is -(-(-(-(-label:spam)))), does the user want to
- ## search spam messages or not?
- ##
- ## so, we rely on the fact that turning these extra options ON turns OFF
- ## the adding of "-label:deleted" or "-label:spam" terms at the very
- ## final stage of query processing. if the user wants to search spam
- ## messages, not adding that is the right thing; if he doesn't want to
- ## search spam messages, then not adding it won't have any effect.
- extraopts[:load_spam] = true if subs =~ /\blabel:spam\b/
- extraopts[:load_deleted] = true if subs =~ /\blabel:deleted\b/
-
- ## gmail style "is" operator
- subs = subs.gsub(/\b(is):(\S+)\b/) do
- field, label = $1, $2
- case label
- when "read"
- "-label:unread"
- when "spam"
- extraopts[:load_spam] = true
- "label:spam"
- when "deleted"
- extraopts[:load_deleted] = true
- "label:deleted"
- else
- "label:#{$2}"
- end
- end
-
- if $have_chronic
- chronic_failure = false
- subs = subs.gsub(/\b(before|on|in|during|after):(\((.+?)\)\B|(\S+)\b)/) do
- break if chronic_failure
- field, datestr = $1, ($3 || $4)
- realdate = Chronic.parse(datestr, :guess => false, :context => :none)
- if realdate
- case field
- when "after"
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.end}"
- "date:(>= #{sprintf "%012d", realdate.end.to_i})"
- when "before"
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.begin}"
- "date:(<= #{sprintf "%012d", realdate.begin.to_i})"
- else
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate}"
- "date:(<= #{sprintf "%012d", realdate.end.to_i}) date:(>= #{sprintf "%012d", realdate.begin.to_i})"
- end
- else
- BufferManager.flash "Can't understand date #{datestr.inspect}!"
- chronic_failure = true
- end
- end
- subs = nil if chronic_failure
- end
-
- if subs
- [@qparser.parse(subs), extraopts]
- else
- nil
- end
- end