Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.Configuration;
- using agsXMPP;
- using agsXMPP.Collections;
- using agsXMPP.protocol.client;
- using agsXMPP.protocol.x.muc;
- using SKYPE4COMLib;
- namespace TestXMPP
- {
- public partial class Form1 : Form
- {
- private XmppClientConnection xmpp = new XmppClientConnection();
- private MucManager muc = null;
- private Skype skype = new Skype();
- private Hashtable room_maps = new Hashtable();
- /* Grundläggande konstanter */
- private String xmppname = "»";
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e) {
- ArrayList chatlist = new ArrayList();
- for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
- {
- String key = ConfigurationManager.AppSettings.GetKey(i);
- String value = ConfigurationManager.AppSettings[i];
- if (value == "true") chatlist.Add(key);
- }
- String[] chatconfigkeys = chatlist.ToArray(typeof(String)) as String[];
- for (int i = 0; i < chatconfigkeys.Length; i++)
- {
- String skypechat = ConfigurationManager.AppSettings[chatconfigkeys[i] + "_skype"];
- Jid xmppjid = new Jid(ConfigurationManager.AppSettings[chatconfigkeys[i] + "_jid"]);
- room_maps.Add(skypechat, xmppjid);
- }
- /*
- xmpp.OnReadXml += delegate(object o, String s)
- {
- Console.WriteLine("XML Recv: " + s);
- };
- xmpp.OnWriteXml += delegate(object o, String s)
- {
- Console.WriteLine("XML Write: " + s);
- };
- */
- xmpp.OnLogin += delegate(object o)
- {
- xmpp.SendMyPresence();
- muc = new MucManager(xmpp);
- foreach (Jid value in room_maps.Values)
- {
- //muc.AcceptDefaultConfiguration(room);
- muc.JoinRoom(value, xmppname, true);
- agsXMPP.protocol.client.Message message = new agsXMPP.protocol.client.Message(value, MessageType.groupchat , "Hai ppl! //XMPPSkypeBot");
- //xmpp.Send(message);
- }
- };
- xmpp.OnError += delegate(object o, Exception x)
- {
- Console.WriteLine("EXCEPTION RECEIVED!");
- Console.WriteLine(x.Message);
- };
- xmpp.OnMessage += delegate(object o, agsXMPP.protocol.client.Message ms)
- {
- String body = ms.Body;
- //String xmppsender = ms.To.User.ToString();
- String xmppsender = ms.From.Resource.ToString();
- String chatid = ms.From.Bare.ToString();
- Console.WriteLine(xmppsender);
- if (room_maps.ContainsValue(new Jid(chatid)) && !body.StartsWith(skype.CurrentUser.FullName)) //Are we in this chat? + Loop check
- {
- if (xmppsender != xmppname && !body.StartsWith("!"))
- {
- String key = FindKey(new Jid(chatid));
- Console.WriteLine("Mapped to: " + key);
- IChat skypechat = skype.get_Chat(key);
- if (body.StartsWith("/me ")) {
- String mebody = body;
- mebody = mebody.Substring(4); //plocka bort /me + första spacet
- if (!string.IsNullOrEmpty(mebody)) skypechat.SendMessage("- " + xmppsender + " " + mebody);
- }
- else skypechat.SendMessage(xmppsender + " | " + body);
- }
- }
- };
- xmpp.Server = "xmpp.piratpartiet.se";
- xmpp.ConnectServer = "xmpp.piratpartiet.se";
- xmpp.Username = "xmppskype";
- xmpp.Password = "herpderp";
- xmpp.Open();
- skype.Attach(7, false); //Protocol version 7, do not wait
- skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
- skype.UserAuthorizationRequestReceived += new _ISkypeEvents_UserAuthorizationRequestReceivedEventHandler(skype_UserAuthorizationRequestReceived);
- }
- void skype_UserAuthorizationRequestReceived(SKYPE4COMLib.User pUser)
- {
- pUser.BuddyStatus = TBuddyStatus.budPendingAuthorization;
- }
- private void skype_MessageStatus(ChatMessage msg, TChatMessageStatus status)
- {
- String messagebody = msg.Body;
- if (status == TChatMessageStatus.cmsReceived && !messagebody.StartsWith("!") && room_maps.ContainsKey(msg.ChatName))
- {
- //Status = received message + body does not start with ! + chat added to register
- String name = msg.Sender.FullName;
- if (String.IsNullOrEmpty(name)) name = msg.Sender.DisplayName;
- if (string.IsNullOrEmpty(name)) name = msg.Sender.Handle;
- Jid tmpjid = (Jid)room_maps[msg.ChatName];
- agsXMPP.protocol.client.Message message = null;
- if (msg.Type == TChatMessageType.cmeSaid) message = new agsXMPP.protocol.client.Message(tmpjid, MessageType.groupchat, name + " | " + messagebody);
- if (msg.Type == TChatMessageType.cmeEmoted) message = new agsXMPP.protocol.client.Message(tmpjid, MessageType.groupchat, "- " + name + " " + messagebody);
- xmpp.Send(message);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- String debugmessage = "Connection State: " + xmpp.XmppConnectionState.ToString() + "\n" +
- "Authenticated: " + xmpp.Authenticated.ToString() + "\n" +
- "My JID: " + xmpp.MyJID.ToString();
- MessageBox.Show(debugmessage);
- }
- public string FindKey(Jid myValue)
- {
- string myKey="";
- foreach (String aKey in room_maps.Keys)
- {
- if (room_maps[aKey].Equals(myValue)) myKey = aKey;
- }
- return myKey;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement