PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/miranda/protocols/Yahoo/links.cpp

http://miranda.googlecode.com/
C++ | 184 lines | 124 code | 36 blank | 24 comment | 38 complexity | 10c1effbc101ef0738291c26a123b61b MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, LGPL-2.1
  1. /*
  2. * $Id: services.c 5376 2007-05-07 16:13:18Z gena01 $
  3. *
  4. * myYahoo Miranda Plugin
  5. *
  6. * Authors: Gennady Feldman (aka Gena01)
  7. * Laurent Marechal (aka Peorth)
  8. *
  9. * This code is under GPL and is based on AIM, MSN and Miranda source code.
  10. * I want to thank Robert Rainwater and George Hazan for their code and support
  11. * and for answering some of my questions during development of this plugin.
  12. */
  13. #include "yahoo.h"
  14. #include "m_addcontact.h"
  15. #include "m_message.h"
  16. #include "m_assocmgr.h"
  17. #include "resource.h"
  18. static HANDLE hServiceParseLink;
  19. static int SingleHexToDecimal(TCHAR c)
  20. {
  21. if (c >= '0' && c <= '9') return c - '0';
  22. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  23. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  24. return -1;
  25. }
  26. static void url_decode(TCHAR* str)
  27. {
  28. TCHAR* s = str, *d = str;
  29. while(*s)
  30. {
  31. if (*s == '%')
  32. {
  33. int digit1 = SingleHexToDecimal(s[1]);
  34. if (digit1 != -1)
  35. {
  36. int digit2 = SingleHexToDecimal(s[2]);
  37. if (digit2 != -1)
  38. {
  39. s += 3;
  40. *d++ = (TCHAR)((digit1 << 4) | digit2);
  41. continue;
  42. }
  43. }
  44. }
  45. *d++ = *s++;
  46. }
  47. *d = 0;
  48. }
  49. static char* get_buddy(TCHAR ** arg)
  50. {
  51. TCHAR *buf = *arg;
  52. TCHAR *tok = _tcschr(buf, '&'); /* first token */
  53. if (tok) *tok = 0;
  54. if (!buf[0]) return NULL;
  55. url_decode(buf);
  56. *arg = tok ? tok + 1 : NULL;
  57. return mir_t2a(buf);
  58. }
  59. /*
  60. add user: ymsgr:addfriend?ID
  61. send message: ymsgr:sendim?ID&m=MESSAGE
  62. add chatroom: ymsgr:chat?ROOM
  63. */
  64. static INT_PTR ServiceParseYmsgrLink(WPARAM wParam, LPARAM lParam)
  65. {
  66. TCHAR *arg = (TCHAR*)lParam;
  67. if (arg == NULL) return 1; /* sanity check */
  68. /* skip leading prefix */
  69. arg = _tcschr(arg, ':');
  70. if (arg == NULL) return 1; /* parse failed */
  71. for (++arg; *arg == '/'; ++arg) {}
  72. if (g_instances.getCount() == 0) return 0;
  73. CYahooProto *proto = g_instances[0];
  74. for (int i = 0; i < g_instances.getCount(); ++i)
  75. {
  76. if (g_instances[i]->m_iStatus > ID_STATUS_OFFLINE)
  77. {
  78. proto = g_instances[i];
  79. break;
  80. }
  81. }
  82. if (!proto) return 1;
  83. /* add a contact to the list */
  84. if (!_tcsnicmp(arg, _T("addfriend?"), 10))
  85. {
  86. arg += 10;
  87. char *id = get_buddy(&arg);
  88. if (!id) return 1;
  89. if (proto->getbuddyH(id) == NULL) /* does not yet check if id is current user */
  90. {
  91. ADDCONTACTSTRUCT acs = {0};
  92. PROTOSEARCHRESULT psr = {0};
  93. acs.handleType = HANDLE_SEARCHRESULT;
  94. acs.szProto = proto->m_szModuleName;
  95. acs.psr = &psr;
  96. psr.cbSize = sizeof(PROTOSEARCHRESULT);
  97. psr.id = (TCHAR*)id;
  98. CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
  99. }
  100. mir_free(id);
  101. return 0;
  102. }
  103. /* send a message to a contact */
  104. else if (!_tcsnicmp(arg, _T("sendim?"), 7))
  105. {
  106. arg += 7;
  107. char *id = get_buddy(&arg);
  108. if (!id) return 1;
  109. TCHAR *msg = NULL;
  110. while (arg)
  111. {
  112. if (!_tcsnicmp(arg, _T("m="), 2))
  113. {
  114. msg = arg + 2;
  115. url_decode(msg);
  116. break;
  117. }
  118. arg = _tcschr(arg + 1, '&'); /* first token */
  119. if (arg) *arg = 0;
  120. }
  121. HANDLE hContact = proto->add_buddy(id, id, 0, PALF_TEMPORARY); /* ensure contact is on list */
  122. if (hContact)
  123. CallService(MS_MSG_SENDMESSAGET, (WPARAM)hContact, (LPARAM)msg);
  124. mir_free(id);
  125. return 0;
  126. }
  127. /* open a chatroom */
  128. else if (!_tcsnicmp(arg, _T("chat?"), 5))
  129. {
  130. arg += 5;
  131. // char *id = get_buddy(&arg);
  132. // if (!id) return 1;
  133. /* not yet implemented (rm contains name of chatroom)*/
  134. return 0;
  135. }
  136. return 1; /* parse failed */
  137. }
  138. void YmsgrLinksInit(void)
  139. {
  140. static const char szService[] = "YAHOO/ParseYmsgrLink";
  141. hServiceParseLink = CreateServiceFunction(szService, ServiceParseYmsgrLink);
  142. AssocMgr_AddNewUrlTypeT("ymsgr:", TranslateT("YAHOO Link Protocol"), hInstance, IDI_YAHOO, szService, 0);
  143. }
  144. void YmsgrLinksUninit(void)
  145. {
  146. DestroyServiceFunction(hServiceParseLink);
  147. CallService(MS_ASSOCMGR_REMOVEURLTYPE, 0, (LPARAM)"ymsgr:");
  148. }