|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Drawing; |
| 4 | +using System.Drawing.Imaging; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Cryptography; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace WhatsAppApi |
| 10 | +{ |
| 11 | + public class ApiBase |
| 12 | + { |
| 13 | + public enum CONNECTION_STATUS |
| 14 | + { |
| 15 | + UNAUTHORIZED, |
| 16 | + DISCONNECTED, |
| 17 | + CONNECTED, |
| 18 | + LOGGEDIN |
| 19 | + } |
| 20 | + |
| 21 | + public enum ImageType |
| 22 | + { |
| 23 | + JPEG, |
| 24 | + GIF, |
| 25 | + PNG |
| 26 | + } |
| 27 | + |
| 28 | + public enum VideoType |
| 29 | + { |
| 30 | + MOV, |
| 31 | + AVI, |
| 32 | + MP4 |
| 33 | + } |
| 34 | + |
| 35 | + public enum AudioType |
| 36 | + { |
| 37 | + WAV, |
| 38 | + OGG, |
| 39 | + MP3 |
| 40 | + } |
| 41 | + |
| 42 | + public enum VisibilityCategory |
| 43 | + { |
| 44 | + ProfilePhoto, |
| 45 | + Status, |
| 46 | + LastSeenTime |
| 47 | + } |
| 48 | + |
| 49 | + public enum VisibilitySetting |
| 50 | + { |
| 51 | + None, |
| 52 | + Contacts, |
| 53 | + Everyone |
| 54 | + } |
| 55 | + |
| 56 | + public enum SyncMode |
| 57 | + { |
| 58 | + Full, |
| 59 | + Delta, |
| 60 | + Query, |
| 61 | + Chunked |
| 62 | + } |
| 63 | + |
| 64 | + public enum SyncContext |
| 65 | + { |
| 66 | + Interactive, |
| 67 | + Background, |
| 68 | + Registration |
| 69 | + } |
| 70 | + |
| 71 | + protected string privacySettingToString(VisibilitySetting s) |
| 72 | + { |
| 73 | + switch (s) |
| 74 | + { |
| 75 | + case VisibilitySetting.None: |
| 76 | + return "none"; |
| 77 | + case VisibilitySetting.Contacts: |
| 78 | + return "contacts"; |
| 79 | + case VisibilitySetting.Everyone: |
| 80 | + return "all"; |
| 81 | + default: |
| 82 | + throw new Exception("Invalid visibility setting"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + protected string privacyCategoryToString(VisibilityCategory c) |
| 87 | + { |
| 88 | + switch (c) |
| 89 | + { |
| 90 | + case VisibilityCategory.LastSeenTime: |
| 91 | + return "last"; |
| 92 | + case VisibilityCategory.Status: |
| 93 | + return "status"; |
| 94 | + case VisibilityCategory.ProfilePhoto: |
| 95 | + return "profile"; |
| 96 | + default: |
| 97 | + throw new Exception("Invalid privacy category"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + protected VisibilityCategory parsePrivacyCategory(string data) |
| 102 | + { |
| 103 | + switch (data) |
| 104 | + { |
| 105 | + case "last": |
| 106 | + return VisibilityCategory.LastSeenTime; |
| 107 | + case "status": |
| 108 | + return VisibilityCategory.Status; |
| 109 | + case "profile": |
| 110 | + return VisibilityCategory.ProfilePhoto; |
| 111 | + default: |
| 112 | + throw new Exception(String.Format("Could not parse {0} as privacy category", data)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + protected VisibilitySetting parsePrivacySetting(string data) |
| 117 | + { |
| 118 | + switch (data) |
| 119 | + { |
| 120 | + case "none": |
| 121 | + return VisibilitySetting.None; |
| 122 | + case "contacts": |
| 123 | + return VisibilitySetting.Contacts; |
| 124 | + case "all": |
| 125 | + return VisibilitySetting.Everyone; |
| 126 | + default: |
| 127 | + throw new Exception(string.Format("Cound not parse {0} as privacy setting", data)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected byte[] CreateThumbnail(byte[] imageData) |
| 132 | + { |
| 133 | + Image image; |
| 134 | + using (System.IO.MemoryStream m = new System.IO.MemoryStream(imageData)) |
| 135 | + { |
| 136 | + image = Image.FromStream(m); |
| 137 | + } |
| 138 | + if (image != null) |
| 139 | + { |
| 140 | + int newHeight = 0; |
| 141 | + int newWidth = 0; |
| 142 | + float imgWidth = float.Parse(image.Width.ToString()); |
| 143 | + float imgHeight = float.Parse(image.Height.ToString()); |
| 144 | + if (image.Width > image.Height) |
| 145 | + { |
| 146 | + newHeight = (int)((imgHeight / imgWidth) * 100); |
| 147 | + newWidth = 100; |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + newWidth = (int)((imgWidth / imgHeight) * 100); |
| 152 | + newHeight = 100; |
| 153 | + } |
| 154 | + |
| 155 | + Bitmap newImage = new Bitmap(newWidth, newHeight); |
| 156 | + using (Graphics gr = Graphics.FromImage(newImage)) |
| 157 | + { |
| 158 | + gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
| 159 | + gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
| 160 | + gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; |
| 161 | + gr.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight)); |
| 162 | + } |
| 163 | + System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
| 164 | + newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); |
| 165 | + ms.Close(); |
| 166 | + return ms.ToArray(); |
| 167 | + } |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + protected static DateTime GetDateTimeFromTimestamp(string timestamp) |
| 172 | + { |
| 173 | + long data = 0; |
| 174 | + if (long.TryParse(timestamp, out data)) |
| 175 | + { |
| 176 | + return GetDateTimeFromTimestamp(data); |
| 177 | + } |
| 178 | + return DateTime.Now; |
| 179 | + } |
| 180 | + |
| 181 | + protected static DateTime GetDateTimeFromTimestamp(long timestamp) |
| 182 | + { |
| 183 | + DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); |
| 184 | + return UnixEpoch.AddSeconds(timestamp); |
| 185 | + } |
| 186 | + |
| 187 | + protected byte[] ProcessProfilePicture(byte[] bytes) |
| 188 | + { |
| 189 | + Bitmap image; |
| 190 | + using (System.IO.MemoryStream m = new System.IO.MemoryStream(bytes)) |
| 191 | + { |
| 192 | + image = new Bitmap(Image.FromStream(m)); |
| 193 | + } |
| 194 | + if (image != null) |
| 195 | + { |
| 196 | + int size = 640; |
| 197 | + if (size > image.Width) |
| 198 | + size = image.Width; |
| 199 | + if (size > image.Height) |
| 200 | + size = image.Height; |
| 201 | + |
| 202 | + int newHeight = 0; |
| 203 | + int newWidth = 0; |
| 204 | + float imgWidth = float.Parse(image.Width.ToString()); |
| 205 | + float imgHeight = float.Parse(image.Height.ToString()); |
| 206 | + if (image.Width < image.Height) |
| 207 | + { |
| 208 | + newHeight = (int)((imgHeight / imgWidth) * size); |
| 209 | + newWidth = size; |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + newWidth = (int)((imgWidth / imgHeight) * size); |
| 214 | + newHeight = size; |
| 215 | + } |
| 216 | + |
| 217 | + Bitmap newImage = new Bitmap(newWidth, newHeight); |
| 218 | + using (Graphics gr = Graphics.FromImage(newImage)) |
| 219 | + { |
| 220 | + gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
| 221 | + gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
| 222 | + gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; |
| 223 | + gr.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight)); |
| 224 | + } |
| 225 | + |
| 226 | + //crop square |
| 227 | + Bitmap dest = newImage.Clone(new Rectangle( |
| 228 | + new Point(0, 0), |
| 229 | + new Size(size, size) |
| 230 | + ), image.PixelFormat); |
| 231 | + |
| 232 | + System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
| 233 | + |
| 234 | + System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.Quality; |
| 235 | + EncoderParameters encParams = new EncoderParameters(1); |
| 236 | + |
| 237 | + EncoderParameter param = new EncoderParameter(enc, 50L); |
| 238 | + encParams.Param[0] = param; |
| 239 | + dest.Save(ms, GetEncoder(ImageFormat.Jpeg), encParams); |
| 240 | + ms.Close(); |
| 241 | + return ms.ToArray(); |
| 242 | + } |
| 243 | + return bytes; |
| 244 | + } |
| 245 | + |
| 246 | + private static ImageCodecInfo GetEncoder(ImageFormat format) |
| 247 | + { |
| 248 | + |
| 249 | + ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); |
| 250 | + |
| 251 | + foreach (ImageCodecInfo codec in codecs) |
| 252 | + { |
| 253 | + if (codec.FormatID == format.Guid) |
| 254 | + { |
| 255 | + return codec; |
| 256 | + } |
| 257 | + } |
| 258 | + return null; |
| 259 | + } |
| 260 | + |
| 261 | + protected string md5(string pass) |
| 262 | + { |
| 263 | + MD5 md5 = MD5.Create(); |
| 264 | + byte[] dataMd5 = md5.ComputeHash(WhatsApp.SYSEncoding.GetBytes(pass)); |
| 265 | + var sb = new StringBuilder(); |
| 266 | + for (int i = 0; i < dataMd5.Length; i++) |
| 267 | + sb.AppendFormat("{0:x2}", dataMd5[i]); |
| 268 | + return sb.ToString(); |
| 269 | + } |
| 270 | + |
| 271 | + public static string GetJID(string target) |
| 272 | + { |
| 273 | + target = target.TrimStart(new char[] { '+', '0' }); |
| 274 | + if (!target.Contains('@')) |
| 275 | + { |
| 276 | + //check if group message |
| 277 | + if (target.Contains('-')) |
| 278 | + { |
| 279 | + //to group |
| 280 | + target += "@g.us"; |
| 281 | + } |
| 282 | + else |
| 283 | + { |
| 284 | + //to normal user |
| 285 | + target += "@s.whatsapp.net"; |
| 286 | + } |
| 287 | + } |
| 288 | + return target; |
| 289 | + } |
| 290 | + } |
| 291 | +} |
0 commit comments