Search in sources :

Example 1 with Contact

use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.

the class Archive method prepareAllDocs.

/**
 * prepares all docs for indexing, incl. applying filters, removing dups and
 * sorting
 *
 * @throws Exception
 */
private void prepareAllDocs(Collection<Document> docs, IndexOptions io) throws Exception {
    allDocs = new ArrayList<>();
    allDocs.addAll(docs);
    allDocs = EmailUtils.removeDupsAndSort(allDocs);
    log.info(allDocs.size() + " documents after removing duplicates");
    if (addressBook == null && !io.noRecipients) {
        log.warn("no address book previously set up!");
        // set up without the benefit of ownaddrs
        setupAddressBook(allDocs);
    }
    if (io.filter != null && addressBook != null) {
        // may return null
        Contact ownCI = addressBook.getContactForSelf();
        // if we don't
        // have own info
        io.filter.setOwnContactInfo(ownCI);
    }
    // if no filter, accept doc (default)
    List<Document> newAllDocs = new ArrayList<>();
    for (Document d : allDocs) if (io.filter == null || (io.filter != null && io.filter.matches(d)))
        newAllDocs.add(d);
    EmailUtils.cleanDates(newAllDocs);
    log.info(newAllDocs.size() + " documents after filtering");
    allDocs = newAllDocs;
    // may not be essential
    Collections.sort(allDocs);
    allDocsAsSet = null;
}
Also used : Contact(edu.stanford.muse.AddressBookManager.Contact)

Example 2 with Contact

use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.

the class Filter method matches.

public boolean matches(Document d) {
    if (d instanceof DatedDocument) {
        DatedDocument dd = (DatedDocument) d;
        if (!matchesDate(dd))
            return false;
    }
    if (keywords != null && keywords.size() > 0) {
        log.warn("Filtering by keywords during fetch&index is currently disabled");
        Util.softAssert(false, log);
    // String s = d.getContents().toLowerCase();
    // // check for all keywords, if any absent return false
    // for (String keyword: keywords)
    // if (s.indexOf(keyword) < 0)
    // return false;
    }
    // extra checks for email doc
    if (d instanceof EmailDocument) {
        // check if any of the people involved in this message are one of personContacts
        EmailDocument ed = (EmailDocument) d;
        if (personContacts.size() > 0) {
            // if this is the case, we will explicitly apply the filter again, so its ok.
            if (addressBook != null) {
                List<String> list = ed.getAllAddrs();
                Set<Contact> contactsInThisMessage = new LinkedHashSet<>();
                for (String s : list) {
                    Contact c = addressBook.lookupByEmail(s);
                    if (c != null)
                        contactsInThisMessage.add(c);
                }
                contactsInThisMessage.retainAll(personContacts);
                if (contactsInThisMessage.size() == 0)
                    return false;
            }
        }
        if (sentMessagesOnly) {
            if (ownContact != null) {
                String fromEmail = ed.getFromEmailAddress();
                Set<String> ownAddrs = ownContact.getEmails();
                if (!ownAddrs.contains(fromEmail))
                    return false;
            } else {
                log.warn("WARNING: user error: trying to use sent-only option without setting user's own contact info");
            // in this case, we assume a match implicitly because we don't want to filter out all messages
            }
        }
    }
    return true;
}
Also used : EmailDocument(edu.stanford.muse.index.EmailDocument) DatedDocument(edu.stanford.muse.index.DatedDocument) Contact(edu.stanford.muse.AddressBookManager.Contact)

Example 3 with Contact

use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.

the class SearchResult method filterForCorrespondents.

/**
 * returns only the docs where the name or email address in the given field matches correspondentsStr in the given field(s).
 * correspondentsStr can be or-delimited and specify multiple strings.
 */
public static SearchResult filterForCorrespondents(SearchResult inputSet, String correspondentsStr, boolean checkToField, boolean checkFromField, boolean checkCcField, boolean checkBccField) {
    Set<Contact> searchedContacts = new LinkedHashSet<>();
    AddressBook ab = inputSet.archive.addressBook;
    Set<String> correspondents = Util.splitFieldForOr(correspondentsStr);
    for (String s : correspondents) {
        // this lookup will normalize, be case-insensitive, etc.
        Collection<Contact> contacts = ab.lookupByEmailOrName(s);
        if (contacts != null)
            searchedContacts.addAll(contacts);
    }
    // keep on removing those documents from allDocs which do not have any contact that matches ANY of searchedContacts
    inputSet.matchedDocs = inputSet.matchedDocs.entrySet().stream().filter(k -> {
        EmailDocument ed = (EmailDocument) k.getKey();
        Collection<Contact> contactsOfInterest = new LinkedHashSet<>();
        if (// add from addresses
        checkFromField && ed.from != null)
            contactsOfInterest.addAll(Arrays.stream(ed.from).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add to address
        checkToField && ed.to != null)
            contactsOfInterest.addAll(Arrays.stream(ed.to).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add ccd addresses
        checkCcField && ed.cc != null)
            contactsOfInterest.addAll(Arrays.stream(ed.cc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add bcc address
        checkToField & ed.bcc != null)
            contactsOfInterest.addAll(Arrays.stream(ed.bcc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        // Collection<Contact> contactsInMessage = EmailUtils.getContactsForMessage(ab, ed);
        return contactsOfInterest.stream().anyMatch(searchedContacts::contains);
    }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    return inputSet;
}
Also used : Address(javax.mail.Address) Config(edu.stanford.muse.Config) java.util(java.util) Blob(edu.stanford.muse.datacache.Blob) AnnotationManager(edu.stanford.muse.AnnotationManager.AnnotationManager) JSPHelper(edu.stanford.muse.webapp.JSPHelper) Multimap(com.google.common.collect.Multimap) InternetAddress(javax.mail.internet.InternetAddress) LabelManager(edu.stanford.muse.LabelManager.LabelManager) MappedEntity(edu.stanford.muse.ie.variants.MappedEntity) CSVReader(au.com.bytecode.opencsv.CSVReader) EntityBook(edu.stanford.muse.ie.variants.EntityBook) EmailUtils(edu.stanford.muse.util.EmailUtils) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Span(edu.stanford.muse.util.Span) BlobStore(edu.stanford.muse.datacache.BlobStore) AddressBook(edu.stanford.muse.AddressBookManager.AddressBook) MailingList(edu.stanford.muse.AddressBookManager.MailingList) Util(edu.stanford.muse.util.Util) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Entity(edu.stanford.muse.ie.Entity) Contact(edu.stanford.muse.AddressBookManager.Contact) Pair(edu.stanford.muse.util.Pair) Logger(org.apache.logging.log4j.Logger) FileReader(java.io.FileReader) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AddressBook(edu.stanford.muse.AddressBookManager.AddressBook) Contact(edu.stanford.muse.AddressBookManager.Contact)

Example 4 with Contact

use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.

the class JSONUtils method jsonForNewNewAlgResults.

public static String jsonForNewNewAlgResults(AddressBook ab, String resultsFile) throws IOException, JSONException {
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(resultsFile)));
    JSONObject result = new JSONObject();
    JSONArray groups = new JSONArray();
    int groupNum = 0;
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        line = line.trim();
        // line: group 8, freq 49: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
        // ignore lines without a ':'
        int idx = line.indexOf(":");
        if (idx == -1)
            continue;
        String vars = line.substring(0, idx);
        // vars: freq=5 foo bar somevar=someval
        // we'll pick up all tokens with a = in them and assume they mean key=value
        StringTokenizer varsSt = new StringTokenizer(vars);
        JSONObject group = new JSONObject();
        while (varsSt.hasMoreTokens()) {
            String str = varsSt.nextToken();
            // str: freq=5
            int x = str.indexOf("=");
            if (x >= 0) {
                String key = str.substring(0, x);
                String value = "";
                // we should handle the case of key= (empty string)
                if (x < str.length() - 1)
                    value = str.substring(x + 1);
                group.put(key, value);
            }
        }
        String groupsStr = line.substring(idx + 1);
        // groupsStr: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
        StringTokenizer st = new StringTokenizer(groupsStr);
        JSONArray groupMembers = new JSONArray();
        int i = 0;
        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            Contact ci = ab.lookupByEmail(s);
            if (ci == null) {
                System.out.println("WARNING: no contact info for email address: " + s);
                continue;
            }
            groupMembers.put(i++, ci.toJson());
        }
        group.put("members", groupMembers);
        groups.put(groupNum, group);
        groupNum++;
    }
    result.put("groups", groups);
    return result.toString();
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) Contact(edu.stanford.muse.AddressBookManager.Contact)

Example 5 with Contact

use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.

the class JSONUtils method jsonForAddressBook.

private static JSONObject jsonForAddressBook(AddressBook ab) throws JSONException {
    JSONObject result = new JSONObject();
    JSONArray entries = new JSONArray();
    List<Contact> allContacts = ab.allContacts();
    // ankita wants the first contact to always be me
    // ** CHANGED CODE BY ANKITA **
    int i = 1;
    // Set<String> myAddresses = ab.getOwnAddrsSet();
    Contact me = ab.getContactForSelf();
    Set<String> myAddresses = me.getEmails();
    for (Contact c : allContacts) {
        if (myAddresses.equals(c.getEmails()))
            entries.put(0, c.toJson());
        else
            entries.put(i++, c.toJson());
    }
    // entries.put(0, myAddresses);
    /*
		 * OLD CODE:
		int i = 0;
		Contact me = ab.getOwnContact();
		if (me != null)
		{
			entries.put (0, me.toJson());  // ORIGINAL:	entries.put(0, me);
			i++;
		}

		for (Contact c: allContacts)
		{
			if (me == c)
				continue;
			entries.put(i++, c.toJson());
		}
		*/
    result.put("entries", entries);
    return result;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Contact(edu.stanford.muse.AddressBookManager.Contact)

Aggregations

Contact (edu.stanford.muse.AddressBookManager.Contact)27 AddressBook (edu.stanford.muse.AddressBookManager.AddressBook)10 InternetAddress (javax.mail.internet.InternetAddress)7 Address (javax.mail.Address)6 JSONObject (org.json.JSONObject)5 Blob (edu.stanford.muse.datacache.Blob)4 Pair (edu.stanford.muse.util.Pair)4 Util (edu.stanford.muse.util.Util)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 JSONArray (org.json.JSONArray)4 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)3 Multimap (com.google.common.collect.Multimap)3 EmailUtils (edu.stanford.muse.util.EmailUtils)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 MailingList (edu.stanford.muse.AddressBookManager.MailingList)2 AnnotationManager (edu.stanford.muse.AnnotationManager.AnnotationManager)2 Config (edu.stanford.muse.Config)2 BlobStore (edu.stanford.muse.datacache.BlobStore)2