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 19 by douglas, 2004-07-15T09:25:00-07:00 vs.
Revision 46 by douglas, 2004-08-16T18:52:59-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),
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 < int Collector::collect(const std::set<Buddy>& buddies)
31 > int Collector::collect()
32   {
33 <        cerr << "Collector::collect()\n";
33 >        cerr << bright << green << "Started collector daemon at " << Stamp()
34 >                << ".\n" << reset;
35  
36          net::Oscar::Session session;
37          net::Oscar::AuthTool auth(session);
# Line 39 | Line 46 | int Collector::collect(const std::set<Bu
46  
47          this->buddy = &buddy;
48          this->icbm = &icbm;
49 +        this->info = &info;
50  
51 < //      buddy.OnOnline += etl::Bind<0>(&Collector::status, this);
52 <        icbm.OnReceive += etl::Bind<0>(&Collector::reply, this);
51 >        buddy.OnOnline += etl::Bind<0>(&Collector::status, this);
52 >        icbm.OnReceive += etl::Bind<0>(&Collector::receive, this);
53  
54 <        net::Oscar::StringSet buddies_;
54 >        net::Oscar::StringSet buddies;
55  
56 <        for (std::set<Buddy>::const_iterator buddy(buddies.begin()); buddy !=
57 <                buddies.end(); ++buddy) buddies_.Insert(*buddy);
56 >        for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
57 >                buddy != this->buddies.end(); ++buddy) buddies.Insert(*buddy);
58  
59 <        buddy.Insert(buddies_);
52 <        icbm.RequestParams();
59 >        buddy.Insert(buddies);
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=\""
65 >                + Zoe::generator(Zoe::url) + "\">Zoe</a>.</font> :-*", "<font face=\""
66 >                "Tahoma\" size=2>Mommy told me not to talk to strangers.</font> O:-)",
67 >                capabilities);
68  
69 <        // figure out whether or not we logged in
69 >        // XXX figure out whether or not we logged in
70  
71 <        while (true) sleep(60);
71 >        while (true) sleep(Hour(1));
72   }
73  
74   void Collector::prompt()
# Line 70 | Line 80 | void Collector::prompt()
80  
81          this->password = password;
82  
83 <        for (size_t index(strlen(password)); index > 0; --index) password[index -
84 <                1] = '\0';
83 >        for (size_t index(std::strlen(password)); index > 0; --index)
84 >                password[index - 1] = '\0';
85   }
86  
87 < void Collector::reply(const ext::String& login, const ext::String& reply)
87 > void Collector::status(const net::Oscar::UserInfo& user)
88   {
89 <        icbm->Simple(login, reply);
89 >        Buddy buddy(*buddies.find(user));
90 >        AwayMessage message(buddy, *info);
91 >
92 >        if (!ext::String(message).IsEmpty()) // XXX
93 >        {
94 >                if (Zoe::debug) cerr << "buddy = " << buddy << "\nmessage = " << message
95 >                        << '\n';
96 >
97 >                ext::Handle<dbi::Connection> db(dbi::Connect(database.driver,
98 >                        database.host, database.user, database.password, database.db));
99 >                ext::Handle<dbi::ResultSet> previous(db->Execute("SELECT message "
100 >                        "FROM messages WHERE stamp=(SELECT MAX(stamp) FROM messages "
101 >                        "WHERE id='" + lexical_cast<ext::String>(buddy.getId())
102 >                        + "') AND id='" + lexical_cast<ext::String>(buddy.getId()) + "'"));
103 >
104 >                if (previous->MoveNext())
105 >                {
106 >                        if (ext::String(message) == previous->GetString("message")) return;
107 >                }
108 >
109 >                db->Execute("INSERT INTO messages (stamp, message, id) VALUES ('"
110 >                        + ext::String(message.getStamp()) + "', '" + db->Escape(message)
111 >                        + "', '" + lexical_cast<ext::String>(buddy.getId()) + "')");
112 >
113 >                cerr << bright << red << "Collected away message from " << blue << buddy
114 >                        << red << " at " << Stamp() << ".\n" << reset;
115 >        }
116 > }
117 >
118 > void Collector::receive(const ext::String& buddy, const ext::String& message)
119 > {
120 >        if (Zoe::debug) cerr << "buddy = " << Buddy(buddy) << "\nmessage = "
121 >                << message << '\n';
122 >
123 >        if (buddies.find(buddy) != buddies.end())
124 >        {
125 >                Buddy buddy_(*buddies.find(buddy));
126 >                
127 >                sleep(1);
128 >
129 >                icbm->Simple(buddy, "<font face=\"Tahoma\" size=2>Hello! I'm Zoe. I am "
130 >                        "supposed to be collecting your <a href=\"" + buddy_.getLink()
131 >                        + "\">Away Messages</a> (<a href=\"" + buddy_.getRssLink() + "\">"
132 >                        "RSS</a>) (<a href=\"" + buddy_.getAtomLink() + "\">Atom</a>)."
133 >                        "</font> :-)");
134 >        }
135 >        else
136 >        {
137 >                sleep(1);
138 >
139 >                icbm->Simple(buddy, "<font face=\"Tahoma\" size=2>Mommy told me not to "
140 >                        "talk to strangers.</font> O:-)");
141 >        }
142   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines