Browse Source

Initial commit

tags/0.0.1^0
Kim Grytøyr 9 months ago
commit
85b0d82a55
Signed by: Kim Grytøyr <kim@grytoyr.io> GPG Key ID: 759EF8C94E0B45B7

+ 7
- 0
Context.sublime-menu View File

@@ -0,0 +1,7 @@
[
{
"id" : "npasteUpload",
"caption" : "Paste to npaste",
"command" : "npaste_upload"
}
]

+ 6
- 0
Default (Linux).sublime-keymap View File

@@ -0,0 +1,6 @@
[
{
"keys": ["ctrl+alt+p"],
"command": "npaste_upload"
}
]

+ 6
- 0
Default (OSX).sublime-keymap View File

@@ -0,0 +1,6 @@
[
{
"keys": ["super+alt+n"],
"command": "npaste_upload"
}
]

+ 6
- 0
Default (Windows).sublime-keymap View File

@@ -0,0 +1,6 @@
[
{
"keys": ["ctrl+alt+p"],
"command": "npaste_upload"
}
]

+ 7
- 0
dependencies.json View File

@@ -0,0 +1,7 @@
{
"*": {
"*": [
"requests"
]
}
}

+ 57
- 0
npaste-upload.py View File

@@ -0,0 +1,57 @@
import sublime
import sublime_plugin
import os
import uuid
import string
import random
import base64
import requests
from subprocess import *
from requests.auth import HTTPBasicAuth

class NpasteUploadCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings('npaste.sublime-settings')
selection = self.view.sel()
selections = "\n".join(list(map(lambda s: self.view.substr(s), selection)))

if selections == "":
npaste_data = self.view.substr(sublime.Region(0, self.view.size()))
else:
npaste_data = selections

data = ""
passphrase = None
payload = {'age': settings.get('npaste.age') }

# Encrypt paste using GPG
if (settings.get('npaste.encrypt') == True):
passphrase = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(settings.get('npaste.encryption_key_length')))

process = Popen(['gpg', '--armor', '--batch', '--passphrase', passphrase, '--symmetric'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
d = bytes(npaste_data, 'utf-8')
process.stdin.write(base64.b64encode(bytes(npaste_data, 'utf-8')))
paste, errs = process.communicate()

if (not errs):
data = paste.decode("utf-8")
payload["encrypted"] = 1
else:
sublime.message_dialog(errs.decode("utf-8"))
else:
data = npaste_data

if settings.get('npaste.archive') == True:
payload["archive"] = 1

# Upload paste to npaste
files = {'paste': ('paste.txt', data, 'text/plain')}
r = requests.post(settings.get('npaste.url'), files=files, data=payload, auth=HTTPBasicAuth(settings.get('npaste.username'), settings.get('npaste.password')))

url = r.text

if (passphrase is not None):
url = url + "#" + passphrase

sublime.set_clipboard(url)
sublime.message_dialog('Copied to clipboard!')

+ 22
- 0
settings/npaste.sublime-settings View File

@@ -0,0 +1,22 @@
{
// Specifies the URL of the npaste server to use
"npaste.url": "http://localhost:3000/",

// Specifies the npaste API username
"npaste.username": "user",

// Specifies the npaste API password
"npaste.password": "password",

// If true, pastes will be archived instead of deleted when they expire.
"npaste.archive": true,

// Sets the age of pastes (s,m,h,d,y)
"npaste.age": "14d",

// If true, pastes will be encrypted using GPG
"npaste.encrypt": true,

// Encryption key length
"npaste.encryption_key_length": 32
}

Loading…
Cancel
Save