ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Collector.cpp
Revision: 35
Committed: 2004-07-21T20:43:31-07:00 (20 years, 11 months ago) by douglas
File size: 3652 byte(s)
Log Message:
Adding Atom support, other stuff.

File Contents

# Content
1 // Zoe AIM Away Message RSS Feed Generator
2 //
3 // Seth King and Douglas Thrift
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 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::Bind<0>(&Collector::collect, this));
28 }
29 }
30
31 int Collector::collect()
32 {
33 cerr << bright << green << "Collector::collect()\n" << reset;
34
35 net::Oscar::Session session;
36 net::Oscar::AuthTool auth(session);
37 net::Oscar::BuddyTool buddy(session);
38 net::Oscar::ChatTool chat(session);
39 net::Oscar::IcbmTool icbm(session);
40 net::Oscar::InfoTool info(session);
41
42 auth.Login(login, password);
43
44 if (prompted) password.Clear();
45
46 this->buddy = &buddy;
47 this->icbm = &icbm;
48 this->info = &info;
49
50 buddy.OnOnline += etl::Bind<0>(&Collector::status, this);
51 icbm.OnReceive += etl::Bind<0>(&Collector::receive, this);
52
53 net::Oscar::StringSet buddies;
54
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);
59
60 net::Oscar::Capabilities capabilities;
61
62 capabilities.Insert(net::Oscar::ChatCapability);
63 info.SetProfile("<font face=\"Tahoma\" size=2>I am <a href=\"http://computers.douglasthrift.net/zoe.xml\">Zoe</a>."
64 "</font> :-*", "<font face=\"Tahoma\" size=2>Mommy told me not to talk to strangers.</font> O:-)", capabilities);
65
66 // figure out whether or not we logged in
67
68 while (true) sleep(Minute(1));
69 }
70
71 void Collector::prompt()
72 {
73 if (!prompted) prompted = true;
74
75 ext::String prompt(login + "'s AIM Password: ");
76 char* password(getpass(prompt.NullTerminate()));
77
78 this->password = password;
79
80 for (size_t index(std::strlen(password)); index > 0; --index) password[index - 1] = '\0';
81 }
82
83 void Collector::status(const net::Oscar::UserInfo& user)
84 {
85 Buddy buddy(*buddies.find(user));
86 AwayMessage message(buddy, *info);
87
88 if (!ext::String(message).IsEmpty()) // XXX
89 {
90 ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host, database.user, database.password,
91 database.db));
92 ext::Handle<dbi::ResultSet> previous(db->Execute("SELECT message FROM messages WHERE stamp=(SELECT MAX(stamp) "
93 "FROM messages WHERE id='" + lexical_cast<ext::String>(buddy.getId()) + "') AND id='"
94 + lexical_cast<ext::String>(buddy.getId()) + "'"));
95
96 if (previous->MoveNext()) if (ext::String(message) == previous->GetString("message")) return;
97
98 cerr << bright << red << "Collector::status(" << blue << buddy << red << ", " << reset << message << bright << red
99 << ")\n" << reset;
100
101 db->Execute("INSERT INTO messages (stamp, message, id) VALUES ('" + ext::String(message.getStamp()) + "', '"
102 + db->Escape(message) + "', '" + lexical_cast<ext::String>(buddy.getId()) + "')");
103 }
104 }
105
106 void Collector::receive(const ext::String& buddy, const ext::String& message)
107 {
108 cerr << bright << red << "Collector::receive(" << blue << Buddy(buddy) << red << ", " << reset << message << bright
109 << red << ")\n" << reset;
110
111 if (buddies.find(buddy) != buddies.end())
112 {
113 Stamp stamp;
114
115 sleep(1);
116
117 icbm->Simple(buddy, ext::String(stamp) + " " + lexical_cast<ext::String>(std::time(NULL)) + " "
118 + lexical_cast<ext::String>(api::GetWallTimerSeconds()));
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.</font> O:-)");
125 }
126 }

Properties

Name Value
svn:eol-style native
svn:keywords Id