William Bowling is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

wbowling / adium (fork of adium / adium)

Fork of Adium for patches/improvements

Clone this repository (size: 338.7 MB): HTTPS / SSH
hg clone https://bitbucket.org/wbowling/adium
hg clone ssh://hg@bitbucket.org/wbowling/adium

adium / Plugins / Purple Service / adiumPurpleEventloop.m

  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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* 
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "adiumPurpleEventloop.h"
#import <AIUtilities/AIApplicationAdditions.h>
#import <poll.h>
#import <unistd.h>
#import <sys/socket.h>
#import <sys/select.h>

//#define PURPLE_SOCKET_DEBUG

static guint                            sourceId = 0;           //The next source key; continuously incrementing
static CFRunLoopRef                     purpleRunLoop = nil;

static void socketCallback(CFSocketRef s,
                           CFSocketCallBackType callbackType,
                           CFDataRef address,
                           const void *data,
                           void *infoVoid);
/*
 * The sources, keyed by integer key id (wrapped in an NSNumber), holding
 * SourceInfo * objects
 */
static NSMutableDictionary      *sourceInfoDict = nil;

/*!
 * @class SourceInfo
 * @brief Holder for various source/timer information
 *
 * This serves as the context info for source and timer callbacks.  We use it just as a
 * struct (declaring all the class's ivars to be public) but make it an object so we can use
 * reference counting on it easily.
 */
@interface SourceInfo : NSObject {

@public CFSocketRef socket;
@public NSInteger fd;
@public CFRunLoopSourceRef run_loop_source;
        
@public guint timer_tag;
@public GSourceFunc timer_function;
@public CFRunLoopTimerRef timer;
@public gpointer timer_user_data;
        
@public guint read_tag;
@public PurpleInputFunction read_ioFunction;
@public gpointer read_user_data;
        
@public guint write_tag;
@public PurpleInputFunction write_ioFunction;
@public gpointer write_user_data;       
}
@end

@implementation SourceInfo
- (NSString *)description
{
        return [NSString stringWithFormat:@"<SourceInfo %p: Socket %p: fd %i; timer_tag %i; read_tag %i; write_tag %i>",
                        self, socket, fd, timer_tag, read_tag, write_tag];
}
@end

static SourceInfo *createSourceInfo(void)
{
        SourceInfo *info = [[SourceInfo alloc] init];

        info->socket = NULL;
        info->fd = 0;
        info->run_loop_source = NULL;

        info->timer_tag = 0;
        info->timer_function = NULL;
        info->timer = NULL;
        info->timer_user_data = NULL;

        info->write_tag = 0;
        info->write_ioFunction = NULL;
        info->write_user_data = NULL;

        info->read_tag = 0;
        info->read_ioFunction = NULL;
        info->read_user_data = NULL;    
        
        return info;
}

#pragma mark Remove

/*!
 * @brief Given a SourceInfo struct for a socket which was for reading *and* writing, recreate its socket to be for just one
 *
 * If the sourceInfo still has a read_tag, the resulting CFSocket will be just for reading.
 * If the sourceInfo still has a write_tag, the resulting CFSocket will be just for writing.
 *
 * This is necessary to prevent the now-unneeded condition from triggerring its callback.
 */
void updateSocketForSourceInfo(SourceInfo *sourceInfo)
{
        CFSocketRef socket = sourceInfo->socket;
        
        if (!socket) return;

        //Reading
#ifdef PURPLE_SOCKET_DEBUG
        AILogWithSignature(@"%@", sourceInfo);
#endif
        if (sourceInfo->read_tag)
                CFSocketEnableCallBacks(socket, kCFSocketReadCallBack);
        else
                CFSocketDisableCallBacks(socket, kCFSocketReadCallBack);

        //Writing
        if (sourceInfo->write_tag)
                CFSocketEnableCallBacks(socket, kCFSocketWriteCallBack);
        else
                CFSocketDisableCallBacks(socket, kCFSocketWriteCallBack);
        
        //Re-enable callbacks automatically and, by starting with 0, _don't_ close the socket on invalidate
        CFOptionFlags flags = 0;
        
        if (sourceInfo->read_tag) flags |= kCFSocketAutomaticallyReenableReadCallBack;
        if (sourceInfo->write_tag) flags |= kCFSocketAutomaticallyReenableWriteCallBack;
        
        CFSocketSetSocketFlags(socket, flags);
}

gboolean adium_source_remove(guint tag) {
        NSNumber *tagNumber = [[NSNumber alloc] initWithUnsignedInteger:tag];
    SourceInfo *sourceInfo = (SourceInfo *)[sourceInfoDict objectForKey:tagNumber];
        BOOL didRemove;

    if (sourceInfo) {
#ifdef PURPLE_SOCKET_DEBUG
                AILog(@"adium_source_remove(): Removing for fd %i [sourceInfo %x]: tag is %i (timer %i, read %i, write %i)",sourceInfo->fd,
                          sourceInfo, tag, sourceInfo->timer_tag, sourceInfo->read_tag, sourceInfo->write_tag);
#endif
                if (sourceInfo->timer_tag == tag) {
                        sourceInfo->timer_tag = 0;

                } else if (sourceInfo->read_tag == tag) {
                        sourceInfo->read_tag = 0;

                } else if (sourceInfo->write_tag == tag) {
                        sourceInfo->write_tag = 0;

                }
                
                if (sourceInfo->timer_tag == 0 && sourceInfo->read_tag == 0 && sourceInfo->write_tag == 0) {
                        //It's done
                        if (sourceInfo->timer) { 
                                CFRunLoopTimerInvalidate(sourceInfo->timer);
                                CFRelease(sourceInfo->timer);
                                sourceInfo->timer = NULL;
                        }
                        
                        if (sourceInfo->socket) {
#ifdef PURPLE_SOCKET_DEBUG
                                AILog(@"adium_source_remove(): Done with a socket %x, so invalidating it",sourceInfo->socket);
#endif
                                CFSocketInvalidate(sourceInfo->socket);
                                CFRelease(sourceInfo->socket);
                                sourceInfo->socket = NULL;
                        }

                        if (sourceInfo->run_loop_source) {
                                CFRelease(sourceInfo->run_loop_source);
                                sourceInfo->run_loop_source = NULL;
                        }
                } else {
                        if ((sourceInfo->timer_tag == 0) && (sourceInfo->timer)) {
                                CFRunLoopTimerInvalidate(sourceInfo->timer);
                                CFRelease(sourceInfo->timer);
                                sourceInfo->timer = NULL;
                        }
                        
                        if (sourceInfo->socket && (sourceInfo->read_tag || sourceInfo->write_tag)) {
#ifdef PURPLE_SOCKET_DEBUG
                                AILog(@"adium_source_remove(): Calling updateSocketForSourceInfo(%x)",sourceInfo);
#endif                          
                                updateSocketForSourceInfo(sourceInfo);
                        }
                }
                
                [sourceInfoDict removeObjectForKey:tagNumber];

                didRemove = TRUE;

        } else {
                didRemove = FALSE;
        }

        [tagNumber release];

        return didRemove;
}

//Like g_source_remove, return TRUE if successful, FALSE if not
gboolean adium_timeout_remove(guint tag) {
    return (adium_source_remove(tag));
}

#pragma mark Add

void callTimerFunc(CFRunLoopTimerRef timer, void *info)
{
        SourceInfo *sourceInfo = info;

        if (![sourceInfoDict objectForKey:[NSNumber numberWithUnsignedInteger:sourceInfo->timer_tag]])
                NSLog(@"**** WARNING: %@ has already been removed, but we're calling its timer function!", info);

        if (!sourceInfo->timer_function ||
                !sourceInfo->timer_function(sourceInfo->timer_user_data)) {
        adium_source_remove(sourceInfo->timer_tag);
        }
}

guint adium_timeout_add(guint interval, GSourceFunc function, gpointer data)
{
    SourceInfo *info = createSourceInfo();
        
        NSTimeInterval intervalInSec = (NSTimeInterval)interval/1000;
        
        CFRunLoopTimerContext runLoopTimerContext = { 0, info, CFRetain, CFRelease, /* CFAllocatorCopyDescriptionCallBack */ NULL };
        CFRunLoopTimerRef runLoopTimer = CFRunLoopTimerCreate(
                                                                                                                  NULL, /* default allocator */
                                                                                                                  (CFAbsoluteTimeGetCurrent() + intervalInSec), /* The time at which the timer should first fire */
                                                                                                                  intervalInSec, /* firing interval */
                                                                                                                  0, /* flags, currently ignored */
                                                                                                                  0, /* order, currently ignored */
                                                                                                                  callTimerFunc, /* CFRunLoopTimerCallBack callout */
                                                                                                                  &runLoopTimerContext /* context */
                                                                                                                  );
        guint timer_tag = ++sourceId;
        info->timer_function = function;
        info->timer = runLoopTimer;
        info->timer_user_data = data;   
        info->timer_tag = timer_tag;

        NSNumber *tagNumber = [[NSNumber alloc] initWithUnsignedInteger:timer_tag];
        [sourceInfoDict setObject:info
                                           forKey:tagNumber];
        [tagNumber release];

        CFRunLoopAddTimer(purpleRunLoop, runLoopTimer, kCFRunLoopCommonModes);
        [info release];

        return timer_tag;
}

guint adium_input_add(gint fd, PurpleInputCondition condition,
                                          PurpleInputFunction func, gpointer user_data)
{       
        if (fd < 0) {
                NSLog(@"INVALID: fd was %i; returning tag %i",fd,sourceId+1);
                return ++sourceId;
        }

    SourceInfo *info = createSourceInfo();
        
    // And likewise the entire CFSocket
    CFSocketContext context = { 0, info, CFRetain, CFRelease, /* CFAllocatorCopyDescriptionCallBack */ NULL };

        /*
         * From CFSocketCreateWithNative:
         * If a socket already exists on this fd, CFSocketCreateWithNative() will return that existing socket, and the other parameters
         * will be ignored.
         */
#ifdef PURPLE_SOCKET_DEBUG
        AILog(@"adium_input_add(): Adding input %i on fd %i", condition, fd);
#endif
        CFSocketRef socket = CFSocketCreateWithNative(NULL,
                                                                                                  fd,
                                                                                                  (kCFSocketReadCallBack | kCFSocketWriteCallBack),
                                                                                                  socketCallback,
                                                                                                  &context);

        /* If we did not create a *new* socket, it is because there is already one for this fd in the run loop.
         * See the CFSocketCreateWithNative() documentation), add it to the run loop.
         * In that case, the socket's info was not updated.
         */
        CFSocketContext actualSocketContext = { 0, NULL, NULL, NULL, NULL };
        CFSocketGetContext(socket, &actualSocketContext);
        if (actualSocketContext.info != info) {
                [info release];
                CFRelease(socket);
                info = [(SourceInfo *)(actualSocketContext.info) retain];
        }

        info->fd = fd;
        info->socket = socket;

    if ((condition & PURPLE_INPUT_READ)) {
                info->read_tag = ++sourceId;
                info->read_ioFunction = func;
                info->read_user_data = user_data;
                
                NSNumber *tagNumber = [[NSNumber alloc] initWithUnsignedInteger:info->read_tag];
                [sourceInfoDict setObject:info
                                                   forKey:tagNumber];
                [tagNumber release];
                
        } else {
                info->write_tag = ++sourceId;
                info->write_ioFunction = func;
                info->write_user_data = user_data;
                
                NSNumber *tagNumber = [[NSNumber alloc] initWithUnsignedInteger:info->write_tag];
                [sourceInfoDict setObject:info
                                                   forKey:tagNumber];
                [tagNumber release];
        }
        
        updateSocketForSourceInfo(info);
        
        //Add it to our run loop
        if (!(info->run_loop_source)) {
                info->run_loop_source = CFSocketCreateRunLoopSource(NULL, socket, 0);
                if (info->run_loop_source) {
                        CFRunLoopAddSource(purpleRunLoop, info->run_loop_source, kCFRunLoopCommonModes);
                } else {
                        AILog(@"*** Unable to create run loop source for %p",socket);
                }               
        }

        [info release];

    return sourceId;
}

#pragma mark Socket Callback
static void socketCallback(CFSocketRef s,
                                                   CFSocketCallBackType callbackType,
                                                   CFDataRef address,
                                                   const void *data,
                                                   void *infoVoid)
{
    SourceInfo *sourceInfo = (SourceInfo *)infoVoid;
        gpointer user_data;
    PurpleInputCondition c;
        PurpleInputFunction ioFunction = NULL;
        gint     fd = sourceInfo->fd;

    if ((callbackType & kCFSocketReadCallBack)) {
                if (sourceInfo->read_tag) {
                        user_data = sourceInfo->read_user_data;
                        c = PURPLE_INPUT_READ;
                        ioFunction = sourceInfo->read_ioFunction;
                } else {
                        AILog(@"Called read with no read_tag %@", sourceInfo);
                }

        } else /* if ((callbackType & kCFSocketWriteCallBack)) */ {
                if (sourceInfo->write_tag) {
                        user_data = sourceInfo->write_user_data;
                        c = PURPLE_INPUT_WRITE; 
                        ioFunction = sourceInfo->write_ioFunction;
                } else {
                        AILog(@"Called write with no write_tag %@", sourceInfo);
                }
        }

        if (ioFunction) {
#ifdef PURPLE_SOCKET_DEBUG
                AILog(@"socketCallback(): Calling the ioFunction for %x, callback type %i (%s: tag is %i)",s,callbackType,
                          ((callbackType & kCFSocketReadCallBack) ? "reading" : "writing"),
                          ((callbackType & kCFSocketReadCallBack) ? sourceInfo->read_tag : sourceInfo->write_tag));
#endif
                ioFunction(user_data, fd, c);
        }
}

gint adium_input_get_error(gint fd, NSInteger *error)
{
        gint              ret;
        socklen_t len;
        len = sizeof(*error);
        
        ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, error, &len);
        if (!ret && !(*error)) {
                /*
                 * Taken from Fire's FaimP2PConnection.m:
                 * The job of this function is to detect if the connection failed or not
                 * There has to be a better way to do this
                 *
                 * Any socket that fails to connect will select for reading and writing
                 * and all reads and writes will fail
                 * Any listening socket will select for reading, and any read will fail
                 * So, select for writing, if you can write, and the write fails, not connected
                 */
                
                {
                        fd_set thisfd;
                        struct timeval timeout;
                        
                        FD_ZERO(&thisfd);
                        FD_SET(fd, &thisfd);
                        timeout.tv_sec = 0;
                        timeout.tv_usec = 0;
                        select(fd+1, NULL, &thisfd, NULL, &timeout);
                        if(FD_ISSET(fd, &thisfd)){
                                ssize_t length = 0;
                                char buffer[4] = {0, 0, 0, 0};
                                
                                length = write(fd, buffer, length);
                                if(length == -1)
                                {
                                        /* Not connected */
                                        ret = -1;
                                        *error = ENOTCONN;
                                        AILog(@"adium_input_get_error(%i): Socket is NOT valid", fd);
                                }
                        }
                }
        }

        return ret;
}

static PurpleEventLoopUiOps adiumEventLoopUiOps = {
    adium_timeout_add,
    adium_timeout_remove,
    adium_input_add,
    adium_source_remove,
        adium_input_get_error,
        /* timeout_add_seconds */ NULL
};

PurpleEventLoopUiOps *adium_purple_eventloop_get_ui_ops(void)
{
        if (!sourceInfoDict) sourceInfoDict = [[NSMutableDictionary alloc] init];

        //Determine our run loop
        purpleRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
        CFRetain(purpleRunLoop);

        return &adiumEventLoopUiOps;
}