Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.59 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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using WhatsAppApi;
  11. using WhatsAppApi.Account;
  12. using WhatsAppApi.Helper;
  13. using WhatsAppApi.Register;
  14. using WhatsAppApi.Response;
  15. using System.IO;
  16. using System.Net;
  17. using System.Threading;
  18.  
  19. namespace whatsapptest
  20. {
  21. public partial class Form1 : Form
  22. {
  23. Encoding tmpEncoding = Encoding.UTF8;
  24. // System.Console.OutputEncoding = Encoding.Default;
  25. // System.Console.InputEncoding = Encoding.Default;
  26. static string nickname = "WhatsApiNet";
  27. static string sender = "97**********"; // Mobile number with country code (but without + or 00)
  28. static string password = "WF*********=";//v2 password
  29. static string target = "97**********";// Mobile number to send the message to
  30.  
  31. WhatsApp wa = new WhatsApp(sender, password, nickname, true);
  32.  
  33. public Form1()
  34. {
  35. InitializeComponent();
  36.  
  37. wa.OnLoginSuccess += wa_OnLoginSuccess;
  38. wa.OnLoginFailed += wa_OnLoginFailed;
  39. wa.OnGetMessage += wa_OnGetMessage;
  40. wa.OnGetMessageReceivedClient += wa_OnGetMessageReceivedClient;
  41. wa.OnGetMessageReceivedServer += wa_OnGetMessageReceivedServer;
  42. wa.OnNotificationPicture += wa_OnNotificationPicture;
  43. wa.OnGetPresence += wa_OnGetPresence;
  44. wa.OnGetGroupParticipants += wa_OnGetGroupParticipants;
  45. wa.OnGetLastSeen += wa_OnGetLastSeen;
  46. wa.OnGetTyping += wa_OnGetTyping;
  47. wa.OnGetPaused += wa_OnGetPaused;
  48. wa.OnGetMessageImage += wa_OnGetMessageImage;
  49. wa.OnGetMessageAudio += wa_OnGetMessageAudio;
  50. wa.OnGetMessageVideo += wa_OnGetMessageVideo;
  51. wa.OnGetMessageLocation += wa_OnGetMessageLocation;
  52. wa.OnGetMessageVcard += wa_OnGetMessageVcard;
  53. wa.OnGetPhoto += wa_OnGetPhoto;
  54. wa.OnGetPhotoPreview += wa_OnGetPhotoPreview;
  55. wa.OnGetGroups += wa_OnGetGroups;
  56. wa.OnGetSyncResult += wa_OnGetSyncResult;
  57. wa.OnGetStatus += wa_OnGetStatus;
  58. wa.OnGetPrivacySettings += wa_OnGetPrivacySettings;
  59. DebugAdapter.Instance.OnPrintDebug += Instance_OnPrintDebug;
  60.  
  61. wa.Connect();
  62.  
  63. string datFile = getDatFileName(sender);
  64. byte[] nextChallenge = null;
  65. if (File.Exists(datFile))
  66. {
  67. try
  68. {
  69. string foo = File.ReadAllText(datFile);
  70. nextChallenge = Convert.FromBase64String(foo);
  71. }
  72. catch (Exception) { };
  73. }
  74.  
  75. wa.Login(nextChallenge);
  76.  
  77. ProcessChat(wa, target);
  78. }
  79.  
  80. static void Instance_OnPrintDebug(object value)
  81. {
  82. Console.WriteLine(value);
  83. }
  84.  
  85. static void wa_OnGetPrivacySettings(Dictionary<ApiBase.VisibilityCategory, ApiBase.VisibilitySetting> settings)
  86. {
  87. throw new NotImplementedException();
  88. }
  89.  
  90. static void wa_OnGetStatus(string from, string type, string name, string status)
  91. {
  92. Console.WriteLine(String.Format("Got status from {0}: {1}", from, status));
  93. }
  94.  
  95. static string getDatFileName(string pn)
  96. {
  97. string filename = string.Format("{0}.next.dat", pn);
  98. return Path.Combine(Directory.GetCurrentDirectory(), filename);
  99. }
  100.  
  101. static void wa_OnGetSyncResult(int index, string sid, Dictionary<string, string> existingUsers, string[] failedNumbers)
  102. {
  103. Console.WriteLine("Sync result for {0}:", sid);
  104. foreach (KeyValuePair<string, string> item in existingUsers)
  105. {
  106. Console.WriteLine("Existing: {0} (username {1})", item.Key, item.Value);
  107. }
  108. foreach (string item in failedNumbers)
  109. {
  110. Console.WriteLine("Non-Existing: {0}", item);
  111. }
  112. }
  113.  
  114. static void wa_OnGetGroups(WaGroupInfo[] groups)
  115. {
  116. Console.WriteLine("Got groups:");
  117. foreach (WaGroupInfo info in groups)
  118. {
  119. Console.WriteLine("t{0} {1}", info.subject, info.id);
  120. }
  121. }
  122.  
  123. static void wa_OnGetPhotoPreview(string from, string id, byte[] data)
  124. {
  125. Console.WriteLine("Got preview photo for {0}", from);
  126. File.WriteAllBytes(string.Format("preview_{0}.jpg", from), data);
  127. }
  128.  
  129. static void wa_OnGetPhoto(string from, string id, byte[] data)
  130. {
  131. Console.WriteLine("Got full photo for {0}", from);
  132. File.WriteAllBytes(string.Format("{0}.jpg", from), data);
  133. }
  134.  
  135. static void wa_OnGetMessageVcard(ProtocolTreeNode vcardNode, string from, string id, string name, byte[] data)
  136. {
  137. Console.WriteLine("Got vcard "{0}" from {1}", name, from);
  138. File.WriteAllBytes(string.Format("{0}.vcf", name), data);
  139. }
  140.  
  141. static void wa_OnGetMessageLocation(ProtocolTreeNode locationNode, string from, string id, double lon, double lat, string url, string name, byte[] preview, string username)
  142. {
  143. Console.WriteLine("Got location from {0} ({1}, {2})", from, lat, lon);
  144. if (!string.IsNullOrEmpty(name))
  145. {
  146. Console.WriteLine("t{0}", name);
  147. }
  148. File.WriteAllBytes(string.Format("{0}{1}.jpg", lat, lon), preview);
  149. }
  150.  
  151. static void wa_OnGetMessageVideo(ProtocolTreeNode mediaNode, string from, string id, string fileName, int fileSize, string url, byte[] preview, string username)
  152. {
  153. Console.WriteLine("Got video from {0}", from, fileName);
  154. OnGetMedia(fileName, url, preview);
  155. }
  156.  
  157. static void OnGetMedia(string file, string url, byte[] data)
  158. {
  159. //save preview
  160. File.WriteAllBytes(string.Format("preview_{0}.jpg", file), data);
  161. //download
  162. using (WebClient wc = new WebClient())
  163. {
  164. wc.DownloadFileAsync(new Uri(url), file, null);
  165. }
  166. }
  167.  
  168. static void wa_OnGetMessageAudio(ProtocolTreeNode mediaNode, string from, string id, string fileName, int fileSize, string url, byte[] preview, string username)
  169. {
  170. Console.WriteLine("Got audio from {0}", from, fileName);
  171. OnGetMedia(fileName, url, preview);
  172. }
  173.  
  174. static void wa_OnGetMessageImage(ProtocolTreeNode mediaNode, string from, string id, string fileName, int size, string url, byte[] preview, string username)
  175. {
  176. Console.WriteLine("Got image from {0}", from, fileName);
  177. OnGetMedia(fileName, url, preview);
  178. }
  179.  
  180. static void wa_OnGetPaused(string from)
  181. {
  182. Console.WriteLine("{0} stopped typing", from);
  183. }
  184.  
  185. static void wa_OnGetTyping(string from)
  186. {
  187. Console.WriteLine("{0} is typing...", from);
  188. }
  189.  
  190. static void wa_OnGetLastSeen(string from, DateTime lastSeen)
  191. {
  192. Console.WriteLine("{0} last seen on {1}", from, lastSeen.ToString());
  193. }
  194.  
  195. static void wa_OnGetMessageReceivedServer(string from, string participant, string id)
  196. {
  197. Console.WriteLine("Message {0} to {1} received by server", id, from);
  198. }
  199.  
  200. static void wa_OnGetMessageReceivedClient(string from, string participant, string id)
  201. {
  202. Console.WriteLine("Message {0} to {1} received by client", id, from);
  203. }
  204.  
  205. static void wa_OnGetGroupParticipants(string gjid, string[] jids)
  206. {
  207. Console.WriteLine("Got participants from {0}:", gjid);
  208. foreach (string jid in jids)
  209. {
  210. Console.WriteLine("t{0}", jid);
  211. }
  212. }
  213.  
  214. static void wa_OnGetPresence(string from, string type)
  215. {
  216. Console.WriteLine("Presence from {0}: {1}", from, type);
  217. }
  218.  
  219. static void wa_OnNotificationPicture(string type, string jid, string id)
  220. {
  221. //TODO
  222. //throw new NotImplementedException();
  223. }
  224.  
  225. static void wa_OnGetMessage(ProtocolTreeNode node, string from, string id, string name, string message, bool receipt_sent)
  226. {
  227. Console.WriteLine("Message from {0} {1}: {2}", name, from, message);
  228. }
  229.  
  230. private static void wa_OnLoginFailed(string data)
  231. {
  232. Console.WriteLine("Login failed. Reason: {0}", data);
  233. }
  234.  
  235. private static void wa_OnLoginSuccess(string phoneNumber, byte[] data)
  236. {
  237. Console.WriteLine("Login success. Next password:");
  238. string sdata = Convert.ToBase64String(data);
  239. Console.WriteLine(sdata);
  240. try
  241. {
  242. File.WriteAllText(getDatFileName(phoneNumber), sdata);
  243. }
  244. catch (Exception) { }
  245. }
  246.  
  247.  
  248. private static void ProcessChat(WhatsApp wa, string dst)
  249. {
  250. var thRecv = new Thread(t =>
  251. {
  252. try
  253. {
  254. while (wa != null)
  255. {
  256. wa.PollMessages();
  257. Thread.Sleep(100);
  258. continue;
  259. }
  260.  
  261. }
  262. catch (ThreadAbortException)
  263. {
  264. }
  265. }) { IsBackground = true };
  266. thRecv.Start();
  267.  
  268. WhatsUserManager usrMan = new WhatsUserManager();
  269. var tmpUser = usrMan.CreateUser(dst, "User");
  270.  
  271. while (true)
  272. {
  273. string line = "hello world";//Console.ReadLine();
  274. if (line == null && line.Length == 0)
  275. continue;
  276.  
  277. string command = line.Trim();
  278. switch (command)
  279. {
  280. case "/query":
  281. //var dst = dst//trim(strstr($line, ' ', FALSE));
  282. Console.WriteLine("[] Interactive conversation with {0}:", tmpUser);
  283. break;
  284. case "/accountinfo":
  285. Console.WriteLine("[] Account Info: {0}", wa.GetAccountInfo().ToString());
  286. break;
  287. case "/lastseen":
  288. Console.WriteLine("[] Request last seen {0}", tmpUser);
  289. wa.SendQueryLastOnline(tmpUser.GetFullJid());
  290. break;
  291. case "/exit":
  292. wa = null;
  293. thRecv.Abort();
  294. return;
  295. case "/start":
  296. wa.SendComposing(tmpUser.GetFullJid());
  297. break;
  298. case "/pause":
  299. wa.SendPaused(tmpUser.GetFullJid());
  300. break;
  301. default:
  302. Console.WriteLine("[] Send message to {0}: {1}", tmpUser, line);
  303. wa.SendMessage(tmpUser.GetFullJid(), line);
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. }
  310.  
  311. string line = "hello world";//Console.ReadLine();
  312.  
  313. this.buffer.Add((byte)token);
  314.  
  315. throw e;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement