diff --git a/Makefile b/Makefile deleted file mode 100644 index f58b415..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -CC = gcc -CFLAGS = -g -Wall - -LIBS = -lssh - -TARGET = popcorn -BUILDDIR = build - -$(TARGET): $(TARGET).c - $(CC) $(CFLAGS) -o $(BUILDDIR)/$(TARGET) $(TARGET).c $(LIBS) diff --git a/README.md b/README.md index 78956c5..06c0cbb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,4 @@ # popcorn -Simple program for finding and reading remote videos with libssh - -Inspired by the shell script: -```bash -HOST=192.168.1.2 -USER=pi -PATH=$((ssh $USER@$HOST find /var/www/public/media -type f -name "*mp4" | rev | cut -d'/' -f1 | rev | sort) | dmenu -l 30 | xargs -I% ssh $USER@$HOST find /var/www/public/media -name '"%"') - -/usr/bin/vlc "sftp://$USER@$HOST$PATH" -``` +Simple shell program for finding and watching remote videos over SFTP. +Use this if you must, but please prefer 9p. diff --git a/popcorn.c b/popcorn.c deleted file mode 100644 index 2f566ee..0000000 --- a/popcorn.c +++ /dev/null @@ -1,140 +0,0 @@ -#include -#include -#include -#include -#include - -int verify_host(ssh_session session) { - enum ssh_known_hosts_e state; - - state = ssh_session_is_known_server(session); - - // only handling known hosts for now - // will implement other responses later. - switch (state) { - case SSH_KNOWN_HOSTS_OK: - break; - default: - return -1; - } - return 0; -} - -int get_movies(ssh_session session, char **buffer, size_t size) { - ssh_channel channel; - int rc; - char read_buffer[256]; - int nbytes; - size_t len = 0; - char *temp; - - channel = ssh_channel_new(session); - if (channel == NULL) - return SSH_ERROR; - - rc = ssh_channel_open_session(channel); - if (rc != SSH_OK) { - fprintf(stderr, "Failed to open channel"); - ssh_channel_free(channel); - return rc; - } - - rc = ssh_channel_request_exec( - channel, "find /var/www/public/media -type f -name \"*mp4\""); - if (rc != SSH_OK) { - fprintf(stderr, "Failed to request exec"); - ssh_channel_close(channel); - ssh_channel_free(channel); - return rc; - } - - nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0); - while (nbytes > 0) { - if ((len += nbytes) > size) { - size = len; - temp = realloc(*buffer, len); - if (!temp) { - fprintf(stderr, "Failed to reallocate char buffer"); - ssh_channel_close(channel); - ssh_channel_free(channel); - return -1; - } - *buffer = temp; - } - - memcpy(*buffer + len - nbytes, read_buffer, nbytes); - - nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0); - - } - - if (nbytes < 0) { - fprintf(stderr, "Failed to read bytes from channel"); - ssh_channel_close(channel); - ssh_channel_free(channel); - return SSH_ERROR; - } - - ssh_channel_send_eof(channel); - ssh_channel_close(channel); - ssh_channel_free(channel); - return SSH_OK; -} - -int main(int argc, char *argv[]) { - char *host = "localhost"; - char *user = getlogin(); - int opt; - int rc; - - while ((opt = getopt(argc, argv, "h:u:")) != -1) - switch (opt) { - case 'h': - host = optarg; - break; - case 'u': - user = optarg; - break; - case '?': - printf("Unknown option\n"); - break; - } - - ssh_session session = ssh_new(); - if (session == NULL) { - fprintf(stderr, "Failed to get new ssh_session"); - exit(-1); - } - - ssh_options_set(session, SSH_OPTIONS_HOST, host); - ssh_options_set(session, SSH_OPTIONS_USER, user); - - if (ssh_connect(session) != SSH_OK) { - fprintf(stderr, "Unable to connect to host: %s\n", ssh_get_error(session)); - exit(-1); - } - - if (verify_host(session) != 0) { - fprintf(stderr, "Attempted to connect to unknown host\n"); - ssh_disconnect(session); - ssh_free(session); - exit(-1); - } - - rc = ssh_userauth_publickey_auto(session, NULL, NULL); - if (rc == SSH_AUTH_ERROR) { - fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session)); - exit(-1); - } - - int size = 16; - char *b = malloc(size); - memset(b, 0, size); - if (get_movies(session, &b, size) != SSH_OK) { - fprintf(stderr, "Unable to list movies: %s\n", ssh_get_error(session)); - exit(-1); - } - - ssh_disconnect(session); - ssh_free(session); -} diff --git a/popcorn.sh b/popcorn.sh new file mode 100644 index 0000000..9a1841c --- /dev/null +++ b/popcorn.sh @@ -0,0 +1,25 @@ +#!/bin/sh +usage="$0 [-u -h -p -m -v ]" + +host=localhost +path="~" +menu='dmenu-wl' +video='mpv' + +while getopts ':u:h:m:p:v' opt +do + case $opt in + u) USER=$OPTARG;; + h) host=$OPTARG;; + p) path=$OPTARG;; + m) menu=$OPTARG;; + v) video=$OPTARG;; + ?) echo "error: invalid option: $usage" + exit 1;; + esac +done + +find="cd $path; find ~+ -type f -name \"*.mp4\"" +user=$USER + +ssh $user@$host $find | $menu | xargs -I% $video "sftp://$user@$host%"