Skip to content

Commit

Permalink
smuxi: Add pastebin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaxl committed Feb 5, 2015
1 parent 4c0753a commit 449bf8d
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 5 deletions.
122 changes: 122 additions & 0 deletions src/Common/BernamyBinPaste.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.IO;
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, @"<a href='//(paste.debian.net/[0-9a-zA-Z]+)'>", 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");
}
}

public void CtrlV(string filePath)
{
if (String.IsNullOrEmpty(filePath))
throw new System.ArgumentNullException("Parameter cannot be null or empty", "filePath");

var message = Session.UploadFile ("http://ctrlv.in//do/upload_from_file.php", "POST", filePath);
var url = ASCIIEncoding.Default.GetString(message);
var match = Regex.Match(url, @"\[url=(http://ctrlv.in/[0-9a-zA-Z]+)\]",RegexOptions.IgnoreCase);
if (match.Success) {
url = match.Groups [1].Value;
GetUrl = url;
} else {
GetUrl = ("Check URL");
}
}
}
}

72 changes: 67 additions & 5 deletions src/Frontend-GNOME/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class Entry : Gtk.TextView

ChatViewManager ChatViewManager;
event EventHandler<EventArgs> Activated;

private Gtk.ComboBox PasteBinDropDown;
private BernamyBinPaste PasteBinProviders;
/*
public StringCollection History {
get {
Expand Down Expand Up @@ -116,6 +117,7 @@ public Entry(ChatViewManager chatViewManager)
ChatViewManager = chatViewManager;
Settings = new EntrySettings();
WrapMode = Gtk.WrapMode.WordChar;
PasteBinProviders = new BernamyBinPaste();

InitSpellCheck();
InitCommandManager();
Expand Down Expand Up @@ -322,6 +324,39 @@ protected virtual void ProcessKey(Gtk.KeyPressEventArgs e)
case Gdk.Key.End:
ChatViewManager.CurrentChatView.ScrollToEnd();
break;
case Gdk.Key.i:
case Gdk.Key.I:
string msg = String.Format(_("You are going To uplaod an Image on http://picpaste.com/."));
Gtk.MessageDialog md = new Gtk.MessageDialog(
Frontend.MainWindow,
Gtk.DialogFlags.Modal,
Gtk.MessageType.Info,
Gtk.ButtonsType.YesNo,
msg);
var res = (Gtk.ResponseType) md.Run();
md.Destroy();
if (res == Gtk.ResponseType.Yes) {
var filechooser = new Gtk.FileChooserDialog("Chose an Image", null,
Gtk.FileChooserAction.Open,
"Cancel", Gtk.ResponseType.Cancel,
"Open", Gtk.ResponseType.Accept);
var filter = new Gtk.FileFilter();
filter.Name = "PNG and JPEG images";
filter.AddMimeType("image/png");
filter.AddPattern("*.png");
filter.AddMimeType("image/jpeg");
filter.AddPattern("*.jpg");
filechooser.AddFilter(filter);

if (filechooser.Run() == (int)Gtk.ResponseType.Accept)
{
PasteBinProviders.CtrlV(filechooser.Filename);
Text = PasteBinProviders.GetUrl;
}

filechooser.Destroy();
}
break;
// anything else we let GTK+ handle
default:
e.RetVal = false;
Expand Down Expand Up @@ -481,20 +516,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) {
Expand Down

0 comments on commit 449bf8d

Please sign in to comment.