PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/noob_vc/twitcurl-read-only/twitcurl.cpp

https://code.google.com/
C++ | 1801 lines | 806 code | 133 blank | 862 comment | 108 complexity | 6fe22749d674a1d2c0bae57426885970 MD5 | raw file
  1. #include <memory.h>
  2. #include "twitcurl.h"
  3. #include "urlencode.h"
  4. /*++
  5. * @method: twitCurl::twitCurl
  6. *
  7. * @description: constructor
  8. *
  9. * @input: none
  10. *
  11. * @output: none
  12. *
  13. *--*/
  14. twitCurl::twitCurl():
  15. m_curlHandle( NULL ),
  16. m_twitterUsername( "" ),
  17. m_twitterPassword( "" ),
  18. m_callbackData( "" ),
  19. m_proxyUserName( "" ),
  20. m_proxyPassword( "" ),
  21. m_proxyServerIp( "" ),
  22. m_proxyServerPort( "" ),
  23. m_curlProxyParamsSet( false ),
  24. m_curlLoginParamsSet( false ),
  25. m_curlCallbackParamsSet( false )
  26. {
  27. /* Clear callback buffers */
  28. clearCurlCallbackBuffers();
  29. /* Initialize cURL */
  30. m_curlHandle = curl_easy_init();
  31. if( NULL == m_curlHandle )
  32. {
  33. std::string dummyStr( "" );
  34. getLastCurlError( dummyStr );
  35. }
  36. }
  37. /*++
  38. * @method: twitCurl::~twitCurl
  39. *
  40. * @description: destructor
  41. *
  42. * @input: none
  43. *
  44. * @output: none
  45. *
  46. *--*/
  47. twitCurl::~twitCurl()
  48. {
  49. /* Cleanup cURL */
  50. if( m_curlHandle )
  51. {
  52. curl_easy_cleanup( m_curlHandle );
  53. m_curlHandle = NULL;
  54. }
  55. }
  56. /*++
  57. * @method: twitCurl::isCurlInit
  58. *
  59. * @description: method to check if cURL is initialized properly
  60. *
  61. * @input: none
  62. *
  63. * @output: true if cURL is intialized, otherwise false
  64. *
  65. *--*/
  66. bool twitCurl::isCurlInit()
  67. {
  68. return ( NULL != m_curlHandle ) ? true : false;
  69. }
  70. /*++
  71. * @method: twitCurl::getTwitterUsername
  72. *
  73. * @description: method to get stored Twitter username
  74. *
  75. * @input: none
  76. *
  77. * @output: twitter username
  78. *
  79. *--*/
  80. std::string& twitCurl::getTwitterUsername()
  81. {
  82. return m_twitterUsername;
  83. }
  84. /*++
  85. * @method: twitCurl::getTwitterPassword
  86. *
  87. * @description: method to get stored Twitter password
  88. *
  89. * @input: none
  90. *
  91. * @output: twitter password
  92. *
  93. *--*/
  94. std::string& twitCurl::getTwitterPassword()
  95. {
  96. return m_twitterPassword;
  97. }
  98. /*++
  99. * @method: twitCurl::setTwitterUsername
  100. *
  101. * @description: method to set username
  102. *
  103. * @input: userName
  104. *
  105. * @output: none
  106. *
  107. *--*/
  108. void twitCurl::setTwitterUsername( std::string& userName )
  109. {
  110. if( userName.length() )
  111. {
  112. m_twitterUsername = userName;
  113. m_curlLoginParamsSet = false;
  114. }
  115. }
  116. /*++
  117. * @method: twitCurl::setTwitterPassword
  118. *
  119. * @description: method to set password
  120. *
  121. * @input: passWord
  122. *
  123. * @output: none
  124. *
  125. *--*/
  126. void twitCurl::setTwitterPassword( std::string& passWord )
  127. {
  128. if( passWord.length() )
  129. {
  130. m_twitterPassword = passWord;
  131. m_curlLoginParamsSet = false;
  132. }
  133. }
  134. /*++
  135. * @method: twitCurl::getProxyServerIp
  136. *
  137. * @description: method to get proxy server IP address
  138. *
  139. * @input: none
  140. *
  141. * @output: proxy server IP address
  142. *
  143. *--*/
  144. std::string& twitCurl::getProxyServerIp()
  145. {
  146. return m_proxyServerIp;
  147. }
  148. /*++
  149. * @method: twitCurl::getProxyServerPort
  150. *
  151. * @description: method to get proxy server port
  152. *
  153. * @input: none
  154. *
  155. * @output: proxy server port
  156. *
  157. *--*/
  158. std::string& twitCurl::getProxyServerPort()
  159. {
  160. return m_proxyServerPort;
  161. }
  162. /*++
  163. * @method: twitCurl::getProxyUserName
  164. *
  165. * @description: method to get proxy user name
  166. *
  167. * @input: none
  168. *
  169. * @output: proxy server user name
  170. *
  171. *--*/
  172. std::string& twitCurl::getProxyUserName()
  173. {
  174. return m_proxyUserName;
  175. }
  176. /*++
  177. * @method: twitCurl::getProxyPassword
  178. *
  179. * @description: method to get proxy server password
  180. *
  181. * @input: none
  182. *
  183. * @output: proxy server password
  184. *
  185. *--*/
  186. std::string& twitCurl::getProxyPassword()
  187. {
  188. return m_proxyPassword;
  189. }
  190. /*++
  191. * @method: twitCurl::setProxyServerIp
  192. *
  193. * @description: method to set proxy server IP address
  194. *
  195. * @input: proxyServerIp
  196. *
  197. * @output: none
  198. *
  199. *--*/
  200. void twitCurl::setProxyServerIp( std::string& proxyServerIp )
  201. {
  202. if( proxyServerIp.length() )
  203. {
  204. m_proxyServerIp = proxyServerIp;
  205. /*
  206. * Reset the flag so that next cURL http request
  207. * would set proxy details again into cURL.
  208. */
  209. m_curlProxyParamsSet = false;
  210. }
  211. }
  212. /*++
  213. * @method: twitCurl::setProxyServerPort
  214. *
  215. * @description: method to set proxy server port
  216. *
  217. * @input: proxyServerPort
  218. *
  219. * @output: none
  220. *
  221. *--*/
  222. void twitCurl::setProxyServerPort( std::string& proxyServerPort )
  223. {
  224. if( proxyServerPort.length() )
  225. {
  226. m_proxyServerPort = proxyServerPort;
  227. /*
  228. * Reset the flag so that next cURL http request
  229. * would set proxy details again into cURL.
  230. */
  231. m_curlProxyParamsSet = false;
  232. }
  233. }
  234. /*++
  235. * @method: twitCurl::setProxyUserName
  236. *
  237. * @description: method to set proxy server username
  238. *
  239. * @input: proxyUserName
  240. *
  241. * @output: none
  242. *
  243. *--*/
  244. void twitCurl::setProxyUserName( std::string& proxyUserName )
  245. {
  246. if( proxyUserName.length() )
  247. {
  248. m_proxyUserName = proxyUserName;
  249. /*
  250. * Reset the flag so that next cURL http request
  251. * would set proxy details again into cURL.
  252. */
  253. m_curlProxyParamsSet = false;
  254. }
  255. }
  256. /*++
  257. * @method: twitCurl::setProxyPassword
  258. *
  259. * @description: method to set proxy server password
  260. *
  261. * @input: proxyPassword
  262. *
  263. * @output: none
  264. *
  265. *--*/
  266. void twitCurl::setProxyPassword( std::string& proxyPassword )
  267. {
  268. if( proxyPassword.length() )
  269. {
  270. m_proxyPassword = proxyPassword;
  271. /*
  272. * Reset the flag so that next cURL http request
  273. * would set proxy details again into cURL.
  274. */
  275. m_curlProxyParamsSet = false;
  276. }
  277. }
  278. /*++
  279. * @method: twitCurl::search
  280. *
  281. * @description: method to return tweets that match a specified query.
  282. *
  283. * @input: query - search query in string format
  284. *
  285. * @output: true if GET is success, otherwise false. This does not check http
  286. * response by twitter. Use getLastWebResponse() for that.
  287. *
  288. *--*/
  289. bool twitCurl::search( std::string& query )
  290. {
  291. bool retVal = false;
  292. if( isCurlInit() )
  293. {
  294. /* Prepare URL */
  295. std::string buildUrl( "" );
  296. buildUrl = twitterDefaults::TWITCURL_SEARCH_URL;
  297. buildUrl.append( twitCurlDefaults::TWITCURL_SEARCHQUERYSTRING.c_str() );
  298. buildUrl.append( query.c_str() );
  299. /* Perform GET */
  300. retVal = performGet( buildUrl );
  301. }
  302. return retVal;
  303. }
  304. /*++
  305. * @method: twitCurl::statusUpdate
  306. *
  307. * @description: method to update new status message in twitter profile
  308. *
  309. * @input: newStatus
  310. *
  311. * @output: true if POST is success, otherwise false. This does not check http
  312. * response by twitter. Use getLastWebResponse() for that.
  313. *
  314. *--*/
  315. bool twitCurl::statusUpdate( std::string& newStatus )
  316. {
  317. bool retVal = false;
  318. if( isCurlInit() && newStatus.length() )
  319. {
  320. /* Prepare new status message */
  321. std::string newStatusMsg( "" );
  322. newStatusMsg = twitCurlDefaults::TWITCURL_STATUSSTRING;
  323. newStatusMsg.append( newStatus.c_str() );
  324. /* Perform POST */
  325. retVal = performPost( twitterDefaults::TWITCURL_STATUSUPDATE_URL, newStatusMsg );
  326. }
  327. return retVal;
  328. }
  329. /*++
  330. * @method: twitCurl::statusShowById
  331. *
  332. * @description: method to get a status message by its id
  333. *
  334. * @input: statusId - a number in std::string format
  335. *
  336. * @output: true if GET is success, otherwise false. This does not check http
  337. * response by twitter. Use getLastWebResponse() for that.
  338. *
  339. *--*/
  340. bool twitCurl::statusShowById( std::string& statusId )
  341. {
  342. bool retVal = false;
  343. if( isCurlInit() && statusId.length() )
  344. {
  345. /* Prepare URL */
  346. std::string buildUrl( "" );
  347. buildUrl = twitterDefaults::TWITCURL_STATUSSHOW_URL;
  348. buildUrl.append( statusId.c_str() );
  349. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  350. /* Perform GET */
  351. retVal = performGet( buildUrl );
  352. }
  353. return retVal;
  354. }
  355. /*++
  356. * @method: twitCurl::statusDestroyById
  357. *
  358. * @description: method to delete a status message by its id
  359. *
  360. * @input: statusId - a number in std::string format
  361. *
  362. * @output: true if DELETE is success, otherwise false. This does not check http
  363. * response by twitter. Use getLastWebResponse() for that.
  364. *
  365. *--*/
  366. bool twitCurl::statusDestroyById( std::string& statusId )
  367. {
  368. bool retVal = false;
  369. if( isCurlInit() && statusId.length() )
  370. {
  371. /* Prepare URL */
  372. std::string buildUrl( "" );
  373. buildUrl = twitterDefaults::TWITCURL_STATUDESTROY_URL;
  374. buildUrl.append( statusId.c_str() );
  375. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  376. /* Perform DELETE */
  377. retVal = performDelete( buildUrl );
  378. }
  379. return retVal;
  380. }
  381. /*++
  382. * @method: twitCurl::timelinePublicGet
  383. *
  384. * @description: method to get public timeline
  385. *
  386. * @input: none
  387. *
  388. * @output: true if GET is success, otherwise false. This does not check http
  389. * response by twitter. Use getLastWebResponse() for that.
  390. *
  391. *--*/
  392. bool twitCurl::timelinePublicGet()
  393. {
  394. bool retVal = false;
  395. if( isCurlInit() )
  396. {
  397. /* Perform GET */
  398. retVal = performGet( twitterDefaults::TWITCURL_PUBLIC_TIMELINE_URL );
  399. }
  400. return retVal;
  401. }
  402. /*++
  403. * @method: twitCurl::featuredUsersGet
  404. *
  405. * @description: method to get featured users
  406. *
  407. * @input: none
  408. *
  409. * @output: true if GET is success, otherwise false. This does not check http
  410. * response by twitter. Use getLastWebResponse() for that.
  411. *
  412. *--*/
  413. bool twitCurl::featuredUsersGet()
  414. {
  415. bool retVal = false;
  416. if( isCurlInit() )
  417. {
  418. /* Perform GET */
  419. retVal = performGet( twitterDefaults::TWITCURL_FEATURED_USERS_URL );
  420. }
  421. return retVal;
  422. }
  423. /*++
  424. * @method: twitCurl::timelineFriendsGet
  425. *
  426. * @description: method to get friends timeline
  427. *
  428. * @input: none
  429. *
  430. * @output: true if GET is success, otherwise false. This does not check http
  431. * response by twitter. Use getLastWebResponse() for that.
  432. *
  433. *--*/
  434. bool twitCurl::timelineFriendsGet()
  435. {
  436. bool retVal = false;
  437. if( isCurlInit() )
  438. {
  439. /* Perform GET */
  440. retVal = performGet( twitterDefaults::TWITCURL_FRIENDS_TIMELINE_URL );
  441. }
  442. return retVal;
  443. }
  444. /*++
  445. * @method: twitCurl::mentionsGet
  446. *
  447. * @description: method to get mentions
  448. *
  449. * @input: none
  450. *
  451. * @output: true if GET is success, otherwise false. This does not check http
  452. * response by twitter. Use getLastWebResponse() for that.
  453. *
  454. *--*/
  455. bool twitCurl::mentionsGet()
  456. {
  457. bool retVal = false;
  458. if( isCurlInit() )
  459. {
  460. /* Perform GET */
  461. retVal = performGet( twitterDefaults::TWITCURL_MENTIONS_URL );
  462. }
  463. return retVal;
  464. }
  465. /*++
  466. * @method: twitCurl::timelineUserGet
  467. *
  468. * @description: method to get mentions
  469. *
  470. * @input: userInfo - screen name or user id in string format,
  471. * isUserId - true if userInfo contains an id
  472. *
  473. * @output: true if GET is success, otherwise false. This does not check http
  474. * response by twitter. Use getLastWebResponse() for that.
  475. *
  476. *--*/
  477. bool twitCurl::timelineUserGet( std::string userInfo, bool isUserId )
  478. {
  479. bool retVal = false;
  480. if( isCurlInit() )
  481. {
  482. /* Prepare URL */
  483. std::string buildUrl( "" );
  484. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_USERTIMELINE_URL, userInfo, isUserId );
  485. /* Perform GET */
  486. retVal = performGet( buildUrl );
  487. }
  488. return retVal;
  489. }
  490. /*++
  491. * @method: twitCurl::userGet
  492. *
  493. * @description: method to get a user's profile
  494. *
  495. * @input: userInfo - screen name or user id in string format,
  496. * isUserId - true if userInfo contains an id
  497. *
  498. * @output: true if GET is success, otherwise false. This does not check http
  499. * response by twitter. Use getLastWebResponse() for that.
  500. *
  501. *--*/
  502. bool twitCurl::userGet( std::string& userInfo, bool isUserId )
  503. {
  504. bool retVal = false;
  505. if( isCurlInit() && userInfo.length() )
  506. {
  507. /* Set URL */
  508. std::string buildUrl( "" );
  509. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_SHOWUSERS_URL, userInfo, isUserId );
  510. /* Perform GET */
  511. retVal = performGet( buildUrl );
  512. }
  513. return retVal;
  514. }
  515. /*++
  516. * @method: twitCurl::friendsGet
  517. *
  518. * @description: method to get a user's friends
  519. *
  520. * @input: userInfo - screen name or user id in string format,
  521. * isUserId - true if userInfo contains an id
  522. *
  523. * @output: true if GET is success, otherwise false. This does not check http
  524. * response by twitter. Use getLastWebResponse() for that.
  525. *
  526. *--*/
  527. bool twitCurl::friendsGet( std::string userInfo, bool isUserId )
  528. {
  529. bool retVal = false;
  530. if( isCurlInit() )
  531. {
  532. /* Set URL */
  533. std::string buildUrl( "" );
  534. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_SHOWFRIENDS_URL, userInfo, isUserId );
  535. /* Perform GET */
  536. retVal = performGet( buildUrl );
  537. }
  538. return retVal;
  539. }
  540. /*++
  541. * @method: twitCurl::followersGet
  542. *
  543. * @description: method to get a user's followers
  544. *
  545. * @input: userInfo - screen name or user id in string format,
  546. * isUserId - true if userInfo contains an id
  547. *
  548. * @output: true if GET is success, otherwise false. This does not check http
  549. * response by twitter. Use getLastWebResponse() for that.
  550. *
  551. *--*/
  552. bool twitCurl::followersGet( std::string userInfo, bool isUserId )
  553. {
  554. bool retVal = false;
  555. if( isCurlInit() )
  556. {
  557. /* Prepare URL */
  558. std::string buildUrl( "" );
  559. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_SHOWFOLLOWERS_URL, userInfo, isUserId );
  560. /* Perform GET */
  561. retVal = performGet( buildUrl );
  562. }
  563. return retVal;
  564. }
  565. /*++
  566. * @method: twitCurl::directMessageGet
  567. *
  568. * @description: method to get direct messages
  569. *
  570. * @input: none
  571. *
  572. * @output: true if GET is success, otherwise false. This does not check http
  573. * response by twitter. Use getLastWebResponse() for that.
  574. *
  575. *--*/
  576. bool twitCurl::directMessageGet()
  577. {
  578. bool retVal = false;
  579. if( isCurlInit() )
  580. {
  581. /* Perform GET */
  582. retVal = performGet( twitterDefaults::TWITCURL_DIRECTMESSAGES_URL );
  583. }
  584. return retVal;
  585. }
  586. /*++
  587. * @method: twitCurl::directMessageSend
  588. *
  589. * @description: method to send direct message to a user
  590. *
  591. * @input: userInfo - screen name or user id of a user to whom message needs to be sent,
  592. * dMsg - message
  593. * isUserId - true if userInfo contains target user's id
  594. *
  595. * @output: true if POST is success, otherwise false. This does not check http
  596. * response by twitter. Use getLastWebResponse() for that.
  597. *
  598. *--*/
  599. bool twitCurl::directMessageSend( std::string& userInfo, std::string& dMsg, bool isUserId )
  600. {
  601. bool retVal = false;
  602. if( isCurlInit() && userInfo.length() && dMsg.length() )
  603. {
  604. /* Prepare new direct message */
  605. std::string newDm( "" );
  606. newDm = twitCurlDefaults::TWITCURL_TEXTSTRING;
  607. newDm.append( dMsg.c_str() );
  608. /* Prepare URL */
  609. std::string buildUrl( "" );
  610. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_DIRECTMESSAGENEW_URL, userInfo, isUserId );
  611. /* Perform POST */
  612. retVal = performPost( buildUrl, newDm );
  613. }
  614. return retVal;
  615. }
  616. /*++
  617. * @method: twitCurl::directMessageGetSent
  618. *
  619. * @description: method to get sent direct messages
  620. *
  621. * @input: none
  622. *
  623. * @output: true if GET is success, otherwise false. This does not check http
  624. * response by twitter. Use getLastWebResponse() for that.
  625. *
  626. *--*/
  627. bool twitCurl::directMessageGetSent()
  628. {
  629. bool retVal = false;
  630. if( isCurlInit() )
  631. {
  632. /* Perform GET */
  633. retVal = performGet( twitterDefaults::TWITCURL_DIRECTMESSAGESSENT_URL );
  634. }
  635. return retVal;
  636. }
  637. /*++
  638. * @method: twitCurl::directMessageDestroyById
  639. *
  640. * @description: method to delete direct messages by its id
  641. *
  642. * @input: dMsgId - id of direct message in string format
  643. *
  644. * @output: true if DELETE is success, otherwise false. This does not check http
  645. * response by twitter. Use getLastWebResponse() for that.
  646. *
  647. *--*/
  648. bool twitCurl::directMessageDestroyById( std::string& dMsgId )
  649. {
  650. bool retVal = false;
  651. if( isCurlInit() && dMsgId.length() )
  652. {
  653. /* Prepare URL */
  654. std::string buildUrl( "" );
  655. buildUrl = twitterDefaults::TWITCURL_DIRECTMESSAGEDESTROY_URL;
  656. buildUrl.append( dMsgId.c_str() );
  657. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  658. /* Perform DELETE */
  659. retVal = performDelete( buildUrl );
  660. }
  661. return retVal;
  662. }
  663. /*++
  664. * @method: twitCurl::friendshipCreate
  665. *
  666. * @description: method to add a twitter user as friend (follow a user)
  667. *
  668. * @input: userInfo - user id or screen name of a user
  669. * isUserId - true if userInfo contains a user id instead of screen name
  670. *
  671. * @output: true if POST is success, otherwise false. This does not check http
  672. * response by twitter. Use getLastWebResponse() for that.
  673. *
  674. *--*/
  675. bool twitCurl::friendshipCreate( std::string& userInfo, bool isUserId )
  676. {
  677. bool retVal = false;
  678. if( isCurlInit() && userInfo.length() )
  679. {
  680. /* Prepare URL */
  681. std::string buildUrl( "" );
  682. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_FRIENDSHIPSCREATE_URL, userInfo, isUserId );
  683. /* Send some dummy data in POST */
  684. std::string dummyData( "" );
  685. dummyData = twitCurlDefaults::TWITCURL_TEXTSTRING;
  686. dummyData.append( "dummy" );
  687. /* Perform POST */
  688. retVal = performPost( buildUrl, dummyData );
  689. }
  690. return retVal;
  691. }
  692. /*++
  693. * @method: twitCurl::friendshipDestroy
  694. *
  695. * @description: method to delete a twitter user from friend list (unfollow a user)
  696. *
  697. * @input: userInfo - user id or screen name of a user
  698. * isUserId - true if userInfo contains a user id instead of screen name
  699. *
  700. * @output: true if DELETE is success, otherwise false. This does not check http
  701. * response by twitter. Use getLastWebResponse() for that.
  702. *
  703. *--*/
  704. bool twitCurl::friendshipDestroy( std::string& userInfo, bool isUserId )
  705. {
  706. bool retVal = false;
  707. if( isCurlInit() && userInfo.length() )
  708. {
  709. /* Prepare URL */
  710. std::string buildUrl( "" );
  711. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_FRIENDSHIPSDESTROY_URL, userInfo, isUserId );
  712. /* Perform DELETE */
  713. retVal = performDelete( buildUrl );
  714. }
  715. return retVal;
  716. }
  717. /*++
  718. * @method: twitCurl::friendshipShow
  719. *
  720. * @description: method to show all friends
  721. *
  722. * @input: userInfo - user id or screen name of a user of whom friends need to be shown
  723. * isUserId - true if userInfo contains a user id instead of screen name
  724. *
  725. * @output: true if GET is success, otherwise false. This does not check http
  726. * response by twitter. Use getLastWebResponse() for that.
  727. *
  728. *--*/
  729. bool twitCurl::friendshipShow( std::string& userInfo, bool isUserId )
  730. {
  731. bool retVal = false;
  732. if( isCurlInit() )
  733. {
  734. /* Prepare URL */
  735. std::string buildUrl( "" );
  736. buildUrl = twitterDefaults::TWITCURL_FRIENDSHIPSSHOW_URL;
  737. if( userInfo.length() )
  738. {
  739. /* Append username to the URL */
  740. if( isUserId )
  741. {
  742. buildUrl.append( twitCurlDefaults::TWITCURL_TARGETUSERID.c_str() );
  743. }
  744. else
  745. {
  746. buildUrl.append( twitCurlDefaults::TWITCURL_TARGETSCREENNAME.c_str() );
  747. }
  748. buildUrl.append( userInfo.c_str() );
  749. }
  750. /* Perform GET */
  751. retVal = performGet( buildUrl );
  752. }
  753. return retVal;
  754. }
  755. /*++
  756. * @method: twitCurl::friendsIdsGet
  757. *
  758. * @description: method to show IDs of all friends of a twitter user
  759. *
  760. * @input: userInfo - user id or screen name of a user
  761. * isUserId - true if userInfo contains a user id instead of screen name
  762. *
  763. * @output: true if GET is success, otherwise false. This does not check http
  764. * response by twitter. Use getLastWebResponse() for that.
  765. *
  766. *--*/
  767. bool twitCurl::friendsIdsGet( std::string& userInfo, bool isUserId )
  768. {
  769. bool retVal = false;
  770. if( isCurlInit() )
  771. {
  772. /* Prepare URL */
  773. std::string buildUrl( "" );
  774. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_FRIENDSIDS_URL, userInfo, isUserId );
  775. /* Perform GET */
  776. retVal = performGet( buildUrl );
  777. }
  778. return retVal;
  779. }
  780. /*++
  781. * @method: twitCurl::followersIdsGet
  782. *
  783. * @description: method to show IDs of all followers of a twitter user
  784. *
  785. * @input: userInfo - user id or screen name of a user
  786. * isUserId - true if userInfo contains a user id instead of screen name
  787. *
  788. * @output: true if GET is success, otherwise false. This does not check http
  789. * response by twitter. Use getLastWebResponse() for that.
  790. *
  791. *--*/
  792. bool twitCurl::followersIdsGet( std::string& userInfo, bool isUserId )
  793. {
  794. bool retVal = false;
  795. if( isCurlInit() )
  796. {
  797. /* Prepare URL */
  798. std::string buildUrl( "" );
  799. utilMakeUrlForUser( buildUrl, twitterDefaults::TWITCURL_FOLLOWERSIDS_URL, userInfo, isUserId );
  800. /* Perform GET */
  801. retVal = performGet( buildUrl );
  802. }
  803. return retVal;
  804. }
  805. /*++
  806. * @method: twitCurl::accountRateLimitGet
  807. *
  808. * @description: method to get API rate limit of current user
  809. *
  810. * @input: none
  811. *
  812. * @output: true if GET is success, otherwise false. This does not check http
  813. * response by twitter. Use getLastWebResponse() for that.
  814. *
  815. *--*/
  816. bool twitCurl::accountRateLimitGet()
  817. {
  818. bool retVal = false;
  819. if( isCurlInit() )
  820. {
  821. /* Perform GET */
  822. retVal = performGet( twitterDefaults::TWITCURL_ACCOUNTRATELIMIT_URL );
  823. }
  824. return retVal;
  825. }
  826. /*++
  827. * @method: twitCurl::favoriteGet
  828. *
  829. * @description: method to get favorite users' statuses
  830. *
  831. * @input: none
  832. *
  833. * @output: true if GET is success, otherwise false. This does not check http
  834. * response by twitter. Use getLastWebResponse() for that.
  835. *
  836. *--*/
  837. bool twitCurl::favoriteGet()
  838. {
  839. bool retVal = false;
  840. if( isCurlInit() )
  841. {
  842. /* Perform GET */
  843. retVal = performGet( twitterDefaults::TWITCURL_FAVORITESGET_URL );
  844. }
  845. return retVal;
  846. }
  847. /*++
  848. * @method: twitCurl::favoriteCreate
  849. *
  850. * @description: method to favorite a status message
  851. *
  852. * @input: statusId - id in string format of the status to be favorited
  853. *
  854. * @output: true if POST is success, otherwise false. This does not check http
  855. * response by twitter. Use getLastWebResponse() for that.
  856. *
  857. *--*/
  858. bool twitCurl::favoriteCreate( std::string& statusId )
  859. {
  860. bool retVal = false;
  861. if( isCurlInit() )
  862. {
  863. /* Prepare URL */
  864. std::string buildUrl( "" );
  865. buildUrl = twitterDefaults::TWITCURL_FAVORITECREATE_URL;
  866. buildUrl.append( statusId.c_str() );
  867. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  868. /* Send some dummy data in POST */
  869. std::string dummyData( "" );
  870. dummyData = twitCurlDefaults::TWITCURL_TEXTSTRING;
  871. dummyData.append( "dummy" );
  872. /* Perform POST */
  873. retVal = performPost( buildUrl, dummyData );
  874. }
  875. return retVal;
  876. }
  877. /*++
  878. * @method: twitCurl::favoriteDestroy
  879. *
  880. * @description: method to delete a favorited the status
  881. *
  882. * @input: statusId - id in string format of the favorite status to be deleted
  883. *
  884. * @output: true if DELETE is success, otherwise false. This does not check http
  885. * response by twitter. Use getLastWebResponse() for that.
  886. *
  887. *--*/
  888. bool twitCurl::favoriteDestroy( std::string& statusId )
  889. {
  890. bool retVal = false;
  891. if( isCurlInit() )
  892. {
  893. /* Prepare URL */
  894. std::string buildUrl( "" );
  895. buildUrl = twitterDefaults::TWITCURL_FAVORITEDESTROY_URL;
  896. buildUrl.append( statusId.c_str() );
  897. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  898. /* Perform DELETE */
  899. retVal = performDelete( buildUrl );
  900. }
  901. return retVal;
  902. }
  903. /*++
  904. * @method: twitCurl::blockCreate
  905. *
  906. * @description: method to block a user
  907. *
  908. * @input: userInfo - user id or screen name
  909. *
  910. * @output: true if POST is success, otherwise false. This does not check http
  911. * response by twitter. Use getLastWebResponse() for that.
  912. *
  913. *--*/
  914. bool twitCurl::blockCreate( std::string& userInfo )
  915. {
  916. bool retVal = false;
  917. if( isCurlInit() )
  918. {
  919. /* Prepare URL */
  920. std::string buildUrl( "" );
  921. buildUrl = twitterDefaults::TWITCURL_BLOCKSCREATE_URL;
  922. buildUrl.append( userInfo.c_str() );
  923. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  924. /* Send some dummy data in POST */
  925. std::string dummyData( "" );
  926. dummyData = twitCurlDefaults::TWITCURL_TEXTSTRING;
  927. dummyData.append( "dummy" );
  928. /* Perform POST */
  929. retVal = performPost( buildUrl, dummyData );
  930. }
  931. return retVal;
  932. }
  933. /*++
  934. * @method: twitCurl::blockDestroy
  935. *
  936. * @description: method to unblock a user
  937. *
  938. * @input: userInfo - user id or screen name
  939. *
  940. * @output: true if DELETE is success, otherwise false. This does not check http
  941. * response by twitter. Use getLastWebResponse() for that.
  942. *
  943. *--*/
  944. bool twitCurl::blockDestroy( std::string& userInfo )
  945. {
  946. bool retVal = false;
  947. if( isCurlInit() )
  948. {
  949. /* Prepare URL */
  950. std::string buildUrl( "" );
  951. buildUrl = twitterDefaults::TWITCURL_BLOCKSDESTROY_URL;
  952. buildUrl.append( userInfo.c_str() );
  953. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  954. /* Perform DELETE */
  955. retVal = performDelete( buildUrl );
  956. }
  957. return retVal;
  958. }
  959. /*++
  960. * @method: twitCurl::savedSearchGet
  961. *
  962. * @description: gets authenticated user's saved search queries.
  963. *
  964. * @input: none
  965. *
  966. * @output: true if GET is success, otherwise false. This does not check http
  967. * response by twitter. Use getLastWebResponse() for that.
  968. *
  969. *--*/
  970. bool twitCurl::savedSearchGet( )
  971. {
  972. bool retVal = false;
  973. if( isCurlInit() )
  974. {
  975. /* Perform GET */
  976. retVal = performGet( twitterDefaults::TWITCURL_SAVEDSEARCHGET_URL );
  977. }
  978. return retVal;
  979. }
  980. /*++
  981. * @method: twitCurl::savedSearchShow
  982. *
  983. * @description: method to retrieve the data for a saved search owned by the authenticating user
  984. * specified by the given id.
  985. *
  986. * @input: searchId - id in string format of the search to be displayed
  987. *
  988. * @output: true if GET is success, otherwise false. This does not check http
  989. * response by twitter. Use getLastWebResponse() for that.
  990. *
  991. *--*/
  992. bool twitCurl::savedSearchShow( std::string& searchId )
  993. {
  994. bool retVal = false;
  995. if( isCurlInit() )
  996. {
  997. /* Prepare URL */
  998. std::string buildUrl( "" );
  999. buildUrl = twitterDefaults::TWITCURL_SAVEDSEARCHSHOW_URL;
  1000. buildUrl.append( searchId.c_str() );
  1001. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  1002. /* Perform GET */
  1003. retVal = performGet( buildUrl );
  1004. }
  1005. return retVal;
  1006. }
  1007. /*++
  1008. * @method: twitCurl::savedSearchCreate
  1009. *
  1010. * @description: creates a saved search for the authenticated user
  1011. *
  1012. * @input: query - the query of the search the user would like to save
  1013. *
  1014. * @output: true if POST is success, otherwise false. This does not check http
  1015. * response by twitter. Use getLastWebResponse() for that.
  1016. *
  1017. *--*/
  1018. bool twitCurl::savedSearchCreate( std::string& query )
  1019. {
  1020. bool retVal = false;
  1021. if( isCurlInit() )
  1022. {
  1023. /* Prepare URL */
  1024. std::string buildUrl( "" );
  1025. buildUrl = twitterDefaults::TWITCURL_SAVEDSEARCHCREATE_URL;
  1026. /* Send some dummy data in POST */
  1027. std::string queryStr;
  1028. queryStr = twitCurlDefaults::TWITCURL_QUERYSTRING;
  1029. queryStr.append( query );
  1030. /* Perform POST */
  1031. retVal = performPost( buildUrl, queryStr );
  1032. }
  1033. return retVal;
  1034. }
  1035. /*++
  1036. * @method: twitCurl::savedSearchDestroy
  1037. *
  1038. * @description: method to destroy a saved search for the authenticated user. The search specified
  1039. * by id must be owned by the authenticating user.
  1040. *
  1041. * @input: searchId - search id of item to be deleted
  1042. *
  1043. * @output: true if DELETE is success, otherwise false. This does not check http
  1044. * response by twitter. Use getLastWebResponse() for that.
  1045. *
  1046. *--*/
  1047. bool twitCurl::savedSearchDestroy( std::string& searchId )
  1048. {
  1049. bool retVal = false;
  1050. if( isCurlInit() )
  1051. {
  1052. /* Prepare URL */
  1053. std::string buildUrl( "" );
  1054. buildUrl = twitterDefaults::TWITCURL_SAVEDSEARCHDESTROY_URL;
  1055. buildUrl.append( searchId.c_str() );
  1056. buildUrl.append( twitCurlDefaults::TWITCURL_EXTENSIONFORMAT.c_str() );
  1057. /* Perform DELETE */
  1058. retVal = performDelete( buildUrl );
  1059. }
  1060. return retVal;
  1061. }
  1062. /*++
  1063. * @method: twitCurl::getLastWebResponse
  1064. *
  1065. * @description: method to get http response for the most recent request sent.
  1066. * twitcurl users need to call this method and parse the XML
  1067. * data returned by twitter to see what has happened.
  1068. *
  1069. * @input: outWebResp - string in which twitter's response is supplied back to caller
  1070. *
  1071. * @output: none
  1072. *
  1073. *--*/
  1074. void twitCurl::getLastWebResponse( std::string& outWebResp )
  1075. {
  1076. outWebResp = "";
  1077. if( m_callbackData.length() )
  1078. {
  1079. outWebResp = m_callbackData;
  1080. }
  1081. }
  1082. /*++
  1083. * @method: twitCurl::getLastCurlError
  1084. *
  1085. * @description: method to get cURL error response for most recent http request.
  1086. * twitcurl users can call this method if any of the APIs return
  1087. * false.
  1088. *
  1089. * @input: none
  1090. *
  1091. * @output: none
  1092. *
  1093. *--*/
  1094. void twitCurl::getLastCurlError( std::string& outErrResp )
  1095. {
  1096. m_errorBuffer[twitCurlDefaults::TWITCURL_DEFAULT_BUFFSIZE-1] = twitCurlDefaults::TWITCURL_EOS;
  1097. outErrResp.assign( m_errorBuffer );
  1098. }
  1099. /*++
  1100. * @method: twitCurl::curlCallback
  1101. *
  1102. * @description: static method to get http response back from cURL.
  1103. * this is an internal method, users of twitcurl need not
  1104. * use this.
  1105. *
  1106. * @input: as per cURL convention.
  1107. *
  1108. * @output: size of data stored in our buffer
  1109. *
  1110. * @remarks: internal method
  1111. *
  1112. *--*/
  1113. int twitCurl::curlCallback( char* data, size_t size, size_t nmemb, twitCurl* pTwitCurlObj )
  1114. {
  1115. int writtenSize = 0;
  1116. if( ( NULL != pTwitCurlObj ) && ( NULL != data ) )
  1117. {
  1118. /* Save http response in twitcurl object's buffer */
  1119. writtenSize = pTwitCurlObj->saveLastWebResponse( data, ( size*nmemb ) );
  1120. }
  1121. return writtenSize;
  1122. }
  1123. /*++
  1124. * @method: twitCurl::saveLastWebResponse
  1125. *
  1126. * @description: method to save http responses. this is an internal method
  1127. * and twitcurl users need not use this.
  1128. *
  1129. * @input: data - character buffer from cURL,
  1130. * size - size of character buffer
  1131. *
  1132. * @output: size of data stored in our buffer
  1133. *
  1134. * @remarks: internal method
  1135. *
  1136. *--*/
  1137. int twitCurl::saveLastWebResponse( char*& data, size_t size )
  1138. {
  1139. int bytesWritten = 0;
  1140. if( data && size )
  1141. {
  1142. /* Append data in our internal buffer */
  1143. m_callbackData.append( data, size );
  1144. bytesWritten = (int)size;
  1145. }
  1146. return bytesWritten;
  1147. }
  1148. /*++
  1149. * @method: twitCurl::clearCurlCallbackBuffers
  1150. *
  1151. * @description: method to clear callback buffers used by cURL. this is an
  1152. * internal method and twitcurl users need not use this.
  1153. *
  1154. * @input: none
  1155. *
  1156. * @output: none
  1157. *
  1158. * @remarks: internal method
  1159. *
  1160. *--*/
  1161. void twitCurl::clearCurlCallbackBuffers()
  1162. {
  1163. m_callbackData = "";
  1164. memset( m_errorBuffer, 0, twitCurlDefaults::TWITCURL_DEFAULT_BUFFSIZE );
  1165. }
  1166. /*++
  1167. * @method: twitCurl::prepareCurlProxy
  1168. *
  1169. * @description: method to set proxy details into cURL. this is an internal method.
  1170. * twitcurl users should not use this method, instead use setProxyXxx
  1171. * methods to set proxy server information.
  1172. *
  1173. * @input: none
  1174. *
  1175. * @output: none
  1176. *
  1177. * @remarks: internal method
  1178. *
  1179. *--*/
  1180. void twitCurl::prepareCurlProxy()
  1181. {
  1182. if( !m_curlProxyParamsSet )
  1183. {
  1184. /* Reset existing proxy details in cURL */
  1185. curl_easy_setopt( m_curlHandle, CURLOPT_PROXY, NULL );
  1186. curl_easy_setopt( m_curlHandle, CURLOPT_PROXYUSERPWD, NULL );
  1187. curl_easy_setopt( m_curlHandle, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY );
  1188. /* Set proxy details in cURL */
  1189. std::string proxyIpPort( "" );
  1190. utilMakeCurlParams( proxyIpPort, getProxyServerIp(), getProxyServerPort() );
  1191. curl_easy_setopt( m_curlHandle, CURLOPT_PROXY, proxyIpPort.c_str() );
  1192. /* Prepare username and password for proxy server */
  1193. if( m_proxyUserName.length() && m_proxyPassword.length() )
  1194. {
  1195. std::string proxyUserPass( "" );
  1196. utilMakeCurlParams( proxyUserPass, getProxyUserName(), getProxyPassword() );
  1197. curl_easy_setopt( m_curlHandle, CURLOPT_PROXYUSERPWD, proxyUserPass.c_str() );
  1198. }
  1199. /* Set the flag to true indicating that proxy info is set in cURL */
  1200. m_curlProxyParamsSet = true;
  1201. }
  1202. }
  1203. /*++
  1204. * @method: twitCurl::prepareCurlCallback
  1205. *
  1206. * @description: method to set callback details into cURL. this is an internal method.
  1207. * twitcurl users should not use this method.
  1208. *
  1209. * @input: none
  1210. *
  1211. * @output: none
  1212. *
  1213. * @remarks: internal method
  1214. *
  1215. *--*/
  1216. void twitCurl::prepareCurlCallback()
  1217. {
  1218. if( !m_curlCallbackParamsSet )
  1219. {
  1220. /* Set buffer to get error */
  1221. curl_easy_setopt( m_curlHandle, CURLOPT_ERRORBUFFER, m_errorBuffer );
  1222. /* Set callback function to get response */
  1223. curl_easy_setopt( m_curlHandle, CURLOPT_WRITEFUNCTION, curlCallback );
  1224. curl_easy_setopt( m_curlHandle, CURLOPT_WRITEDATA, this );
  1225. /* Set the flag to true indicating that callback info is set in cURL */
  1226. m_curlCallbackParamsSet = true;
  1227. }
  1228. }
  1229. /*++
  1230. * @method: twitCurl::prepareCurlUserPass
  1231. *
  1232. * @description: method to set twitter credentials into cURL. this is an internal method.
  1233. * twitcurl users should not use this method, instead use setTwitterXxx
  1234. * methods to set twitter username and password.
  1235. *
  1236. * @input: none
  1237. *
  1238. * @output: none
  1239. *
  1240. * @remarks: internal method
  1241. *
  1242. *--*/
  1243. void twitCurl::prepareCurlUserPass()
  1244. {
  1245. if( !m_curlLoginParamsSet )
  1246. {
  1247. /* Reset existing username and password stored in cURL */
  1248. curl_easy_setopt( m_curlHandle, CURLOPT_USERPWD, NULL );
  1249. /* Prepare username:password */
  1250. std::string userNamePassword( "" );
  1251. utilMakeCurlParams( userNamePassword, getTwitterUsername(), getTwitterPassword() );
  1252. /* Set username and password */
  1253. curl_easy_setopt( m_curlHandle, CURLOPT_USERPWD, userNamePassword.c_str() );
  1254. /* Set the flag to true indicating that twitter credentials are set in cURL */
  1255. m_curlLoginParamsSet = true;
  1256. }
  1257. }
  1258. /*++
  1259. * @method: twitCurl::prepareStandardParams
  1260. *
  1261. * @description: method to set standard params into cURL. this is an internal method.
  1262. * twitcurl users should not use this method.
  1263. *
  1264. * @input: none
  1265. *
  1266. * @output: none
  1267. *
  1268. * @remarks: internal method
  1269. *
  1270. *--*/
  1271. void twitCurl::prepareStandardParams()
  1272. {
  1273. /* Restore any custom request we may have */
  1274. curl_easy_setopt( m_curlHandle, CURLOPT_CUSTOMREQUEST, NULL );
  1275. /* Clear callback and error buffers */
  1276. clearCurlCallbackBuffers();
  1277. /* Prepare proxy */
  1278. prepareCurlProxy();
  1279. /* Prepare cURL callback data and error buffer */
  1280. prepareCurlCallback();
  1281. /* Prepare username and password for twitter */
  1282. prepareCurlUserPass();
  1283. }
  1284. /*++
  1285. * @method: twitCurl::performGet
  1286. *
  1287. * @description: method to send http GET request. this is an internal method.
  1288. * twitcurl users should not use this method.
  1289. *
  1290. * @input: getUrl - url
  1291. *
  1292. * @output: none
  1293. *
  1294. * @remarks: internal method
  1295. *
  1296. *--*/
  1297. bool twitCurl::performGet( const std::string& getUrl )
  1298. {
  1299. std::string dataStr( "" );
  1300. std::string oAuthHttpHeader( "" );
  1301. struct curl_slist* pOAuthHeaderList = NULL;
  1302. /* Prepare standard params */
  1303. prepareStandardParams();
  1304. /* urlencode data */
  1305. size_t nPos = getUrl.find_first_of( "?" );
  1306. if( std::string::npos != nPos )
  1307. {
  1308. std::string dataKey( "" );
  1309. std::string dataValue( "" );
  1310. dataStr = getUrl.substr( nPos + 1 );
  1311. nPos = dataStr.find_first_of( "=" );
  1312. if( std::string::npos != nPos )
  1313. {
  1314. dataKey = dataStr.substr( 0, nPos );
  1315. dataValue = dataStr.substr( nPos + 1 );
  1316. dataValue = urlencode( dataValue );
  1317. dataStr.assign( dataKey );
  1318. dataStr.append( "=" );
  1319. dataStr.append( dataValue );
  1320. }
  1321. }
  1322. /* Set OAuth header */
  1323. m_oAuth.getOAuthHeader( eOAuthHttpGet, getUrl, dataStr, oAuthHttpHeader );
  1324. if( oAuthHttpHeader.length() > 0 )
  1325. {
  1326. pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() );
  1327. if( pOAuthHeaderList )
  1328. {
  1329. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList );
  1330. }
  1331. }
  1332. /* Set http request and url */
  1333. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPGET, 1 );
  1334. curl_easy_setopt( m_curlHandle, CURLOPT_URL, getUrl.c_str() );
  1335. /* Send http request */
  1336. if( CURLE_OK == curl_easy_perform( m_curlHandle ) )
  1337. {
  1338. if( pOAuthHeaderList )
  1339. {
  1340. curl_slist_free_all( pOAuthHeaderList );
  1341. }
  1342. return true;
  1343. }
  1344. if( pOAuthHeaderList )
  1345. {
  1346. curl_slist_free_all( pOAuthHeaderList );
  1347. }
  1348. return false;
  1349. }
  1350. /*++
  1351. * @method: twitCurl::performGet
  1352. *
  1353. * @description: method to send http GET request. this is an internal method.
  1354. * twitcurl users should not use this method.
  1355. *
  1356. * @input: const std::string& getUrl, const std::string& oAuthHttpHeader
  1357. *
  1358. * @output: none
  1359. *
  1360. * @remarks: internal method
  1361. *
  1362. *--*/
  1363. bool twitCurl::performGet( const std::string& getUrl, const std::string& oAuthHttpHeader )
  1364. {
  1365. struct curl_slist* pOAuthHeaderList = NULL;
  1366. /* Prepare standard params */
  1367. prepareStandardParams();
  1368. /* Set http request and url */
  1369. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPGET, 1 );
  1370. curl_easy_setopt( m_curlHandle, CURLOPT_URL, getUrl.c_str() );
  1371. /* Set header */
  1372. if( oAuthHttpHeader.length() > 0 )
  1373. {
  1374. pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() );
  1375. if( pOAuthHeaderList )
  1376. {
  1377. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList );
  1378. }
  1379. }
  1380. /* Send http request */
  1381. if( CURLE_OK == curl_easy_perform( m_curlHandle ) )
  1382. {
  1383. if( pOAuthHeaderList )
  1384. {
  1385. curl_slist_free_all( pOAuthHeaderList );
  1386. }
  1387. return true;
  1388. }
  1389. if( pOAuthHeaderList )
  1390. {
  1391. curl_slist_free_all( pOAuthHeaderList );
  1392. }
  1393. return false;
  1394. }
  1395. /*++
  1396. * @method: twitCurl::performDelete
  1397. *
  1398. * @description: method to send http DELETE request. this is an internal method.
  1399. * twitcurl users should not use this method.
  1400. *
  1401. * @input: deleteUrl - url
  1402. *
  1403. * @output: none
  1404. *
  1405. * @remarks: internal method
  1406. *
  1407. *--*/
  1408. bool twitCurl::performDelete( const std::string& deleteUrl )
  1409. {
  1410. std::string dataStr( "" );
  1411. std::string oAuthHttpHeader( "" );
  1412. struct curl_slist* pOAuthHeaderList = NULL;
  1413. /* Prepare standard params */
  1414. prepareStandardParams();
  1415. /* urlencode data */
  1416. size_t nPos = deleteUrl.find_first_of( "?" );
  1417. if( std::string::npos != nPos )
  1418. {
  1419. std::string dataKey( "" );
  1420. std::string dataValue( "" );
  1421. dataStr = deleteUrl.substr( nPos + 1 );
  1422. nPos = dataStr.find_first_of( "=" );
  1423. if( std::string::npos != nPos )
  1424. {
  1425. dataKey = dataStr.substr( 0, nPos );
  1426. dataValue = dataStr.substr( nPos + 1 );
  1427. dataValue = urlencode( dataValue );
  1428. dataStr.assign( dataKey );
  1429. dataStr.append( "=" );
  1430. dataStr.append( dataValue );
  1431. }
  1432. }
  1433. /* Set OAuth header */
  1434. m_oAuth.getOAuthHeader( eOAuthHttpDelete, deleteUrl, dataStr, oAuthHttpHeader );
  1435. if( oAuthHttpHeader.length() > 0 )
  1436. {
  1437. pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() );
  1438. if( pOAuthHeaderList )
  1439. {
  1440. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList );
  1441. }
  1442. }
  1443. /* Set http request and url */
  1444. curl_easy_setopt( m_curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE" );
  1445. curl_easy_setopt( m_curlHandle, CURLOPT_URL, deleteUrl.c_str() );
  1446. /* Send http request */
  1447. if( CURLE_OK == curl_easy_perform( m_curlHandle ) )
  1448. {
  1449. if( pOAuthHeaderList )
  1450. {
  1451. curl_slist_free_all( pOAuthHeaderList );
  1452. }
  1453. return true;
  1454. }
  1455. if( pOAuthHeaderList )
  1456. {
  1457. curl_slist_free_all( pOAuthHeaderList );
  1458. }
  1459. return false;
  1460. }
  1461. /*++
  1462. * @method: twitCurl::performPost
  1463. *
  1464. * @description: method to send http POST request. this is an internal method.
  1465. * twitcurl users should not use this method.
  1466. *
  1467. * @input: postUrl - url,
  1468. * dataStr - data to be posted
  1469. *
  1470. * @output: none
  1471. *
  1472. * @remarks: internal method
  1473. *
  1474. *--*/
  1475. bool twitCurl::performPost( const std::string& postUrl, std::string dataStr )
  1476. {
  1477. std::string oAuthHttpHeader( "" );
  1478. struct curl_slist* pOAuthHeaderList = NULL;
  1479. /* Prepare standard params */
  1480. prepareStandardParams();
  1481. /* urlencode data */
  1482. if( dataStr.length() > 0 )
  1483. {
  1484. /* Data should already be urlencoded once */
  1485. std::string dataKey( "" );
  1486. std::string dataValue( "" );
  1487. size_t nPos = dataStr.find_first_of( "=" );
  1488. if( std::string::npos != nPos )
  1489. {
  1490. dataKey = dataStr.substr( 0, nPos );
  1491. dataValue = dataStr.substr( nPos + 1 );
  1492. dataValue = urlencode( dataValue );
  1493. dataStr.assign( dataKey );
  1494. dataStr.append( "=" );
  1495. dataStr.append( dataValue );
  1496. }
  1497. }
  1498. /* Set OAuth header */
  1499. m_oAuth.getOAuthHeader( eOAuthHttpPost, postUrl, dataStr, oAuthHttpHeader );
  1500. if( oAuthHttpHeader.length() > 0 )
  1501. {
  1502. pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() );
  1503. if( pOAuthHeaderList )
  1504. {
  1505. curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList );
  1506. }
  1507. }
  1508. /* Set http request, url and data */
  1509. curl_easy_setopt( m_curlHandle, CURLOPT_POST, 1 );
  1510. curl_easy_setopt( m_curlHandle, CURLOPT_URL, postUrl.c_str() );
  1511. if( dataStr.length() )
  1512. {
  1513. curl_easy_setopt( m_curlHandle, CURLOPT_COPYPOSTFIELDS, dataStr.c_str() );
  1514. }
  1515. /* Send http request */
  1516. if( CURLE_OK == curl_easy_perform( m_curlHandle ) )
  1517. {
  1518. if( pOAuthHeaderList )
  1519. {
  1520. curl_slist_free_all( pOAuthHeaderList );
  1521. }
  1522. return true;
  1523. }
  1524. if( pOAuthHeaderList )
  1525. {
  1526. curl_slist_free_all( pOAuthHeaderList );
  1527. }
  1528. return false;
  1529. }
  1530. /*++
  1531. * @method: utilMakeCurlParams
  1532. *
  1533. * @description: utility function to build parameter strings in the format
  1534. * required by cURL ("param1:param2"). twitcurl users should
  1535. * not use this function.
  1536. *
  1537. * @input: inParam1 - first parameter,
  1538. * inParam2 - second parameter
  1539. *
  1540. * @output: outStr - built parameter
  1541. *
  1542. * @remarks: internal method
  1543. *
  1544. *--*/
  1545. void utilMakeCurlParams( std::string& outStr, std::string& inParam1, std::string& inParam2 )
  1546. {
  1547. outStr = inParam1;
  1548. outStr.append( twitCurlDefaults::TWITCURL_COLON.c_str() );
  1549. outStr.append( inParam2.c_str() );
  1550. }
  1551. /*++
  1552. * @method: utilMakeUrlForUser
  1553. *
  1554. * @description: utility function to build url compatible to twitter. twitcurl
  1555. * users should not use this function.
  1556. *
  1557. * @input: baseUrl - base twitter url,
  1558. * userInfo - user name,
  1559. * isUserId - indicates if userInfo contains a user id or scree name
  1560. *
  1561. * @output: outUrl - built url
  1562. *
  1563. * @remarks: internal method
  1564. *
  1565. *--*/
  1566. void utilMakeUrlForUser( std::string& outUrl, const std::string& baseUrl, std::string& userInfo, bool isUserId )
  1567. {
  1568. /* Copy base URL */
  1569. outUrl = baseUrl;
  1570. if( userInfo.length() )
  1571. {
  1572. /* Append username to the URL */
  1573. if( isUserId )
  1574. {
  1575. outUrl.append( twitCurlDefaults::TWITCURL_USERID.c_str() );
  1576. }
  1577. else
  1578. {
  1579. outUrl.append( twitCurlDefaults::TWITCURL_SCREENNAME.c_str() );
  1580. }
  1581. outUrl.append( userInfo.c_str() );
  1582. }
  1583. }
  1584. /*++
  1585. * @method: twitCurl::getOAuth
  1586. *
  1587. * @description: method to get a reference to oAuth object.
  1588. *
  1589. * @input: none
  1590. *
  1591. * @output: reference to oAuth object
  1592. *
  1593. *--*/
  1594. oAuth& twitCurl::getOAuth()
  1595. {
  1596. return m_oAuth;
  1597. }
  1598. /*++
  1599. * @method: twitCurl::oAuthRequestToken
  1600. *
  1601. * @description: method to get a request token key and secret. this token
  1602. * will be used to get authorize user and get PIN from twitter
  1603. *
  1604. * @input: authorizeUrl is an output parameter. this method will set the url
  1605. * in this string. user should visit this link and get PIN from that page.
  1606. *
  1607. * @output: true if everything went sucessfully, otherwise false
  1608. *
  1609. *--*/
  1610. bool twitCurl::oAuthRequestToken( std::string& authorizeUrl /* out */ )
  1611. {
  1612. bool retVal = false;
  1613. authorizeUrl.assign( "" );
  1614. if( isCurlInit() )
  1615. {
  1616. /* Get OAuth header for request token */
  1617. std::string oAuthHeader( "" );
  1618. if( m_oAuth.getOAuthHeader( eOAuthHttpGet, oAuthTwitterApiUrls::OAUTHLIB_TWITTER_REQUEST_TOKEN_URL, std::string( "" ), oAuthHeader ) )
  1619. {
  1620. if( performGet( oAuthTwitterApiUrls::OAUTHLIB_TWITTER_REQUEST_TOKEN_URL, oAuthHeader ) )
  1621. {
  1622. /* Tell OAuth object to save access token and secret from web response */
  1623. std::string twitterResp( "" );
  1624. getLastWebResponse( twitterResp );
  1625. m_oAuth.extractOAuthTokenKeySecret( twitterResp );
  1626. /* Get access token and secret from OAuth object */
  1627. std::string oAuthTokenKey( "" );
  1628. m_oAuth.getOAuthTokenKey( oAuthTokenKey );
  1629. /* Build authorize url so that user can visit in browser and get PIN */
  1630. authorizeUrl.assign( oAuthTwitterApiUrls::OAUTHLIB_TWITTER_AUTHORIZE_URL.c_str() );
  1631. authorizeUrl.append( oAuthTokenKey.c_str() );
  1632. retVal = true;
  1633. }
  1634. }
  1635. }
  1636. return retVal;
  1637. }
  1638. /*++
  1639. * @method: twitCurl::oAuthAccessToken
  1640. *
  1641. * @description: method to exchange request token with access token
  1642. *
  1643. * @input: none
  1644. *
  1645. * @output: true if everything went sucessfully, otherwise false
  1646. *
  1647. *--*/
  1648. bool twitCurl::oAuthAccessToken()
  1649. {
  1650. bool retVal = false;
  1651. if( isCurlInit() )
  1652. {
  1653. /* Get OAuth header for access token */
  1654. std::string oAuthHeader( "" );
  1655. if( m_oAuth.getOAuthHeader( eOAuthHttpGet, oAuthTwitterApiUrls::OAUTHLIB_TWITTER_ACCESS_TOKEN_URL, std::string( "" ), oAuthHeader, true ) )
  1656. {
  1657. if( performGet( oAuthTwitterApiUrls::OAUTHLIB_TWITTER_ACCESS_TOKEN_URL, oAuthHeader ) )
  1658. {
  1659. /* Tell OAuth object to save access token and secret from web response */
  1660. std::string twitterResp( "" );
  1661. getLastWebResponse( twitterResp );
  1662. m_oAuth.extractOAuthTokenKeySecret( twitterResp );
  1663. retVal = true;
  1664. }
  1665. }
  1666. }
  1667. return retVal;
  1668. }