Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Configuration;
  11. using agsXMPP;
  12. using agsXMPP.Collections;
  13. using agsXMPP.protocol.client;
  14. using agsXMPP.protocol.x.muc;
  15. using SKYPE4COMLib;
  16.  
  17. namespace TestXMPP
  18. {
  19. public partial class Form1 : Form
  20. {
  21. private XmppClientConnection xmpp = new XmppClientConnection();
  22. private MucManager muc = null;
  23. private Skype skype = new Skype();
  24. private Hashtable room_maps = new Hashtable();
  25.  
  26. /* Grundläggande konstanter */
  27.  
  28. private String xmppname = "»";
  29.  
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. private void Form1_Load(object sender, EventArgs e) {
  36.  
  37. ArrayList chatlist = new ArrayList();
  38. for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
  39. {
  40. String key = ConfigurationManager.AppSettings.GetKey(i);
  41. String value = ConfigurationManager.AppSettings[i];
  42.  
  43. if (value == "true") chatlist.Add(key);
  44. }
  45.  
  46. String[] chatconfigkeys = chatlist.ToArray(typeof(String)) as String[];
  47. for (int i = 0; i < chatconfigkeys.Length; i++)
  48. {
  49. String skypechat = ConfigurationManager.AppSettings[chatconfigkeys[i] + "_skype"];
  50. Jid xmppjid = new Jid(ConfigurationManager.AppSettings[chatconfigkeys[i] + "_jid"]);
  51.  
  52. room_maps.Add(skypechat, xmppjid);
  53. }
  54.  
  55. /*
  56. xmpp.OnReadXml += delegate(object o, String s)
  57. {
  58. Console.WriteLine("XML Recv: " + s);
  59. };
  60.  
  61. xmpp.OnWriteXml += delegate(object o, String s)
  62. {
  63. Console.WriteLine("XML Write: " + s);
  64. };
  65. */
  66.  
  67. xmpp.OnLogin += delegate(object o)
  68. {
  69.  
  70. xmpp.SendMyPresence();
  71. muc = new MucManager(xmpp);
  72.  
  73. foreach (Jid value in room_maps.Values)
  74. {
  75. //muc.AcceptDefaultConfiguration(room);
  76. muc.JoinRoom(value, xmppname, true);
  77.  
  78. agsXMPP.protocol.client.Message message = new agsXMPP.protocol.client.Message(value, MessageType.groupchat , "Hai ppl! //XMPPSkypeBot");
  79. //xmpp.Send(message);
  80. }
  81. };
  82.  
  83.  
  84. xmpp.OnError += delegate(object o, Exception x)
  85. {
  86. Console.WriteLine("EXCEPTION RECEIVED!");
  87. Console.WriteLine(x.Message);
  88.  
  89.  
  90. };
  91.  
  92. xmpp.OnMessage += delegate(object o, agsXMPP.protocol.client.Message ms)
  93. {
  94.  
  95. String body = ms.Body;
  96. //String xmppsender = ms.To.User.ToString();
  97. String xmppsender = ms.From.Resource.ToString();
  98. String chatid = ms.From.Bare.ToString();
  99.  
  100. Console.WriteLine(xmppsender);
  101.  
  102. if (room_maps.ContainsValue(new Jid(chatid)) && !body.StartsWith(skype.CurrentUser.FullName)) //Are we in this chat? + Loop check
  103. {
  104. if (xmppsender != xmppname && !body.StartsWith("!"))
  105. {
  106. String key = FindKey(new Jid(chatid));
  107.  
  108. Console.WriteLine("Mapped to: " + key);
  109. IChat skypechat = skype.get_Chat(key);
  110. if (body.StartsWith("/me ")) {
  111. String mebody = body;
  112. mebody = mebody.Substring(4); //plocka bort /me + första spacet
  113. if (!string.IsNullOrEmpty(mebody)) skypechat.SendMessage("- " + xmppsender + " " + mebody);
  114. }
  115. else skypechat.SendMessage(xmppsender + " | " + body);
  116. }
  117. }
  118. };
  119.  
  120. xmpp.Server = "xmpp.piratpartiet.se";
  121. xmpp.ConnectServer = "xmpp.piratpartiet.se";
  122. xmpp.Username = "xmppskype";
  123. xmpp.Password = "herpderp";
  124.  
  125. xmpp.Open();
  126.  
  127. skype.Attach(7, false); //Protocol version 7, do not wait
  128. skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
  129. skype.UserAuthorizationRequestReceived += new _ISkypeEvents_UserAuthorizationRequestReceivedEventHandler(skype_UserAuthorizationRequestReceived);
  130. }
  131.  
  132. void skype_UserAuthorizationRequestReceived(SKYPE4COMLib.User pUser)
  133. {
  134. pUser.BuddyStatus = TBuddyStatus.budPendingAuthorization;
  135. }
  136.  
  137.  
  138. private void skype_MessageStatus(ChatMessage msg, TChatMessageStatus status)
  139. {
  140. String messagebody = msg.Body;
  141.  
  142. if (status == TChatMessageStatus.cmsReceived && !messagebody.StartsWith("!") && room_maps.ContainsKey(msg.ChatName))
  143. {
  144. //Status = received message + body does not start with ! + chat added to register
  145.  
  146. String name = msg.Sender.FullName;
  147. if (String.IsNullOrEmpty(name)) name = msg.Sender.DisplayName;
  148. if (string.IsNullOrEmpty(name)) name = msg.Sender.Handle;
  149. Jid tmpjid = (Jid)room_maps[msg.ChatName];
  150.  
  151. agsXMPP.protocol.client.Message message = null;
  152.  
  153. if (msg.Type == TChatMessageType.cmeSaid) message = new agsXMPP.protocol.client.Message(tmpjid, MessageType.groupchat, name + " | " + messagebody);
  154. if (msg.Type == TChatMessageType.cmeEmoted) message = new agsXMPP.protocol.client.Message(tmpjid, MessageType.groupchat, "- " + name + " " + messagebody);
  155. xmpp.Send(message);
  156. }
  157. }
  158.  
  159. private void button1_Click(object sender, EventArgs e)
  160. {
  161. String debugmessage = "Connection State: " + xmpp.XmppConnectionState.ToString() + "\n" +
  162. "Authenticated: " + xmpp.Authenticated.ToString() + "\n" +
  163. "My JID: " + xmpp.MyJID.ToString();
  164. MessageBox.Show(debugmessage);
  165. }
  166.  
  167.  
  168. public string FindKey(Jid myValue)
  169. {
  170. string myKey="";
  171.  
  172. foreach (String aKey in room_maps.Keys)
  173. {
  174. if (room_maps[aKey].Equals(myValue)) myKey = aKey;
  175. }
  176.  
  177. return myKey;
  178. }
  179.  
  180.  
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement