Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. using agsXMPP;
  10.  
  11.  
  12. namespace GTalk
  13. {
  14.    
  15.     public partial class Form1 : Form
  16.     {
  17.         DateTime last_communication;
  18.         DateTime last_connection_attempt;
  19.         string myemail="blah@gmail.com";
  20.         string mypass = "password";
  21.         string mynick = "MyNick";
  22.         XmppClientConnection xmppCon;
  23.        
  24.        
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.  
  29.             Init();
  30.         }            
  31.  
  32.         private void Init()
  33.         {
  34.             listEvents.Items.Clear();
  35.  
  36.             DoLogin();
  37.         }
  38.  
  39.         void xmppCon_StateChange(object sender, XmppConnectionState state)
  40.         {
  41.             this.Text = state.ToString();
  42.         }
  43.  
  44.  
  45.         void xmppCon_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
  46.         {
  47.             // ignore empty messages (events)
  48.             if (msg.Body == null)
  49.                 return;
  50.  
  51.             if (InvokeRequired)
  52.             {
  53.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  54.                 // We're not in the UI thread, so we need to call BeginInvoke              
  55.                 BeginInvoke(new agsXMPP.protocol.client.MessageHandler(xmppCon_OnMessage), new object[] { sender, msg });
  56.                 return;
  57.             }
  58.  
  59.             switch (msg.Body.ToLower())
  60.             {
  61.                 case "help": sendmsg("commands: clearchat time"); break;
  62.                 case "clearchat": { listEvents.Items.Clear(); Rtxt.Text = ""; } break;
  63.                 case "time": sendmsg(DateTime.Now.ToString()); break;
  64.                 default:
  65.                     othernick.Text = msg.From.Bare.Substring(0, msg.From.Bare.IndexOf("@"));
  66.                     listEvents.Items.Add(String.Format("OnMessage from:{0} type:{1}", msg.From.Bare, msg.Type.ToString()));
  67.                     listEvents.Items.Add(msg.Body);
  68.                     listEvents.SelectedIndex = listEvents.Items.Count - 1;
  69.                     addtxt(msg.Body + Environment.NewLine, othernick.Text);
  70.                     popup();
  71.                     System.Media.SystemSounds.Beep.Play();
  72.                     Console.Beep();
  73.                     break;
  74.             }
  75.             last_communication = DateTime.Now;
  76.         }
  77.  
  78.         void addtxt(string s, string fromnick)
  79.         {
  80.             Random r = new Random();
  81.             Rtxt.Text = Rtxt.Text + "<" + fromnick + "> " + s;
  82.             //Rtxt.Select(Rtxt.Text.Length - 10, 7);
  83.             //Rtxt.SelectionColor = (r.Next(0,1)==0)?(Color.Red):(Color.Blue);
  84.             int nick,nl=0;
  85.             while (true)
  86.             {
  87.                 nick = Rtxt.Text.IndexOf("<" + mynick + ">",nl);
  88.                 nl = Rtxt.Text.IndexOf("<", nick + 2); if (nl == -1) nl = Rtxt.Text.Length;
  89.                 if ((nick != -1) && (nl != -1))
  90.                 {
  91.                     Rtxt.Select(nick, nl - nick);
  92.                     Rtxt.SelectionColor = Color.Blue;
  93.                 }
  94.                 else break;
  95.             }
  96.             nl = 0;
  97.             while (true)
  98.             {
  99.                 nick = Rtxt.Text.IndexOf("<" + othernick.Text + ">", nl);
  100.                 nl = Rtxt.Text.IndexOf("<", nick + 2); if (nl == -1) nl = Rtxt.Text.Length;
  101.                 if ((nick != -1) && (nl != -1))
  102.                 {
  103.                     Rtxt.Select(nick, nl - nick);
  104.                     Rtxt.SelectionColor = Color.Black;
  105.                 }
  106.                 else break;
  107.             }
  108.  
  109.             Rtxt.Select(Rtxt.Text.Length, 0);
  110.             Rtxt.ScrollToCaret();
  111.         }
  112.  
  113.         void xmppCon_OnClose(object sender)
  114.         {
  115.             if (InvokeRequired)
  116.             {
  117.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  118.                 // We're not in the UI thread, so we need to call BeginInvoke              
  119.                 BeginInvoke(new ObjectHandler(xmppCon_OnClose), new object[] { sender });
  120.                 return;
  121.             }
  122.             listEvents.Items.Add("OnClose Connection closed");
  123.             listEvents.SelectedIndex = listEvents.Items.Count -1;
  124.         }
  125.  
  126.         void xmppCon_OnError(object sender, Exception ex)
  127.         {
  128.             if (InvokeRequired)
  129.             {
  130.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  131.                 // We're not in the UI thread, so we need to call BeginInvoke              
  132.                 BeginInvoke(new ErrorHandler(xmppCon_OnError), new object[] { sender, ex });
  133.                 return;
  134.             }
  135.             listEvents.Items.Add("OnError");
  136.             listEvents.SelectedIndex = listEvents.Items.Count -1;
  137.         }
  138.  
  139.         void xmppCon_OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
  140.         {
  141.             if (InvokeRequired)
  142.             {
  143.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  144.                 // We're not in the UI thread, so we need to call BeginInvoke              
  145.                 BeginInvoke(new XmppElementHandler(xmppCon_OnAuthError), new object[] { sender, e });
  146.                 return;
  147.             }
  148.             listEvents.Items.Add("OnAuthError");
  149.             listEvents.SelectedIndex = listEvents.Items.Count -1;
  150.         }
  151.  
  152.         void xmppCon_OnPresence(object sender, agsXMPP.protocol.client.Presence pres)
  153.         {
  154.             if (InvokeRequired)
  155.             {
  156.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  157.                 // We're not in the UI thread, so we need to call BeginInvoke              
  158.                 BeginInvoke(new agsXMPP.protocol.client.PresenceHandler(xmppCon_OnPresence), new object[] { sender, pres });
  159.                 return;
  160.             }
  161.             listEvents.Items.Add(String.Format("Received Presence from:{0} show:{1} status:{2}", pres.From.ToString(), pres.Show.ToString(), pres.Status));
  162.             listEvents.SelectedIndex = listEvents.Items.Count - 1;
  163.             if (pres.From.Bare != myemail)
  164.             {
  165.                 this.Text = "Mobile Chat - with " + pres.From.Bare.Substring(0, pres.From.Bare.IndexOf("@"));
  166.                 othernick.Text = pres.From.Bare.Substring(0, pres.From.Bare.IndexOf("@")); //limits chat to 1 person online
  167.             }
  168.             popup();
  169.         }
  170.  
  171.         void xmppCon_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
  172.         {
  173.             if (InvokeRequired)
  174.             {
  175.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  176.                 // We're not in the UI thread, so we need to call BeginInvoke              
  177.                 BeginInvoke(new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem), new object[] { sender, item });
  178.                 return;
  179.             }
  180.             listEvents.Items.Add(String.Format("Received Contact {0}", item.Jid.Bare));
  181.             listEvents.SelectedIndex = listEvents.Items.Count - 1;
  182.         }
  183.  
  184.         void xmppCon_OnRosterEnd(object sender)
  185.         {
  186.             if (InvokeRequired)
  187.             {
  188.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  189.                 // We're not in the UI thread, so we need to call BeginInvoke              
  190.                 BeginInvoke(new ObjectHandler(xmppCon_OnRosterEnd), new object[] { sender });
  191.                 return;
  192.             }
  193.             listEvents.Items.Add("OnRosterEnd");
  194.             listEvents.SelectedIndex = listEvents.Items.Count - 1;
  195.             this.Text = "Mobile Chat - Online... waiting for other side";
  196.  
  197.             // Send our own presence to teh server, so other epople send us online
  198.             // and the server sends us the presences of our contacts when they are
  199.             // available
  200.             xmppCon.SendMyPresence();
  201.             last_communication = DateTime.Now;
  202.         }
  203.  
  204.         void xmppCon_OnRosterStart(object sender)
  205.         {
  206.             if (InvokeRequired)
  207.             {
  208.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  209.                 // We're not in the UI thread, so we need to call BeginInvoke              
  210.                 BeginInvoke(new ObjectHandler(xmppCon_OnRosterStart), new object[] { sender });
  211.                 return;
  212.             }
  213.             listEvents.Items.Add("OnRosterStart");
  214.             listEvents.SelectedIndex = listEvents.Items.Count - 1;
  215.         }
  216.  
  217.         void xmppCon_OnLogin(object sender)
  218.         {
  219.             if (InvokeRequired)
  220.             {
  221.                 // Windows Forms are not Thread Safe, we need to invoke this :(
  222.                 // We're not in the UI thread, so we need to call BeginInvoke              
  223.                 BeginInvoke(new ObjectHandler(xmppCon_OnLogin), new object[] { sender });
  224.                 return;
  225.             }
  226.             listEvents.Items.Add("OnLogin");
  227.             listEvents.SelectedIndex = listEvents.Items.Count - 1;
  228.         }
  229.  
  230.         private void DoLogin()
  231.         {
  232.         xmppCon = new XmppClientConnection()
  233.  
  234.             // Subscribe to Events
  235.             xmppCon.OnLogin         += new ObjectHandler(xmppCon_OnLogin);
  236.             xmppCon.OnRosterStart   += new ObjectHandler(xmppCon_OnRosterStart);
  237.             xmppCon.OnRosterEnd     += new ObjectHandler(xmppCon_OnRosterEnd);
  238.             xmppCon.OnRosterItem    += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
  239.             xmppCon.OnPresence      += new agsXMPP.protocol.client.PresenceHandler(xmppCon_OnPresence);
  240.             xmppCon.OnAuthError     += new XmppElementHandler(xmppCon_OnAuthError);
  241.             xmppCon.OnError         += new ErrorHandler(xmppCon_OnError);
  242.             xmppCon.OnClose         += new ObjectHandler(xmppCon_OnClose);
  243.             xmppCon.OnMessage       += new agsXMPP.protocol.client.MessageHandler(xmppCon_OnMessage);
  244.  
  245.             Jid jidUser = new Jid(myemail);
  246.  
  247.             xmppCon.Username = jidUser.User;
  248.             xmppCon.Server = jidUser.Server;
  249.             xmppCon.Password = mypass;
  250.             xmppCon.AutoResolveConnectServer = true;
  251.             xmppCon.KeepAlive = true;
  252.             //xmppCon.KeepAliveInterval = 60;  //default 120 seconds
  253.  
  254.             xmppCon.Open();
  255.             last_connection_attempt = DateTime.Now;
  256.         }
  257.  
  258.         private void cmdLogout_Click(object sender, EventArgs e)
  259.         {
  260.             // close the xmpp connection
  261.             xmppCon.Close();
  262.             this.Text = "Mobile Chat - Offline";
  263.         }
  264.  
  265.         private void cmdSend_Click(object sender, EventArgs e)
  266.         {
  267.             // Send a message
  268.             if (othernick.Text != "othernick")
  269.             {
  270.                 sendmsg(txtMessage.Text);
  271.                 addtxt(txtMessage.Text + Environment.NewLine, mynick);
  272.                 txtMessage.Text = "";
  273.             }
  274.         }
  275.  
  276.         private void sendmsg(string message) {
  277.             if (othernick.Text != "othernick")
  278.             {
  279.                 agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
  280.                 msg.Type = agsXMPP.protocol.client.MessageType.chat;
  281.                 msg.To = new Jid(othernick.Text + "@gmail.com");
  282.                 msg.Body = message;
  283.                 xmppCon.Send(msg);
  284.             }
  285.         }
  286.  
  287.         private void txtMessage_KeyUp(object sender, KeyEventArgs e)
  288.         {
  289.             if (e.KeyCode == System.Windows.Forms.Keys.Enter)
  290.             {
  291.                 cmdSend_Click(txtMessage, e);
  292.             }
  293.             //if (e.KeyCode == System.Windows.Forms.Keys.Enter) cmdSend_Click(txtMessage, e);
  294.         }
  295.  
  296.         private void chkOnline_CheckedChanged(object sender, EventArgs e)
  297.         {
  298.             if (chkOnline.Checked) DoLogin(); else cmdLogout_Click(chkOnline,e);
  299.             tmrOnline.Enabled = false;
  300.             tmrOnline.Enabled = !(chkOnline.Checked);
  301.         }
  302.  
  303.         private void Form1_Load(object sender, EventArgs e)
  304.         {
  305.  
  306.         }
  307.  
  308.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  309.         {
  310.             if ((e.CloseReason == CloseReason.UserClosing) && (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToString().IndexOf(".vshost")==-1))
  311.             {
  312.                 e.Cancel = true;
  313.                 this.WindowState = FormWindowState.Minimized;
  314.             }
  315.         }
  316.  
  317.         private void tmrOnline_Tick(object sender, EventArgs e)
  318.         {
  319.             chkOnline.Checked = true;
  320.         }
  321.  
  322.         private void popup()
  323.         {
  324.             this.WindowState = FormWindowState.Normal;
  325.             this.TopMost = true;
  326.             this.TopMost = false;
  327.             //this.Focus();
  328.         }
  329.  
  330.         private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)
  331.         {
  332.             if (e.KeyChar == '\r') e.Handled = true;
  333.         }
  334.  
  335.         private void tmrStayOnline_Tick(object sender, EventArgs e)
  336.         {
  337.             if (chkOnline.Checked)
  338.             {
  339.                 TimeSpan ca = DateTime.Now - last_connection_attempt;
  340.                 int ca_mins = (int)ca.TotalMinutes;
  341.                 if (ca_mins > 1)//been a while since we tried to connect
  342.                 {
  343.                     TimeSpan ts = DateTime.Now - last_communication;
  344.                     int lastcomm_mins = (int)ts.TotalMinutes;
  345.                     if (lastcomm_mins ==59)
  346.                         xmppCon.RequestRoster();
  347.                     else if (lastcomm_mins > 60)
  348.                         chkOnline.Checked = false;
  349.                 }
  350.             }
  351.         }
  352.  
  353.  
  354.     }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement