Java Code
Main
//WebSquared
//Written by Allison Lacker
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Main {
static HashMap contacts = new HashMap();
static String uname="";
public static void main(String[] args) {
try {
File f = new File("contacts.txt");
FileWriter fw = new FileWriter(f);
File f2 = new File("neighbors.txt");
FileWriter fw2 = new FileWriter(f2);
Store store=null;
Folder folder=null;
Message[] m= null;
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
uname = "exampleemail@gmail.com";
try{
store = session.getStore("imaps");
store.connect("imap.gmail.com", uname, "examplepassword");
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
m = folder.getMessages();
processInbox(m);
folder = store.getFolder("[Gmail]/Sent Mail");
folder.open(Folder.READ_ONLY);
m = folder.getMessages();
processSent(m);
}
catch(Exception e){}
Iterator iterator = contacts.keySet().iterator();
while( iterator.hasNext() ){
Contact person;
person = (Contact)contacts.get(iterator.next());
fw.write(person.uniqueID + "," + person.emailAddress + ","+ person.numEmails + ",");
//say which tier they are in
if(person.them_solo && person.you_solo)
fw.write("1\r\n");
else if((person.them_solo && person.you_group) || (person.you_solo && person.them_group))
fw.write("2\r\n");
else if(person.them_solo || person.you_solo || person.them_group || person.you_group)
fw.write("3\r\n");
else
fw.write("4\r\n");
}
iterator = contacts.keySet().iterator();
while( iterator.hasNext() ){
Contact person;
person = (Contact)contacts.get(iterator.next());
HashMap pneighbors = person.neighbors;
Iterator it = pneighbors.keySet().iterator();
fw2.write(person.uniqueID + ":");
while( it.hasNext() ){
Neighbor n = (Neighbor)person.neighbors.get(it.next());
fw2.write(n.contactID + "," + n.strength_ties + ";");
}
fw2.write("\r\n");
}
fw.close();
fw2.close();
}
catch (IOException e) {
System.out.println(e);
}
}
//process the emails that have been sent to you
public static void processInbox(Message[] m){
Contact person;
String recipient_name = "";
String fromname = "";
for (int msgNum = 0; msgNum < m.length; msgNum++){
//add the from address to contacts
try{
fromname = ((InternetAddress)m[msgNum].getFrom()[0]).getAddress();
Address[] recipients = (m[msgNum].getAllRecipients());
//add person to the list if it isn't already there
if(!contacts.containsKey(fromname)){
person = new Contact(fromname);
person.setID();
contacts.put(fromname, person);
}
//if you are the sole recipient
if(recipients != null && ((InternetAddress)recipients[0]).getAddress().equalsIgnoreCase(uname)){
person = (Contact)contacts.get(fromname);
person.incrimentEmailCount();
person.setThemSoloYou();
}
//if they email you and others
else if(recipients != null){
//set the sender
person=(Contact)contacts.get(fromname);
person.setThemGroupYou();
Integer[] recipientsAsNeighbors = new Integer[recipients.length];
//add recipients to contacts
for(int counter = 0; counter< recipients.length; counter++){
recipient_name = ((InternetAddress)recipients[counter]).getAddress();
if(!contacts.containsKey(recipient_name)){
person = new Contact(recipient_name);
person.setID();
contacts.put(recipient_name, person);
}
else{
person = (Contact)contacts.get(recipient_name);
person.incrimentEmailCount();
}
person = (Contact)contacts.get(recipient_name);
recipientsAsNeighbors[counter] = person.uniqueID;
}
//add recipientsAsNeighbors collection to each recipient
for(int counter = 0; counter< recipients.length; counter++){
recipient_name = ((InternetAddress)recipients[counter]).getAddress();
person = (Contact)contacts.get(recipient_name);
person.addNeighbors(recipientsAsNeighbors);
}
}
}
catch(Exception e){System.out.println(e);}
}
}
//process the emails that you have sent to other people
public static void processSent(Message[] m){
Contact person;
String recipient_name = "";
for (int msgNum = 0; msgNum < m.length; msgNum++){
//add the to address to contacts
try{
Address[] recipients = (m[msgNum].getAllRecipients());
if(recipients != null && recipients.length>0){
//if you send it to a group
if (recipients.length>1){
Integer[] recipientsAsNeighbors = new Integer[recipients.length];
for(int counter = 0; counter< recipients.length; counter++){
recipient_name = ((InternetAddress)recipients[counter]).getAddress();
if(!contacts.containsKey(recipient_name)){
person = new Contact(recipient_name);
person.setID();
contacts.put(recipient_name, person);
}
person = (Contact)contacts.get(recipient_name);
recipientsAsNeighbors[counter] = person.uniqueID;
person.setYouGroupThem();
person.incrimentEmailCount();
}
//add recipientsAsNeighbors to each recipient
for(int counter = 0; counter< recipients.length; counter++){
recipient_name = ((InternetAddress)recipients[counter]).getAddress();
person = (Contact)contacts.get(recipient_name);
person.addNeighbors(recipientsAsNeighbors);
}
}
//if you sent it to one person
else{
recipient_name = ((InternetAddress)recipients[0]).getAddress();
//if the person is not in contacts, add them
if(!contacts.containsKey(recipient_name)){
person = new Contact(recipient_name);
person.setID();
contacts.put(recipient_name, person);
}
person = (Contact)contacts.get(recipient_name);
person.setYouSoloThem();
person.incrimentEmailCount();
}
}
}
catch(Exception e){System.out.println(e);}
}
}
}
Contact Class
import java.util.*;
public class Contact {
boolean them_solo; //true when they have emailed you solo
boolean you_solo; //true when you have emailed them solo
boolean them_group; //true when they have emailed you and a group
boolean you_group; //true when you have emailed them and a group
final String emailAddress;
int numEmails;
int uniqueID; //integer id to refer to the contacts
static int numContacts=0; //the number of contacts so far
HashMap neighbors;
public Contact(String contactEmail){
them_solo = false;
you_solo = false;
them_group = false;
you_group = false;
numEmails = 1;
emailAddress = contactEmail;
neighbors = new HashMap();
}
public void setID(){
uniqueID = ++numContacts;
}
public void setThemSoloYou(){
them_solo=true;
}
public void setYouSoloThem(){
you_solo=true;
}
public void setThemGroupYou(){
them_group = true;
}
public void setYouGroupThem(){
you_group = true;
}
public void incrimentEmailCount(){
numEmails++;
}
public void addNeighbors(Integer[] potentialNeighbors){
for(int counter = 0; counter