avatar.c
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 06 Nov 2009 00:43:15 +0200
changeset 0 29423ceb6adf
child 1 285017b7713c
permissions -rw-r--r--
Initial commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
/*
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
 * avatar.c             -- Pep avatar events
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
 *
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
 * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
 *
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
 * the Free Software Foundation; either version 2 of the License, or (at
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
 * your option) any later version.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
 *
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
 * General Public License for more details.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
 *
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
 * USA
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
 */
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
#define PNG_USER_MEM_SUPPORTED
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
#include <glib.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
#include <gmodule.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
#include <loudmouth/loudmouth.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
#include <aalib.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
#include <png.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
#include <string.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
#include <unistd.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
#include <sys/types.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
#include <sys/stat.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
#include <fcntl.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
#include <stdlib.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
#include <errno.h>
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
#include "compl.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
#include "commands.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
#include "utils.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
#include "xmpp.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
#include "screen.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
#include "hbuf.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
#include "hooks.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
#include "settings.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
#include "pep.h"
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
#define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
#define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
#define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
static guint avatar_cid = 0;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
GSList *reply_handlers = NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
// png stuff
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
static png_voidp png_glib_malloc (png_structp ignore, png_size_t size)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
	return g_malloc (size);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
static void png_glib_free (png_structp ignore, png_voidp chunk)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
	g_free (chunk);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
// reads file and returns array of pointers to image rows in
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
// grayscale 8-bit with alpha-channel format (i.e. two bytes per pixel)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
// after use just do g_free of this pointer - all data are in one memory chunk
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
static png_bytep *png_read_file (const char *file, int *rheight, int *rwidth)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
	FILE        *fd       = fopen (file, "rb");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
	png_infop    info_ptr;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
	png_structp  png_ptr;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
	int          rowbytes;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
	png_bytep   *row_pointers;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
	int          width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
	int          height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
	if (!fd)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
	{ // check signature
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
		char header[8];
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
		fread (header, 1, 8, fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
		if (png_sig_cmp (header, 0, 8)) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
			fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
			return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
	// initialize reader
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
	png_ptr = png_create_read_struct_2 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, NULL, png_glib_malloc, png_glib_free);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
	if (!png_ptr) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
		fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
	info_ptr = png_create_info_struct (png_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
	if (!info_ptr) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
		png_destroy_read_struct (&png_ptr, NULL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
		fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
	if (setjmp (png_jmpbuf (png_ptr))) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
		fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
	png_init_io (png_ptr, fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
	png_set_sig_bytes (png_ptr, 8);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
	// get information
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
	png_read_info (png_ptr, info_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
	{ // set up transformations
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
		png_byte color_type = info_ptr->color_type;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
		png_byte bit_depth  = info_ptr->bit_depth;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
		// trying to convert anything to grayscale 8-bit color with alpha channel
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
		if (color_type == PNG_COLOR_TYPE_PALETTE) // XXX
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
			png_set_palette_to_rgb (png_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
		if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
			png_set_expand_gray_1_2_4_to_8 (png_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
		if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_PALETTE /* ??? */) // XXX
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
			png_set_add_alpha (png_ptr, 0xff, PNG_FILLER_AFTER);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
		if (bit_depth == 16)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
			png_set_strip_16 (png_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
		if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
			png_set_tRNS_to_alpha (png_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
		if (color_type == PNG_COLOR_TYPE_PALETTE /* ??? */ || color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) // XXX
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
			png_set_rgb_to_gray_fixed (png_ptr, 1, -1, -1);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
		// renew information in info structure
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
		png_read_update_info (png_ptr, info_ptr);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
	width    = info_ptr->width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
	height   = info_ptr->height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
	rowbytes = info_ptr->rowbytes;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
	{ // allocate buffer for image data
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
		png_bytep chunk = g_malloc ((sizeof(png_bytep) + rowbytes) * height);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
		png_bytep start = chunk + sizeof(png_bytep) * height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
		int       y;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
		row_pointers = (png_bytep *) chunk;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
		for (y = 0; y < height; ++y)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
			row_pointers[y] = start + y * rowbytes;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
	// read image
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
	if (setjmp (png_jmpbuf (png_ptr))) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
		g_free (row_pointers);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
		fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
	png_read_image (png_ptr, row_pointers);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
	png_read_end (png_ptr, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
	// free resources
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
	png_destroy_read_struct (&png_ptr, &info_ptr, NULL); 
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
	fclose (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
	*rheight = height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
	*rwidth  = width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
	return row_pointers;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
// aa stuff
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
// converts image in format, returned by png_read_file into ascii,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
// scaling it to fit specified max values. If font parameters are
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
// set properly, should result in image on screen with original png
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
// aspect ratio.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
static gchar *aa_convert_image (png_bytep *row_pointers, int height, int width, int maxcharh, int maxcharw, int fonth, int fontw)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
	gchar                     *result;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
	struct aa_hardware_params  hwparams = {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
		.font      = NULL,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
		.supported = AA_NORMAL_MASK,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
		.width     = 0,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
		.height    = 0,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
	};
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
	// params for zooming
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
	int finalcharh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
	int finalcharw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
	int finalh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
	int finalw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
	int starty;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
	int startx;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   202
	int finalratioh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
	int finalratiow;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   205
	// process aa parameters from environment variables
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   206
	aa_parseoptions (&hwparams, NULL, NULL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   207
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
	{ // calculate parameters for image zooming into aa surface
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   209
	  // this always makes me a hard time, thus such a mess :/
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
	  // I hope, compiler will optimize out all these variables...
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   211
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
		// font size in font pixels (real?)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
		//int fonth = 16;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
		//int fontw = 8;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
		float fontaspect = (float) fontw / fonth;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   216
		// aa pixels per character
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   217
		const int aappch = 2;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
		const int aappcw = 2;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
		const float aaaspect = (float) aappcw / aappch;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
		// font pixels per aa pixel
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
		int aappph = (float) fonth / aappch;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
		int aapppw = (float) fontw / aappcw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
		float aapixelaspect = (float) aapppw / aappph;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
		// original image size in real pixels
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
		int originh = height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
		int originw = width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
		float originaspect = (float) originw / originh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
		// maximal resulting image size in chars
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
		//int maxcharh = res_height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
		//int maxcharw = res_width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
		// maximal resulting image size in aa pixels
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
		int maxh = maxcharh * aappch;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
		int maxw = maxcharw * aappcw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
		// maximal resulting image size in real pixels
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   235
		int maxh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   236
		int maxw_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
		if (aapixelaspect > 1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
			maxh_rp = maxh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
			maxw_rp = (float) maxw * aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   240
		} else {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   241
			maxh_rp = (float) maxh / aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   242
			maxw_rp = maxw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
		float maxaspect_rp = (float) maxw_rp / maxh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   245
		// resulting image size in real pixels
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   246
		float ratio_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   247
		int resh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   248
		int resw_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   249
		if (originh > maxh_rp || originw > maxw_rp) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   250
			// boundaries crossed, will zoom
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   251
			if (originaspect > maxaspect_rp) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   252
				// width is bigger
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   253
				ratio_rp = (float) maxw_rp / originw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
				resh_rp = (float) originh * ratio_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
				resw_rp = maxw_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
			} else {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
				// height is bigger
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
				ratio_rp = (float) maxh_rp / originh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   259
				resh_rp = maxh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
				resw_rp = (float) originw * ratio_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
			}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
		} else {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
			// image fits, no zooming
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
			ratio_rp = 1;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   265
			resh_rp = originh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   266
			resw_rp = originw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   267
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   268
		// resulting image size in aa pixels
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   269
		int resh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   270
		int resw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   271
		if (aapixelaspect > 1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   272
			resh = resh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   273
			resw = (float) resw_rp / aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   274
		} else {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   275
			resh = (float) resh_rp * aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   276
			resw = resw_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   277
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   278
		// number of original image pixels per one resulting image pixel
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   279
		int ratioh = (float) originh / resh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   280
		int ratiow = (float) originw / resw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   281
		// resulting image size in chars
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   282
		int rescharh = (float) resh / aappch;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   283
		int rescharw = (float) resw / aappcw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   284
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   285
		// re-calculate parameters, as they may change due to rounding (?)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   286
		finalcharh = rescharh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   287
		finalcharw = rescharw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   288
		finalh = rescharh * aappch;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   289
		finalw = rescharw * aappcw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   290
		int finalh_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   291
		int finalw_rp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   292
		if (aapixelaspect > 1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   293
			finalh_rp = finalh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   294
			finalw_rp = (float) finalw * aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   295
		} else {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   296
			finalh_rp = (float) finalh / aapixelaspect;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   297
			finalw_rp = finalw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   298
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   299
		finalratioh = (float) originh / finalh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   300
		finalratiow = (float) originw / finalw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   301
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   302
		// center image part actually used
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   303
		int areah = (float) finalh * finalratioh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   304
		int areaw = (float) finalw * finalratiow;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   305
		int losth = originh - areah;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   306
		int lostw = originw - areaw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   307
		starty = losth / 2;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   308
		startx = lostw / 2;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   309
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   310
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   311
	// create aa image
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   312
	hwparams.height = finalcharh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   313
	hwparams.width  = finalcharw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   314
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   315
	aa_context *context = aa_init (&mem_d, &hwparams, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   316
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   317
	if (context == NULL)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   318
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   319
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   320
	{ // fill aa image from png buffer with scaling
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   321
		int       height, width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   322
		const int bpp = 2;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   323
		int       y;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   324
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   325
		height = aa_imgheight (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   326
		width  = aa_imgwidth (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   327
		
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   328
		if (height > finalh)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   329
			height = finalh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   330
		if (width > finalw)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   331
			width = finalw;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   332
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   333
		for (y = 0;  y < height; ++y) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   334
			int x;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   335
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   336
			for (x = 0; x < width; ++x) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   337
				png_bytep    *row_pointer = row_pointers + starty + y * finalratioh;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   338
				unsigned int  color = 0;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   339
				unsigned int  cy;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   340
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   341
				// just arithmetic average
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   342
				for (cy = 0; cy < finalratioh; ++cy) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   343
					png_bytep row = row_pointer[cy] + (startx + x * finalratiow) * bpp;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   344
					int       cx;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   345
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   346
					for (cx = 0; cx < finalratiow; ++cx)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   347
						color += row[cx * bpp] * row[cx * bpp + 1] / 255;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   348
				}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   349
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   350
				color /= finalratioh * finalratiow;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   351
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   352
				aa_putpixel (context, x, y, color);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   353
			}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   354
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   355
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   356
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   357
	// render ascii image
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   358
	aa_render (context, &aa_defrenderparams, 0, 0, aa_scrwidth (context), aa_scrheight (context));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   359
	aa_flush (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   360
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   361
	{ // format the result
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   362
		char    *text   = aa_text (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   363
		int      width  = aa_scrwidth (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   364
		int      height = aa_scrheight (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   365
		GString *avatar = g_string_new (NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   366
		char    *end    = text + width * height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   367
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   368
		while (text < end) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   369
			g_string_append_len (avatar, text, width);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   370
			g_string_append_c (avatar, '\n');
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   371
			text += width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   372
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   373
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   374
		// strip last '\n'
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   375
		g_string_truncate (avatar, avatar->len - 1);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   376
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   377
		result = g_string_free (avatar, FALSE);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   378
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   379
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   380
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   381
	// free resources
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   382
	aa_close (context);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   383
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   384
	return result;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   385
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   386
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   387
// avatar stuff
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   388
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   389
// returns filename to save this avatar. NULL on error. g_free result.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   390
static gchar *avatar_id_filename (const gchar *id)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   391
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   392
	gchar *escid = g_strdup (id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   393
	gchar *dir   = (gchar *) settings_opt_get ("avatar_directory");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   394
	gchar *file;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   395
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   396
	if (!dir)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   397
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   398
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   399
	{ // neutralize id
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   400
		const gchar *valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   401
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   402
		g_strcanon (escid, valid, '_');
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   403
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   404
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   405
	dir = expand_filename (dir);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   406
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   407
	file = g_strdup_printf ("%s/%s.png", dir, escid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   408
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   409
	g_free (dir);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   410
	g_free (escid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   411
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   412
	return file;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   413
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   414
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   415
// returns name of symlink to avatar image file for this jid
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   416
static gchar *avatar_jid_filename (const gchar *jid)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   417
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   418
	gchar *dir  = (gchar *) settings_opt_get ("avatar_directory");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   419
	gchar *file = NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   420
	gchar *bjid;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   421
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   422
	if (!dir)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   423
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   424
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   425
	// if jid validity alone is enough for mcabber to use as
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   426
	// names for history logs, it's fine for us too.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   427
	if (check_jid_syntax (jid))
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   428
		return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   429
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   430
	bjid = jidtodisp (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   431
	mc_strtolower (bjid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   432
	dir  = expand_filename (dir);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   433
	file = g_strdup_printf ("%s/%s", dir, bjid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   434
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   435
	g_free (bjid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   436
	return file;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   437
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   438
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   439
// symlinks specified jid to specified file. performs jid
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   440
// checks, but not file checks.
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   441
static void set_jid_avatar (const char *jid, const char *file)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   442
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   443
	gchar *jfile;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   444
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   445
	if (!jid || !file)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   446
		return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   447
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   448
	jfile = avatar_jid_filename (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   449
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   450
	if (!jfile)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   451
		return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   452
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   453
	unlink (jfile);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   454
	if (symlink (file, jfile) == -1)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   455
		scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot symlink jid-file to avatar: %s.", strerror (errno));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   456
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   457
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   458
// reply handler for metadata publish request
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   459
// just prints error message if unsuccessful
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   460
LmHandlerResult avatar_publish_metadata_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   461
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   462
	reply_handlers = g_slist_remove (reply_handlers, handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   463
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   464
	switch (lm_message_get_sub_type (message)) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   465
	case LM_MESSAGE_SUB_TYPE_RESULT:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   466
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   467
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   468
	case LM_MESSAGE_SUB_TYPE_ERROR:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   469
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   470
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   471
			LmMessageNode *node   = lm_message_get_node (message);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   472
			const gchar   *type;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   473
			const gchar   *reason;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   474
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   475
			node = lm_message_node_get_child (node, "error");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   476
			type = lm_message_node_get_attribute (node, "type");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   477
			if (node->children)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   478
				reason = node->children->name;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   479
			else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   480
				reason = "undefined";
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   481
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   482
			scr_LogPrint (LPRINT_LOGNORM, "avatar: Metadata publishing failed: %s - %s.", type, reason);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   483
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   484
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   485
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   486
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   487
	default:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   488
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   489
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   490
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   491
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   492
	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   493
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   494
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   495
// reply handler for data publish request
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   496
// sends metadata update request or prints error message
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   497
LmHandlerResult avatar_publish_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   498
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   499
	LmMessage *request = (LmMessage *) userdata;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   500
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   501
	reply_handlers = g_slist_remove (reply_handlers, handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   502
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   503
	switch (lm_message_get_sub_type (message)) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   504
	case LM_MESSAGE_SUB_TYPE_RESULT:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   505
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   506
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   507
			// XXX we may use only one instance of this...
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   508
			LmMessageHandler *mhandler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   509
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   510
			reply_handlers = g_slist_append (reply_handlers, mhandler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   511
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   512
			lm_connection_send_with_reply (connection, request, mhandler, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   513
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   514
			lm_message_handler_unref (mhandler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   515
			lm_message_unref (request);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   516
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   517
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   518
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   519
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   520
	case LM_MESSAGE_SUB_TYPE_ERROR:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   521
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   522
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   523
			LmMessageNode *node   = lm_message_get_node (message);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   524
			const gchar   *type;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   525
			const gchar   *reason;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   526
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   527
			node = lm_message_node_get_child (node, "error");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   528
			type = lm_message_node_get_attribute (node, "type");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   529
			if (node->children)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   530
				reason = node->children->name;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   531
			else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   532
				reason = "undefined";
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   533
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   534
			scr_LogPrint (LPRINT_LOGNORM, "avatar: Data publishing failed: %s - %s.", type, reason);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   535
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   536
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   537
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   538
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   539
	default:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   540
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   541
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   542
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   543
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   544
	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   545
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   546
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   547
// prints avatar (if available) for jid into jid's buffer
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   548
// id can be omitted
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   549
static gboolean print_avatar (const gchar *file, const gchar *jid, const gchar *id)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   550
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   551
	png_bytep *row_pointers;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   552
	gchar     *avatar;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   553
	int        width;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   554
	int        height;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   555
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   556
	if (!file)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   557
		return FALSE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   558
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   559
	{ // check if file exists to not trigger unnecessary error messages
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   560
		struct stat buf;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   561
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   562
		if (stat (file, &buf) == -1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   563
			gchar *bjid = jidtodisp (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   564
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   565
			scr_WriteIncomingMessage (bjid, "No avatar for this buddy yet.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   566
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   567
			g_free (bjid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   568
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   569
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   570
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   571
	row_pointers = png_read_file (file, &width, &height);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   572
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   573
	if (!row_pointers) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   574
		scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot decode png data from file %s.", file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   575
		return FALSE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   576
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   577
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   578
	{ // convert to ascii
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   579
		int maxcharh = settings_opt_get_int ("avatar_max_height");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   580
		int maxcharw = settings_opt_get_int ("avatar_max_width");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   581
		int fonth    = settings_opt_get_int ("avatar_font_height");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   582
		int fontw    = settings_opt_get_int ("avatar_font_width");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   583
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   584
		// if not set explicitly, try to calculate available space
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   585
		if (!maxcharh) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   586
			char *var = getenv ("LINES");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   587
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   588
			if (var)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   589
				maxcharh = atoi (var);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   590
			else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   591
				maxcharh = 25;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   592
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   593
			maxcharh -= settings_opt_get_int ("log_win_height") + 3 + 1; // +1 due to avatar header line
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   594
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   595
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   596
		if (!maxcharw) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   597
			char *var = getenv ("COLUMNS");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   598
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   599
			if (var)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   600
				maxcharw = atoi (var);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   601
			else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   602
				maxcharw = 80;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   603
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   604
			maxcharw -= settings_opt_get_int ("roster_width") + scr_getprefixwidth ();
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   605
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   606
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   607
		if (!fonth)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   608
			fonth = 16;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   609
		if (!fontw)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   610
			fontw = 8;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   611
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   612
		avatar = aa_convert_image (row_pointers, width, height, maxcharh, maxcharw, fonth, fontw);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   613
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   614
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   615
	g_free (row_pointers);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   616
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   617
	if (!avatar) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   618
		scr_LogPrint (LPRINT_LOGNORM, "avatar: Error converting image to ascii.");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   619
		return FALSE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   620
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   621
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   622
	{ // print out avatar
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   623
		gchar *bjid = jidtodisp (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   624
		gchar *mesg;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   625
		if (id)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   626
			mesg = g_strdup_printf ("Avatar [%s]:\n%s", id, avatar);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   627
		else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   628
			mesg = g_strdup_printf ("Avatar:\n%s", avatar);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   629
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   630
		scr_WriteIncomingMessage (bjid, mesg, 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   631
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   632
		g_free (bjid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   633
		g_free (mesg);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   634
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   635
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   636
	g_free (avatar);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   637
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   638
	return TRUE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   639
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   640
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   641
// reply handler for image/png data request
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   642
// saves image and prints it to buddy buffer
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   643
LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   644
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   645
	gchar *from = (gchar *) userdata;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   646
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   647
	reply_handlers = g_slist_remove (reply_handlers, handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   648
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   649
	switch (lm_message_get_sub_type (message)) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   650
	case LM_MESSAGE_SUB_TYPE_RESULT:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   651
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   652
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   653
			LmMessageNode *node = lm_message_get_node (message);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   654
			node = lm_message_node_get_child (node, "pubsub");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   655
			if (!node)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   656
				return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   657
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   658
			node = lm_message_node_get_child (node, "items");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   659
			if (!node)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   660
				return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   661
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   662
			{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   663
				LmMessageNode *item;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   664
				
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   665
				for (item = node->children; item; item = item->next) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   666
					if (!strcmp (item->name, "item")) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   667
						LmMessageNode *data = lm_message_node_get_child (item, "data");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   668
						const gchar   *id   = lm_message_node_get_attribute (item, "id");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   669
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   670
						if (!data)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   671
							continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   672
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   673
						{ // save to file and display in ascii
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   674
							const gchar *base64 = lm_message_node_get_value (data);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   675
							gchar       *png;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   676
							gsize        len;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   677
							gchar       *file;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   678
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   679
							if (!base64)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   680
								continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   681
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   682
							png = g_base64_decode (base64, &len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   683
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   684
							if (!png)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   685
								continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   686
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   687
							file = avatar_id_filename (id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   688
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   689
							if (!file) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   690
								g_free (png);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   691
								continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   692
							}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   693
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   694
							{ // write image to file
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   695
								int fd = open (file, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   696
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   697
								if (fd == -1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   698
									scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot create file: %s.", strerror (errno));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   699
									g_free (png);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   700
									g_free (file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   701
									continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   702
								}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   703
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   704
								write (fd, png, len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   705
								close (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   706
							}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   707
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   708
							// create jid symlink to avatar image
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   709
							set_jid_avatar (from, file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   710
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   711
							g_free (png);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   712
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   713
							// read it back :/
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   714
							print_avatar (file, from, id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   715
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   716
							g_free (file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   717
						}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   718
					}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   719
				}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   720
			}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   721
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   722
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   723
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   724
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   725
	case LM_MESSAGE_SUB_TYPE_ERROR:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   726
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   727
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   728
			LmMessageNode *node   = lm_message_get_node (message);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   729
			const gchar   *type;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   730
			const gchar   *reason;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   731
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   732
			node = lm_message_node_get_child (node, "error");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   733
			type = lm_message_node_get_attribute (node, "type");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   734
			if (node->children)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   735
				reason = node->children->name;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   736
			else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   737
				reason = "undefined";
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   738
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   739
			scr_LogPrint (LPRINT_LOGNORM, "avatar: Obtaining avatar data for %s failed: %s - %s.", from, type, reason);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   740
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   741
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   742
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   743
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   744
	default:
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   745
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   746
		break;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   747
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   748
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   749
	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   750
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   751
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   752
// data is image/png data of length len
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   753
// call with len = 0 to publish empty avatar
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   754
static void avatar_publish (const gchar *data, gsize len)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   755
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   756
	LmMessage        *datarequest;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   757
	LmMessageHandler *handler;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   758
	gchar            *id;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   759
	LmMessage        *request;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   760
	LmMessageNode    *node;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   761
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   762
	request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   763
	node = lm_message_get_node (request);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   764
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   765
	if (len) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   766
		// create data publish request
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   767
		lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   768
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   769
		node = lm_message_node_add_child (node, "pubsub", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   770
		lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   771
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   772
		node = lm_message_node_add_child (node, "publish", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   773
		lm_message_node_set_attribute (node, "node", NS_AVATAR_DATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   774
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   775
		node = lm_message_node_add_child (node, "item", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   776
		id = g_compute_checksum_for_data (G_CHECKSUM_SHA1, data, len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   777
		lm_message_node_set_attribute (node, "id", id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   778
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   779
		{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   780
			gchar *base64 = g_base64_encode (data, len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   781
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   782
			node = lm_message_node_add_child (node, "data", base64);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   783
			lm_message_node_set_attribute (node, "xmlns", NS_AVATAR_DATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   784
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   785
			g_free (base64);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   786
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   787
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   788
		// then create metadata publish request to be supplied to reply handler as userdata
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   789
		datarequest = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   790
		node = lm_message_get_node (datarequest);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   791
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   792
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   793
	lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   794
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   795
	node = lm_message_node_add_child (node, "pubsub", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   796
	lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   797
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   798
	node = lm_message_node_add_child (node, "publish", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   799
	lm_message_node_set_attribute (node, "node", NS_AVATAR_METADATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   800
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   801
	node = lm_message_node_add_child (node, "item", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   802
	if (len)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   803
		lm_message_node_set_attribute (node, "id", id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   804
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   805
	node = lm_message_node_add_child (node, "metadata", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   806
	lm_message_node_set_attribute (node, "xmlns", NS_AVATAR_METADATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   807
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   808
	if (len) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   809
		gchar *bytes = g_strdup_printf ("%d", len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   810
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   811
		node = lm_message_node_add_child (node, "info", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   812
		lm_message_node_set_attributes (node, "bytes", bytes, "id", id, "type", "image/png", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   813
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   814
		g_free (bytes);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   815
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   816
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   817
	// create handler
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   818
	if (len) 
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   819
		handler = lm_message_handler_new (avatar_publish_data_reply_handler, (gpointer) datarequest, (GDestroyNotify) lm_message_unref /* custom, remove from queue? */);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   820
	else 
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   821
		handler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   822
	reply_handlers = g_slist_append (reply_handlers, handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   823
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   824
	// send
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   825
	lm_connection_send_with_reply (lconnection, request, handler, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   826
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   827
	lm_message_handler_unref (handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   828
	lm_message_unref (request);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   829
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   830
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   831
// pep node handler
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   832
// if avatar is not yet saved, sends data request, else updates jid's symlink
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   833
static void avatar_handler (const gchar *from, const gchar *enode, LmMessageNode *n, const gchar *id, gpointer ignore)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   834
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   835
	LmMessageNode *node;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   836
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   837
	// well, anyway we now do not store information about jid-avatar relationship,
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   838
	// thus "no avatar" publishes are meaningless to us
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   839
	for (node = n->children; node; node = node->next) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   840
		const gchar *name = node->name;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   841
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   842
		if (!strcmp (name, "info")) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   843
			gchar       *jid    = jidtodisp (from);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   844
			const gchar *url    = lm_message_node_get_attribute (node, "url");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   845
			const gchar *type   = lm_message_node_get_attribute (node, "type");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   846
			const gchar *id     = lm_message_node_get_attribute (node, "id");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   847
			const gchar *bytes  = lm_message_node_get_attribute (node, "bytes");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   848
			const gchar *height = lm_message_node_get_attribute (node, "height");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   849
			const gchar *width  = lm_message_node_get_attribute (node, "width");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   850
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   851
			{ // print to buddy's buffer
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   852
				GString *mesg = g_string_new ("Avatar:");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   853
				gchar   *text;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   854
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   855
				// [id] type w x h (# bytes) url
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   856
				g_string_append_printf (mesg, " [%s]", id ? id : "(none)");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   857
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   858
				if (type) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   859
					g_string_append_c (mesg, ' ');
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   860
					g_string_append (mesg, type);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   861
				}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   862
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   863
				if (width && height)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   864
					g_string_append_printf (mesg, " %s x %s", width, height);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   865
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   866
				if (bytes)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   867
					g_string_append_printf (mesg, " (%s bytes)", bytes);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   868
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   869
				if (url) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   870
					g_string_append_c (mesg, ' ');
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   871
					g_string_append (mesg, url);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   872
				}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   873
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   874
				text = g_string_free (mesg, FALSE);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   875
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   876
				scr_WriteIncomingMessage (jid, text, 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   877
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   878
				g_free (text);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   879
			}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   880
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   881
			if (!url) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   882
				// it is server-published png, go ahead and request data
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   883
				LmMessage        *request;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   884
				LmMessageNode    *node;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   885
				LmMessageHandler *dhandler;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   886
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   887
				{ // check, if file already exists
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   888
					gchar       *file = avatar_id_filename (id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   889
					struct stat  buf;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   890
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   891
					if (!file) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   892
						scr_LogPrint (LPRINT_LOGNORM, "avatar: Cannot obtain filename to save file, probably avatar_directory is not set.");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   893
						g_free (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   894
						continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   895
					}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   896
					
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   897
					if (stat (file, &buf) != -1) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   898
						scr_WriteIncomingMessage (jid, "Avatar file exists, will not download.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   899
						// link jid to this file
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   900
						set_jid_avatar (jid, file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   901
						g_free (file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   902
						g_free (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   903
						continue;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   904
					}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   905
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   906
					g_free (file);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   907
				}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   908
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   909
				// create data request
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   910
				request = lm_message_new_with_sub_type (jid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   911
				node = lm_message_get_node (request);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   912
				lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   913
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   914
				node = lm_message_node_add_child (node, "pubsub", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   915
				lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   916
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   917
				node = lm_message_node_add_child (node, "items", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   918
				lm_message_node_set_attribute (node, "node", NS_AVATAR_DATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   919
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   920
				node = lm_message_node_add_child (node, "item", NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   921
				lm_message_node_set_attribute (node, "id", id);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   922
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   923
				// create handler
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   924
				dhandler = lm_message_handler_new (avatar_retrieve_data_reply_handler, jid, g_free);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   925
				reply_handlers = g_slist_append (reply_handlers, dhandler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   926
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   927
				// send
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   928
				lm_connection_send_with_reply (lconnection, request, dhandler, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   929
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   930
				lm_message_handler_unref (dhandler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   931
				lm_message_unref (request);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   932
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   933
				// NOT free jid here - it will be freed on handler destruction
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   934
			} else
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   935
				g_free (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   936
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   937
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   938
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   939
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   940
// /AVATAR [filename|-]
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   941
static void do_avatar (char *arg)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   942
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   943
	gchar *fname;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   944
	gchar *data;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   945
	gsize  len;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   946
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   947
	if (!*arg) { // print current byddy's avatar
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   948
		const gchar *jid = CURRENT_JID;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   949
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   950
		if (!jid) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   951
			scr_LogPrint (LPRINT_NORMAL, "This item cannot have avatar.");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   952
			return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   953
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   954
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   955
		fname = avatar_jid_filename (jid);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   956
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   957
		print_avatar (fname, jid, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   958
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   959
		g_free (fname);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   960
		return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   961
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   962
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   963
	// publish
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   964
	if (arg[0] == '-' && arg[1] == '\0') {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   965
		// no avatar
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   966
		avatar_publish (NULL, 0);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   967
		return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   968
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   969
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   970
	fname = expand_filename (arg);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   971
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   972
	{ // read file
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   973
		char     buffer[1024];
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   974
		GString *datastring;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   975
		int      fd           = open (fname, O_RDONLY);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   976
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   977
		if (!fd) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   978
			scr_LogPrint (LPRINT_NORMAL, "Cannot open file '%s': %s.", fname, strerror (errno));
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   979
			g_free (fname);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   980
			return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   981
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   982
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   983
		datastring = g_string_new (NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   984
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   985
		{ // read data
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   986
			int ret;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   987
			while ((ret = read (fd, buffer, 1024)) > 0)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   988
				g_string_append_len (datastring, buffer, ret);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   989
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   990
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   991
		close (fd);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   992
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   993
		len  = datastring->len;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   994
		data = g_string_free (datastring, FALSE);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   995
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   996
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   997
	g_free (fname);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   998
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   999
	avatar_publish (data, len);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1000
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1001
	g_free (data);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1002
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1003
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1004
static void avatar_free_reply_handlers (void)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1005
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1006
	GSList *hel;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1007
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1008
	// let's hope, that after invalidation, lm will remove and free unreffed by us handler
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1009
	for (hel = reply_handlers; hel; hel = hel->next) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1010
		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1011
		lm_message_handler_invalidate (handler);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1012
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1013
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1014
	g_slist_free (reply_handlers);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1015
	reply_handlers = NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1016
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1017
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1018
// release handlers before reconnect
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1019
static void avatar_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1020
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1021
	hk_arg_t *arg;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1022
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1023
	for (arg = args; arg->name; arg++) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1024
		if (!strcmp (arg->name, "hook") && !strcmp (arg->value, "hook-pre-disconnect")) {
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1025
			avatar_free_reply_handlers ();
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1026
			return;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1027
		}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1028
	}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1029
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1030
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1031
const gchar *g_module_check_init (GModule *module)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1032
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1033
	pep_register_xmlns_handler (NS_AVATAR_METADATA, avatar_handler, NULL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1034
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1035
	cmd_add ("avatar", "", COMPL_FILENAME, 0, do_avatar, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1036
	
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1037
	hk_add_handler (avatar_hh, HOOK_INTERNAL, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1038
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1039
	xmpp_add_feature (NS_AVATAR_METADATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1040
	xmpp_add_feature (NS_AVATAR_METADATA_NOTIFY);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1041
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1042
	return NULL;
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1043
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1044
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1045
void g_module_unload (GModule *module)
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1046
{
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1047
	pep_unregister_xmlns_handler (NS_AVATAR_METADATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1048
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1049
	avatar_free_reply_handlers ();
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1050
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1051
	hk_del_handler (avatar_hh, NULL);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1052
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1053
	cmd_del ("avatar");
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1054
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1055
	xmpp_del_feature (NS_AVATAR_METADATA);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1056
	xmpp_del_feature (NS_AVATAR_METADATA_NOTIFY);
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1057
}
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1058
29423ceb6adf Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
  1059
/* vim: se ts=4: */