about summary refs log tree commit diff stats
path: root/leo.pl
blob: d33604eee41383c0a273b4afddb1baf0d1aaea44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use IPC::Run3;
use Path::Tiny;
use Getopt::Long qw/ GetOptions /;

my %options = ();
GetOptions(
    \%options,
    qw{ verbose encrypt sign delete }
) or die "Error in command line arguments\n";

my $gpg_fingerprint = "D9AE4AEEE1F1B3598E81D9DFB67D55D482A799FD";
my $archive_dir = "/tmp/archive";
my $ymd = ymd(); # YYYY-MM-DD.

# Dispatch table.
my %dispatch = (
    "documents" => sub {
        archive("$archive_dir/documents_$ymd.tar",
                "-C", "$ENV{HOME}/documents", ".");
    },
    "journal" => sub {
        archive("$archive_dir/journal_$ymd.tar",
                "-C", "$ENV{HOME}/documents",
                "andinus.org.gpg", "archive.org.gpg");
    },
    "ssh" => sub {
        archive("$archive_dir/ssh_$ymd.tar",
                "-C", "$ENV{HOME}/.ssh", ".");
    },
    "pass" => sub {
        archive("$archive_dir/pass_$ymd.tar",
                "-C", "$ENV{HOME}/.password-store", ".");
    },
);

# User must pass $tar_file first & `-C' optionally.
sub archive {
    my $tar_file = shift @_;

    my ( $cwd, @archive_paths );
    # Passing `-C' won't print "tar: Removing leading / from absolute
    # path names in the archive" to STDERR in some cases.
    if ( $_[0] eq "-C" ) {
        $cwd = $_[1];

        # Remove `-C' & cwd to get @archive_paths;
        my @tmp = @_;
        shift @tmp; shift @tmp;
        @archive_paths = @tmp;
    } else {
        @archive_paths = @_;
    }

    say "Archive: $tar_file";
    warn "[WARN] $tar_file exists, might overwrite.\n" if -e $tar_file;
    print "\n";

    tar_create($tar_file, @_);

    $? # tar returns 1 on errors.
        ? die "Archive creation failed :: $?\n"
        # Print absolute paths for all archived files/directories.
        : say path($_)->absolute($cwd), " archived."
        foreach @archive_paths;

    tar_list($tar_file) if $options{verbose};
    encrypt_sign($tar_file) if $options{encrypt} or $options{sign};
}

# Encrypt, Sign archives.
sub encrypt_sign() {
    my $file = shift @_;
    my @options = ("--recipient", $gpg_fingerprint);
    push @options, "--encrypt" if $options{encrypt};
    push @options, "--sign" if $options{sign};
    push @options, "--verbose" if $options{verbose};

    say "\nEncrypt/Sign: $file";
    run3 ["gpg2", "--yes", "-o", "$file.gpg", @options, $file];
    warn "[WARN] $file.gpg exists, might overwrite.\n" if -e "$file.gpg";
    say "\nOutput: $file.gpg";

    unlink $file and say "$file deleted."
        or warn "[WARN] Could not delete $file: $!\n";
}

sub HelpMessage {
    say qq{Archive files to $archive_dir.

Usage:
    documents
        Archive $ENV{HOME}/documents
    journal
        Archive $ENV{HOME}/documents/andinus.org.gpg,
                $ENV{HOME}/documents/archive.org.gpg
    ssh
        Archive $ENV{HOME}/.ssh
    pass
        Archive $ENV{HOME}/.password-store

Options:
    --encrypt
        Encrypt files with $gpg_fingerprint
    --sign
        Sign files with $gpg_fingerprint
    --delete
        Delete the archive after running gpg2
    --verbose};
}

if ( $ARGV[0] and $dispatch{ $ARGV[0] } ) {
    path($archive_dir)->mkpath; # Create archive directory.
    $dispatch{ $ARGV[0] }->();
} elsif ( scalar @ARGV == 0 ) {
    HelpMessage();
} else {
    die say "leo: no such option\n";
}

sub tar_create { run3 ["/bin/tar", "cf", @_]; }
sub tar_list { run3 ["/bin/tar", "tvf", @_]; }

sub ymd {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
        localtime(time);

    $year += 1900; # $year contains the number of years since 1900.

    # $mon the month in the range 0..11 , with 0 indicating January
    # and 11 indicating December.
    my @months = qw( 01 02 03 04 05 06 07 08 09 10 11 12 );
    my $month = $months[$mon];

    return "$year-$month-$mday";
}
pan class="n">g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); } gboolean roster_update_presence(const char * const barejid, Resource *resource, GDateTime *last_activity) { assert(barejid != NULL); assert(resource != NULL); PContact contact = g_hash_table_lookup(contacts, barejid); if (contact == NULL) { return FALSE; } if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { p_contact_set_last_activity(contact, last_activity); } p_contact_set_presence(contact, resource); Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); autocomplete_add(fulljid_ac, jid->fulljid); jid_destroy(jid); return TRUE; } PContact roster_get_contact(const char * const barejid) { return g_hash_table_lookup(contacts, barejid); } gboolean roster_contact_offline(const char * const barejid, const char * const resource, const char * const status) { PContact contact = g_hash_table_lookup(contacts, barejid); if (contact == NULL) { return FALSE; } if (resource == NULL) { return TRUE; } else { gboolean result = p_contact_remove_resource(contact, resource); if (result == TRUE) { Jid *jid = jid_create_from_bare_and_resource(barejid, resource); autocomplete_remove(fulljid_ac, jid->fulljid); jid_destroy(jid); } return result; } } void roster_reset_search_attempts(void) { autocomplete_reset(name_ac); autocomplete_reset(barejid_ac); autocomplete_reset(fulljid_ac); autocomplete_reset(groups_ac); } void roster_init(void) { name_ac = autocomplete_new(); barejid_ac = autocomplete_new(); fulljid_ac = autocomplete_new(); groups_ac = autocomplete_new(); contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free); name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); } void roster_free(void) { autocomplete_free(name_ac); autocomplete_free(barejid_ac); autocomplete_free(fulljid_ac); autocomplete_free(groups_ac); } void roster_change_name(PContact contact, const char * const new_name) { assert(contact != NULL); const char *current_name = NULL; const char *barejid = p_contact_barejid(contact); if (p_contact_name(contact) != NULL) { current_name = strdup(p_contact_name(contact)); } p_contact_set_name(contact, new_name); _replace_name(current_name, new_name, barejid); } void roster_remove(const char * const name, const char * const barejid) { autocomplete_remove(barejid_ac, barejid); autocomplete_remove(name_ac, name); g_hash_table_remove(name_to_barejid, name); // remove each fulljid PContact contact = roster_get_contact(barejid); if (contact != NULL) { GList *resources = p_contact_get_available_resources(contact); while (resources != NULL) { GString *fulljid = g_string_new(strdup(barejid)); g_string_append(fulljid, "/"); g_string_append(fulljid, resources->data); autocomplete_remove(fulljid_ac, fulljid->str); g_string_free(fulljid, TRUE); resources = g_list_next(resources); } g_list_free(resources); } // remove the contact g_hash_table_remove(contacts, barejid); } void roster_update(const char * const barejid, const char * const name, GSList *groups, const char * const subscription, gboolean pending_out) { PContact contact = g_hash_table_lookup(contacts, barejid); assert(contact != NULL); p_contact_set_subscription(contact, subscription); p_contact_set_pending_out(contact, pending_out); const char * const new_name = name; const char * current_name = NULL; if (p_contact_name(contact) != NULL) { current_name = strdup(p_contact_name(contact)); } p_contact_set_name(contact, new_name); p_contact_set_groups(contact, groups); _replace_name(current_name, new_name, barejid); // add groups while (groups != NULL) { autocomplete_add(groups_ac, groups->data); groups = g_slist_next(groups); } } gboolean roster_add(const char * const barejid, const char * const name, GSList *groups, const char * const subscription, gboolean pending_out) { PContact contact = g_hash_table_lookup(contacts, barejid); if (contact != NULL) { return FALSE; } contact = p_contact_new(barejid, name, groups, subscription, NULL, pending_out); // add groups while (groups != NULL) { autocomplete_add(groups_ac, groups->data); groups = g_slist_next(groups); } g_hash_table_insert(contacts, strdup(barejid), contact); autocomplete_add(barejid_ac, barejid); _add_name_and_barejid(name, barejid); return TRUE; } char * roster_barejid_from_name(const char * const name) { if (name) { return g_hash_table_lookup(name_to_barejid, name); } else { return NULL; } } GSList * roster_get_contacts_by_presence(const char * const presence) { GSList *result = NULL; GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { PContact contact = (PContact)value; if (g_strcmp0(p_contact_presence(contact), presence) == 0) { result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); } } // resturn all contact structs return result; } GSList * roster_get_contacts(void) { GSList *result = NULL; GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); } // resturn all contact structs return result; } GSList * roster_get_contacts_online(void) { GSList *result = NULL; GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { if(strcmp(p_contact_presence(value), "offline")) result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); } // resturn all contact structs return result; } gboolean roster_has_pending_subscriptions(void) { GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { PContact contact = (PContact) value; if (p_contact_pending_out(contact)) { return TRUE; } } return FALSE; } char * roster_contact_autocomplete(const char * const search_str) { return autocomplete_complete(name_ac, search_str, TRUE); } char * roster_fulljid_autocomplete(const char * const search_str) { return autocomplete_complete(fulljid_ac, search_str, TRUE); } GSList * roster_get_nogroup(void) { GSList *result = NULL; GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { GSList *groups = p_contact_groups(value); if (groups == NULL) { result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); } } // resturn all contact structs return result; } GSList * roster_get_group(const char * const group) { GSList *result = NULL; GHashTableIter iter; gpointer key; gpointer value; g_hash_table_iter_init(&iter, contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { GSList *groups = p_contact_groups(value); while (groups != NULL) { if (strcmp(groups->data, group) == 0) { result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); break; } groups = g_slist_next(groups); } } // resturn all contact structs return result; } GSList * roster_get_groups(void) { return autocomplete_create_list(groups_ac); } char * roster_group_autocomplete(const char * const search_str) { return autocomplete_complete(groups_ac, search_str, TRUE); } char * roster_barejid_autocomplete(const char * const search_str) { return autocomplete_complete(barejid_ac, search_str, TRUE); } static gboolean _key_equals(void *key1, void *key2) { gchar *str1 = (gchar *) key1; gchar *str2 = (gchar *) key2; return (g_strcmp0(str1, str2) == 0); } static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2) { if ((dt1 == NULL) && (dt2 == NULL)) { return TRUE; } else if ((dt1 == NULL) && (dt2 != NULL)) { return FALSE; } else if ((dt1 != NULL) && (dt2 == NULL)) { return FALSE; } else { return g_date_time_equal(dt1, dt2); } } static void _replace_name(const char * const current_name, const char * const new_name, const char * const barejid) { // current handle exists already if (current_name != NULL) { autocomplete_remove(name_ac, current_name); g_hash_table_remove(name_to_barejid, current_name); _add_name_and_barejid(new_name, barejid); // no current handle } else if (new_name != NULL) { autocomplete_remove(name_ac, barejid); g_hash_table_remove(name_to_barejid, barejid); _add_name_and_barejid(new_name, barejid); } } static void _add_name_and_barejid(const char * const name, const char * const barejid) { if (name != NULL) { autocomplete_add(name_ac, name); g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); } else { autocomplete_add(name_ac, barejid); g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); } } static gint _compare_contacts(PContact a, PContact b) { const char * utf8_str_a = NULL; const char * utf8_str_b = NULL; if (p_contact_name(a) != NULL) { utf8_str_a = p_contact_name(a); } else { utf8_str_a = p_contact_barejid(a); } if (p_contact_name(b) != NULL) { utf8_str_b = p_contact_name(b); } else { utf8_str_b = p_contact_barejid(b); } gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); gint result = g_strcmp0(key_a, key_b); g_free(key_a); g_free(key_b); return result; }