From be8843f9bb204b721cc4ae1a89298e5664ec8ca2 Mon Sep 17 00:00:00 2001 From: James Axl Date: Wed, 4 Feb 2015 22:45:01 +0000 Subject: [PATCH] smuxi: Add pastebin feature --- src/Common/BernamyBinPaste.cs | 106 ++++++++++++++++++++++++++++++++++ src/Frontend-GNOME/Entry.cs | 39 +++++++++++-- 2 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 src/Common/BernamyBinPaste.cs diff --git a/src/Common/BernamyBinPaste.cs b/src/Common/BernamyBinPaste.cs new file mode 100644 index 000000000..4f51be536 --- /dev/null +++ b/src/Common/BernamyBinPaste.cs @@ -0,0 +1,106 @@ +// This file is part of Smuxi and is licensed under the terms of MIT/X11 +// +// Copyright (c) 2015 jamesaxl +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System.Collections.Specialized; + +namespace Smuxi.Common +{ + public class BernamyBinPaste + { + private readonly WebClient Session = new WebClient(); + + private IWebProxy Proxy { get; set;} + public string GetUrl { get; private set; } + + public BernamyBinPaste() + { + Session.Proxy = Proxy; + } + + public void DebianPast(string content, string nick , string language, string expiry) + { + if (String.IsNullOrEmpty(content)) + throw new ArgumentNullException("Parameter cannot be null or empty", "content"); + if (String.IsNullOrEmpty (nick)) + nick = "Anonymous"; + if (String.IsNullOrEmpty (language)) + language = "-1"; + if (String.IsNullOrEmpty(expiry)) + expiry = "3600"; + + var servicePoint = ServicePointManager.FindServicePoint("http://paste.debian.net/./", Session.Proxy); + servicePoint.Expect100Continue = false; + + var values = new NameValueCollection(); + values["code"] = content; + values["poster"] = nick; + values["lang"] = language; + values["expire"] = expiry; + Session.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); + var message = Session.UploadValues("http://paste.debian.net/./", "POST", values); + var url = ASCIIEncoding.Default.GetString(message); + var match = Regex.Match(url, @"", RegexOptions.IgnoreCase); + if (match.Success) { + url = match.Groups [1].Value; + GetUrl = "http://" + url; + } else { + throw new ArgumentException("Check URL"); + } + } + + public void Fpaste(string content, string nick, string language ,string expiry) { + if (String.IsNullOrEmpty(content)) + throw new System.ArgumentNullException("Parameter cannot be null or empty", "content"); + if (String.IsNullOrEmpty (nick)) + nick = "Anonymous"; + if (String.IsNullOrEmpty (language)) + language = "text"; + if (String.IsNullOrEmpty(expiry)) + expiry = "1800"; + + var servicePoint = ServicePointManager.FindServicePoint("http://fpaste.org/", Session.Proxy); + servicePoint.Expect100Continue = false; + + var values = new NameValueCollection(); + values["paste_data"] = content; + values["paste_user"] = nick; + values["paste_lang"] = language; + values["api_submit"] = "true"; + values["mode"] = "json"; + Session.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); + var message = Session.UploadValues("http://fpaste.org/", "POST", values); + var url = ASCIIEncoding.Default.GetString(message); + var match = Regex.Match(url, @"""id"": ""([0-9a-zA-Z]+)""",RegexOptions.IgnoreCase); + if (match.Success) { + url = match.Groups [1].Value; + GetUrl = "http://fpaste.org/" + url; + } else { + throw new ArgumentException("Check URL"); + } + } + } +} + + diff --git a/src/Frontend-GNOME/Entry.cs b/src/Frontend-GNOME/Entry.cs index 1fdc580d4..5887f0077 100644 --- a/src/Frontend-GNOME/Entry.cs +++ b/src/Frontend-GNOME/Entry.cs @@ -48,7 +48,8 @@ public class Entry : Gtk.TextView ChatViewManager ChatViewManager; event EventHandler Activated; - + private Gtk.ComboBox PasteBinDropDown; + private BernamyBinPaste PasteBinProviders; /* public StringCollection History { get { @@ -116,6 +117,7 @@ public Entry(ChatViewManager chatViewManager) ChatViewManager = chatViewManager; Settings = new EntrySettings(); WrapMode = Gtk.WrapMode.WordChar; + PasteBinProviders = new BernamyBinPaste(); InitSpellCheck(); InitCommandManager(); @@ -481,20 +483,47 @@ private void _OnActivated(object sender, EventArgs e) // seems to be a paste, so let's break it apart string[] msgParts = text.Split(new char[] {'\n'}); if (msgParts.Length > 3) { - string msg = String.Format(_("You are going to paste {0} lines. Do you want to continue?"), + string msg = String.Format(_("Pasting {0} of lines. Do you want to continue?"), msgParts.Length); Gtk.MessageDialog md = new Gtk.MessageDialog( Frontend.MainWindow, Gtk.DialogFlags.Modal, Gtk.MessageType.Warning, - Gtk.ButtonsType.YesNo, + Gtk.ButtonsType.None, msg); - Gtk.ResponseType res = (Gtk.ResponseType)md.Run(); + + PasteBinDropDown = new Gtk.ComboBox(new string[] {"Directly", "DebianPaste", "Fpaste"}); + try { + PasteBinDropDown.Active = (int)Frontend.FrontendConfig["PasteBinDropDown"]; + } catch { + PasteBinDropDown.Active = 0; + } + PasteBinDropDown.Show(); + md.AddActionWidget(PasteBinDropDown, Gtk.ResponseType.None); + + md.AddButton("Yes", 73); + md.AddButton("No", 75); + var res = md.Run(); md.Destroy(); - if (res != Gtk.ResponseType.Yes) { + + if (res != 73) { Text = String.Empty; return; + } else if (res == 73 && PasteBinDropDown.ActiveText.Equals("DebianPaste")) { + PasteBinProviders.DebianPast(text, "Anonymous", "-1", "3600"); + Text = PasteBinProviders.GetUrl; + Frontend.FrontendConfig["PasteBinDropDown"] = 1; + Frontend.FrontendConfig.Save(); + return; + } else if (res == 73 && PasteBinDropDown.ActiveText.Equals("Fpaste")) { + PasteBinProviders.Fpaste(text, "Anonymous", "text", "21600"); + Text = PasteBinProviders.GetUrl; + Frontend.FrontendConfig["PasteBinDropDown"] = 2; + Frontend.FrontendConfig.Save(); + return; } + Frontend.FrontendConfig["PasteBinDropDown"] = 0; + Frontend.FrontendConfig.Save(); } if (Frontend.EngineVersion < new Version(0,8,11)) { foreach (string msg in msgParts) {