-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDPager.cpp
More file actions
109 lines (95 loc) · 3.02 KB
/
LEDPager.cpp
File metadata and controls
109 lines (95 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <unordered_map>
#include <string>
#include <gloox/vcardmanager.h>
#include <gloox/vcardhandler.h>
#include <gloox/rostermanager.h>
#include <gloox/rosteritem.h>
#include <gloox/client.h>
#include <gloox/message.h>
#include <gloox/messagehandler.h>
#include <gloox/connectionlistener.h>
using namespace std;
using namespace gloox;
class LEDPager : public MessageHandler, VCardHandler, ConnectionListener {
public:
LEDPager (string newjid, string password, void (*onRecvFunc)(void), int debug)
{
DEBUG = debug;
JID jid(newjid);
onRecv = onRecvFunc;
client = new Client( jid, password );
vCardManager = new VCardManager(client);
client->registerMessageHandler( this );
client->registerConnectionListener(this);
this->connect(false);
barejid = jid.bare();
}
void handleVCard (const JID& jid, const VCard* vcard )
{
cout << barejid << " - got vcard for jid: " << jid.bare() << endl;
if (DEBUG) {
cout << "-" << endl;
cout << "nickname: " << vcard->formattedname() << endl;
cout << "-" << endl;
}
vCardIndex.emplace(jid.username(), vcard->formattedname());
}
void handleVCardResult ( VCardContext context, const JID& jid, StanzaError se = StanzaErrorUndefined )
{
cout << barejid << " - got error processing vcard for " << jid.bare() << endl;
}
void handleMessage( const Message& stanza, MessageSession* session = 0 )
{
auto vCardName = vCardIndex.find(stanza.from().username());
RosterItem* myItem = client->rosterManager()->getRosterItem(stanza.from());
if (vCardName == vCardIndex.end()) {
vCardManager->fetchVCard(stanza.from(), this);
/* todo: we may want to block until the vcard is resolved... or we may not... remains to be seen */
cout << barejid << " - Received message: " << (myItem ? myItem->jidJID().bare() : stanza.from().bare()) << " - " << stanza.body() << endl;
}
else {
cout << barejid << " - Received message: "<< vCardName->second << " - " << stanza.body() << endl;
}
if (stanza.body().size() != 0) {
/* ffs I don't care about typing notifications */
onRecv();
}
if (DEBUG) {
cout << "Raw debug output: " << stanza.tag()->xml() << endl;
}
}
void onConnect() {
std::cout << barejid << " - ConnListener::onConnect()" << endl;
}
void onDisconnect(ConnectionError e) {
std::cout << barejid << " - ConnListener::onDisconnect() " << e << endl;
client->connect(false);
}
bool onTLSConnect(const CertInfo& info) {
std::cout << barejid << " - ConnListener::onTLSConnect()" << endl;
return true;
}
/* wrappers around the client connection functions */
void recv (int timeout)
{
client->recv(timeout);
}
void connect (bool blocking)
{
client->connect(blocking);
}
void disconnect ()
{
client->disconnect();
}
private:
string barejid;
Client* client;
int DEBUG;
unordered_map<string, string> vCardIndex;
VCardManager* vCardManager;
void (*onRecv)(void);
};
/* vim: et ts=2 sw=2
*/