Files @ b297329ba32f
Branch filter:

Location: libtransport.git/backends/twitter/TwitterPlugin.cpp - annotation

Sarang Bharadwaj
Handling exceptions related to XML parser
  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
f8a183661cfa
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
f8a183661cfa
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
81a71207d534
5b13c80ee9ea
81a71207d534
728517cf2312
728517cf2312
728517cf2312
728517cf2312
728517cf2312
cb62e786889f
e9891aa2000a
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
c2fefc36c734
11937227ff60
cb62e786889f
cb62e786889f
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
e9891aa2000a
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
11937227ff60
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
f8a183661cfa
f8a183661cfa
f8a183661cfa
f8a183661cfa
f8a183661cfa
f8a183661cfa
5b13c80ee9ea
5b13c80ee9ea
f8a183661cfa
f8a183661cfa
c79af87410fd
f8a183661cfa
f8a183661cfa
c2fefc36c734
c2fefc36c734
c79af87410fd
c2fefc36c734
c2fefc36c734
c2fefc36c734
c79af87410fd
c2fefc36c734
c2fefc36c734
c2fefc36c734
c79af87410fd
c2fefc36c734
c2fefc36c734
f8a183661cfa
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
b62ef77146eb
b62ef77146eb
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
cb62e786889f
b62ef77146eb
b62ef77146eb
cb62e786889f
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
f8a183661cfa
728517cf2312
c2fefc36c734
cb62e786889f
728517cf2312
c2fefc36c734
728517cf2312
728517cf2312
728517cf2312
728517cf2312
728517cf2312
728517cf2312
728517cf2312
728517cf2312
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
c79af87410fd
c2fefc36c734
c2fefc36c734
c2fefc36c734
c2fefc36c734
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
c2fefc36c734
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
c2fefc36c734
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
5b13c80ee9ea
728517cf2312
5b13c80ee9ea
11937227ff60
c2fefc36c734
c2fefc36c734
c2fefc36c734
11937227ff60
11937227ff60
11937227ff60
11937227ff60
11937227ff60
11937227ff60
b297329ba32f
b297329ba32f
b297329ba32f
b297329ba32f
b297329ba32f
11937227ff60
b297329ba32f
b297329ba32f
b297329ba32f
b297329ba32f
11937227ff60
b297329ba32f
b297329ba32f
b297329ba32f
b297329ba32f
11937227ff60
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
11937227ff60
c79af87410fd
c79af87410fd
c79af87410fd
c79af87410fd
11937227ff60
c79af87410fd
c79af87410fd
c79af87410fd
c2fefc36c734
c2fefc36c734
#include "TwitterPlugin.h"
#include "Requests/StatusUpdateRequest.h"
#include "Requests/DirectMessageRequest.h"
#include "Requests/TimelineRequest.h"
#include "Requests/FetchFriends.h"
#include "Requests/HelpMessageRequest.h"
#include "Requests/PINExchangeProcess.h"
#include "Requests/OAuthFlow.h"

DEFINE_LOGGER(logger, "Twitter Backend");

TwitterPlugin *np = NULL;
Swift::SimpleEventLoop *loop_; // Event Loop

#define abs(x) ((x)<0?-(x):(x))
static int cmp(std::string a, std::string b)
{
	int diff = abs((int)a.size() - (int)b.size());
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
	else b = std::string(diff,'0') + b;
	
	if(a == b) return 0;
	if(a < b) return -1;
	return 1;
}


TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() 
{
	this->config = config;
	this->storagebackend = storagebackend;

	if(CONFIG_HAS_KEY(config, "twitter.consumer_key") == false ||
	   CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
		LOG4CXX_ERROR(logger, "Couldn't find consumer key and/or secret. Please check config file.");
		exit(0);
	}
	
	twitterMode = SINGLECONTACT;
	if(CONFIG_HAS_KEY(config, "twitter.mode") == false) {
		LOG4CXX_INFO(logger, "Using default single contact mode!");
	} else twitterMode = (mode)CONFIG_INT(config, "twitter.mode");


	consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
	consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
	OAUTH_KEY = "oauth_key";
	OAUTH_SECRET = "oauth_secret";

	m_factories = new Swift::BoostNetworkFactories(loop);
	m_conn = m_factories->getConnectionFactory()->createConnection();
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));

	tp = new ThreadPool(loop_, 10);
		
	tweet_timer = m_factories->getTimerFactory()->createTimer(60000);
	message_timer = m_factories->getTimerFactory()->createTimer(60000);

	tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
	message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));

	tweet_timer->start();
	message_timer->start();
	
	LOG4CXX_INFO(logger, "Starting the plugin.");
}

TwitterPlugin::~TwitterPlugin() 
{
	delete storagebackend;
	std::map<std::string, twitCurl*>::iterator it;
	for(it = sessions.begin() ; it != sessions.end() ; it++) delete it->second;
	delete tp;
}

// Send data to NetworkPlugin server
void TwitterPlugin::sendData(const std::string &string) 
{
	m_conn->write(Swift::createSafeByteArray(string));
}

// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
void TwitterPlugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) 
{
	std::string d(data->begin(), data->end());
	handleDataRead(d);
}

// User trying to login into his twitter account
void TwitterPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) 
{
	if(connectionState.count(user) && (connectionState[user] == NEW || 
										connectionState[user] == CONNECTED || 
										connectionState[user] == WAITING_FOR_PIN)) {
		LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
		return;
	}
	
	LOG4CXX_INFO(logger, std::string("Received login request for ") + user)
	
	initUserSession(user, password);
	
	handleConnected(user);
	handleBuddyChanged(user, "twitter-account", "twitter", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
	
	LOG4CXX_INFO(logger, "Querying database for usersettings of " << user)
	
	std::string key, secret;
	getUserOAuthKeyAndSecret(user, key, secret);

	if(key == "" || secret == "") {			
		LOG4CXX_INFO(logger, "Intiating OAuth Flow for user " << user)
		tp->runAsThread(new OAuthFlow(np, sessions[user], user, sessions[user]->getTwitterUsername()));
	} else {
		LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
		LOG4CXX_INFO(logger, key << " " << secret)	
		pinExchangeComplete(user, key, secret);
	}
}

// User logging out
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
{
	delete sessions[user];
	sessions[user] = NULL;
	connectionState[user] = DISCONNECTED;
	onlineUsers.erase(user);
}


void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) 
{
	
	if(legacyName == "twitter-account") {

		//char ch;
		std::string cmd = "", data = "";
	 	
		int i;
		for(i=0 ; i<message.size() && message[i] != ' '; i++) cmd += message[i];
		while(i<message.size() && message[i] == ' ') i++;
		data = message.substr(i);
		
		//handleMessage(user, "twitter-account", cmd + " " + data);

		if(cmd == "#pin") tp->runAsThread(new PINExchangeProcess(np, sessions[user], user, data));
		else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(np, user));
		else if(cmd[0] == '@') {
			std::string username = cmd.substr(1); 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data,
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
		}
		else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
		else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(sessions[user], user, data, "",
														boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
		else if(cmd == "#friends") tp->runAsThread(new FetchFriends(sessions[user], user,
													   boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3)));
		else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
	} 

	else {
		tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message,
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
	}
}

void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
{
	LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
	handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
}

void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) 
{

}


void TwitterPlugin::pollForTweets()
{
	boost::mutex::scoped_lock lock(userlock);
	std::set<std::string>::iterator it = onlineUsers.begin();
	while(it != onlineUsers.end()) {
		std::string user = *it;
		tp->runAsThread(new TimelineRequest(sessions[user], user, "", mostRecentTweetID[user],
											boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
		it++;
	}
	tweet_timer->start();
}

void TwitterPlugin::pollForDirectMessages()
{
	boost::mutex::scoped_lock lock(userlock);
	std::set<std::string>::iterator it = onlineUsers.begin();
	while(it != onlineUsers.end()) {
		std::string user = *it;
		tp->runAsThread(new DirectMessageRequest(sessions[user], user, "", mostRecentDirectMessageID[user],
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
		it++;
	}
	message_timer->start();
}


bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) 
{
	boost::mutex::scoped_lock lock(dblock);
	
	UserInfo info;
	if(storagebackend->getUser(user, info) == false) {
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
		return false;
	}

	key="", secret=""; int type;
	storagebackend->getUserSetting((long)info.id, OAUTH_KEY, type, key);
	storagebackend->getUserSetting((long)info.id, OAUTH_SECRET, type, secret);
	return true;
}

bool TwitterPlugin::storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret) 
{

	boost::mutex::scoped_lock lock(dblock);

	UserInfo info;
	if(storagebackend->getUser(user, info) == false) {
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
		return false;
	}

	storagebackend->updateUserSetting((long)info.id, OAUTH_KEY, OAuthKey);	
	storagebackend->updateUserSetting((long)info.id, OAUTH_SECRET, OAuthSecret);
	return true;
}

void TwitterPlugin::initUserSession(const std::string user, const std::string password)
{
	boost::mutex::scoped_lock lock(userlock);

	std::string username = user.substr(0,user.find('@'));
	std::string passwd = password;
	LOG4CXX_INFO(logger, username + "  " + passwd)

	sessions[user] = new twitCurl();	
	if(CONFIG_HAS_KEY(config,"proxy.server")) {			
		std::string ip = CONFIG_STRING(config,"proxy.server");

		std::ostringstream out; 
		out << CONFIG_INT(config,"proxy.port");
		std::string port = out.str();

		std::string puser = CONFIG_STRING(config,"proxy.user");
		std::string ppasswd = CONFIG_STRING(config,"proxy.password");

		LOG4CXX_INFO(logger, ip << " " << port << " " << puser << " " << ppasswd)
		
		if(ip != "localhost" && port != "0") {
			sessions[user]->setProxyServerIp(ip);
			sessions[user]->setProxyServerPort(port);
			sessions[user]->setProxyUserName(puser);
			sessions[user]->setProxyPassword(ppasswd);
		}
	}

	connectionState[user] = NEW;			
	sessions[user]->setTwitterUsername(username);
	sessions[user]->setTwitterPassword(passwd); 
	sessions[user]->getOAuth().setConsumerKey(consumerKey);
	sessions[user]->getOAuth().setConsumerSecret(consumerSecret);
}

void TwitterPlugin::OAuthFlowComplete(const std::string user, twitCurl *obj) 
{
	boost::mutex::scoped_lock lock(userlock);	

	delete sessions[user];
	sessions[user] = obj->clone();	
	connectionState[user] = WAITING_FOR_PIN;
}	

void TwitterPlugin::pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret) 
{
	boost::mutex::scoped_lock lock(userlock);	
		
	sessions[user]->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey );
	sessions[user]->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret );
	connectionState[user] = CONNECTED;
	
	if(twitterMode == MULTIPLECONTACT) {
		tp->runAsThread(new FetchFriends(sessions[user], user,
										 boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3)));
	}

	onlineUsers.insert(user);
	mostRecentTweetID[user] = "";
	mostRecentDirectMessageID[user] = "";
}	

void TwitterPlugin::updateLastTweetID(const std::string user, const std::string ID)
{
	boost::mutex::scoped_lock lock(userlock);	
	mostRecentTweetID[user] = ID;
}

std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
{
	boost::mutex::scoped_lock lock(userlock);	
	std::string ID = "-1";
	if(onlineUsers.count(user)) ID = mostRecentTweetID[user];
	return ID;
}

void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID)
{
	boost::mutex::scoped_lock lock(userlock);	
	mostRecentDirectMessageID[user] = ID;
}

std::string TwitterPlugin::getMostRecentDMID(const std::string user)
{
	boost::mutex::scoped_lock lock(userlock);	
	std::string ID = "";
	if(onlineUsers.count(user)) ID = mostRecentDirectMessageID[user];
	return ID;
}

/************************************** Twitter response functions **********************************/

void TwitterPlugin::populateRoster(std::string &user, std::vector<User> &friends, std::string &errMsg) 
{
	if(errMsg.length() == 0) 
	{
		for(int i=0 ; i<friends.size() ; i++) {
			handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
		}
	} else handleMessage(user, "twitter-account", std::string("Error populating roster - ") + errMsg);	
}

void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg)
{
	if(errMsg.length() == 0) 
	{
		std::string userlist = "\n***************USER LIST****************\n";
		for(int i=0 ; i < friends.size() ; i++) {
			userlist += " - " + friends[i].getUserName() + " (" + friends[i].getScreenName() + ")\n";
		}	
		userlist += "***************************************\n";
		handleMessage(user, "twitter-account", userlist);	
	} else handleMessage(user, "twitter-account", errMsg);	
 
}

void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg)
{
	if(errMsg.length() == 0) {
		std::string timeline = "";

		for(int i=0 ; i<tweets.size() ; i++) {
			timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + "\n";
		}

		if((userRequested == "" || userRequested == user) && tweets.size()) {
			std::string tweetID = getMostRecentTweetID(user);
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
			else timeline = ""; //have already sent the tweet earlier
		}

		if(timeline.length()) handleMessage(user, "twitter-account", timeline);
	} else handleMessage(user, "twitter-account", errMsg);	
}

void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg)
{
	if(errMsg.length()) {
		handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg);	
		return;
	}
	
	if(!messages.size()) return;
	
	if(twitterMode == SINGLECONTACT) {

		std::string msglist = "";
		std::string msgID = getMostRecentDMID(user);
		std::string maxID = msgID;
		
		for(int i=0 ; i < messages.size() ; i++) {
			if(cmp(msgID, messages[i].getID()) == -1) {
				msglist += " - " + messages[i].getSenderData().getScreenName() + ": " + messages[i].getMessage() + "\n";
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
			}
		}	

		if(msglist.length()) handleMessage(user, "twitter-account", msglist);	
		updateLastDMID(user, maxID);

	} else if(twitterMode == MULTIPLECONTACT) {
		
		std::string msgID = getMostRecentDMID(user);
		std::string maxID = msgID;

		for(int i=0 ; i < messages.size() ; i++) {
			if(cmp(msgID, messages[i].getID()) == -1) {
				handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage());
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
			}
		}	
		
		if(maxID == getMostRecentDMID(user)) LOG4CXX_INFO(logger, "No new direct messages for " << user)
		updateLastDMID(user, maxID);
	}
}