-
Notifications
You must be signed in to change notification settings - Fork 0
/
PastebinLib.cs
518 lines (510 loc) · 17.7 KB
/
PastebinLib.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
namespace PastebinLib
{
public class Pastebin
{
// Protected variables
protected string _developer_api_key;
protected string _user_key;
// URL CONSTANTS
protected const string _post_url = "http://pastebin.com/api/api_post.php";
protected const string _login_url = "http://pastebin.com/api/api_login.php";
protected const string _get_raw_url = "http://pastebin.com/raw.php";
// Properties accessible to the public
public string DeveloperApiKey
{
get { return _developer_api_key; }
set { _developer_api_key = value; }
}
public string UserApiKey
{
get { return _user_key; }
set { _user_key = value; }
}
// Gets a raw post
public string GetPost(string key)
{
string url = String.Format("{0}?i={1}", _get_raw_url, key);
string res = MakeGet(url);
if (res.Length == 0) { return "ERROR: Nothing returned."; }
return res;
}
// Makes a new post
public string NewPaste(Hashtable args)
{
string retval;
try
{
// If there's no code to paste throw an error
if (args.ContainsKey("api_paste_code") == false)
{
throw new Exception("Missing api_paste_code");
}
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("api_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
// Default to paste
if (args.ContainsKey("api_option") == false)
{
args.Add("api_option", "paste");
}
retval = MakePost(_post_url, args);
}
catch (Exception ex)
{
retval = String.Format("ERROR: (NewPaste) {0}", ex.Message);
}
return retval;
}
// Delete a Paste
public string DeletePaste(Hashtable args)
{
string retval;
try
{
// If there's no code to paste throw an error
if (args.ContainsKey("api_paste_key") == false)
{
throw new Exception("Missing api_paste_key");
}
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("Missingapi_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
// You do not have to pass an api_user_key but it must be set
if (args.ContainsKey("api_user_key") == false)
{
if (_user_key.Length == 0)
{
throw new Exception("Mising api_user_key");
}
else
{
args.Add("api_user_key", _user_key);
}
}
// Default to delete
if (args.ContainsKey("api_option") == false)
{
args.Add("api_option", "delete");
}
retval = MakePost(_post_url, args);
}
catch (Exception ex)
{
retval = String.Format("ERROR: (DeletePaste) {0}", ex.Message);
}
return retval;
}
// Get User info and settings
public string UserInfo(Hashtable args)
{
string retval;
try
{
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("Missingapi_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
// You do not have to pass an api_user_key but it must be set
if (args.ContainsKey("api_user_key") == false)
{
if (_user_key.Length == 0)
{
throw new Exception("Mising api_user_key");
}
else
{
args.Add("api_user_key", _user_key);
}
}
// Default to delete
if (args.ContainsKey("api_option") == false)
{
args.Add("api_option", "userdetails");
}
retval = MakePost(_post_url, args);
}
catch (Exception ex)
{
retval = String.Format("ERROR: (UserInfo) {0}", ex.Message);
}
return retval;
}
// List Trending Pastes
public string TrendingPastes(Hashtable args)
{
string retval;
try
{
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("Missingapi_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
// Default to trends
if (args.ContainsKey("api_option") == false)
{
args.Add("api_option", "trends");
}
retval = MakePost(_post_url, args);
}
catch (Exception ex)
{
retval = String.Format("ERROR: (TrendingPastes) {0}", ex.Message);
}
return retval;
}
// List User Pastes
public string ListUserPastes(Hashtable args)
{
string retval;
try
{
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("Missingapi_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
// You do not have to pass an api_user_key but it must be set
if (args.ContainsKey("api_user_key") == false)
{
if (_user_key.Length == 0)
{
throw new Exception("Mising api_user_key");
}
else
{
args.Add("api_user_key", _user_key);
}
}
// Make sure maximum api_results_limit is under 1000
if (args.ContainsKey("api_results_limit") == true)
{
int limit = Int16.Parse(args["api_results_limit"].ToString());
if (limit > 1000) { args["api_results_limit"] = "1000"; }
}
// Default to delete
if (args.ContainsKey("api_option") == false)
{
args.Add("api_option", "list");
}
retval = MakePost(_post_url, args);
}
catch (Exception ex)
{
retval = String.Format("ERROR: (ListUserInfo) {0}", ex.Message);
}
return retval;
}
// Get the UserApiKey
public string UserKey(Hashtable args)
{
string retval;
try
{
// Check for userid
if (args.ContainsKey("api_user_name") == false)
{
throw new Exception("No api_user_name");
}
// Check for password
if (args.ContainsKey("api_user_password") == false)
{
throw new Exception("No api_user_password");
}
// You do not have to pass an api_dev_key but it must be set
if (args.ContainsKey("api_dev_key") == false)
{
if (_developer_api_key.Length == 0)
{
throw new Exception("api_dev_key");
}
else
{
args.Add("api_dev_key", _developer_api_key);
}
}
retval = MakePost(_login_url, args);
// Go on and assign the local variable if we get this far
if (retval.Length > 0) { _user_key = retval; }
}
catch (Exception ex)
{
retval = String.Format("ERROR: (UserKey) {0}", ex.Message);
}
return retval;
}
// Compresses a Hashtable into a POST data string
protected string MakeDataString(Hashtable h)
{
ArrayList temp = new ArrayList();
foreach (string k in h.Keys)
{
temp.Add(String.Format("{0}={1}", k, HttpUtility.UrlEncode(h[k].ToString())));
}
return String.Join("&", (string[])temp.ToArray(typeof(string)));
}
// Handles the POST requests
protected string MakePost(string url, Hashtable args)
{
string retval = "";
string data = MakeDataString(args);
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Encoding = Encoding.UTF8;
try
{
byte[] result = wc.UploadData(url, "POST", Encoding.UTF8.GetBytes(data));
retval = Encoding.UTF8.GetString(result);
}
catch (WebException we)
{
retval = String.Format("ERROR: WEB EXCEPTION (MakeGet) {0}", we.Message);
}
catch (Exception ex)
{
retval = String.Format("ERROR: EXCEPTION (MakeGet) {0}", ex.Message);
}
finally
{
wc.Dispose();
}
return retval;
}
// Handles GET requests
protected string MakeGet(string url)
{
string retval = "";
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
try
{
retval = wc.DownloadString(url);
wc.Dispose();
}
catch (WebException we)
{
retval = String.Format("ERROR: WEB EXCEPTION (MakeGet) {0}", we.Message);
}
catch (Exception ex)
{
retval = String.Format("ERROR: EXCEPTION (MakeGet) {0}", ex.Message);
}
finally
{
wc.Dispose();
}
return retval;
}
// CONSTRUCTORS: No arguments
public Pastebin() { }
// With one argument: DeveloperApiKey
public Pastebin(string dak) { _developer_api_key = dak; }
// With two arguments: DeveloperApiKey, UserApiKey
public Pastebin(string dak, string uak) { _developer_api_key = dak; _user_key = uak; }
}
// Pastebin class functions often take Hashtabe arguments.
// PastebinAtgs will accept arguments and then output them as a Hashtable.
public class PastebinArgs
{
// PRIVATE hashtable where values are stored
private Hashtable ht;
// PUBLIC string properties to set and get API arguments
public string api_dev_key
{
get { return GetVal("api_dev_key"); }
set { ht["api_dev_key"] = value; }
}
public string api_option
{
get { return GetVal("api_option"); }
set { ht["api_option"] = value; }
}
public string api_paste_code
{
get { return GetVal("api_paste_code"); }
set { ht["api_paste_code"] = value; }
}
public string api_paste_expire_date
{
get { return GetVal("api_paste_expire_date"); }
set { ht["api_paste_expire_date"] = value; }
}
public string api_paste_format
{
get { return GetVal("api_paste_format"); }
set { ht["api_paste_format"] = value; }
}
public string api_paste_key
{
get { return GetVal("api_paste_key"); }
set { ht["api_paste_key"] = value; }
}
public string api_paste_name
{
get { return GetVal("api_paste_name"); }
set { ht["api_paste_name"] = value; }
}
public string api_paste_private
{
get { return GetVal("api_paste_private"); }
set { ht["api_paste_private"] = value; }
}
public string api_results_limit
{
get { return GetVal("api_results_limit"); }
set { ht["api_results_limit"] = value; }
}
public string api_user_key
{
get { return GetVal("api_user_key"); }
set { ht["api_user_key"] = value; }
}
public string api_user_name
{
get { return GetVal("api_user_name"); }
set { ht["api_user_name"] = value; }
}
public string api_user_password
{
get { return GetVal("api_user_password"); }
set { ht["api_user_password"] = value; }
}
public string paste_key
{
get { return GetVal("paste_key"); }
set { ht["paste_key"] = value; }
}
// Returns the key is it exists
private string GetVal(string key)
{
return ht.ContainsKey(key) ? ht[key].ToString() : "";
}
// Return the hash, a function to match ToString
public Hashtable ToHashtable() { return ht; }
// Display all set keys
public override string ToString()
{
string temp = "";
if (ht.Keys.Count > 0)
{
foreach (string key in ht.Keys.Cast<string>().OrderBy(c => c))
{
temp += String.Format("{0} = {1}\r\n", key, ht[key].ToString());
}
}
else
{
temp = "No keys set.";
}
return temp;
}
// CONSTRUCTOR
public PastebinArgs() { ht = new Hashtable(); }
}
// Pastebin language options
public static class PastebinOptions
{
// PRIVATE CONSTANTS
static private string LANGUAGES = "PastebinLib.Languages.txt";
static private string PRIVACY = "PastebinLib.Privacy.txt";
static private string EXPIRES = "PastebinLib.Expires.txt";
// PUBLIC READ-ONLY PROPERTIES
static public PastebinOption[] Languages { get { return LoadOptions(LANGUAGES); } }
static public PastebinOption[] Privacy { get { return LoadOptions(PRIVACY); } }
static public PastebinOption[] Expires { get { return LoadOptions(EXPIRES); } }
// Fetch the results
static private PastebinOption[] LoadOptions(string choice)
{
string bulk_read = "";
string[] rows_read;
List<PastebinOption> values = new List<PastebinOption>();
// Read the embedded file
Assembly a = Assembly.GetExecutingAssembly();
StreamReader sr = new StreamReader(a.GetManifestResourceStream(choice));
bulk_read = sr.ReadToEnd();
rows_read = bulk_read.Replace("\n", "").Split('\r');
if (rows_read.Length > 0)
{
foreach (string row in rows_read)
{
string[] parts = row.Split('=');
values.Add(new PastebinOption(parts[0], parts[1]));
}
}
values.Sort();
return values.ToArray<PastebinOption>();
}
}
// PastebinOption object for sorting
public class PastebinOption : IComparable<PastebinOption>
{
public string Key;
public string Val;
public int CompareTo(PastebinOption other)
{
if (this.Val == other.Val)
{
return this.Key.CompareTo(other.Key);
}
return this.Val.CompareTo(other.Val);
}
public override string ToString()
{
return String.Format("{0} ({1})", this.Val, this.Key);
}
public PastebinOption(string k, string v)
{
this.Key = k;
this.Val = v;
}
}
}