Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using System.Net;
  9. using SteamKit2;
  10. using System.IO;
  11. using System.Threading;
  12.  
  13. namespace _1703_Bot
  14. {
  15.     class Program
  16.     {
  17.         static bool is_running = false;
  18.         static string user_name, pass;
  19.         static string auth_code;
  20.  
  21.         static SteamClient steam_client;
  22.         static CallbackManager callback_manager;
  23.         static SteamUser steam_user;
  24.         static SteamFriends steam_friends;
  25.  
  26.         static void Main(string[] args)
  27.         {
  28.             if (!File.Exists("chat.txt"))
  29.             {
  30.                 File.Create("chat.txt").Close();
  31.                 File.WriteAllText("chat.txt", "Hello | <MSG> Hello. Use '!help' to view commands.");
  32.             }
  33.             if (!File.Exists("admin.txt"))
  34.             {
  35.                 File.Create("admin.txt").Close();
  36.                 File.WriteAllText("admin.txt", "76561198053054477");
  37.             }
  38.  
  39.             Console.Title = "1703 Bot Console";
  40.             Console.Write("Username: ");
  41.             user_name = Console.ReadLine();
  42.             Console.Write("Password: ");
  43.             pass = Console.ReadLine();
  44.  
  45.             SteamLogIn();
  46.         }
  47.  
  48.         static void SteamLogIn()
  49.         {
  50.             steam_client = new SteamClient();
  51.             callback_manager = new CallbackManager(steam_client);
  52.             steam_user = steam_client.GetHandler<SteamUser>();
  53.             steam_friends = steam_client.GetHandler<SteamFriends>();
  54.  
  55.             callback_manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
  56.             callback_manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
  57.             callback_manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
  58.             callback_manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
  59.             callback_manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
  60.             callback_manager.Subscribe<SteamFriends.FriendMsgCallback>(OnChatMessage);
  61.             callback_manager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
  62.             callback_manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
  63.  
  64.             is_running = true;
  65.  
  66.             Console.Write("Connecting to Steam...");
  67.             steam_client.Connect();
  68.  
  69.             while (is_running)
  70.             {
  71.                 callback_manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  72.             }
  73.             Console.ReadKey();
  74.         }
  75.  
  76.         static void OnConnected(SteamClient.ConnectedCallback callback)
  77.         {
  78.             if (callback.Result != EResult.OK)
  79.             {
  80.                 Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
  81.                 is_running = false;
  82.                 return;
  83.             }
  84.             Console.WriteLine("Connected to Steam! Logging in '{0}'", callback.Result);
  85.  
  86.             byte[] sentry_hash = null;
  87.             if (File.Exists("sentry.bin"))
  88.             {
  89.                 byte[] sentry_file = File.ReadAllBytes("sentry.bin");
  90.                 sentry_hash = CryptoHelper.SHAHash(sentry_file);
  91.             }
  92.  
  93.             steam_user.LogOn(new SteamUser.LogOnDetails
  94.             {
  95.                 Username = user_name,
  96.                 Password = pass,
  97.                 AuthCode = auth_code,
  98.                 SentryFileHash = sentry_hash,
  99.             });
  100.         }
  101.  
  102.         static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  103.         {
  104.             if (callback.Result == EResult.AccountLogonDenied)
  105.             {
  106.                 Console.WriteLine("This account is SteamGuard protected.");
  107.                 Console.WriteLine("Please enter the authorization code sent ot the email at {0}: ", callback.EmailDomain);
  108.                 auth_code = Console.ReadLine();
  109.                 return;
  110.             }
  111.  
  112.             if (callback.Result != EResult.OK)
  113.             {
  114.                 Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  115.                 is_running = false;
  116.                 return;
  117.             }
  118.             Console.WriteLine("{0} Successfully Logged in to Steam!", user_name);
  119.         }
  120.  
  121.         static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
  122.         {
  123.             Console.WriteLine("Updating Sentryfile...");
  124.             byte[] sentry_hash = CryptoHelper.SHAHash(callback.Data);
  125.             File.WriteAllBytes("sentry.bin", callback.Data);
  126.             steam_user.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  127.             {
  128.                 JobID = callback.JobID,
  129.                 FileName = callback.FileName,
  130.                 BytesWritten = callback.BytesToWrite,
  131.                 FileSize = callback.Data.Length,
  132.                 Offset = callback.Offset,
  133.                 Result = EResult.OK,
  134.                 LastError = 0,
  135.                 OneTimePassword = callback.OneTimePassword,
  136.                 SentryFileHash = sentry_hash,
  137.             });
  138.             Console.WriteLine("Done!");
  139.         }
  140.  
  141.         static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  142.         {
  143.             Console.WriteLine("\n{0} Disconnected from Steam, reconnecting...\n", user_name);
  144.             Thread.Sleep(TimeSpan.FromSeconds(5));
  145.             steam_client.Connect();
  146.         }
  147.  
  148.         static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  149.         {
  150.             Console.WriteLine("Logged off of Steam: {0}", callback.Result);
  151.         }
  152.  
  153.         static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  154.         {
  155.             steam_friends.SetPersonaState(EPersonaState.Online);
  156.         }
  157.  
  158.         static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  159.         {
  160.             Thread.Sleep(2500);
  161.             foreach (var friend in callback.FriendList)
  162.             {
  163.                 if (friend.Relationship == EFriendRelationship.RequestRecipient)
  164.                 {
  165.                     steam_friends.AddFriend(friend.SteamID);
  166.                     Thread.Sleep(500);
  167.                     steam_friends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "<MSG> hello, I am 1703 bot. I will notify you of any 1703 outfit events. Use '!help' to view my commands.");
  168.                 }
  169.             }
  170.         }
  171.  
  172.         static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  173.         {
  174.             string[] args;
  175.             if (callback.EntryType == EChatEntryType.ChatMsg)
  176.             {
  177.                 if (callback.Message.Length > 1)
  178.                 {
  179.                     if (callback.Message.Remove(1) == "!")
  180.                     {
  181.                         string command = callback.Message;
  182.  
  183.                         if (callback.Message.Contains(" "))
  184.                         {
  185.                             command = callback.Message.Remove(callback.Message.IndexOf(' '));
  186.                         }
  187.  
  188.                         switch (command)
  189.                         {
  190.                             #region send
  191.                             case "!send":
  192.                                 if (!isBotAdmin(callback.Sender))
  193.                                     break;
  194.  
  195.                                 args = seperate(2, ' ', callback.Message);
  196.                                 Console.WriteLine("!send '" + args[1] + "' '" + args[2] + "'Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  197.                                 if (args[0] == "-1")
  198.                                 {
  199.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<ERROR> Incorrect Syntax. Use '!send [friend] [message]'.");
  200.                                     return;
  201.                                 }
  202.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  203.                                 {
  204.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  205.                                     if (steam_friends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  206.                                     {
  207.                                         steam_friends.SendChatMessage(friend, EChatEntryType.ChatMsg, "<MSG> " + args[2]);
  208.                                     }
  209.                                 }
  210.                                 break;
  211.                             #endregion
  212.                             #region broadcast
  213.                             case "!broadcast":
  214.                                 if (!isBotAdmin(callback.Sender))
  215.                                     break;
  216.  
  217.                                 args = seperate(1, ' ', callback.Message);
  218.                                 Console.WriteLine("!broadcast '" + args[1] + "' Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  219.                                 if (args[0] == "-1")
  220.                                 {
  221.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<ERROR> Inccorect syntax. Use '!broadcast [message]'.");
  222.                                 }
  223.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  224.                                 {
  225.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  226.                                     if (friend > 0)
  227.                                     {
  228.                                         steam_friends.SendChatMessage(friend, EChatEntryType.ChatMsg, "<BROADCAST> " + args[1]);
  229.                                     }
  230.                                 }
  231.                                 break;
  232.                             #endregion
  233.                             #region friends
  234.                             case "!friends":
  235.                                 Console.WriteLine("!friends Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  236.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of bot friends: ");
  237.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  238.                                 {
  239.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  240.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> Friend: " + steam_friends.GetFriendPersonaName(friend) + " State: " + steam_friends.GetFriendPersonaState(friend));
  241.                                 }
  242.                                 break;
  243.                             #endregion
  244.                             #region help
  245.                             case "!help":
  246.                                 Console.Write("!help Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  247.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of User Commands:");
  248.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!help' : Lists all Available Commands");
  249.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!friends' : Lists all the friends of the 1703 Bot");
  250.                                 if (!isBotAdmin(callback.Sender))
  251.                                 {
  252.                                     break;
  253.                                 }
  254.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of Admin Commands:");
  255.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!broadcast [message]' : Broadcasts a message to all 1703 Bot Friends");
  256.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!send [friend] [message]' : sends a message to specified 1703 Bot Friend");
  257.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!pester [friend] [message] [message ammount]' : sends a message to a specified 1703 Bot Friend a specified number of times");
  258.                                 break;
  259.                             #endregion
  260.                             #region pester
  261.                             //case "!pester":
  262.                             //    int counter = 0;
  263.                             //    if (!isBotAdmin(callback.Sender))
  264.                             //        break;
  265.                             //    args = seperate(3, ' ', callback.Message);
  266.                             //    Console.WriteLine("!pester '" + args[1] + "' '" + args[2] + "'Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  267.                             //    if (args[0] == "-1")
  268.                             //    {
  269.                             //        steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<ERROR> Incorrect Syntax. Use '!pester [friend] [message'.");
  270.                             //        return;
  271.                             //    }
  272.                             //    for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  273.                             //    {
  274.                             //        SteamID friend = steam_friends.GetFriendByIndex(i);
  275.                             //        if (steam_friends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  276.                             //        {
  277.                             //            while (counter < int.Parse(args[3]))
  278.                             //            {
  279.                             //                steam_friends.SendChatMessage(friend, EChatEntryType.ChatMsg, "<MSG> " + args[2]);
  280.                             //                counter++;
  281.                             //            }
  282.                             //        }
  283.                             //    }
  284.                             //    break;
  285.                             #endregion
  286.                             #region online
  287.                                 WebClient web_client = new System.Net.WebClient();
  288.                                 web_client.DownloadFile("http://census.daybreakgames.com/s:17034223270/xml/get/ps2:v2/outfit_member/?outfit_id=37530159341123962&c:limit=100&c:resolve=online_status&c:show=character_id&c:resolve=character_name", "member_list.xml");
  289.                                 XmlDocument doc = new XmlDocument();
  290.                                 doc.Load("member_list.xml");
  291.                                 XmlNodeList member_nodes = doc.GetElementsByTagName("outfit_member");
  292.                                 foreach (XmlNode member_node in member_nodes)
  293.                                 {
  294.                                     XmlAttribute character_id = member_node.Attributes["character_id"];
  295.                                     XmlAttribute online_status = member_node.Attributes["online_status"];
  296.                                     XmlNode character_name_node = member_node["character"];
  297.                                     string character_name_string = character_name_node.InnerXml;
  298.                                     string remove_1 = ("<name first=");
  299.                                     character_name_string = character_name_string.Replace(remove_1, "");
  300.                                     string[] character_name_string_temp = character_name_string.Split(' ');
  301.                                     character_name_string_temp[1] = ("");
  302.                                     character_name_string_temp[2] = ("");
  303.                                     character_name_string = string.Join("", character_name_string_temp);
  304.                                     int online_status_true = System.Convert.ToInt32(online_status.Value);
  305.                                     if (online_status_true > 0)
  306.                                     {
  307.                                         Console.WriteLine("Character_ID: " + character_id.Value);
  308.                                         Console.WriteLine("Character_Name: " + character_name_string);
  309.                                         Console.WriteLine("Online_Status: " + online_status.Value);
  310.                                         Console.WriteLine("");
  311.                                     }
  312.                                 }
  313.                                 break;
  314.                                 #endregion
  315.  
  316.                         }
  317.                     }
  318.                 }
  319.                 string rLine;
  320.                 string trimmed = callback.Message;
  321.                 char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  322.                 for (int i = 0; i < 30; i++)
  323.                 {
  324.                     trimmed = trimmed.Replace(trim[i].ToString(), "");
  325.                 }
  326.  
  327.                 StreamReader stream_reader = new StreamReader("chat.txt");
  328.  
  329.                 while ((rLine = stream_reader.ReadLine()) != null)
  330.                 {
  331.                     string text = rLine.Remove(rLine.IndexOf('|') - 1);
  332.                     string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  333.  
  334.                     if (callback.Message.Contains(text))
  335.                     {
  336.                         Console.WriteLine("Chat reply Sent: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  337.                         steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  338.                         stream_reader.Close();
  339.                         return;
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.  
  345.         public static bool isBotAdmin(SteamID sid)
  346.         {
  347.             string[] lines = null;
  348.             try
  349.             {
  350.                 lines = File.ReadAllLines("admin.txt");
  351.                 foreach (string i in lines)
  352.                 {
  353.                     if (sid.ConvertToUInt64() == Convert.ToUInt64(i))
  354.                     {
  355.                         return true;
  356.                     }
  357.                 }
  358.                 steam_friends.SendChatMessage(sid, EChatEntryType.ChatMsg, "<ERROR> You do not have sufficient permission to use that command. If this is an error, and you are an admin, please contact D3i (Dei) or Infilze (Koolbade)");
  359.                 Console.WriteLine(steam_friends.GetFriendPersonaName(sid) + " attemted to use an administrator command while not an administrator.");
  360.                 return false;
  361.             }
  362.  
  363.                 catch (Exception e)
  364.             {
  365.                 Console.WriteLine(e.Message);
  366.                 return false;
  367.             }
  368.         }
  369.  
  370.         public static string[] seperate(int number, char seperator, string thestring)
  371.         {
  372.             string[] returned = new string[4];
  373.             int i = 0;
  374.             int error = 0;
  375.             int length = thestring.Length;
  376.  
  377.             foreach (char c in thestring)
  378.             {
  379.                 if (i != number)
  380.                 {
  381.                     if (error > length || number > 5)
  382.                     {
  383.                         returned[0] = "-1";
  384.                         return returned;
  385.                     }
  386.                     else if (c == seperator)
  387.                     {
  388.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  389.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  390.                         i++;
  391.                     }
  392.                     error++;
  393.                     if (error == length && i != number)
  394.                     {
  395.                         returned[0] = "-1";
  396.                         return returned;
  397.                     }
  398.                 }
  399.                 else
  400.                 {
  401.                     returned[i] = thestring;
  402.                 }
  403.             }
  404.             return returned;
  405.         }
  406.     }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement