summaryrefslogtreecommitdiff
path: root/libsocialweb/sw-banned.c
blob: 133cd6d1d3a92c513f017e4b237d3cf16500bf25 (plain)
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
142
143
144
/*
 * libsocialweb - social data store
 * Copyright (C) 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>
#include "sw-banned.h"
#include <gio/gio.h>

/* TODO: turn this into a proper object which manages the ban list properly */

static gchar *
_make_banned_filename (const gchar *service_name)
{
  gchar *path;

  if (service_name)
  {
    gchar *filename;

    filename = g_strdup_printf ("%s-banned.txt", service_name);

    path = g_build_filename (g_get_user_cache_dir (),
                             PACKAGE,
                             filename,
                             NULL);
    g_free (filename);
  } else {
    path = g_build_filename (g_get_user_cache_dir (),
                             PACKAGE,
                             "banned.txt",
                             NULL);
  }

  return path;
}

GHashTable *
sw_ban_load (const gchar *service_name)
{
  GHashTable *hash;
  char *path;
  GFile *file;
  GFileInputStream *stream = NULL;
  GDataInputStream *dstream = NULL;
  GError *error = NULL;
  char *line;

  hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
  path = _make_banned_filename (service_name);
  file = g_file_new_for_path (path);
  g_free (path);

  stream = g_file_read (file, NULL, &error);
  if (error) goto done;
  dstream = g_data_input_stream_new ((GInputStream *)stream);

  while ((line = g_data_input_stream_read_line (dstream, NULL, NULL, &error))) {
    g_hash_table_insert (hash, line, GINT_TO_POINTER (42));
  }

 done:
  if (error) {
    if (!(error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_FOUND))
      g_message ("Cannot load ban list: %s", error->message);
    g_error_free (error);
  }
  if (dstream)
    g_object_unref (dstream);
  if (stream)
    g_object_unref (stream);
  g_object_unref (file);
  return hash;
}

void
sw_ban_save (const gchar *service_name,
             GHashTable  *hash)
{
  char *path;
  GFile *file;
  GFileOutputStream *stream = NULL;
  GDataOutputStream *dstream = NULL;
  GError *error = NULL;
  GHashTableIter iter;
  gpointer key, value;

  g_return_if_fail (hash);

  path = g_build_filename (g_get_user_cache_dir (),
                           PACKAGE,
                           NULL);

  if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
    int err;

    err = g_mkdir_with_parents (path, 0777);

    if (err)
      g_critical (G_STRLOC "Cannot create cache directory: %s", g_strerror (err));
  }

  g_free (path);

  path = _make_banned_filename (service_name);
  file = g_file_new_for_path (path);
  g_free (path);

  stream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
  if (error) goto done;
  dstream = g_data_output_stream_new ((GOutputStream *)stream);

  g_hash_table_iter_init (&iter, hash);
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    if (!g_data_output_stream_put_string (dstream, key, NULL, &error))
      goto done;
    if (!g_data_output_stream_put_string (dstream, "\n", NULL, &error))
      goto done;
  }

 done:
  if (error) {
    g_message ("Cannot save ban list: %s", error->message);
    g_error_free (error);
  }
  if (dstream)
    g_object_unref (dstream);
  if (stream)
    g_object_unref (stream);
  g_object_unref (file);
}