コード例 #1
0
ファイル: growl.c プロジェクト: kissthink/GNTPCom
int growl_tcp_register( const char *const server ,
                        const char *const appname ,
                        const char **const notifications ,
                        const int notifications_count ,
		                const char *const password,
                        const char *const icon  )
{
	int sock = -1;
	int i=0;
	char *authheader;
	
	growl_init();
	authheader = growl_generate_authheader_alloc(password);
	sock = growl_tcp_open(server);
	if (sock == -1) goto leave;
    
	growl_tcp_write(sock, "GNTP/1.0 REGISTER NONE %s", authheader ? authheader : "");
	growl_tcp_write(sock, "Application-Name: %s ", appname);
	if(icon) growl_tcp_write(sock, "Application-Icon: %s ", icon);	
	growl_tcp_write(sock, "Notifications-Count: %d", notifications_count);
	growl_tcp_write(sock, "" );

	for(i=0;i<notifications_count;i++)
	{
		growl_tcp_write(sock, "Notification-Name: %s", notifications[i]);
		growl_tcp_write(sock, "Notification-Display-Name: %s", notifications[i]);
		growl_tcp_write(sock, "Notification-Enabled: True" );
		if(icon) growl_tcp_write(sock, "Notification-Icon: %s",  icon);
		growl_tcp_write(sock, "" );
	}
	while (1) {
		char* line = growl_tcp_read(sock);
		int len = strlen(line);
		/* fprintf(stderr, "%s\n", line); */
		if (strncmp(line, "GNTP/1.0 -ERROR", 15) == 0) {
			fprintf(stderr, "failed to register notification\n");
			free(line);
			goto leave;
		}
		free(line);
		if (len == 0) break;
	}
	growl_tcp_close(sock);
	sock = 0;

	leave:
	if (authheader) free(authheader);

	return (sock == 0) ? 0 : -1;
}
コード例 #2
0
ファイル: growl.c プロジェクト: kissthink/GNTPCom
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)
{
	int sock = -1;

	char *authheader = growl_generate_authheader_alloc(password);
	
	growl_init();

	sock = growl_tcp_open(server);
	if (sock == -1) goto leave;

	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 (icon) growl_tcp_write(sock, "Notification-Icon: %s", icon);
	if (url) growl_tcp_write(sock, "Notification-Callback-Target: %s", url  );

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

leave:
	if (authheader) free(authheader);

	return (sock == 0) ? 0 : -1;
}
コード例 #3
0
ファイル: growl.c プロジェクト: Azarien/processhacker2
/* 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;
}