/
growl-irssi.c
238 lines (180 loc) · 7.18 KB
/
growl-irssi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// =======================
// = Growl irssi module =
// = Reza Jelveh =
// =======================
#include <Carbon/Carbon.h>
#include "growl-irssi.h"
#include "GrowlApplicationBridge-Carbon.h"
#include "GrowlDefines.h"
#define NOTIFICATION_NAME CFSTR("IRSSI Growl")
#ifndef CFSafeRelease
#define CFSafeRelease(_cf) \
do \
{ \
if ((_cf) != NULL) \
{ \
CFRelease((CFTypeRef) _cf); \
_cf = NULL; \
} \
} \
while (0)
#endif // CFSafeRelease
static void message_incoming(SERVER_REC *server, char *msg, char *nick, char *address, char *target)
{
CFStringRef desc, title;
char * win_name;
// Get active window item
if((win_name = window_get_active_name(active_win)) && (strcmp(nick, win_name) == 0))
return;
title = CFStringCreateWithCString(kCFAllocatorDefault, nick, kCFStringEncodingUTF8);
desc = CFStringCreateWithCString(kCFAllocatorDefault, msg, kCFStringEncodingUTF8);
Growl_NotifyWithTitleDescriptionNameIconPriorityStickyClickContext(title, desc, NOTIFICATION_NAME, NULL, 0, FALSE, NULL);
CFSafeRelease(title);
CFSafeRelease(desc);
}
static void message_public(SERVER_REC *server, const char *msg,
const char *nick, const char *address,
const char *target)
{
CFStringRef desc, title;
// int nick_match_msg(CHANNEL_REC *channel, const char *msg, const char *nick)
CHANNEL_REC *chanrec;
chanrec = channel_find(server, target);
g_return_if_fail(chanrec != NULL);
if (nick_match_msg(chanrec, msg, server->nick) == FALSE)
return;
title = CFStringCreateWithCString(kCFAllocatorDefault, chanrec->name, kCFStringEncodingUTF8);
desc = CFStringCreateWithCString(kCFAllocatorDefault, msg, kCFStringEncodingUTF8);
Growl_NotifyWithTitleDescriptionNameIconPriorityStickyClickContext(title, desc, NOTIFICATION_NAME, NULL, 0, FALSE, NULL);
CFSafeRelease(title);
CFSafeRelease(desc);
}
static void command_helpgrowl(const char *arg, SERVER_REC *server, WI_ITEM_REC *item)
{
printtext(NULL, NULL, MSGLEVEL_CRAP,
"\n\002Growl help:\002 \n\n"
" /growlhelp to show this info\n"
"Growl will show notifications when you get a query or someone writes your name in public\n"
"Settings are not yet implemented.\n");
}
CFDataRef copyIconData(CFStringRef path)
{
CFDataRef data = NULL;
CFURLRef URL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
path, // file path name
kCFURLPOSIXPathStyle, // interpret as POSIX path
false ); // is it a directory?
if (URL) {
FSRef ref;
if (CFURLGetFSRef(URL, &ref)) {
IconRef icon = NULL;
SInt16 label_noOneCares;
OSStatus err = GetIconRefFromFileInfo(&ref,
/*inFileNameLength*/ 0U, /*inFileName*/ NULL,
kFSCatInfoNone, /*inCatalogInfo*/ NULL,
kIconServicesNoBadgeFlag | kIconServicesUpdateIfNeededFlag,
&icon,
&label_noOneCares);
if (err != noErr) {
printf("Could not get icon\n");
} else {
IconFamilyHandle fam = NULL;
// err = IconRefToIconFamily(icon, kSelectorAllAvailableData, &fam);
err = ReadIconFromFSRef(&ref, &fam);
if (err != noErr) {
printf("Could not get icon\n");
} else {
HLock((Handle)fam);
data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)*(Handle)fam, GetHandleSize((Handle)fam));
HUnlock((Handle)fam);
DisposeHandle((Handle)fam);
}
ReleaseIconRef(icon);
}
}
}
CFSafeRelease(URL);
return data;
}
void growl_init(void)
{
struct Growl_Delegate * delegateObject;
CFStringRef applicationName;
CFStringRef identifierString;
CFDataRef icon = NULL;
CFMutableStringRef iconPath = CFStringCreateMutable(kCFAllocatorDefault, 0);
char *home = getenv( "HOME" );
applicationName = CFSTR("growl-irssi");
identifierString = NULL;
CFStringAppendCString(iconPath, home, kCFStringEncodingUTF8);
CFStringAppend(iconPath, CFSTR("/.irssi/irssi.icns"));
icon = (CFDataRef)copyIconData(iconPath);
CFSafeRelease(iconPath);
// Can't register console app growl without icon data so don't even try
g_return_if_fail(icon != NULL);
CFStringRef name = NOTIFICATION_NAME;
CFArrayRef defaultAndAllNotifications = CFArrayCreate(kCFAllocatorDefault, (const void **)&name, 1, &kCFTypeArrayCallBacks);
CFTypeRef registerKeys[4] = {
GROWL_APP_NAME,
GROWL_NOTIFICATIONS_ALL,
GROWL_NOTIFICATIONS_DEFAULT,
GROWL_APP_ICON,
// GROWL_APP_ID
};
CFTypeRef registerValues[4] = {
applicationName,
defaultAndAllNotifications,
defaultAndAllNotifications,
icon
};
CFDictionaryRef registerInfo = CFDictionaryCreate(kCFAllocatorDefault, registerKeys, registerValues, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
delegateObject = (struct Growl_Delegate *)calloc(1, sizeof(struct Growl_Delegate));
delegateObject->applicationName = CFSTR("growl-irssi");
delegateObject->registrationDictionary = registerInfo;
delegateObject->applicationIconData = icon;
Growl_SetDelegate(delegateObject);
CFRelease(defaultAndAllNotifications);
CFRelease(icon);
CFRelease(applicationName);
// Setup irssi
signal_add_last("message private", (SIGNAL_FUNC) message_incoming);
signal_add_last("message public", (SIGNAL_FUNC) message_public);
command_bind("growlhelp", NULL, (SIGNAL_FUNC) command_helpgrowl);
command_bind("helpgrowl", NULL, (SIGNAL_FUNC) command_helpgrowl);
// Growl_NotifyWithTitleDescriptionNameIconPriorityStickyClickContext(CFSTR("growl-irssi"), CFSTR("Plugin successfully loaded."), NOTIFICATION_NAME, NULL, 0, FALSE, NULL);
printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
"Growl Support successfully loaded\n"
"Try /helpgrowl or /growlhelp for a short command overview");
module_register("growl", "core");
}
void growl_deinit(void)
{
signal_remove("message private", (SIGNAL_FUNC) message_incoming);
signal_remove("message public", (SIGNAL_FUNC) message_public);
command_unbind("growlhelp", (SIGNAL_FUNC) command_helpgrowl);
command_unbind("helpgrowl", (SIGNAL_FUNC) command_helpgrowl);
}
#if 0
CGImageRef MyCreateJPEGImageRef(){
CGImageRef image;
CGDataProviderRef provider;
CFStringRef name;
CFURLRef url;
// Get the URL to the bundle resource.
url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
CFSTR("/Users/reza/.irssi/irss.jpg"), // file path name
kCFURLPOSIXPathStyle, // interpret as POSIX path
false ); // is it a directory?
// Create the data provider object
provider = CGDataProviderCreateWithURL (url);
CFRelease (url);
printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,"debug1\n");
// Create the image object from that provider.
image = CGImageCreateWithJPEGDataProvider (provider, NULL, true,
kCGRenderingIntentDefault);
if (image == NULL)
printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,"debug2\n");
CGDataProviderRelease (provider);
return (image);
}
#endif