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 / adiumPurpleRequest.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
/* 
 * 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 "adiumPurpleRequest.h"
#import "ESPurpleRequestActionController.h"
#import "ESPurpleRequestWindowController.h"
#import "ESPurpleFileReceiveRequestController.h"
#import "AILibpurplePlugin.h"
#import "AMPurpleRequestFieldsController.h"

#import <Adium/AIContactAlertsControllerProtocol.h>
#import <Adium/ESFileTransfer.h>
#import <Adium/AIListContact.h>
#import <AIUtilities/AIObjectAdditions.h>

#import <AdiumLibpurple/SLPurpleCocoaAdapter.h>

#import <libintl/libintl.h>

/*
 * Purple requires us to return a handle from each of the request functions.  This handle is passed back to use in 
 * adiumPurpleRequestClose() if the request window is no longer valid -- for example, a chat invitation window is open,
 * and then the account disconnects.  All window controllers created from adiumPurpleRequest.m should return non-autoreleased
 * instances of themselves.  They then release themselves when their window closes.  Rather than calling
 * [[self window] close], they should use purple_request_close_with_handle(self) to ensure proper bookkeeping purpleside.
 */
 
//Jabber registration
#import <libpurple/jabber.h>

/* resolved id for Meanwhile */
struct resolved_id {
        char *id;
        char *name;
};

/*!
 * @brief Process button text, removing gtk+ accelerator underscores
 *
 * Textual underscores are indicated by "__"
 */
NSString *processButtonText(NSString *inButtonText)
{
        NSMutableString *processedText = [inButtonText mutableCopy];
        
#define UNDERSCORE_PLACEHOLDER @"&&&&&"

        //Replace escaped underscores with our placeholder
        [processedText replaceOccurrencesOfString:@"__"
                                                                   withString:UNDERSCORE_PLACEHOLDER
                                                                          options:NSLiteralSearch
                                                                                range:NSMakeRange(0, [processedText length])];
        //Remove solitary underscores
        [processedText replaceOccurrencesOfString:@"_"
                                                                   withString:@""
                                                                          options:NSLiteralSearch
                                                                                range:NSMakeRange(0, [processedText length])];

        //Replace the placeholder with an underscore
        [processedText replaceOccurrencesOfString:UNDERSCORE_PLACEHOLDER
                                                                   withString:@"_"
                                                                          options:NSLiteralSearch
                                                                                range:NSMakeRange(0, [processedText length])];
        
        return [processedText autorelease];
        
}

static void *adiumPurpleRequestInput(
                                                                   const char *title, const char *primary,
                                                                   const char *secondary, const char *defaultValue,
                                                                   gboolean multiline, gboolean masked, gchar *hint,
                                                                   const char *okText, GCallback okCb, 
                                                                   const char *cancelText, GCallback cancelCb,
                                                                   PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                   void *userData)
{
        /*
         Multiline should be a paragraph-sized box; otherwise, a single line will suffice.
         Masked means we want to use an NSSecureTextField sort of thing.
         We may receive any combination of primary and secondary text (either, both, or neither).
         */
        id                                      requestController = nil;
        NSString                        *primaryString = (primary ? [NSString stringWithUTF8String:primary] : nil);
        
        //Ignore purple trying to get an account's password; we'll feed it the password and reconnect if it gets here, somehow.
        if ([primaryString rangeOfString:@"Enter password for "].location != NSNotFound) return [NSNull null];
        
        NSMutableDictionary *infoDict;
        NSString                        *okButtonText = processButtonText([NSString stringWithUTF8String:okText]);
        NSString                        *cancelButtonText = processButtonText([NSString stringWithUTF8String:cancelText]);
        
        infoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:okButtonText,@"OK Text",
                cancelButtonText,@"Cancel Text",
                [NSValue valueWithPointer:okCb],@"OK Callback",
                [NSValue valueWithPointer:cancelCb],@"Cancel Callback",
                [NSValue valueWithPointer:userData],@"userData",nil];
        
        
        if (primaryString) [infoDict setObject:primaryString forKey:@"Primary Text"];
        if (title) [infoDict setObject:[NSString stringWithUTF8String:title] forKey:@"Title"];  
        if (defaultValue) [infoDict setObject:[NSString stringWithUTF8String:defaultValue] forKey:@"Default Value"];
        if (secondary) [infoDict setObject:[NSString stringWithUTF8String:secondary] forKey:@"Secondary Text"];
        
        [infoDict setObject:[NSNumber numberWithBool:multiline] forKey:@"Multiline"];
        [infoDict setObject:[NSNumber numberWithBool:masked] forKey:@"Masked"];
        
        AILogWithSignature(@"%@",infoDict);
        
        requestController = [ESPurpleRequestWindowController showInputWindowWithDict:infoDict];
        
        return requestController;
}

static void *adiumPurpleRequestChoice(const char *title, const char *primary,
                                                                        const char *secondary, gint defaultValue,
                                                                        const char *okText, GCallback okCb,
                                                                        const char *cancelText, GCallback cancelCb,
                                                                        PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                        void *userData, va_list choices)
{
        AILogWithSignature(@"%s\n%s\n%s ",
                           (title ? title : ""),
                           (primary ? primary : ""),
                           (secondary ? secondary : ""));
        
        return nil;
}

//Purple requests the user take an action such as accept or deny a buddy's attempt to add us to her list 
static void *adiumPurpleRequestAction(const char *title, const char *primary,
                                                                        const char *secondary, gint default_action,
                                                                        PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                        void *userData,
                                                                        size_t actionCount, va_list actions)
{
    NSString                    *titleString = (title ? [NSString stringWithUTF8String:title] : @"");
        NSString                        *primaryString = (primary ? [NSString stringWithUTF8String:primary] : nil);
        id                                      requestController = nil;
        NSInteger                                       i;
        BOOL                            handled = NO;

        if (primaryString && ([primaryString isEqualToString:[NSString stringWithFormat:[NSString stringWithUTF8String:_("%s has just asked to directly connect to %s")],
                                                                                                                  who, purple_account_get_username(account)]])) {
                AIListContact *adiumContact = contactLookupFromBuddy(purple_find_buddy(account, who));

                // Look up the user preference for this setting -- we use the same settings as the File Transfer code.
                AIFileTransferAutoAcceptType autoAccept = [[adium.preferenceController preferenceForKey:KEY_FT_AUTO_ACCEPT 
                                                                                                                                                                                        group:PREF_GROUP_FILE_TRANSFER] integerValue];
                if ((autoAccept == AutoAccept_All) || 
                        ((autoAccept == AutoAccept_FromContactList) && adiumContact && [adiumContact isIntentionallyNotAStranger])) {
                        GCallback ok_cb;

                        //Get the callback for Connect, skipping over the title
                        va_arg(actions, char *);
                        ok_cb = va_arg(actions, GCallback);
                        
                        ((PurpleRequestActionCb)ok_cb)(userData, default_action);
                        
                        handled = YES;
                }
        } else if (primary && strcmp(primary, _("Accept chat invitation?")) == 0) {
                AIListContact *contact = contactLookupFromBuddy(purple_find_buddy(account, who));
                [adium.contactAlertsController generateEvent:CONTENT_GROUP_CHAT_INVITE
                                                                                 forListObject:contact
                                                                                          userInfo:[NSDictionary dictionary]
                                                  previouslyPerformedActionIDs:nil];    
        }

        if (!handled) {
                NSString                *secondaryString = (secondary ? [NSString stringWithUTF8String:secondary] : nil);
                NSMutableArray  *buttonNamesArray = [NSMutableArray arrayWithCapacity:actionCount];
                GCallback               *callBacks = g_new0(GCallback, actionCount);
                
                //Generate the actions names and callbacks into useable forms
                for (i = 0; i < actionCount; i += 1) {
                        char *buttonName;
                        
                        //Get the name
                        buttonName = va_arg(actions, char *);
                        [buttonNamesArray addObject:processButtonText([NSString stringWithUTF8String:buttonName])];
                        
                        //Get the callback for that name
                        callBacks[i] = va_arg(actions, GCallback);
                }
                
                //Make default_action (or first if none specified) the last one
                if (default_action < (NSInteger)actionCount-1) {
                        // If there's no default_action, assume the first one is, and move it to the end.
                        if (default_action == -1)
                                default_action = 0;

                        GCallback tempCallBack = callBacks[actionCount-1];
                        callBacks[actionCount-1] = callBacks[default_action];
                        callBacks[default_action] = tempCallBack;
                        
                        [buttonNamesArray exchangeObjectAtIndex:default_action withObjectAtIndex:(actionCount-1)];
                }
                
                NSMutableDictionary     *infoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                                                 buttonNamesArray,@"Button Names",
                                                                                 [NSValue valueWithPointer:callBacks],@"callBacks",
                                                                                 [NSValue valueWithPointer:userData],@"userData",
                                                                                 titleString,@"TitleString",nil];
                
                // If we have both a primary and secondary string, use the primary as a header.
                if (secondaryString) {
                        [infoDict setObject:primaryString forKey:@"MessageHeader"];
                        [infoDict setObject:secondaryString forKey:@"Message"];
                } else {
                        [infoDict setObject:primaryString forKey:@"Message"];
                }
                
                AIAccount *adiumAccount = accountLookup(account);
                if (adiumAccount) {
                        [infoDict setObject:adiumAccount forKey:@"AIAccount"];
                }
                
                if (who) {
                        AIListContact *adiumContact = contactLookupFromBuddy(purple_find_buddy(account, who));
                        if (adiumContact) {
                                [infoDict setObject:adiumContact forKey:@"AIListContact"];
                        }
                        
                        [infoDict setObject:[NSString stringWithUTF8String:who] forKey:@"who"];
                }

                requestController = [ESPurpleRequestActionController showActionWindowWithDict:infoDict];
        }

        return requestController;
}

static void *adiumPurpleRequestFields(const char *title, const char *primary,
                                                                        const char *secondary, PurpleRequestFields *fields,
                                                                        const char *okText, GCallback okCb,
                                                                        const char *cancelText, GCallback cancelCb,
                                                                        PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                        void *userData)
{
        id                                      requestController = nil;
        NSString                        *titleString = (title ?  [[NSString stringWithUTF8String:title] lowercaseString] : nil);

    if (titleString && 
                [titleString rangeOfString:@"new jabber"].location != NSNotFound) {
                /* Jabber registration request. Instead of displaying a request dialogue, we fill in the information automatically.
                 * And by that, I mean that we accept all the default empty values, since the username and password are preset for us. */
                ((PurpleRequestFieldsCb)okCb)(userData, fields);
                
        } else {
                AILog(@"adiumPurpleRequestFields: %s\n%s\n%s ",
                                   (title ? title : ""),
                                   (primary ? primary : ""),
                                   (secondary ? secondary : ""));
        
        id self = (CBPurpleAccount*)account->ui_data; // for AILocalizedString
                
        requestController = [[AMPurpleRequestFieldsController alloc] initWithTitle:title?[NSString stringWithUTF8String:title]:nil
                                                                       primaryText:primary?[NSString stringWithUTF8String:primary]:nil
                                                                     secondaryText:secondary?[NSString stringWithUTF8String:secondary]:nil
                                                                     requestFields:fields
                                                                            okText:okText?[NSString stringWithUTF8String:okText]:AILocalizedString(@"OK",nil)
                                                                          callback:okCb
                                                                        cancelText:cancelText?[NSString stringWithUTF8String:cancelText]:AILocalizedString(@"Cancel",nil)
                                                                          callback:cancelCb
                                                                           account:(CBPurpleAccount*)account->ui_data
                                                                               who:who?[NSString stringWithUTF8String:who]:nil
                                                                      conversation:conv
                                                                          userData:userData];
#if 0
                GList                                   *gl, *fl, *field_list;
                PurpleRequestFieldGroup *group;

                //Look through each group, processing each field
                for (gl = purple_request_fields_get_groups(fields);
                         gl != NULL;
                         gl = gl->next) {
                        
                        group = gl->data;
                        field_list = purple_request_field_group_get_fields(group);
                        
                        for (fl = field_list; fl != NULL; fl = fl->next) {
                                /*
                                typedef enum
                                {
                                        PURPLE_REQUEST_FIELD_NONE,
                                        PURPLE_REQUEST_FIELD_STRING,
                                        PURPLE_REQUEST_FIELD_INTEGER,
                                        PURPLE_REQUEST_FIELD_BOOLEAN,
                                        PURPLE_REQUEST_FIELD_CHOICE,
                                        PURPLE_REQUEST_FIELD_LIST,
                                        PURPLE_REQUEST_FIELD_LABEL,
                                        PURPLE_REQUEST_FIELD_ACCOUNT
                                } PurpleRequestFieldType;
                                */

                                /*
                                PurpleRequestField              *field;
                                PurpleRequestFieldType  type;
                                
                                field = (PurpleRequestField *)fl->data;
                                type = purple_request_field_get_type(field);
                                if (type == PURPLE_REQUEST_FIELD_STRING) {
                                        if (strcasecmp("username", purple_request_field_get_label(field)) == 0) {
                                                purple_request_field_string_set_value(field, purple_account_get_username(account));
                                        } else if (strcasecmp("password", purple_request_field_get_label(field)) == 0) {
                                                purple_request_field_string_set_value(field, purple_account_get_password(account));
                                        }
                                }
                                 */
                        }
                        
                }
//              ((PurpleRequestFieldsCb)okCb)(userData, fields);
#endif
        }
    
        return requestController;
}

static void *adiumPurpleRequestFile(const char *title, const char *filename,
                                                                  gboolean savedialog, GCallback ok_cb,
                                                                  GCallback cancel_cb,
                                                                  PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                  void *user_data)
{       
        if (title) {
                NSString *titleString = (title ? [NSString stringWithUTF8String:title] : nil);
                if (savedialog) {
                        NSSavePanel *savePanel = [NSSavePanel savePanel];
                        if ([titleString length]) [savePanel setTitle:titleString];

                        if ([savePanel runModalForDirectory:nil file:nil] == NSOKButton) {
                                ((PurpleRequestFileCb)ok_cb)(user_data, [[savePanel filename] UTF8String]);
                        }                       
                } else {
                        NSOpenPanel *openPanel = [NSOpenPanel openPanel];
                        if ([titleString length]) [openPanel setTitle:titleString];

                        if ([openPanel runModalForDirectory:nil file:nil types:nil] == NSOKButton) {
                                ((PurpleRequestFileCb)ok_cb)(user_data, [[openPanel filename] UTF8String]);
                        }
                }
        } else {
                /* Only file transfer file requests lack a title */
                PurpleXfer *xfer = (PurpleXfer *)user_data;
                if (xfer) {
                        PurpleXferType xferType = purple_xfer_get_type(xfer);
                        
                        if (xferType == PURPLE_XFER_RECEIVE) {
                                AILog(@"*** WARNING: File request: %s from %s on IP %s which wasn't handled by the file-recv-request signal",
                                          xfer->filename,xfer->who,purple_xfer_get_remote_ip(xfer));
                                /* We should never get here.  The file-recv-request signal is posted before we could.  We handle that signal
                                 * (in adiumPurpleSignals) and set a local filename when we do to prevent being prompted via the request_file() ui op.
                                 */
                                
                        } else if (xferType == PURPLE_XFER_SEND) {
                                /*
                                 * Um, yes, we've already set the local filename... which should be the same as the file name for the transfer itself...
                                 * and we do, in fact, want to send. Call the OK callback immediately.
                                 */
                                if (xfer->local_filename != NULL && xfer->filename != NULL) {
                                        AILog(@"PURPLE_XFER_SEND: %x (%s)",xfer,xfer->local_filename);
                                        ((PurpleRequestFileCb)ok_cb)(user_data, xfer->local_filename);
                                } else {
                                        ((PurpleRequestFileCb)cancel_cb)(user_data, xfer->local_filename);
                                        [[SLPurpleCocoaAdapter sharedInstance] displayFileSendError];
                                }
                        }
                }
        }
        
        return NULL;
}

/*!
 * @brief Purple requests that we close a request window
 *
 * This is not sent after user interaction with the window.  Instead, it is sent when the window is no longer valid;
 * for example, a chat invite window after the relevant account disconnects.  We should immediately close the window.
 *
 * @param type The request type
 * @param uiHandle must be an id; it should either be NSNull or an object which can respond to close, such as NSWindowController.
 */
static void adiumPurpleRequestClose(PurpleRequestType type, void *uiHandle)
{
        id      ourHandle = (id)uiHandle;
        AILogWithSignature(@"%@ (%i)",uiHandle,[ourHandle respondsToSelector:@selector(purpleRequestClose)]);
        if ([ourHandle respondsToSelector:@selector(purpleRequestClose)]) {
                [ourHandle purpleRequestClose];

        } else if ([ourHandle respondsToSelector:@selector(closeWindow:)]) {
                [ourHandle closeWindow:nil];
        }
}

static void *adiumPurpleRequestFolder(const char *title, const char *dirname, GCallback ok_cb, GCallback cancel_cb,
                                                                          PurpleAccount *account, const char *who, PurpleConversation *conv,
                                                                          void *user_data)
{
        AILogWithSignature(@"");

        return NULL;
}

static PurpleRequestUiOps adiumPurpleRequestOps = {
    adiumPurpleRequestInput,
    adiumPurpleRequestChoice,
    adiumPurpleRequestAction,
    adiumPurpleRequestFields,
        adiumPurpleRequestFile,
    adiumPurpleRequestClose,
        adiumPurpleRequestFolder
};

PurpleRequestUiOps *adium_purple_request_get_ui_ops()
{
        return &adiumPurpleRequestOps;
}

@implementation ESPurpleRequestAdapter

+ (void)requestCloseWithHandle:(id)handle
{
        AILogWithSignature(@"%@", handle);
        purple_request_close_with_handle(handle);
}

@end