Click here to Skip to main content
15,886,422 members
Articles / Programming Languages / C#
Tip/Trick

Facebook: Who’s Thinking of You?

,
Rate me:
Please Sign up or sign in to vote.
4.18/5 (6 votes)
19 Oct 2014CPOL2 min read 10.5K   9   2
Experimentation on Facebook chat that aim to determine who's probably thinking about us

Introduction

Some days ago, I was involved in a discussion about ex (ex boyfriend or girlfriend). Well throughout the discussion (it was going fast and colourful seeing what the argument was) between a resentment, a nostalgia and an excessive optimism, a girl says this thing: "My escape ends tomorrow, tomorrow I’ll return to my silence, to my messages first written and then erased in my absence, etc."

Apart from words that could be interpreted more or less sweet, in bold type I’ve highlighted something: a girl was suggesting me a little utility that everyone can develop to try to understand if someone is thinking about us (on Facebook) or not. That girl was talking of a XMPP "composing..." packet that moves on Facebook millions of times!

How to Define Who’s Thinking about You?

Well, Facebook (and the chat in general) allows us to think too much: I could write an entire romance to a beautiful girl, repent and erase it. But I thought about that girl and maybe I was writing the message on the chat.

We all know that while someone writes, Facebook notices that someone is writing. Perhaps an “I’m thinking about you” can be defined as an “I’m writing to you” that stands. If for every "composing..." packet received, I insert an item in a hashtable and let it there since the message arrives on the chat, I can suppose that it was a thought for me! The idea is simple: for every "composing..." received I add an item(Jid, #composing) in a hashtable: if I receive a message from that Jid, I erase the item from the hashtable because the thought was transformed in an action.

Background

Transforming this night romance in a code is pretty simple: another way we use agsXMPP to comunicate with the Facebook chat and C# to write the logic of the application. The code is extremely simple and it explains itself.

PAY ATTENTION: Before you read the code, remember that your Facebook username isn’t the email you use to register BUT THE NAME THAT COMES AFTER WWW.FACEBOOK.COM/… after login.

The Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.vcard;
namespace WindowsFormsApplication1
{
 class Program
 {
      static private bool isConn = false;
      static XmppClientConnection xmpp = new
      XmppClientConnection("chat.facebook.com");

       // map jid => # of think ;)
       static Dictionary hmt = new Dictionary();

       // map jid => user
       static Dictionary users = new Dictionary();
       public static void start()
       {
             if (isConn)
                   return;
             xmpp.Username = "YOUR_USERNAME";
             xmpp.Password = "YOUR_PASSWORD";
             xmpp.OnLogin += new ObjectHandler(OnLogin);
             xmpp.OnError += new ErrorHandler(xmpp_OnError);
             xmpp.OnRosterItem += new
             XmppClientConnection.RosterHandler(XmppCon_OnRosterItem);
             xmpp.OnAuthError += new XmppElementHandler(xmpp_OnAuthError);
             xmpp.OnMessage += new MessageHandler(xmpp_OnMessage);
             xmpp.AutoResolveConnectServer = false;
             xmpp.Open();
       }
       static private void VcardResult(object sender, IQ iq, object data)
       {
             if (iq.Type == IqType.result)
             {
                   Vcard vcard = iq.Vcard;
                   if (vcard != null)
                   {
                         string fullname = vcard.Fullname;
                         string nickname = vcard.Nickname;
                         string description = vcard.Description;
                         Photo photo = vcard.Photo;
                         if (!users.ContainsKey(iq.GetAttribute("from").ToString()))
                         {
                               users.Add(iq.GetAttribute("from").ToString(), vcard);
                         Console.WriteLine("Ricevuta informazione : " +
vcard.Fullname);
                         }
                   }
            }
       }
        static private void XmppCon_OnRosterItem(object sender,agsXMPP.protocol.iq.roster.RosterItem item)
       {
             if (item.Subscription != SubscriptionType.remove)
             {
                   VcardIq viq = new VcardIq(IqType.get,item.Jid);
                   xmpp.IqGrabber.SendIq(viq, new IqCB(VcardResult), null);
             }
             else
                   users.Remove(item.Jid.ToString());
       }
      
       static void xmpp_OnMessage(object sender,agsXMPP.protocol.client.Message msg)
       {
             if (!isConn)
                   return;
             if (msg.Chatstate.ToString() == "composing")
             {
                   // if is the first time that user think you...
                         if (!hmt.ContainsKey(msg.From.Bare.ToString()))
                         {
                               hmt.Add(msg.From.Bare.ToString(),0);
                         }
                         hmt[msg.From.Bare.ToString()]+=1;


             return;
       }

             // But if user write you a message then the 'think' fail :(
             String a = msg.FirstChild.InnerXml;
             if (a == "")
                   return;
             // from
             if (hmt.ContainsKey(msg.From.Bare.ToString()))
             {
                   hmt.Remove(msg.From.Bare.ToString());
             }
             Jid to = msg.From;
             string s;

             agsXMPP.protocol.client.Message nmsg = new
             agsXMPP.protocol.client.Message();
             nmsg.Type = agsXMPP.protocol.client.MessageType.chat;
             nmsg.To = to;
             nmsg.Body = s;
             xmpp.Send(nmsg);
       }
             static void xmpp_OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
             {
                   isConn = false;

                   MessageBox.Show("Username / password not valid.\nRiprova!
                  ", "Cilentiamoci.it");
             }

             static void xmpp_OnError(object sender, Exception ex)
             {
                   isConn = false;
                   MessageBox.Show("Error:" +ex.Message, "test.it");
             }

             static private void OnLogin(object sender)
             {
                   Presence p = new Presence(ShowType.chat, "Online");
                   p.Type = PresenceType.available;
                   xmpp.Send(p);
                   isConn = true;
                   Console.WriteLine("Connected...\n\nauditing...");
                   xmpp.RequestRoster();
             }

 static private void stop()
 {
 if (!isConn)
 return;
 xmpp.Close();
 isConn = false;
 }
 [STAThread]
 static void Main()
 {
 /**Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new Form1());**/

 start();
 DateTime d1 = DateTime.Now;
 while (true)
 {
 TimeSpan x = DateTime.Now - d1;

 if (x.Seconds>5)
 {
 Console.WriteLine("\n\nwho think you?...\n");
 d1 = DateTime.Now;
 if (hmt.Count == 0)
 {
 Console.WriteLine("\nI'm sorry... no one think you :D");
 }
 foreach (KeyValuePair entry in hmt)
 {
 string uname = entry.Key;
 if (users.ContainsKey(uname))
 uname = users[uname].Fullname;
 Console.WriteLine("\t" + uname + "\t" + entry.Value);
 }
 }
 }
 }
 }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer BiTS - Softmining
Italy Italy
Luigi Di Biasi is a PhD student in a computer science @ University of Salerno.

He is sole director of Arrowsoft.IT SRLs and CIO of Softmining SRL. He is founder of dibiasi.it materiale elettrico.

He studied Computer Science at the University of Salerno in 2013. From 2014 to 2016 he is [assegnista di ricerca] for PRIN 2010-2011 Data-Centric Genomic Computing (GenData 2020)).

He collaborate to the GRIMD developing (grid for molecular dynamics), which permits the parallel and distributed computation of many kinds of scientific software, to the YADA developing (Yet Another Docking Approach), a tool that improve and refine the predictions made by VINA Autodock and to the implementation of ProtComp alignment free tools, a new algorithm for sequences analysis that outperforms existing methods. He collaborate on 7 scientific paper.

In 2017 he is responsible developer for the RIRIBOX Project (in collaboration with Acea Pinerolese and Ecofficina SRL) and for energii.dk (in collaboration with Peme IVS). All these projects has involved collaborations between Italian, Danish and American developers.

In 2016 he is responsible developer for the “Una Buona Occasione” Project (in collaboration with Ecofficina SRL). He developed all the edutainment games related to this project (16 WebGL games).

Actually, his research activities range on the AI (algorithms developing, applications) and on the game developing.

Written By
Student
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood thought but.. Pin
AlphaDeltaTheta19-Oct-14 15:37
AlphaDeltaTheta19-Oct-14 15:37 
And Nice work... Big Grin | :-D

With Facebook APIv1 going out in April 2015, XMPP interface to chat would be gone Sigh | :sigh:
Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas.
Carl von Clausewitz

Source

GeneralMy vote of 4 Pin
Afzaal Ahmad Zeeshan19-Oct-14 8:43
professionalAfzaal Ahmad Zeeshan19-Oct-14 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.