PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ShareX.UploadersLib/TextUploaders/Pastebin.cs

https://gitlab.com/billyprice1/ShareX
C# | 511 lines | 449 code | 41 blank | 21 comment | 16 complexity | 4d8448922bd805db42d5e5f0711dfc5a MD5 | raw file
  1. #region License Information (GPL v3)
  2. /*
  3. ShareX - A program that allows you to take screenshots and share any file type
  4. Copyright (c) 2007-2016 ShareX Team
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. Optionally you can also view the license at <http://www.gnu.org/licenses/>.
  17. */
  18. #endregion License Information (GPL v3)
  19. using ShareX.HelpersLib;
  20. using ShareX.UploadersLib.Properties;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Drawing;
  24. using System.Linq;
  25. using System.Windows.Forms;
  26. namespace ShareX.UploadersLib.TextUploaders
  27. {
  28. public class PastebinTextUploaderService : TextUploaderService
  29. {
  30. public override TextDestination EnumValue { get; } = TextDestination.Pastebin;
  31. public override Icon ServiceIcon => Resources.Pastebin;
  32. public override bool CheckConfig(UploadersConfig config) => true;
  33. public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
  34. {
  35. PastebinSettings settings = config.PastebinSettings;
  36. if (string.IsNullOrEmpty(settings.TextFormat))
  37. {
  38. settings.TextFormat = taskInfo.TextFormat;
  39. }
  40. return new Pastebin(APIKeys.PastebinKey, settings);
  41. }
  42. public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpPastebin;
  43. }
  44. public sealed class Pastebin : TextUploader
  45. {
  46. private string APIKey;
  47. public PastebinSettings Settings { get; private set; }
  48. public Pastebin(string apiKey)
  49. {
  50. APIKey = apiKey;
  51. Settings = new PastebinSettings();
  52. }
  53. public Pastebin(string apiKey, PastebinSettings settings)
  54. {
  55. APIKey = apiKey;
  56. Settings = settings;
  57. }
  58. public bool Login()
  59. {
  60. if (!string.IsNullOrEmpty(Settings.Username) && !string.IsNullOrEmpty(Settings.Password))
  61. {
  62. Dictionary<string, string> loginArgs = new Dictionary<string, string>();
  63. loginArgs.Add("api_dev_key", APIKey);
  64. loginArgs.Add("api_user_name", Settings.Username);
  65. loginArgs.Add("api_user_password", Settings.Password);
  66. string loginResponse = SendRequest(HttpMethod.POST, "http://pastebin.com/api/api_login.php", loginArgs);
  67. if (!string.IsNullOrEmpty(loginResponse) && !loginResponse.StartsWith("Bad API request"))
  68. {
  69. Settings.UserKey = loginResponse;
  70. return true;
  71. }
  72. }
  73. Settings.UserKey = null;
  74. Errors.Add("Pastebin login failed.");
  75. return false;
  76. }
  77. public override UploadResult UploadText(string text, string fileName)
  78. {
  79. UploadResult ur = new UploadResult();
  80. if (!string.IsNullOrEmpty(text) && Settings != null)
  81. {
  82. Dictionary<string, string> args = new Dictionary<string, string>();
  83. args.Add("api_dev_key", APIKey); // which is your unique API Developers Key
  84. args.Add("api_option", "paste"); // set as 'paste', this will indicate you want to create a new paste
  85. args.Add("api_paste_code", text); // this is the text that will be written inside your paste
  86. // Optional args
  87. args.Add("api_paste_name", Settings.Title); // this will be the name / title of your paste
  88. args.Add("api_paste_format", Settings.TextFormat); // this will be the syntax highlighting value
  89. args.Add("api_paste_private", GetPrivacy(Settings.Exposure)); // this makes a paste public or private, public = 0, private = 1
  90. args.Add("api_paste_expire_date", GetExpiration(Settings.Expiration)); // this sets the expiration date of your paste
  91. if (!string.IsNullOrEmpty(Settings.UserKey))
  92. {
  93. args.Add("api_user_key", Settings.UserKey); // this paramater is part of the login system
  94. }
  95. ur.Response = SendRequest(HttpMethod.POST, "http://pastebin.com/api/api_post.php", args);
  96. if (!string.IsNullOrEmpty(ur.Response) && !ur.Response.StartsWith("Bad API request") && ur.Response.IsValidUrl())
  97. {
  98. if (Settings.RawURL)
  99. {
  100. string paste_key = URLHelpers.GetFileName(ur.Response);
  101. ur.URL = "http://pastebin.com/raw/" + paste_key;
  102. }
  103. else
  104. {
  105. ur.URL = ur.Response;
  106. }
  107. }
  108. else
  109. {
  110. Errors.Add(ur.Response);
  111. }
  112. }
  113. return ur;
  114. }
  115. private string GetPrivacy(PastebinPrivacy privacy)
  116. {
  117. switch (privacy)
  118. {
  119. case PastebinPrivacy.Public:
  120. return "0";
  121. default:
  122. case PastebinPrivacy.Unlisted:
  123. return "1";
  124. case PastebinPrivacy.Private:
  125. return "2";
  126. }
  127. }
  128. private string GetExpiration(PastebinExpiration expiration)
  129. {
  130. switch (expiration)
  131. {
  132. default:
  133. case PastebinExpiration.N:
  134. return "N";
  135. case PastebinExpiration.M10:
  136. return "10M";
  137. case PastebinExpiration.H1:
  138. return "1H";
  139. case PastebinExpiration.D1:
  140. return "1D";
  141. case PastebinExpiration.W1:
  142. return "1W";
  143. case PastebinExpiration.W2:
  144. return "2W";
  145. case PastebinExpiration.M1:
  146. return "1M";
  147. }
  148. }
  149. public static List<PastebinSyntaxInfo> GetSyntaxList()
  150. {
  151. string syntaxList = @"4cs = 4CS
  152. 6502acme = 6502 ACME Cross Assembler
  153. 6502kickass = 6502 Kick Assembler
  154. 6502tasm = 6502 TASM/64TASS
  155. abap = ABAP
  156. actionscript = ActionScript
  157. actionscript3 = ActionScript 3
  158. ada = Ada
  159. aimms = AIMMS
  160. algol68 = ALGOL 68
  161. apache = Apache Log
  162. applescript = AppleScript
  163. apt_sources = APT Sources
  164. arm = ARM
  165. asm = ASM (NASM)
  166. asp = ASP
  167. asymptote = Asymptote
  168. autoconf = autoconf
  169. autohotkey = Autohotkey
  170. autoit = AutoIt
  171. avisynth = Avisynth
  172. awk = Awk
  173. bascomavr = BASCOM AVR
  174. bash = Bash
  175. basic4gl = Basic4GL
  176. dos = Batch
  177. bibtex = BibTeX
  178. blitzbasic = Blitz Basic
  179. b3d = Blitz3D
  180. bmx = BlitzMax
  181. bnf = BNF
  182. boo = BOO
  183. bf = BrainFuck
  184. c = C
  185. c_winapi = C (WinAPI)
  186. c_mac = C for Macs
  187. cil = C Intermediate Language
  188. csharp = C#
  189. cpp = C++
  190. cpp-winapi = C++ (WinAPI)
  191. cpp-qt = C++ (with Qt extensions)
  192. c_loadrunner = C: Loadrunner
  193. caddcl = CAD DCL
  194. cadlisp = CAD Lisp
  195. cfdg = CFDG
  196. chaiscript = ChaiScript
  197. chapel = Chapel
  198. clojure = Clojure
  199. klonec = Clone C
  200. klonecpp = Clone C++
  201. cmake = CMake
  202. cobol = COBOL
  203. coffeescript = CoffeeScript
  204. cfm = ColdFusion
  205. css = CSS
  206. cuesheet = Cuesheet
  207. d = D
  208. dart = Dart
  209. dcl = DCL
  210. dcpu16 = DCPU-16
  211. dcs = DCS
  212. delphi = Delphi
  213. oxygene = Delphi Prism (Oxygene)
  214. diff = Diff
  215. div = DIV
  216. dot = DOT
  217. e = E
  218. ezt = Easytrieve
  219. ecmascript = ECMAScript
  220. eiffel = Eiffel
  221. email = Email
  222. epc = EPC
  223. erlang = Erlang
  224. fsharp = F#
  225. falcon = Falcon
  226. fo = FO Language
  227. f1 = Formula One
  228. fortran = Fortran
  229. freebasic = FreeBasic
  230. freeswitch = FreeSWITCH
  231. gambas = GAMBAS
  232. gml = Game Maker
  233. gdb = GDB
  234. genero = Genero
  235. genie = Genie
  236. gettext = GetText
  237. go = Go
  238. groovy = Groovy
  239. gwbasic = GwBasic
  240. haskell = Haskell
  241. haxe = Haxe
  242. hicest = HicEst
  243. hq9plus = HQ9 Plus
  244. html4strict = HTML
  245. html5 = HTML 5
  246. icon = Icon
  247. idl = IDL
  248. ini = INI file
  249. inno = Inno Script
  250. intercal = INTERCAL
  251. io = IO
  252. ispfpanel = ISPF Panel Definition
  253. j = J
  254. java = Java
  255. java5 = Java 5
  256. javascript = JavaScript
  257. jcl = JCL
  258. jquery = jQuery
  259. json = JSON
  260. julia = Julia
  261. kixtart = KiXtart
  262. latex = Latex
  263. ldif = LDIF
  264. lb = Liberty BASIC
  265. lsl2 = Linden Scripting
  266. lisp = Lisp
  267. llvm = LLVM
  268. locobasic = Loco Basic
  269. logtalk = Logtalk
  270. lolcode = LOL Code
  271. lotusformulas = Lotus Formulas
  272. lotusscript = Lotus Script
  273. lscript = LScript
  274. lua = Lua
  275. m68k = M68000 Assembler
  276. magiksf = MagikSF
  277. make = Make
  278. mapbasic = MapBasic
  279. matlab = MatLab
  280. mirc = mIRC
  281. mmix = MIX Assembler
  282. modula2 = Modula 2
  283. modula3 = Modula 3
  284. 68000devpac = Motorola 68000 HiSoft Dev
  285. mpasm = MPASM
  286. mxml = MXML
  287. mysql = MySQL
  288. nagios = Nagios
  289. netrexx = NetRexx
  290. newlisp = newLISP
  291. nginx = Nginx
  292. nimrod = Nimrod
  293. nsis = NullSoft Installer
  294. oberon2 = Oberon 2
  295. objeck = Objeck Programming Langua
  296. objc = Objective C
  297. ocaml-brief = OCalm Brief
  298. ocaml = OCaml
  299. octave = Octave
  300. pf = OpenBSD PACKET FILTER
  301. glsl = OpenGL Shading
  302. oobas = Openoffice BASIC
  303. oracle11 = Oracle 11
  304. oracle8 = Oracle 8
  305. oz = Oz
  306. parasail = ParaSail
  307. parigp = PARI/GP
  308. pascal = Pascal
  309. pawn = Pawn
  310. pcre = PCRE
  311. per = Per
  312. perl = Perl
  313. perl6 = Perl 6
  314. php = PHP
  315. php-brief = PHP Brief
  316. pic16 = Pic 16
  317. pike = Pike
  318. pixelbender = Pixel Bender
  319. plsql = PL/SQL
  320. postgresql = PostgreSQL
  321. postscript = PostScript
  322. povray = POV-Ray
  323. powershell = Power Shell
  324. powerbuilder = PowerBuilder
  325. proftpd = ProFTPd
  326. progress = Progress
  327. prolog = Prolog
  328. properties = Properties
  329. providex = ProvideX
  330. puppet = Puppet
  331. purebasic = PureBasic
  332. pycon = PyCon
  333. python = Python
  334. pys60 = Python for S60
  335. q = q/kdb+
  336. qbasic = QBasic
  337. qml = QML
  338. rsplus = R
  339. racket = Racket
  340. rails = Rails
  341. rbs = RBScript
  342. rebol = REBOL
  343. reg = REG
  344. rexx = Rexx
  345. robots = Robots
  346. rpmspec = RPM Spec
  347. ruby = Ruby
  348. gnuplot = Ruby Gnuplot
  349. rust = Rust
  350. sas = SAS
  351. scala = Scala
  352. scheme = Scheme
  353. scilab = Scilab
  354. scl = SCL
  355. sdlbasic = SdlBasic
  356. smalltalk = Smalltalk
  357. smarty = Smarty
  358. spark = SPARK
  359. sparql = SPARQL
  360. sqf = SQF
  361. sql = SQL
  362. standardml = StandardML
  363. stonescript = StoneScript
  364. sclang = SuperCollider
  365. swift = Swift
  366. systemverilog = SystemVerilog
  367. tsql = T-SQL
  368. tcl = TCL
  369. teraterm = Tera Term
  370. thinbasic = thinBasic
  371. typoscript = TypoScript
  372. unicon = Unicon
  373. uscript = UnrealScript
  374. ups = UPC
  375. urbi = Urbi
  376. vala = Vala
  377. vbnet = VB.NET
  378. vbscript = VBScript
  379. vedit = Vedit
  380. verilog = VeriLog
  381. vhdl = VHDL
  382. vim = VIM
  383. visualprolog = Visual Pro Log
  384. vb = VisualBasic
  385. visualfoxpro = VisualFoxPro
  386. whitespace = WhiteSpace
  387. whois = WHOIS
  388. winbatch = Winbatch
  389. xbasic = XBasic
  390. xml = XML
  391. xorg_conf = Xorg Config
  392. xpp = XPP
  393. yaml = YAML
  394. z80 = Z80 Assembler
  395. zxbasic = ZXBasic";
  396. List<PastebinSyntaxInfo> result = new List<PastebinSyntaxInfo>();
  397. result.Add(new PastebinSyntaxInfo("None", "text"));
  398. foreach (string line in syntaxList.Lines().Select(x => x.Trim()))
  399. {
  400. int index = line.IndexOf('=');
  401. if (index > 0)
  402. {
  403. PastebinSyntaxInfo syntaxInfo = new PastebinSyntaxInfo();
  404. syntaxInfo.Value = line.Remove(index).Trim();
  405. syntaxInfo.Name = line.Substring(index + 1).Trim();
  406. result.Add(syntaxInfo);
  407. }
  408. }
  409. return result;
  410. }
  411. }
  412. public enum PastebinPrivacy
  413. {
  414. [Description("Public")]
  415. Public,
  416. [Description("Unlisted")]
  417. Unlisted,
  418. [Description("Private (members only)")]
  419. Private
  420. }
  421. public enum PastebinExpiration
  422. {
  423. [Description("Never")]
  424. N,
  425. [Description("10 Minutes")]
  426. M10,
  427. [Description("1 Hour")]
  428. H1,
  429. [Description("1 Day")]
  430. D1,
  431. [Description("1 Week")]
  432. W1,
  433. [Description("2 Weeks")]
  434. W2,
  435. [Description("1 Month")]
  436. M1
  437. }
  438. public class PastebinSyntaxInfo
  439. {
  440. public string Name { get; set; }
  441. public string Value { get; set; }
  442. public PastebinSyntaxInfo()
  443. {
  444. }
  445. public PastebinSyntaxInfo(string name, string value)
  446. {
  447. Name = name;
  448. Value = value;
  449. }
  450. public override string ToString()
  451. {
  452. return Name;
  453. }
  454. }
  455. public class PastebinSettings
  456. {
  457. public string Username { get; set; }
  458. public string Password { get; set; }
  459. public PastebinPrivacy Exposure { get; set; } = PastebinPrivacy.Unlisted;
  460. public PastebinExpiration Expiration { get; set; } = PastebinExpiration.N;
  461. public string Title { get; set; }
  462. public string TextFormat { get; set; } = "text";
  463. public string UserKey { get; set; }
  464. public bool RawURL { get; set; }
  465. }
  466. }