1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "msgCore.h"
#include "nsTextFormatter.h"
#include "nsMsgSearchCore.h"
#include "nsMsgSearchAdapter.h"
#include "nsMsgSearchScopeTerm.h"
#include "nsMsgI18N.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefLocalizedString.h"
#include "nsMsgSearchTerm.h"
#include "nsMsgSearchBoolExpression.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#include "prprf.h"
#include "mozilla/UniquePtr.h"
#include "prmem.h"
#include "MailNewsTypes.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsMemory.h"
#include "nsMsgMessageFlags.h"
// Disable deprecation warnings generated by nsISupportsArray and associated
// classes.
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning (disable : 4996)
#endif
#include "nsISupportsArray.h"
#include "nsAlgorithm.h"
#include <algorithm>
#include "mozilla/Attributes.h"
// This stuff lives in the base class because the IMAP search syntax
// is used by the Dredd SEARCH command as well as IMAP itself
// km - the NOT and HEADER strings are not encoded with a trailing
// <space> because they always precede a mnemonic that has a
// preceding <space> and double <space> characters cause some
// imap servers to return an error.
const char *nsMsgSearchAdapter::m_kImapBefore = " SENTBEFORE ";
const char *nsMsgSearchAdapter::m_kImapBody = " BODY ";
const char *nsMsgSearchAdapter::m_kImapCC = " CC ";
const char *nsMsgSearchAdapter::m_kImapFrom = " FROM ";
const char *nsMsgSearchAdapter::m_kImapNot = " NOT";
const char *nsMsgSearchAdapter::m_kImapUnDeleted= " UNDELETED";
const char *nsMsgSearchAdapter::m_kImapOr = " OR";
const char *nsMsgSearchAdapter::m_kImapSince = " SENTSINCE ";
const char *nsMsgSearchAdapter::m_kImapSubject = " SUBJECT ";
const char *nsMsgSearchAdapter::m_kImapTo = " TO ";
const char *nsMsgSearchAdapter::m_kImapHeader = " HEADER";
const char *nsMsgSearchAdapter::m_kImapAnyText = " TEXT ";
const char *nsMsgSearchAdapter::m_kImapKeyword = " KEYWORD ";
const char *nsMsgSearchAdapter::m_kNntpKeywords = " KEYWORDS "; //ggrrrr...
const char *nsMsgSearchAdapter::m_kImapSentOn = " SENTON ";
const char *nsMsgSearchAdapter::m_kImapSeen = " SEEN ";
const char *nsMsgSearchAdapter::m_kImapAnswered = " ANSWERED ";
const char *nsMsgSearchAdapter::m_kImapNotSeen = " UNSEEN ";
const char *nsMsgSearchAdapter::m_kImapNotAnswered = " UNANSWERED ";
const char *nsMsgSearchAdapter::m_kImapCharset = " CHARSET ";
const char *nsMsgSearchAdapter::m_kImapSizeSmaller = " SMALLER ";
const char *nsMsgSearchAdapter::m_kImapSizeLarger = " LARGER ";
const char *nsMsgSearchAdapter::m_kImapNew = " NEW ";
const char *nsMsgSearchAdapter::m_kImapNotNew = " OLD SEEN ";
const char *nsMsgSearchAdapter::m_kImapFlagged = " FLAGGED ";
const char *nsMsgSearchAdapter::m_kImapNotFlagged = " UNFLAGGED ";
#define PREF_CUSTOM_HEADERS "mailnews.customHeaders"
NS_IMETHODIMP nsMsgSearchAdapter::FindTargetFolder(const nsMsgResultElement *,nsIMsgFolder * *)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsMsgSearchAdapter::ModifyResultElement(nsMsgResultElement *, nsMsgSearchValue *)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsMsgSearchAdapter::OpenResultElement(nsMsgResultElement *)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMPL_ISUPPORTS(nsMsgSearchAdapter, nsIMsgSearchAdapter)
nsMsgSearchAdapter::nsMsgSearchAdapter(nsIMsgSearchScopeTerm *scope, nsISupportsArray *searchTerms)
: m_searchTerms(searchTerms)
{
m_scope = scope;
}
nsMsgSearchAdapter::~nsMsgSearchAdapter()
{
}
NS_IMETHODIMP nsMsgSearchAdapter::ClearScope()
{
if (m_scope)
{
m_scope->CloseInputStream();
m_scope = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP nsMsgSearchAdapter::ValidateTerms ()
{
// all this used to do is check if the object had been deleted - we can skip that.
return NS_OK;
}
NS_IMETHODIMP nsMsgSearchAdapter::Abort ()
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsMsgSearchAdapter::Search (bool *aDone)
{
return NS_OK;
}
NS_IMETHODIMP nsMsgSearchAdapter::SendUrl ()
{
return NS_OK;
}
/* void CurrentUrlDone (in nsresult exitCode); */
NS_IMETHODIMP nsMsgSearchAdapter::CurrentUrlDone(nsresult exitCode)
{
// base implementation doesn't need to do anything.
return NS_OK;
}
NS_IMETHODIMP nsMsgSearchAdapter::GetEncoding (char **encoding)
{
return NS_OK;
}
NS_IMETHODIMP nsMsgSearchAdapter::AddResultElement (nsIMsgDBHdr *pHeaders)
{
NS_ASSERTION(false, "shouldn't call this base class impl");
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsMsgSearchAdapter::AddHit(nsMsgKey key)
{
NS_ASSERTION(false, "shouldn't call this base class impl");
return NS_ERROR_FAILURE;
}
char *
nsMsgSearchAdapter::GetImapCharsetParam(const char16_t *destCharset)
{
char *result = nullptr;
// Specify a character set unless we happen to be US-ASCII.
if (NS_strcmp(destCharset, u"us-ascii"))
result = PR_smprintf("%s%s", nsMsgSearchAdapter::m_kImapCharset, NS_ConvertUTF16toUTF8(destCharset).get());
return result;
}
/*
09/21/2000 - taka@netscape.com
This method is bogus. Escape must be done against char * not char16_t *
should be rewritten later.
for now, just duplicate the string.
*/
char16_t *nsMsgSearchAdapter::EscapeSearchUrl (const char16_t *nntpCommand)
{
return nntpCommand ? NS_strdup(nntpCommand) : nullptr;
}
/*
09/21/2000 - taka@netscape.com
This method is bogus. Escape must be done against char * not char16_t *
should be rewritten later.
for now, just duplicate the string.
*/
char16_t *
nsMsgSearchAdapter::EscapeImapSearchProtocol(const char16_t *imapCommand)
{
return imapCommand ? NS_strdup(imapCommand) : nullptr;
}
/*
09/21/2000 - taka@netscape.com
This method is bogus. Escape must be done against char * not char16_t *
should be rewritten later.
for now, just duplicate the string.
*/
char16_t *
nsMsgSearchAdapter::EscapeQuoteImapSearchProtocol(const char16_t *imapCommand)
{
return imapCommand ? NS_strdup(imapCommand) : nullptr;
}
char *nsMsgSearchAdapter::UnEscapeSearchUrl (const char *commandSpecificData)
{
char *result = (char*) PR_Malloc (strlen(commandSpecificData) + 1);
if (result)
{
char *resultPtr = result;
while (1)
{
char ch = *commandSpecificData++;
if (!ch)
break;
if (ch == '\\')
{
char scratchBuf[3];
scratchBuf[0] = (char) *commandSpecificData++;
scratchBuf[1] = (char) *commandSpecificData++;
scratchBuf[2] = '\0';
unsigned int accum = 0;
sscanf (scratchBuf, "%X", &accum);
*resultPtr++ = (char) accum;
}
else
*resultPtr++ = ch;
}
*resultPtr = '\0';
}
return result;
}
nsresult
nsMsgSearchAdapter::GetSearchCharsets(nsAString &srcCharset, nsAString &dstCharset)
{
nsresult rv;
if (m_defaultCharset.IsEmpty())
{
m_forceAsciiSearch = false; // set the default value in case of error
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsIPrefLocalizedString> localizedstr;
rv = prefs->GetComplexValue("mailnews.view_default_charset", NS_GET_IID(nsIPrefLocalizedString),
getter_AddRefs(localizedstr));
if (NS_SUCCEEDED(rv))
localizedstr->GetData(getter_Copies(m_defaultCharset));
prefs->GetBoolPref("mailnews.force_ascii_search", &m_forceAsciiSearch);
}
}
srcCharset = m_defaultCharset.IsEmpty() ?
static_cast<const nsAString&>(NS_LITERAL_STRING("ISO-8859-1")) :
m_defaultCharset;
if (m_scope)
{
// ### DMB is there a way to get the charset for the "window"?
nsCOMPtr<nsIMsgFolder> folder;
rv = m_scope->GetFolder(getter_AddRefs(folder));
// Ask the newsgroup/folder for its csid.
if (NS_SUCCEEDED(rv) && folder)
{
nsCString folderCharset;
folder->GetCharset(folderCharset);
dstCharset.Append(NS_ConvertASCIItoUTF16(folderCharset));
}
}
else
dstCharset.Assign(srcCharset);
// If
// the destination is still CS_DEFAULT, make the destination match
// the source. (CS_DEFAULT is an indication that the charset
// was undefined or unavailable.)
// ### well, it's not really anymore. Is there an equivalent?
if (dstCharset.Equals(m_defaultCharset))
{
dstCharset.Assign(srcCharset);
}
if (m_forceAsciiSearch)
{
// Special cases to use in order to force US-ASCII searching with Latin1
// or MacRoman text. Eurgh. This only has to happen because IMAP
// and Dredd servers currently (4/23/97) only support US-ASCII.
//
// If the dest csid is ISO Latin 1 or MacRoman, attempt to convert the
// source text to US-ASCII. (Not for now.)
// if ((dst_csid == CS_LATIN1) || (dst_csid == CS_MAC_ROMAN))
dstCharset.AssignLiteral("us-ascii");
}
return NS_OK;
}
nsresult nsMsgSearchAdapter::EncodeImapTerm (nsIMsgSearchTerm *term, bool reallyDredd, const char16_t *srcCharset, const char16_t *destCharset, char **ppOutTerm)
{
NS_ENSURE_ARG_POINTER(term);
NS_ENSURE_ARG_POINTER(ppOutTerm);
nsresult err = NS_OK;
bool useNot = false;
bool useQuotes = false;
bool ignoreValue = false;
nsAutoCString arbitraryHeader;
const char *whichMnemonic = nullptr;
const char *orHeaderMnemonic = nullptr;
*ppOutTerm = nullptr;
nsCOMPtr <nsIMsgSearchValue> searchValue;
nsresult rv = term->GetValue(getter_AddRefs(searchValue));
NS_ENSURE_SUCCESS(rv,rv);
nsMsgSearchOpValue op;
term->GetOp(&op);
if (op == nsMsgSearchOp::DoesntContain || op == nsMsgSearchOp::Isnt)
useNot = true;
nsMsgSearchAttribValue attrib;
term->GetAttrib(&attrib);
switch (attrib)
{
case nsMsgSearchAttrib::ToOrCC:
orHeaderMnemonic = m_kImapCC;
// fall through to case nsMsgSearchAttrib::To:
MOZ_FALLTHROUGH;
case nsMsgSearchAttrib::To:
whichMnemonic = m_kImapTo;
break;
case nsMsgSearchAttrib::CC:
whichMnemonic = m_kImapCC;
break;
case nsMsgSearchAttrib::Sender:
whichMnemonic = m_kImapFrom;
break;
case nsMsgSearchAttrib::Subject:
whichMnemonic = m_kImapSubject;
break;
case nsMsgSearchAttrib::Body:
whichMnemonic = m_kImapBody;
break;
case nsMsgSearchAttrib::AgeInDays: // added for searching online for age in days...
// for AgeInDays, we are actually going to perform a search by date, so convert the operations for age
// to the IMAP mnemonics that we would use for date!
{
// If we have a future date, the > and < are reversed.
// e.g. ageInDays > 2 means more than 2 days old ("date before X") whereas
// ageInDays > -2 should be more than 2 days in the future ("date after X")
int32_t ageInDays;
searchValue->GetAge(&ageInDays);
bool dateInFuture = (ageInDays < 0);
switch (op)
{
case nsMsgSearchOp::IsGreaterThan:
whichMnemonic = (!dateInFuture) ? m_kImapBefore : m_kImapSince;
break;
case nsMsgSearchOp::IsLessThan:
whichMnemonic = (!dateInFuture) ? m_kImapSince : m_kImapBefore;
break;
case nsMsgSearchOp::Is:
whichMnemonic = m_kImapSentOn;
break;
default:
NS_ASSERTION(false, "invalid search operator");
return NS_ERROR_INVALID_ARG;
}
}
break;
case nsMsgSearchAttrib::Size:
switch (op)
{
case nsMsgSearchOp::IsGreaterThan:
whichMnemonic = m_kImapSizeLarger;
break;
case nsMsgSearchOp::IsLessThan:
whichMnemonic = m_kImapSizeSmaller;
break;
default:
NS_ASSERTION(false, "invalid search operator");
return NS_ERROR_INVALID_ARG;
}
break;
case nsMsgSearchAttrib::Date:
switch (op)
{
case nsMsgSearchOp::IsBefore:
whichMnemonic = m_kImapBefore;
break;
case nsMsgSearchOp::IsAfter:
whichMnemonic = m_kImapSince;
break;
case nsMsgSearchOp::Isnt: /* we've already added the "Not" so just process it like it was a date is search */
case nsMsgSearchOp::Is:
whichMnemonic = m_kImapSentOn;
break;
default:
NS_ASSERTION(false, "invalid search operator");
return NS_ERROR_INVALID_ARG;
}
break;
case nsMsgSearchAttrib::AnyText:
whichMnemonic = m_kImapAnyText;
break;
case nsMsgSearchAttrib::Keywords:
whichMnemonic = m_kImapKeyword;
break;
case nsMsgSearchAttrib::MsgStatus:
useNot = false; // bizarrely, NOT SEEN is wrong, but UNSEEN is right.
ignoreValue = true; // the mnemonic is all we need
uint32_t status;
searchValue->GetStatus(&status);
switch (status)
{
case nsMsgMessageFlags::Read:
whichMnemonic = op == nsMsgSearchOp::Is ? m_kImapSeen : m_kImapNotSeen;
break;
case nsMsgMessageFlags::Replied:
whichMnemonic = op == nsMsgSearchOp::Is ? m_kImapAnswered : m_kImapNotAnswered;
break;
case nsMsgMessageFlags::New:
whichMnemonic = op == nsMsgSearchOp::Is ? m_kImapNew : m_kImapNotNew;
break;
case nsMsgMessageFlags::Marked:
whichMnemonic = op == nsMsgSearchOp::Is ? m_kImapFlagged : m_kImapNotFlagged;
break;
default:
NS_ASSERTION(false, "invalid search operator");
return NS_ERROR_INVALID_ARG;
}
break;
default:
if ( attrib > nsMsgSearchAttrib::OtherHeader && attrib < nsMsgSearchAttrib::kNumMsgSearchAttributes)
{
nsCString arbitraryHeaderTerm;
term->GetArbitraryHeader(arbitraryHeaderTerm);
if (!arbitraryHeaderTerm.IsEmpty())
{
arbitraryHeader.AssignLiteral(" \"");
arbitraryHeader.Append(arbitraryHeaderTerm);
arbitraryHeader.AppendLiteral("\" ");
whichMnemonic = arbitraryHeader.get();
}
else
return NS_ERROR_FAILURE;
}
else
{
NS_ASSERTION(false, "invalid search operator");
return NS_ERROR_INVALID_ARG;
}
}
char *value = nullptr;
char dateBuf[100];
dateBuf[0] = '\0';
bool valueWasAllocated = false;
if (attrib == nsMsgSearchAttrib::Date)
{
// note that there used to be code here that encoded an RFC822 date for imap searches.
// The IMAP RFC 2060 is misleading to the point that it looks like it requires an RFC822
// date but really it expects dd-mmm-yyyy, like dredd, and refers to the RFC822 date only in that the
// dd-mmm-yyyy date will match the RFC822 date within the message.
PRTime adjustedDate;
searchValue->GetDate(&adjustedDate);
if (whichMnemonic == m_kImapSince)
{
// it looks like the IMAP server searches on Since includes the date in question...
// our UI presents Is, IsGreater and IsLessThan. For the IsGreater case (m_kImapSince)
// we need to adjust the date so we get greater than and not greater than or equal to which
// is what the IMAP server wants to search on
// won't work on Mac.
adjustedDate += PR_USEC_PER_DAY;
}
PRExplodedTime exploded;
PR_ExplodeTime(adjustedDate, PR_LocalTimeParameters, &exploded);
PR_FormatTimeUSEnglish(dateBuf, sizeof(dateBuf), "%d-%b-%Y", &exploded);
// strftime (dateBuf, sizeof(dateBuf), "%d-%b-%Y", localtime (/* &term->m_value.u.date */ &adjustedDate));
value = dateBuf;
}
else
{
if (attrib == nsMsgSearchAttrib::AgeInDays)
{
// okay, take the current date, subtract off the age in days, then do an appropriate Date search on
// the resulting day.
int32_t ageInDays;
searchValue->GetAge(&ageInDays);
PRTime now = PR_Now();
PRTime matchDay = now - ageInDays * PR_USEC_PER_DAY;
PRExplodedTime exploded;
PR_ExplodeTime(matchDay, PR_LocalTimeParameters, &exploded);
PR_FormatTimeUSEnglish(dateBuf, sizeof(dateBuf), "%d-%b-%Y", &exploded);
// strftime (dateBuf, sizeof(dateBuf), "%d-%b-%Y", localtime (&matchDay));
value = dateBuf;
}
else if (attrib == nsMsgSearchAttrib::Size)
{
uint32_t sizeValue;
nsAutoCString searchTermValue;
searchValue->GetSize(&sizeValue);
// Multiply by 1024 to get into kb resolution
sizeValue *= 1024;
// Ensure that greater than is really greater than
// in kb resolution.
if (op == nsMsgSearchOp::IsGreaterThan)
sizeValue += 1024;
searchTermValue.AppendInt(sizeValue);
value = ToNewCString(searchTermValue);
valueWasAllocated = true;
}
else
if (IS_STRING_ATTRIBUTE(attrib))
{
char16_t *convertedValue; // = reallyDredd ? MSG_EscapeSearchUrl (term->m_value.u.string) : msg_EscapeImapSearchProtocol(term->m_value.u.string);
nsString searchTermValue;
searchValue->GetStr(searchTermValue);
// Ugly switch for Korean mail/news charsets.
// We want to do this here because here is where
// we know what charset we want to use.
#ifdef DOING_CHARSET
if (reallyDredd)
dest_csid = INTL_DefaultNewsCharSetID(dest_csid);
else
dest_csid = INTL_DefaultMailCharSetID(dest_csid);
#endif
// do all sorts of crazy escaping
convertedValue = reallyDredd ? EscapeSearchUrl (searchTermValue.get()) :
EscapeImapSearchProtocol(searchTermValue.get());
useQuotes = ((!reallyDredd ||
(nsDependentString(convertedValue).FindChar(char16_t(' ')) != -1)) &&
(attrib != nsMsgSearchAttrib::Keywords));
// now convert to char* and escape quoted_specials
nsAutoCString valueStr;
nsresult rv = ConvertFromUnicode(NS_LossyConvertUTF16toASCII(destCharset).get(),
nsDependentString(convertedValue), valueStr);
if (NS_SUCCEEDED(rv))
{
const char *vptr = valueStr.get();
// max escaped length is one extra character for every character in the cmd.
mozilla::UniquePtr<char[]> newValue = mozilla::MakeUnique<char[]>(2*strlen(vptr) + 1);
if (newValue)
{
char *p = newValue.get();
while (1)
{
char ch = *vptr++;
if (!ch)
break;
if ((useQuotes ? ch == '"' : 0) || ch == '\\')
*p++ = '\\';
*p++ = ch;
}
*p = '\0';
value = strdup(newValue.get()); // realloc down to smaller size
}
}
else
value = strdup("");
NS_Free(convertedValue);
valueWasAllocated = true;
}
}
// this should be rewritten to use nsCString
int subLen =
(value ? strlen(value) : 0) +
(useNot ? strlen(m_kImapNot) : 0) +
strlen(m_kImapHeader);
int len = strlen(whichMnemonic) + subLen + (useQuotes ? 2 : 0) +
(orHeaderMnemonic
? (subLen + strlen(m_kImapOr) + strlen(orHeaderMnemonic) + 2 /*""*/)
: 0) +
10; // add slough for imap string literals
char *encoding = new char[len];
if (encoding)
{
encoding[0] = '\0';
// Remember: if ToOrCC and useNot then the expression becomes NOT To AND Not CC as opposed to (NOT TO) || (NOT CC)
if (orHeaderMnemonic && !useNot)
PL_strcat(encoding, m_kImapOr);
if (useNot)
PL_strcat (encoding, m_kImapNot);
if (!arbitraryHeader.IsEmpty())
PL_strcat (encoding, m_kImapHeader);
PL_strcat (encoding, whichMnemonic);
if (!ignoreValue)
err = EncodeImapValue(encoding, value, useQuotes, reallyDredd);
if (orHeaderMnemonic)
{
if (useNot)
PL_strcat(encoding, m_kImapNot);
PL_strcat (encoding, m_kImapHeader);
PL_strcat (encoding, orHeaderMnemonic);
if (!ignoreValue)
err = EncodeImapValue(encoding, value, useQuotes, reallyDredd);
}
// kmcentee, don't let the encoding end with whitespace,
// this throws off later url STRCMP
if (*encoding && *(encoding + strlen(encoding) - 1) == ' ')
*(encoding + strlen(encoding) - 1) = '\0';
}
if (value && valueWasAllocated)
NS_Free (value);
*ppOutTerm = encoding;
return err;
}
nsresult nsMsgSearchAdapter::EncodeImapValue(char *encoding, const char *value, bool useQuotes, bool reallyDredd)
{
// By NNTP RFC, SEARCH HEADER SUBJECT "" is legal and means 'find messages without a subject header'
if (!reallyDredd)
{
// By IMAP RFC, SEARCH HEADER SUBJECT "" is illegal and will generate an error from the server
if (!value || !value[0])
return NS_ERROR_NULL_POINTER;
}
if (!NS_IsAscii(value))
{
nsAutoCString lengthStr;
PL_strcat(encoding, "{");
lengthStr.AppendInt((int32_t) strlen(value));
PL_strcat(encoding, lengthStr.get());
PL_strcat(encoding, "}" CRLF);
PL_strcat(encoding, value);
return NS_OK;
}
if (useQuotes)
PL_strcat(encoding, "\"");
PL_strcat (encoding, value);
if (useQuotes)
PL_strcat(encoding, "\"");
return NS_OK;
}
nsresult nsMsgSearchAdapter::EncodeImap (char **ppOutEncoding, nsISupportsArray *searchTerms, const char16_t *srcCharset, const char16_t *destCharset, bool reallyDredd)
{
// i've left the old code (before using CBoolExpression for debugging purposes to make sure that
// the new code generates the same encoding string as the old code.....
nsresult err = NS_OK;
*ppOutEncoding = nullptr;
uint32_t termCount;
searchTerms->Count(&termCount);
uint32_t i = 0;
// create our expression
nsMsgSearchBoolExpression * expression = new nsMsgSearchBoolExpression();
if (!expression)
return NS_ERROR_OUT_OF_MEMORY;
for (i = 0; i < termCount && NS_SUCCEEDED(err); i++)
{
char *termEncoding;
bool matchAll;
nsCOMPtr<nsIMsgSearchTerm> pTerm;
searchTerms->QueryElementAt(i, NS_GET_IID(nsIMsgSearchTerm),
(void **)getter_AddRefs(pTerm));
pTerm->GetMatchAll(&matchAll);
if (matchAll)
continue;
err = EncodeImapTerm (pTerm, reallyDredd, srcCharset, destCharset, &termEncoding);
if (NS_SUCCEEDED(err) && nullptr != termEncoding)
{
expression = nsMsgSearchBoolExpression::AddSearchTerm(expression, pTerm, termEncoding);
delete [] termEncoding;
}
}
if (NS_SUCCEEDED(err))
{
// Catenate the intermediate encodings together into a big string
nsAutoCString encodingBuff;
if (!reallyDredd)
encodingBuff.Append(m_kImapUnDeleted);
expression->GenerateEncodeStr(&encodingBuff);
*ppOutEncoding = ToNewCString(encodingBuff);
}
delete expression;
return err;
}
char *nsMsgSearchAdapter::TransformSpacesToStars (const char *spaceString, msg_TransformType transformType)
{
char *starString;
if (transformType == kOverwrite)
{
if ((starString = strdup(spaceString)) != nullptr)
{
char *star = starString;
while ((star = PL_strchr(star, ' ')) != nullptr)
*star = '*';
}
}
else
{
int i, count;
for (i = 0, count = 0; spaceString[i]; )
{
if (spaceString[i++] == ' ')
{
count++;
while (spaceString[i] && spaceString[i] == ' ') i++;
}
}
if (transformType == kSurround)
count *= 2;
if (count > 0)
{
if ((starString = (char *)PR_Malloc(i + count + 1)) != nullptr)
{
int j;
for (i = 0, j = 0; spaceString[i]; )
{
if (spaceString[i] == ' ')
{
starString[j++] = '*';
starString[j++] = ' ';
if (transformType == kSurround)
starString[j++] = '*';
i++;
while (spaceString[i] && spaceString[i] == ' ')
i++;
}
else
starString[j++] = spaceString[i++];
}
starString[j] = 0;
}
}
else
starString = strdup(spaceString);
}
return starString;
}
//-----------------------------------------------------------------------------
//------------------- Validity checking for menu items etc. -------------------
//-----------------------------------------------------------------------------
nsMsgSearchValidityTable::nsMsgSearchValidityTable ()
{
// Set everything to be unavailable and disabled
for (int i = 0; i < nsMsgSearchAttrib::kNumMsgSearchAttributes; i++)
for (int j = 0; j < nsMsgSearchOp::kNumMsgSearchOperators; j++)
{
SetAvailable (i, j, false);
SetEnabled (i, j, false);
SetValidButNotShown (i,j, false);
}
m_numAvailAttribs = 0; // # of attributes marked with at least one available operator
// assume default is Subject, which it is for mail and news search
// it's not for LDAP, so we'll call SetDefaultAttrib()
m_defaultAttrib = nsMsgSearchAttrib::Subject;
}
NS_IMPL_ISUPPORTS(nsMsgSearchValidityTable, nsIMsgSearchValidityTable)
nsresult
nsMsgSearchValidityTable::GetNumAvailAttribs(int32_t *aResult)
{
m_numAvailAttribs = 0;
for (int i = 0; i < nsMsgSearchAttrib::kNumMsgSearchAttributes; i++)
for (int j = 0; j < nsMsgSearchOp::kNumMsgSearchOperators; j++) {
bool available;
GetAvailable(i, j, &available);
if (available)
{
m_numAvailAttribs++;
break;
}
}
*aResult = m_numAvailAttribs;
return NS_OK;
}
nsresult
nsMsgSearchValidityTable::ValidateTerms (nsISupportsArray *searchTerms)
{
nsresult err = NS_OK;
uint32_t count;
NS_ENSURE_ARG(searchTerms);
searchTerms->Count(&count);
for (uint32_t i = 0; i < count; i++)
{
nsCOMPtr<nsIMsgSearchTerm> pTerm;
searchTerms->QueryElementAt(i, NS_GET_IID(nsIMsgSearchTerm),
(void **)getter_AddRefs(pTerm));
nsIMsgSearchTerm *iTerm = pTerm;
nsMsgSearchTerm *term = static_cast<nsMsgSearchTerm *>(iTerm);
// XP_ASSERT(term->IsValid());
bool enabled;
bool available;
GetEnabled(term->m_attribute, term->m_operator, &enabled);
GetAvailable(term->m_attribute, term->m_operator, &available);
if (!enabled || !available)
{
bool validNotShown;
GetValidButNotShown(term->m_attribute, term->m_operator,
&validNotShown);
if (!validNotShown)
err = NS_MSG_ERROR_INVALID_SEARCH_SCOPE;
}
}
return err;
}
nsresult
nsMsgSearchValidityTable::GetAvailableAttributes(uint32_t *length,
nsMsgSearchAttribValue **aResult)
{
NS_ENSURE_ARG_POINTER(length);
NS_ENSURE_ARG_POINTER(aResult);
// count first
uint32_t totalAttributes=0;
int32_t i, j;
for (i = 0; i< nsMsgSearchAttrib::kNumMsgSearchAttributes; i++) {
for (j=0; j< nsMsgSearchOp::kNumMsgSearchOperators; j++) {
if (m_table[i][j].bitAvailable) {
totalAttributes++;
break;
}
}
}
nsMsgSearchAttribValue *array = (nsMsgSearchAttribValue*)
moz_xmalloc(sizeof(nsMsgSearchAttribValue) * totalAttributes);
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
uint32_t numStored=0;
for (i = 0; i< nsMsgSearchAttrib::kNumMsgSearchAttributes; i++) {
for (j=0; j< nsMsgSearchOp::kNumMsgSearchOperators; j++) {
if (m_table[i][j].bitAvailable) {
array[numStored++] = i;
break;
}
}
}
NS_ASSERTION(totalAttributes == numStored, "Search Attributes not lining up");
*length = totalAttributes;
*aResult = array;
return NS_OK;
}
nsresult
nsMsgSearchValidityTable::GetAvailableOperators(nsMsgSearchAttribValue aAttribute,
uint32_t *aLength,
nsMsgSearchOpValue **aResult)
{
NS_ENSURE_ARG_POINTER(aLength);
NS_ENSURE_ARG_POINTER(aResult);
nsMsgSearchAttribValue attr;
if (aAttribute == nsMsgSearchAttrib::Default)
attr = m_defaultAttrib;
else
attr = std::min(aAttribute,
(nsMsgSearchAttribValue)nsMsgSearchAttrib::OtherHeader);
uint32_t totalOperators=0;
int32_t i;
for (i=0; i<nsMsgSearchOp::kNumMsgSearchOperators; i++) {
if (m_table[attr][i].bitAvailable)
totalOperators++;
}
nsMsgSearchOpValue *array = (nsMsgSearchOpValue*)
moz_xmalloc(sizeof(nsMsgSearchOpValue) * totalOperators);
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
uint32_t numStored = 0;
for (i=0; i<nsMsgSearchOp::kNumMsgSearchOperators;i++) {
if (m_table[attr][i].bitAvailable)
array[numStored++] = i;
}
NS_ASSERTION(totalOperators == numStored, "Search Operators not lining up");
*aLength = totalOperators;
*aResult = array;
return NS_OK;
}
NS_IMETHODIMP
nsMsgSearchValidityTable::SetDefaultAttrib(nsMsgSearchAttribValue aAttribute)
{
m_defaultAttrib = aAttribute;
return NS_OK;
}
nsMsgSearchValidityManager::nsMsgSearchValidityManager ()
{
}
nsMsgSearchValidityManager::~nsMsgSearchValidityManager ()
{
// tables released by nsCOMPtr
}
NS_IMPL_ISUPPORTS(nsMsgSearchValidityManager, nsIMsgSearchValidityManager)
//-----------------------------------------------------------------------------
// Bottleneck accesses to the objects so we can allocate and initialize them
// lazily. This way, there's no heap overhead for the validity tables until the
// user actually searches that scope.
//-----------------------------------------------------------------------------
NS_IMETHODIMP nsMsgSearchValidityManager::GetTable (int whichTable, nsIMsgSearchValidityTable **ppOutTable)
{
NS_ENSURE_ARG_POINTER(ppOutTable);
nsresult rv;
*ppOutTable = nullptr;
nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
nsCString customHeaders;
if (NS_SUCCEEDED(rv))
pref->GetCharPref(PREF_CUSTOM_HEADERS, getter_Copies(customHeaders));
switch (whichTable)
{
case nsMsgSearchScope::offlineMail:
if (!m_offlineMailTable)
rv = InitOfflineMailTable ();
if (m_offlineMailTable)
rv = SetOtherHeadersInTable(m_offlineMailTable, customHeaders.get());
*ppOutTable = m_offlineMailTable;
break;
case nsMsgSearchScope::offlineMailFilter:
if (!m_offlineMailFilterTable)
rv = InitOfflineMailFilterTable ();
if (m_offlineMailFilterTable)
rv = SetOtherHeadersInTable(m_offlineMailFilterTable, customHeaders.get());
*ppOutTable = m_offlineMailFilterTable;
break;
case nsMsgSearchScope::onlineMail:
if (!m_onlineMailTable)
rv = InitOnlineMailTable ();
if (m_onlineMailTable)
rv = SetOtherHeadersInTable(m_onlineMailTable, customHeaders.get());
*ppOutTable = m_onlineMailTable;
break;
case nsMsgSearchScope::onlineMailFilter:
if (!m_onlineMailFilterTable)
rv = InitOnlineMailFilterTable ();
if (m_onlineMailFilterTable)
rv = SetOtherHeadersInTable(m_onlineMailFilterTable, customHeaders.get());
*ppOutTable = m_onlineMailFilterTable;
break;
case nsMsgSearchScope::news:
if (!m_newsTable)
rv = InitNewsTable();
if (m_newsTable)
rv = SetOtherHeadersInTable(m_newsTable, customHeaders.get());
*ppOutTable = m_newsTable;
break;
case nsMsgSearchScope::newsFilter:
if (!m_newsFilterTable)
rv = InitNewsFilterTable();
if (m_newsFilterTable)
rv = SetOtherHeadersInTable(m_newsFilterTable, customHeaders.get());
*ppOutTable = m_newsFilterTable;
break;
case nsMsgSearchScope::localNews:
if (!m_localNewsTable)
rv = InitLocalNewsTable();
if (m_localNewsTable)
rv = SetOtherHeadersInTable(m_localNewsTable, customHeaders.get());
*ppOutTable = m_localNewsTable;
break;
case nsMsgSearchScope::localNewsJunk:
if (!m_localNewsJunkTable)
rv = InitLocalNewsJunkTable();
if (m_localNewsJunkTable)
rv = SetOtherHeadersInTable(m_localNewsJunkTable, customHeaders.get());
*ppOutTable = m_localNewsJunkTable;
break;
case nsMsgSearchScope::localNewsBody:
if (!m_localNewsBodyTable)
rv = InitLocalNewsBodyTable();
if (m_localNewsBodyTable)
rv = SetOtherHeadersInTable(m_localNewsBodyTable, customHeaders.get());
*ppOutTable = m_localNewsBodyTable;
break;
case nsMsgSearchScope::localNewsJunkBody:
if (!m_localNewsJunkBodyTable)
rv = InitLocalNewsJunkBodyTable();
if (m_localNewsJunkBodyTable)
rv = SetOtherHeadersInTable(m_localNewsJunkBodyTable, customHeaders.get());
*ppOutTable = m_localNewsJunkBodyTable;
break;
case nsMsgSearchScope::onlineManual:
if (!m_onlineManualFilterTable)
rv = InitOnlineManualFilterTable();
if (m_onlineManualFilterTable)
rv = SetOtherHeadersInTable(m_onlineManualFilterTable, customHeaders.get());
*ppOutTable = m_onlineManualFilterTable;
break;
case nsMsgSearchScope::LDAP:
if (!m_ldapTable)
rv = InitLdapTable ();
*ppOutTable = m_ldapTable;
break;
case nsMsgSearchScope::LDAPAnd:
if (!m_ldapAndTable)
rv = InitLdapAndTable ();
*ppOutTable = m_ldapAndTable;
break;
case nsMsgSearchScope::LocalAB:
if (!m_localABTable)
rv = InitLocalABTable ();
*ppOutTable = m_localABTable;
break;
case nsMsgSearchScope::LocalABAnd:
if (!m_localABAndTable)
rv = InitLocalABAndTable ();
*ppOutTable = m_localABAndTable;
break;
default:
NS_ASSERTION(false, "invalid table type");
rv = NS_MSG_ERROR_INVALID_SEARCH_TERM;
}
NS_IF_ADDREF(*ppOutTable);
return rv;
}
// mapping between ordered attribute values, and property strings
// see search-attributes.properties
static struct
{
nsMsgSearchAttribValue id;
const char* property;
}
nsMsgSearchAttribMap[] =
{
{nsMsgSearchAttrib::Subject, "Subject"},
{nsMsgSearchAttrib::Sender, "From"},
{nsMsgSearchAttrib::Body, "Body"},
{nsMsgSearchAttrib::Date, "Date"},
{nsMsgSearchAttrib::Priority, "Priority"},
{nsMsgSearchAttrib::MsgStatus, "Status"},
{nsMsgSearchAttrib::To, "To"},
{nsMsgSearchAttrib::CC, "Cc"},
{nsMsgSearchAttrib::ToOrCC, "ToOrCc"},
{nsMsgSearchAttrib::AgeInDays, "AgeInDays"},
{nsMsgSearchAttrib::Size, "SizeKB"},
{nsMsgSearchAttrib::Keywords, "Tags"},
{nsMsgSearchAttrib::Name, "AnyName"},
{nsMsgSearchAttrib::DisplayName, "DisplayName"},
{nsMsgSearchAttrib::Nickname, "Nickname"},
{nsMsgSearchAttrib::ScreenName, "ScreenName"},
{nsMsgSearchAttrib::Email, "Email"},
{nsMsgSearchAttrib::AdditionalEmail, "AdditionalEmail"},
{nsMsgSearchAttrib::PhoneNumber, "AnyNumber"},
{nsMsgSearchAttrib::WorkPhone, "WorkPhone"},
{nsMsgSearchAttrib::HomePhone, "HomePhone"},
{nsMsgSearchAttrib::Fax, "Fax"},
{nsMsgSearchAttrib::Pager, "Pager"},
{nsMsgSearchAttrib::Mobile, "Mobile"},
{nsMsgSearchAttrib::City, "City"},
{nsMsgSearchAttrib::Street, "Street"},
{nsMsgSearchAttrib::Title, "Title"},
{nsMsgSearchAttrib::Organization, "Organization"},
{nsMsgSearchAttrib::Department, "Department"},
{nsMsgSearchAttrib::AllAddresses, "FromToCcOrBcc"},
{nsMsgSearchAttrib::JunkScoreOrigin, "JunkScoreOrigin"},
{nsMsgSearchAttrib::JunkPercent, "JunkPercent"},
{nsMsgSearchAttrib::HasAttachmentStatus, "AttachmentStatus"},
{nsMsgSearchAttrib::JunkStatus, "JunkStatus"},
{nsMsgSearchAttrib::Label, "Label"},
{nsMsgSearchAttrib::OtherHeader, "Customize"},
// the last id is -1 to denote end of table
{-1, ""}
};
NS_IMETHODIMP
nsMsgSearchValidityManager::GetAttributeProperty(nsMsgSearchAttribValue aSearchAttribute,
nsAString& aProperty)
{
for (int32_t i = 0; nsMsgSearchAttribMap[i].id >= 0; ++i)
{
if (nsMsgSearchAttribMap[i].id == aSearchAttribute)
{
aProperty.Assign(NS_ConvertUTF8toUTF16(nsMsgSearchAttribMap[i].property));
return NS_OK;
}
}
return NS_ERROR_FAILURE;
}
nsresult
nsMsgSearchValidityManager::NewTable(nsIMsgSearchValidityTable **aTable)
{
NS_ENSURE_ARG_POINTER(aTable);
*aTable = new nsMsgSearchValidityTable;
if (!*aTable)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aTable);
return NS_OK;
}
nsresult
nsMsgSearchValidityManager::SetOtherHeadersInTable (nsIMsgSearchValidityTable *aTable, const char *customHeaders)
{
uint32_t customHeadersLength = strlen(customHeaders);
uint32_t numHeaders=0;
if (customHeadersLength)
{
nsAutoCString hdrStr(customHeaders);
hdrStr.StripWhitespace(); //remove whitespace before parsing
char *newStr = hdrStr.BeginWriting();
char *token = NS_strtok(":", &newStr);
while(token)
{
numHeaders++;
token = NS_strtok(":", &newStr);
}
}
NS_ASSERTION(nsMsgSearchAttrib::OtherHeader + numHeaders < nsMsgSearchAttrib::kNumMsgSearchAttributes, "more headers than the table can hold");
uint32_t maxHdrs = std::min(nsMsgSearchAttrib::OtherHeader + numHeaders + 1,
(uint32_t)nsMsgSearchAttrib::kNumMsgSearchAttributes);
for (uint32_t i=nsMsgSearchAttrib::OtherHeader+1;i< maxHdrs;i++)
{
aTable->SetAvailable (i, nsMsgSearchOp::Contains, 1); // added for arbitrary headers
aTable->SetEnabled (i, nsMsgSearchOp::Contains, 1);
aTable->SetAvailable (i, nsMsgSearchOp::DoesntContain, 1);
aTable->SetEnabled (i, nsMsgSearchOp::DoesntContain, 1);
aTable->SetAvailable (i, nsMsgSearchOp::Is, 1);
aTable->SetEnabled (i, nsMsgSearchOp::Is, 1);
aTable->SetAvailable (i, nsMsgSearchOp::Isnt, 1);
aTable->SetEnabled (i, nsMsgSearchOp::Isnt, 1);
}
//because custom headers can change; so reset the table for those which are no longer used.
for (uint32_t j=maxHdrs; j < nsMsgSearchAttrib::kNumMsgSearchAttributes; j++)
{
for (uint32_t k=0; k < nsMsgSearchOp::kNumMsgSearchOperators; k++)
{
aTable->SetAvailable(j,k,0);
aTable->SetEnabled(j,k,0);
}
}
return NS_OK;
}
nsresult nsMsgSearchValidityManager::EnableDirectoryAttribute(nsIMsgSearchValidityTable *table, nsMsgSearchAttribValue aSearchAttrib)
{
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::Contains, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::Contains, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::DoesntContain, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::DoesntContain, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::Is, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::Is, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::Isnt, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::Isnt, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::BeginsWith, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::BeginsWith, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::EndsWith, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::EndsWith, 1);
table->SetAvailable (aSearchAttrib, nsMsgSearchOp::SoundsLike, 1);
table->SetEnabled (aSearchAttrib, nsMsgSearchOp::SoundsLike, 1);
return NS_OK;
}
nsresult nsMsgSearchValidityManager::InitLdapTable()
{
NS_ASSERTION(!m_ldapTable,"don't call this twice!");
nsresult rv = NewTable(getter_AddRefs(m_ldapTable));
NS_ENSURE_SUCCESS(rv,rv);
rv = SetUpABTable(m_ldapTable, true);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
nsresult nsMsgSearchValidityManager::InitLdapAndTable()
{
NS_ASSERTION(!m_ldapAndTable,"don't call this twice!");
nsresult rv = NewTable(getter_AddRefs(m_ldapAndTable));
NS_ENSURE_SUCCESS(rv,rv);
rv = SetUpABTable(m_ldapAndTable, false);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
nsresult nsMsgSearchValidityManager::InitLocalABTable()
{
NS_ASSERTION(!m_localABTable,"don't call this twice!");
nsresult rv = NewTable(getter_AddRefs(m_localABTable));
NS_ENSURE_SUCCESS(rv,rv);
rv = SetUpABTable(m_localABTable, true);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
nsresult nsMsgSearchValidityManager::InitLocalABAndTable()
{
NS_ASSERTION(!m_localABAndTable,"don't call this twice!");
nsresult rv = NewTable(getter_AddRefs(m_localABAndTable));
NS_ENSURE_SUCCESS(rv,rv);
rv = SetUpABTable(m_localABAndTable, false);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
nsresult
nsMsgSearchValidityManager::SetUpABTable(nsIMsgSearchValidityTable *aTable, bool isOrTable)
{
nsresult rv = aTable->SetDefaultAttrib(isOrTable ? nsMsgSearchAttrib::Name : nsMsgSearchAttrib::DisplayName);
NS_ENSURE_SUCCESS(rv,rv);
if (isOrTable) {
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Name);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::PhoneNumber);
NS_ENSURE_SUCCESS(rv,rv);
}
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::DisplayName);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Email);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::AdditionalEmail);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::ScreenName);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Street);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::City);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Title);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Organization);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Department);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Nickname);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::WorkPhone);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::HomePhone);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Fax);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Pager);
NS_ENSURE_SUCCESS(rv,rv);
rv = EnableDirectoryAttribute(aTable, nsMsgSearchAttrib::Mobile);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
|