Esempio n. 1
0
void send_real_icon( const int sock ,  const char *const icon , const char *const iconid , const size_t icon_size )
{
        uint8_t buffer[1024];

        if (iconid)
        {
                growl_tcp_write(sock, "Identifier: %s", iconid);
                growl_tcp_write(sock, "Length: %zu", icon_size);
                growl_tcp_write(sock, "" );

                FILE *iconfile = fopen( icon , "rb" );
                if( iconfile )
                {
                        while (!feof(iconfile))
                        {
                                size_t bytes_read = fread(buffer, 1, 1024, iconfile);
                                if (bytes_read) growl_tcp_write_raw(sock, buffer, bytes_read);
                        }
                        growl_tcp_write(sock, "" );
                        fclose(iconfile);      
                }
        }
}
Esempio n. 2
0
/* dmex: modified to use INVALID_SOCKET */
int growl_tcp_notify( const char *const server,const char *const appname,const char *const notify,const char *const title, const char *const message ,
                                const char *const password, const char* const url, const char* const icon)
{
    SOCKET sock = INVALID_SOCKET;

    char *authheader = growl_generate_authheader_alloc(password);
    char *iconid = NULL;
    FILE *iconfile = NULL;
    size_t iconsize;
    uint8_t buffer[1024];

    growl_init();

    sock = growl_tcp_open(server);
    if (sock == INVALID_SOCKET) goto leave;

    if (icon)
    {
        size_t bytes_read;
        md5_context md5ctx;
        char md5tmp[20];
        iconfile = fopen(icon, "rb");
        if (iconfile)
        {
            fseek(iconfile, 0, SEEK_END);
            iconsize = ftell(iconfile);
            fseek(iconfile, 0, SEEK_SET);
            memset(md5tmp, 0, sizeof(md5tmp));
            md5_starts(&md5ctx);
            while (!feof(iconfile))
            {
                bytes_read = fread(buffer, 1, 1024, iconfile);
                if (bytes_read) md5_update(&md5ctx, buffer, (uint32_t)bytes_read);
            }
            fseek(iconfile, 0, SEEK_SET);
            md5_finish(&md5ctx, md5tmp);
            iconid = string_to_hex_alloc(md5tmp, 16);
        }
    }

    growl_tcp_write(sock, "GNTP/1.0 NOTIFY NONE %s", authheader ? authheader : "");
    growl_tcp_write(sock, "Application-Name: %s", appname);
    growl_tcp_write(sock, "Notification-Name: %s", notify);
    growl_tcp_write(sock, "Notification-Title: %s", title);
    growl_tcp_write(sock, "Notification-Text: %s", message);
    if(iconid)
    {
        growl_tcp_write(sock, "Notification-Icon: x-growl-resource://%s", iconid);
    }
    else if(icon)
    {
        growl_tcp_write(sock, "Notification-Icon: %s", icon );
    }
    if (url) growl_tcp_write(sock, "Notification-Callback-Target: %s", url  );

    if (iconid)
    {
        growl_tcp_write(sock, "Identifier: %s", iconid);
        growl_tcp_write(sock, "Length: %d", iconsize);
        growl_tcp_write(sock, "");
        while (!feof(iconfile))
        {
            size_t bytes_read = fread(buffer, 1, 1024, iconfile);
            if (bytes_read) growl_tcp_write_raw(sock, buffer, (int)bytes_read);
        }
        growl_tcp_write(sock, "" );
    }
    growl_tcp_write(sock, "");


    while (1)
    {
        char* line = growl_tcp_read(sock);
        if (!line)
        {
            growl_tcp_close(sock);
            sock = INVALID_SOCKET;
            goto leave;
        } else
        {
            int len = (int)strlen(line);
            /* fprintf(stderr, "%s\n", line); */
            if (strncmp(line, "GNTP/1.0 -ERROR", 15) == 0)
            {
                if (strncmp(line + 15, " NONE", 5) != 0)
                {
                    fprintf(stderr, "failed to post notification\n");
                    PhFree(line);
                    goto leave;
                }
            }
            PhFree(line);
            if (len == 0) break;
        }
    }
    growl_tcp_close(sock);
    sock = 0;

leave:
    if (iconfile) fclose(iconfile);
    if (iconid) PhFree(iconid);
    if (authheader) PhFree(authheader);

    return (sock == 0) ? 0 : INVALID_SOCKET;
}