ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Collector.cpp
(Generate patch)

Comparing trunk/Collector.cpp (file contents):
Revision 16 by douglas, 2004-07-14T20:57:30-07:00 vs.
Revision 34 by douglas, 2004-07-20T16:49:06-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 + #include "AwayMessage.hpp"
8   #include "Collector.hpp"
9 + #include "Stamp.hpp"
10  
11   #include <cstring>
12 +
13 + extern "C"
14 + {
15   #include <pwd.h>
16   #include <unistd.h>
17 + }
18  
19 < Collector::Collector(const ext::String& login, ext::String& password, const
20 <        std::set<Buddy>& buddies, bool start) : login(login), password(password),
21 <        start(start)
19 > Collector::Collector(const ext::String& login, ext::String& password, const     std::set<Buddy>& buddies,
20 >        const Database& database, bool start) : login(login), password(password), buddies(buddies), database(database),
21 >        start(start), prompted(false)
22   {
23          if (start)
24          {
25                  if (password.IsEmpty()) prompt();
26  
27 <                collector.Spawn(etl::BindAll(&Collector::collect, this, buddies));
27 >                collector.Spawn(etl::Bind<0>(&Collector::collect, this));
28          }
29   }
30  
31 < net::Oscar::BuddyTool* Collector::buddy(NULL);
26 < net::Oscar::IcbmTool* Collector::icbm(NULL);
27 <
28 < int Collector::collect(const std::set<Buddy>& buddies)
31 > int Collector::collect()
32   {
33 <        cerr << "Collector::collect()\n";
33 >        cerr << bright << green << "Collector::collect()\n" << reset;
34  
35          net::Oscar::Session session;
36          net::Oscar::AuthTool auth(session);
# Line 38 | Line 41 | int Collector::collect(const std::set<Bu
41  
42          auth.Login(login, password);
43  
44 <        Collector::buddy = &buddy;
42 <        Collector::icbm = &icbm;
44 >        if (prompted) password.Clear();
45  
46 <        icbm.OnIncomingIcbm += &Collector::reply;
46 >        this->buddy = &buddy;
47 >        this->icbm = &icbm;
48 >        this->info = &info;
49  
50 < //      for (size_t index(password.GetSize()); index > 0; --index) password[index -
51 < //              1] = '\0';
50 >        buddy.OnOnline += etl::Bind<0>(&Collector::status, this);
51 >        icbm.OnReceive += etl::Bind<0>(&Collector::receive, this);
52  
53 <        net::Oscar::StringSet buddies_;
53 >        net::Oscar::StringSet buddies;
54  
55 <        for (std::set<Buddy>::const_iterator buddy(buddies.begin()); buddy !=
56 <                buddies.end(); ++buddy) buddies_.Insert(*buddy);
55 >        for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy != this->buddies.end(); ++buddy)
56 >                buddies.Insert(*buddy);
57  
58 <        buddy.Insert(buddies_);
58 >        buddy.Insert(buddies);
59          icbm.RequestParams();
60  
61          net::Oscar::Capabilities capabilities;
62  
63          capabilities.Insert(net::Oscar::ChatCapability);
64 <        info.SetProfile("<font size=3>I am <a href=\"http://computers.douglasthrift.net/zoe.xml\">Zoe</a>.</font> :-*", "", capabilities);
64 >        info.SetProfile("<font face=\"Tahoma\" size=2>I am <a href=\"http://computers.douglasthrift.net/zoe.xml\">Zoe</a>."
65 >                "</font> :-*", "<font face=\"Tahoma\" size=3>Mommy told me not to talk to strangers.</font> O:-)", capabilities);
66  
67          // figure out whether or not we logged in
68  
69 <        while (true) sleep(60);
69 >        while (true) sleep(Minute(1));
70   }
71  
72   void Collector::prompt()
73   {
74 +        if (!prompted) prompted = true;
75 +
76          ext::String prompt(login + "'s AIM Password: ");
77          char* password(getpass(prompt.NullTerminate()));
78  
79          this->password = password;
80  
81 <        for (size_t index(strlen(password)); index > 0; --index) password[index -
75 <                1] = '\0';
81 >        for (size_t index(std::strlen(password)); index > 0; --index) password[index - 1] = '\0';
82   }
83  
84 < void Collector::reply(const ext::String& login, const ext::String& reply)
84 > void Collector::status(const net::Oscar::UserInfo& user)
85   {
86 <        icbm->Simple(login, reply);
86 >        Buddy buddy(*buddies.find(user));
87 >        AwayMessage message(buddy, *info);
88 >
89 >        if (!ext::String(message).IsEmpty()) // XXX
90 >        {
91 >                ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host, database.user, database.password,
92 >                        database.db));
93 >                ext::Handle<dbi::ResultSet> previous(db->Execute("SELECT message FROM messages WHERE stamp=(SELECT MAX(stamp) "
94 >                        "FROM messages WHERE id='" + lexical_cast<ext::String>(buddy.getId()) + "') AND id='"
95 >                        + lexical_cast<ext::String>(buddy.getId()) + "'"));
96 >
97 >                if (previous->MoveNext()) if (ext::String(message) == previous->GetString("message")) return;
98 >
99 >                cerr << bright << red << "Collector::status(" << blue << buddy << red << ", " << reset << message << bright << red
100 >                        << ")\n" << reset;
101 >
102 >                db->Execute("INSERT INTO messages (stamp, message, id) VALUES ('" + ext::String(message.getStamp()) + "', '"
103 >                        + db->Escape(message) + "', '" + lexical_cast<ext::String>(buddy.getId()) + "')");
104 >        }
105 > }
106 >
107 > void Collector::receive(const ext::String& buddy, const ext::String& message)
108 > {
109 >        cerr << bright << red << "Collector::receive(" << blue << Buddy(buddy) << red << ", " << reset << message << bright
110 >                << red << ")\n" << reset;
111 >
112 >        if (buddies.find(buddy) != buddies.end())
113 >        {
114 >                Stamp stamp;
115 >                
116 >                sleep(1);
117 >
118 >                icbm->Simple(buddy, stamp);
119 >        }
120 >        else
121 >        {
122 >                sleep(1);
123 >
124 >                icbm->Simple(buddy, "<font face=\"Tahoma\" size=2>Mommy told me not to talk to strangers. O:-)");
125 >        }
126   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines