PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/pako/vs/Plugins/Iq/DiscoCB.cs

http://pako.googlecode.com/
C# | 181 lines | 136 code | 28 blank | 17 comment | 25 complexity | 1e968f54cd9d021a2a856a3179c28e45 MD5 | raw file
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * Pako Jabber-bot. Bbodio's Lab. *
  3. * Copyright. All rights reserved Š 2007-2008 by Klichuk Bogdan (Bbodio's Lab) *
  4. * Contact information is here: http://code.google.com/p/pako *
  5. * *
  6. * Pako is under GNU GPL v3 license: *
  7. * YOU CAN SHARE THIS SOFTWARE WITH YOUR FRIEND, MAKE CHANGES, REDISTRIBUTE, *
  8. * CHANGE THE SOFTWARE TO SUIT YOUR NEEDS, THE GNU GENERAL PUBLIC LICENSE IS *
  9. * FREE, COPYLEFT LICENSE FOR SOFTWARE AND OTHER KINDS OF WORKS. *
  10. * *
  11. * Visit http://www.gnu.org/licenses/gpl.html for more information about *
  12. * GNU General Public License v3 license *
  13. * *
  14. * Download source code: http://pako.googlecode.com/svn/trunk *
  15. * See the general information here: *
  16. * http://code.google.com/p/pako. *
  17. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Collections;
  21. using System.Text.RegularExpressions;
  22. using System.Text;
  23. using agsXMPP;
  24. using agsXMPP.protocol.iq.disco;
  25. using agsXMPP.protocol;
  26. using agsXMPP.protocol.client;
  27. using agsXMPP.Xml.Dom;
  28. using Core.Kernel;
  29. namespace Plugin
  30. {
  31. public class DiscoCB
  32. {
  33. Response m_r;
  34. Jid m_jid;
  35. int num;
  36. bool conferences = false;
  37. public DiscoCB(Response r, Jid Jid, int number)
  38. {
  39. conferences = Jid.ToString().StartsWith("conference.");
  40. m_r = r;
  41. m_jid = Jid;
  42. DiscoItemsIq ti = new DiscoItemsIq();
  43. ti.Type = IqType.get;
  44. ti.To = m_jid;
  45. ti.GenerateId();
  46. num = number;
  47. m_r.Connection.IqGrabber.SendIq(ti, new IqCB(DiscoExtractor), null);
  48. }
  49. public int GetCount(string muc_review)
  50. {
  51. Regex reg = new Regex(@"\([0-9]+\)*[^\(]");
  52. MatchCollection mc = reg.Matches(muc_review);
  53. if (mc.Count == 0) return 0;
  54. string str = mc[mc.Count - 1].ToString();
  55. str = str.Substring(1,str.Length - 2);
  56. try {return Convert.ToInt32(str);} catch {return 0;}
  57. }
  58. private void DiscoExtractor(object obj, IQ iq, object arg)
  59. {
  60. DiscoItems d_items = iq.Query as DiscoItems;
  61. string answer;
  62. string jid = m_jid.ToString();
  63. bool muc = m_r.MUC != null;
  64. if (muc)
  65. {
  66. muc = m_jid.Resource != null;
  67. if (muc)
  68. {
  69. muc = m_r.MUC.UserExists(m_jid.Resource);
  70. if (muc)
  71. jid = m_jid.Resource;
  72. }
  73. }
  74. if (d_items != null)
  75. {
  76. if (iq.Type == IqType.error)
  77. {
  78. answer = m_r.f("version_error", jid);
  79. }
  80. else
  81. {
  82. DiscoItem[] items = d_items.GetDiscoItems();
  83. int all = items.Length;
  84. if (items.Length == 0)
  85. {
  86. answer = m_r.f("disco_empty", jid);
  87. }
  88. else
  89. {
  90. if (!conferences)
  91. {
  92. string data = "";
  93. int i = 1;
  94. num = num < 1 ? items.Length : num;
  95. foreach (DiscoItem item in items)
  96. {
  97. if (i <= num)
  98. {
  99. data += "\n" + i.ToString() + ") " + item.Jid;
  100. if (!String.IsNullOrEmpty(item.Name))
  101. data += " [" + item.Name+"]";
  102. }
  103. else
  104. {
  105. break;
  106. }
  107. i++;
  108. }
  109. answer = m_r.f("disco_result") + data + (all != 0 ? "\n-- "+all.ToString()+" --" : "") ;
  110. }else
  111. {
  112. @out.exe("disco_conference");
  113. num = num < 1 ? items.Length : num;
  114. List<object[]> hash = new List<object[]>();
  115. foreach (DiscoItem item in items) {
  116. int users_count = item.Name != null ? GetCount(item.Name) : 0;
  117. hash.Add(new object[]{ users_count, item});
  118. }
  119. @out.exe("disco_conference_ready_to_group");
  120. for (int i = 1; i < hash.Count; i++) {
  121. for (int j = 0; j < i;j++)
  122. { int c1 = ((int)((object[])hash[i])[0]);
  123. int c2 = ((int)((object[])hash[j])[0]);
  124. if (c2 <= c1)
  125. { object[] s = hash[i]; hash[i] = hash[j]; hash[j] = s; }
  126. }
  127. }
  128. @out.exe("disco_conference_grouping_finished");
  129. string _data=""; int l = 0;
  130. foreach (object[] _i in hash)
  131. {
  132. l++; if (l <= num) {
  133. DiscoItem item = ((DiscoItem)(_i[1]));
  134. _data += "\n" + l.ToString() + ") " + item.Jid;
  135. if (!String.IsNullOrEmpty(item.Name))
  136. _data += " [" + item.Name+"]";
  137. } else break;
  138. }
  139. @out.exe("disco_conference_items ready");
  140. answer = m_r.f("disco_result") + _data + (all != 0 ? "\n-- "+all.ToString()+" --" : "") ;
  141. }
  142. }
  143. }
  144. }
  145. else
  146. {
  147. answer = m_r.f("version_error", jid);
  148. }
  149. @out.exe("disco_answering");
  150. m_r.Reply(answer);
  151. }
  152. }
  153. }