Search in sources :

Example 1 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class GeneralHelper method saveContact.

/**
 * Updates a contact in the address book if the Destination <code>destinationString</code> exists,
 * or adds a new contact to the address book.
 * @param destinationString A base64-encoded Email Destination key
 * @param name
 * @param pictureBase64
 * @param text
 * @return null if sucessful, or an error message if an error occured
 * @throws PasswordException
 * @throws GeneralSecurityException
 */
public static String saveContact(String destinationString, String name, String pictureBase64, String text) throws PasswordException, GeneralSecurityException {
    destinationString = Util.fixAddress(destinationString);
    AddressBook addressBook = getInstance().getAddressBook();
    Contact contact = addressBook.get(destinationString);
    if (contact != null) {
        contact.setName(name);
        contact.setPictureBase64(pictureBase64);
        contact.setText(text);
    } else {
        EmailDestination destination;
        try {
            destination = new EmailDestination(destinationString);
        } catch (GeneralSecurityException e) {
            Log log = new Log(GeneralHelper.class);
            log.error("Can't save contact to address book.", e);
            return e.getLocalizedMessage();
        }
        contact = new Contact(name, destination, pictureBase64, text);
        addressBook.add(contact);
    }
    try {
        addressBook.save();
        return null;
    } catch (IOException e) {
        return e.getLocalizedMessage();
    }
}
Also used : AddressBook(i2p.bote.addressbook.AddressBook) Log(net.i2p.util.Log) EmailDestination(i2p.bote.email.EmailDestination) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Contact(i2p.bote.packet.dht.Contact)

Example 2 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class AddressBookTest method setUp.

@Before
public void setUp() throws Exception {
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    testDir = new File(tmpDir, "AddressBookTest-" + System.currentTimeMillis());
    addressBookFile = new File(testDir, "addressBook");
    assertTrue("Can't create directory: " + testDir.getAbsolutePath(), testDir.mkdir());
    passwordHolder = TestUtil.createPasswordCache(testDir);
    addressBook = new AddressBook(addressBookFile, passwordHolder);
    for (TestUtil.TestIdentity identity : TestUtil.createTestIdentities()) addressBook.add(new Contact(identity.identity.getPublicName(), identity.identity));
}
Also used : TestUtil(i2p.bote.TestUtil) File(java.io.File) Contact(i2p.bote.packet.dht.Contact) Before(org.junit.Before)

Example 3 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class AddressBookTest method testExportImport.

@Test
public void testExportImport() throws Exception {
    File exportFile = new File(testDir, "ExportImportTest-" + System.currentTimeMillis() + ".txt");
    addressBook.export(exportFile, null);
    File tmpAddressBookFile = new File(testDir, "ExportImportAB-" + System.currentTimeMillis());
    AddressBook tmpAddressBook = new AddressBook(tmpAddressBookFile, passwordHolder);
    FileInputStream fis = new FileInputStream(exportFile);
    try {
        tmpAddressBook.importFromFileDescriptor(fis.getFD(), null, false, false);
        SortedSet<Contact> start = addressBook.getAll();
        SortedSet<Contact> end = tmpAddressBook.getAll();
        for (Contact contact : start) {
            assertThat(contact, isIn(end));
        }
        for (Contact contact : end) {
            assertThat(contact, isIn(start));
        }
    } finally {
        try {
            fis.close();
        } catch (IOException e) {
        }
        assertTrue("Can't delete file: " + exportFile.getAbsolutePath(), exportFile.delete());
        assertTrue("Can't delete file: " + tmpAddressBookFile.getAbsolutePath(), tmpAddressBookFile.delete());
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Contact(i2p.bote.packet.dht.Contact) Test(org.junit.Test)

Example 4 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class NewEmailFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mSpinner = (Spinner) view.findViewById(R.id.sender_spinner);
    mMore = (ImageView) view.findViewById(R.id.more);
    mTo = (ContactsCompletionView) view.findViewById(R.id.to);
    mCc = (ContactsCompletionView) view.findViewById(R.id.cc);
    mBcc = (ContactsCompletionView) view.findViewById(R.id.bcc);
    mSubject = (EditText) view.findViewById(R.id.subject);
    mContent = (EditText) view.findViewById(R.id.message);
    mAttachments = (LinearLayout) view.findViewById(R.id.attachments);
    String quoteMsgFolder = getArguments().getString(QUOTE_MSG_FOLDER);
    String quoteMsgId = getArguments().getString(QUOTE_MSG_ID);
    QuoteMsgType quoteMsgType = (QuoteMsgType) getArguments().getSerializable(QUOTE_MSG_TYPE);
    boolean hide = I2PBote.getInstance().getConfiguration().getHideLocale();
    List<Person> toRecipients = new ArrayList<Person>();
    List<Person> ccRecipients = new ArrayList<Person>();
    String origSubject = null;
    String origContent = null;
    String origFrom = null;
    try {
        Email origEmail = BoteHelper.getEmail(quoteMsgFolder, quoteMsgId);
        if (origEmail != null) {
            mSenderKey = BoteHelper.extractEmailDestination(BoteHelper.getOneLocalRecipient(origEmail).toString());
            if (quoteMsgType == QuoteMsgType.REPLY) {
                String recipient = BoteHelper.getNameAndDestination(origEmail.getReplyAddress(I2PBote.getInstance().getIdentities()));
                toRecipients.add(extractPerson(recipient));
            } else if (quoteMsgType == QuoteMsgType.REPLY_ALL) {
                // What happens if an email is received by multiple local identities?
                for (Address address : origEmail.getAllAddresses(true)) {
                    Person person = extractPerson(address.toString());
                    if (person != null)
                        toRecipients.add(person);
                }
            }
            origSubject = origEmail.getSubject();
            origContent = origEmail.getText();
            origFrom = BoteHelper.getShortSenderName(origEmail.getOneFromAddress(), 50);
        }
    } catch (PasswordException e) {
        // Should not happen, we cannot get to this page without authenticating
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Set up identities spinner
    IdentityAdapter identities = new IdentityAdapter(getActivity());
    mSpinner.setAdapter(identities);
    mSpinner.setSelection(mDefaultPos);
    // Set up Cc/Bcc button
    mMore.setImageDrawable(new IconicsDrawable(getActivity(), GoogleMaterial.Icon.gmd_unfold_more).colorRes(R.color.md_grey_600).sizeDp(24).paddingDp(3));
    mMore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            mCc.setVisibility(mMoreVisible ? View.GONE : View.VISIBLE);
            mBcc.setVisibility(mMoreVisible ? View.GONE : View.VISIBLE);
            mMore.setImageDrawable(new IconicsDrawable(getActivity(), mMoreVisible ? GoogleMaterial.Icon.gmd_unfold_more : GoogleMaterial.Icon.gmd_unfold_less).colorRes(R.color.md_grey_600).sizeDp(24).paddingDp(mMoreVisible ? 3 : 4));
            mMoreVisible = !mMoreVisible;
        }
    });
    // Set up contacts auto-complete
    List<Person> contacts = new ArrayList<Person>();
    try {
        for (Contact contact : I2PBote.getInstance().getAddressBook().getAll()) {
            contacts.add(new Person(contact.getName(), contact.getBase64Dest(), BoteHelper.decodePicture(contact.getPictureBase64())));
        }
    } catch (PasswordException e) {
        // TODO handle
        e.printStackTrace();
    }
    mAdapter = new FilteredArrayAdapter<Person>(getActivity(), android.R.layout.simple_list_item_1, contacts) {

        @Override
        protected boolean keepObject(Person obj, String mask) {
            mask = mask.toLowerCase(Locale.US);
            return obj.getName().toLowerCase(Locale.US).startsWith(mask) || obj.getAddress().toLowerCase(Locale.US).startsWith(mask);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null)
                v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listitem_contact, parent, false);
            else
                v = convertView;
            setViewContent(v, position);
            return v;
        }

        private void setViewContent(View v, int position) {
            Person person = getItem(position);
            ((TextView) v.findViewById(R.id.contact_name)).setText(person.getName());
            ImageView picView = (ImageView) v.findViewById(R.id.contact_picture);
            Bitmap picture = person.getPicture();
            if (picture == null) {
                ViewGroup.LayoutParams lp = picView.getLayoutParams();
                picture = BoteHelper.getIdenticonForAddress(person.getAddress(), lp.width, lp.height);
            }
            picView.setImageBitmap(picture);
        }
    };
    mTo.setAdapter(mAdapter);
    mCc.setAdapter(mAdapter);
    mBcc.setAdapter(mAdapter);
    for (Person recipient : toRecipients) {
        mTo.addObject(recipient);
    }
    for (Person recipient : ccRecipients) {
        mCc.addObject(recipient);
    }
    if (origSubject != null) {
        String subjectPrefix;
        if (quoteMsgType == QuoteMsgType.FORWARD) {
            subjectPrefix = getResources().getString(hide ? R.string.subject_prefix_fwd_hide : R.string.subject_prefix_fwd);
        } else {
            subjectPrefix = getResources().getString(hide ? R.string.response_prefix_re_hide : R.string.response_prefix_re);
        }
        if (!origSubject.startsWith(subjectPrefix))
            origSubject = subjectPrefix + " " + origSubject;
        mSubject.setText(origSubject);
    }
    if (origContent != null) {
        StringBuilder quotation = new StringBuilder();
        quotation.append("\n\n");
        quotation.append(getResources().getString(hide ? R.string.response_quote_wrote_hide : R.string.response_quote_wrote, origFrom));
        String[] lines = origContent.split("\r?\n|\r");
        for (String line : lines) quotation = quotation.append("\n> ").append(line);
        mContent.setText(quotation);
    }
    if (savedInstanceState == null) {
        mTo.setPrefix(getResources().getString(R.string.email_to) + " ");
        mCc.setPrefix(getResources().getString(R.string.email_cc) + " ");
        mBcc.setPrefix(getResources().getString(R.string.email_bcc) + " ");
    }
    TextWatcher dirtyWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mDirty = true;
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    mSubject.addTextChangedListener(dirtyWatcher);
    mContent.addTextChangedListener(dirtyWatcher);
}
Also used : Email(i2p.bote.email.Email) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) ImageView(android.widget.ImageView) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) MessagingException(javax.mail.MessagingException) ViewGroup(android.view.ViewGroup) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContactsCompletionView(i2p.bote.android.widget.ContactsCompletionView) SuppressLint(android.annotation.SuppressLint) Contact(i2p.bote.packet.dht.Contact) Person(i2p.bote.android.util.Person)

Example 5 with Contact

use of i2p.bote.packet.dht.Contact in project i2p.i2p-bote by i2p.

the class I2PBote method publishDestination.

/**
 * Publishes an email destination in the address directory.
 */
public void publishDestination(String destination, byte[] picture, String text) throws PasswordException, IOException, GeneralSecurityException, DhtException, InterruptedException {
    EmailIdentity identity = identities.get(destination);
    if (identity != null) {
        identity.setPicture(picture);
        identity.setText(text);
        if (identity.getFingerprint() == null)
            // if no fingerprint exists, generate one and save it in the next step
            identity.generateFingerprint();
        identities.save();
        Contact entry = new Contact(identity, identities, picture, text, identity.getFingerprint());
        dht.store(entry);
    }
}
Also used : EmailIdentity(i2p.bote.email.EmailIdentity) Contact(i2p.bote.packet.dht.Contact)

Aggregations

Contact (i2p.bote.packet.dht.Contact)18 GeneralSecurityException (java.security.GeneralSecurityException)7 EmailDestination (i2p.bote.email.EmailDestination)4 PasswordException (i2p.bote.fileencryption.PasswordException)4 IOException (java.io.IOException)4 File (java.io.File)3 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 AddressBook (i2p.bote.addressbook.AddressBook)2 Person (i2p.bote.android.util.Person)2 SortedProperties (i2p.bote.util.SortedProperties)2 FileInputStream (java.io.FileInputStream)2 Before (org.junit.Before)2 SuppressLint (android.annotation.SuppressLint)1 Bitmap (android.graphics.Bitmap)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1