Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using agsXMPP;
- namespace GTalk
- {
- public partial class Form1 : Form
- {
- DateTime last_communication;
- DateTime last_connection_attempt;
- string myemail="blah@gmail.com";
- string mypass = "password";
- string mynick = "MyNick";
- XmppClientConnection xmppCon;
- public Form1()
- {
- InitializeComponent();
- Init();
- }
- private void Init()
- {
- listEvents.Items.Clear();
- DoLogin();
- }
- void xmppCon_StateChange(object sender, XmppConnectionState state)
- {
- this.Text = state.ToString();
- }
- void xmppCon_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
- {
- // ignore empty messages (events)
- if (msg.Body == null)
- return;
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new agsXMPP.protocol.client.MessageHandler(xmppCon_OnMessage), new object[] { sender, msg });
- return;
- }
- switch (msg.Body.ToLower())
- {
- case "help": sendmsg("commands: clearchat time"); break;
- case "clearchat": { listEvents.Items.Clear(); Rtxt.Text = ""; } break;
- case "time": sendmsg(DateTime.Now.ToString()); break;
- default:
- othernick.Text = msg.From.Bare.Substring(0, msg.From.Bare.IndexOf("@"));
- listEvents.Items.Add(String.Format("OnMessage from:{0} type:{1}", msg.From.Bare, msg.Type.ToString()));
- listEvents.Items.Add(msg.Body);
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- addtxt(msg.Body + Environment.NewLine, othernick.Text);
- popup();
- System.Media.SystemSounds.Beep.Play();
- Console.Beep();
- break;
- }
- last_communication = DateTime.Now;
- }
- void addtxt(string s, string fromnick)
- {
- Random r = new Random();
- Rtxt.Text = Rtxt.Text + "<" + fromnick + "> " + s;
- //Rtxt.Select(Rtxt.Text.Length - 10, 7);
- //Rtxt.SelectionColor = (r.Next(0,1)==0)?(Color.Red):(Color.Blue);
- int nick,nl=0;
- while (true)
- {
- nick = Rtxt.Text.IndexOf("<" + mynick + ">",nl);
- nl = Rtxt.Text.IndexOf("<", nick + 2); if (nl == -1) nl = Rtxt.Text.Length;
- if ((nick != -1) && (nl != -1))
- {
- Rtxt.Select(nick, nl - nick);
- Rtxt.SelectionColor = Color.Blue;
- }
- else break;
- }
- nl = 0;
- while (true)
- {
- nick = Rtxt.Text.IndexOf("<" + othernick.Text + ">", nl);
- nl = Rtxt.Text.IndexOf("<", nick + 2); if (nl == -1) nl = Rtxt.Text.Length;
- if ((nick != -1) && (nl != -1))
- {
- Rtxt.Select(nick, nl - nick);
- Rtxt.SelectionColor = Color.Black;
- }
- else break;
- }
- Rtxt.Select(Rtxt.Text.Length, 0);
- Rtxt.ScrollToCaret();
- }
- void xmppCon_OnClose(object sender)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new ObjectHandler(xmppCon_OnClose), new object[] { sender });
- return;
- }
- listEvents.Items.Add("OnClose Connection closed");
- listEvents.SelectedIndex = listEvents.Items.Count -1;
- }
- void xmppCon_OnError(object sender, Exception ex)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new ErrorHandler(xmppCon_OnError), new object[] { sender, ex });
- return;
- }
- listEvents.Items.Add("OnError");
- listEvents.SelectedIndex = listEvents.Items.Count -1;
- }
- void xmppCon_OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new XmppElementHandler(xmppCon_OnAuthError), new object[] { sender, e });
- return;
- }
- listEvents.Items.Add("OnAuthError");
- listEvents.SelectedIndex = listEvents.Items.Count -1;
- }
- void xmppCon_OnPresence(object sender, agsXMPP.protocol.client.Presence pres)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new agsXMPP.protocol.client.PresenceHandler(xmppCon_OnPresence), new object[] { sender, pres });
- return;
- }
- listEvents.Items.Add(String.Format("Received Presence from:{0} show:{1} status:{2}", pres.From.ToString(), pres.Show.ToString(), pres.Status));
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- if (pres.From.Bare != myemail)
- {
- this.Text = "Mobile Chat - with " + pres.From.Bare.Substring(0, pres.From.Bare.IndexOf("@"));
- othernick.Text = pres.From.Bare.Substring(0, pres.From.Bare.IndexOf("@")); //limits chat to 1 person online
- }
- popup();
- }
- void xmppCon_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem), new object[] { sender, item });
- return;
- }
- listEvents.Items.Add(String.Format("Received Contact {0}", item.Jid.Bare));
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- }
- void xmppCon_OnRosterEnd(object sender)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new ObjectHandler(xmppCon_OnRosterEnd), new object[] { sender });
- return;
- }
- listEvents.Items.Add("OnRosterEnd");
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- this.Text = "Mobile Chat - Online... waiting for other side";
- // Send our own presence to teh server, so other epople send us online
- // and the server sends us the presences of our contacts when they are
- // available
- xmppCon.SendMyPresence();
- last_communication = DateTime.Now;
- }
- void xmppCon_OnRosterStart(object sender)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new ObjectHandler(xmppCon_OnRosterStart), new object[] { sender });
- return;
- }
- listEvents.Items.Add("OnRosterStart");
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- }
- void xmppCon_OnLogin(object sender)
- {
- if (InvokeRequired)
- {
- // Windows Forms are not Thread Safe, we need to invoke this :(
- // We're not in the UI thread, so we need to call BeginInvoke
- BeginInvoke(new ObjectHandler(xmppCon_OnLogin), new object[] { sender });
- return;
- }
- listEvents.Items.Add("OnLogin");
- listEvents.SelectedIndex = listEvents.Items.Count - 1;
- }
- private void DoLogin()
- {
- xmppCon = new XmppClientConnection()
- // Subscribe to Events
- xmppCon.OnLogin += new ObjectHandler(xmppCon_OnLogin);
- xmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
- xmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
- xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
- xmppCon.OnPresence += new agsXMPP.protocol.client.PresenceHandler(xmppCon_OnPresence);
- xmppCon.OnAuthError += new XmppElementHandler(xmppCon_OnAuthError);
- xmppCon.OnError += new ErrorHandler(xmppCon_OnError);
- xmppCon.OnClose += new ObjectHandler(xmppCon_OnClose);
- xmppCon.OnMessage += new agsXMPP.protocol.client.MessageHandler(xmppCon_OnMessage);
- Jid jidUser = new Jid(myemail);
- xmppCon.Username = jidUser.User;
- xmppCon.Server = jidUser.Server;
- xmppCon.Password = mypass;
- xmppCon.AutoResolveConnectServer = true;
- xmppCon.KeepAlive = true;
- //xmppCon.KeepAliveInterval = 60; //default 120 seconds
- xmppCon.Open();
- last_connection_attempt = DateTime.Now;
- }
- private void cmdLogout_Click(object sender, EventArgs e)
- {
- // close the xmpp connection
- xmppCon.Close();
- this.Text = "Mobile Chat - Offline";
- }
- private void cmdSend_Click(object sender, EventArgs e)
- {
- // Send a message
- if (othernick.Text != "othernick")
- {
- sendmsg(txtMessage.Text);
- addtxt(txtMessage.Text + Environment.NewLine, mynick);
- txtMessage.Text = "";
- }
- }
- private void sendmsg(string message) {
- if (othernick.Text != "othernick")
- {
- agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
- msg.Type = agsXMPP.protocol.client.MessageType.chat;
- msg.To = new Jid(othernick.Text + "@gmail.com");
- msg.Body = message;
- xmppCon.Send(msg);
- }
- }
- private void txtMessage_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == System.Windows.Forms.Keys.Enter)
- {
- cmdSend_Click(txtMessage, e);
- }
- //if (e.KeyCode == System.Windows.Forms.Keys.Enter) cmdSend_Click(txtMessage, e);
- }
- private void chkOnline_CheckedChanged(object sender, EventArgs e)
- {
- if (chkOnline.Checked) DoLogin(); else cmdLogout_Click(chkOnline,e);
- tmrOnline.Enabled = false;
- tmrOnline.Enabled = !(chkOnline.Checked);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if ((e.CloseReason == CloseReason.UserClosing) && (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToString().IndexOf(".vshost")==-1))
- {
- e.Cancel = true;
- this.WindowState = FormWindowState.Minimized;
- }
- }
- private void tmrOnline_Tick(object sender, EventArgs e)
- {
- chkOnline.Checked = true;
- }
- private void popup()
- {
- this.WindowState = FormWindowState.Normal;
- this.TopMost = true;
- this.TopMost = false;
- //this.Focus();
- }
- private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == '\r') e.Handled = true;
- }
- private void tmrStayOnline_Tick(object sender, EventArgs e)
- {
- if (chkOnline.Checked)
- {
- TimeSpan ca = DateTime.Now - last_connection_attempt;
- int ca_mins = (int)ca.TotalMinutes;
- if (ca_mins > 1)//been a while since we tried to connect
- {
- TimeSpan ts = DateTime.Now - last_communication;
- int lastcomm_mins = (int)ts.TotalMinutes;
- if (lastcomm_mins ==59)
- xmppCon.RequestRoster();
- else if (lastcomm_mins > 60)
- chkOnline.Checked = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement