Update the icon in the personal preferences if it's changed elsewhere (the contact...
[adiumx.git] / Source / ESOTRUnknownFingerprintController.m
blob1c1dcc58b6d595f5a40725ee3764040a527d801a
1 /* 
2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "ESOTRUnknownFingerprintController.h"
18 #import "ESTextAndButtonsWindowController.h"
19 #import <Adium/AIAccount.h>
20 #import <Adium/AIService.h>
21 #import "AIHTMLDecoder.h"
22 #import <Adium/AIServiceIcons.h>
23 #import <Adium/AIAccountControllerProtocol.h>
25 #import "AdiumOTREncryption.h"
27 @interface ESOTRUnknownFingerprintController (PRIVATE)
28 + (void)showFingerprintPromptWithMessageString:(NSString *)messageString 
29                                                                   acceptButton:(NSString *)acceptButton
30                                                                         denyButton:(NSString *)denyButton
31                                                                   responseInfo:(NSDictionary *)responseInfo;
32 + (void)unknownFingerprintResponseInfo:(NSDictionary *)responseInfo wasAccepted:(BOOL)fingerprintAccepted;
33 @end
35 @implementation ESOTRUnknownFingerprintController
37 + (void)showUnknownFingerprintPromptWithResponseInfo:(NSDictionary *)responseInfo
39         NSString        *messageString;
40         AIAccount       *account = [responseInfo objectForKey:@"AIAccount"];
41         NSString        *who = [responseInfo objectForKey:@"who"];
42         NSString        *ourHash = [responseInfo objectForKey:@"Outgoing SessionID"];
43         NSString        *theirHash = [responseInfo objectForKey:@"Incoming SessionID"];
44         
45         messageString = [NSString stringWithFormat:
46                 AILocalizedString(@"%@ has sent you (%@) an unknown encryption fingerprint.\n\n"
47                                                   "Fingerprint for you: %@\n\n"
48                                                   "Purported fingerprint for %@: %@\n\n"
49                                                   "Accept this fingerprint as verified?",nil),
50                 who,
51                 [account formattedUID],
52                 ourHash,
53                 who,
54                 theirHash];
55         
56         [self showFingerprintPromptWithMessageString:messageString 
57                                                                         acceptButton:AILocalizedString(@"Accept",nil)
58                                                                           denyButton:AILocalizedString(@"Verify Later",nil)
59                                                                         responseInfo:responseInfo];
62 + (void)showVerifyFingerprintPromptWithResponseInfo:(NSDictionary *)responseInfo
64         NSString        *messageString;
65         AIAccount       *account = [responseInfo objectForKey:@"AIAccount"];
66         NSString        *who = [responseInfo objectForKey:@"who"];
67         NSString        *ourHash = [responseInfo objectForKey:@"Outgoing SessionID"];
68         NSString        *theirHash = [responseInfo objectForKey:@"Incoming SessionID"];
70         messageString = [NSString stringWithFormat:
71                 AILocalizedString(@"Fingerprint for you (%@): %@\n\n"
72                                                   "Purported fingerprint for %@: %@\n\n"
73                                                   "Is this the verifiably correct fingerprint for %@?",nil),
74                 [account formattedUID],
75                 ourHash,
76                 who,
77                 theirHash,
78                 who];
80         [self showFingerprintPromptWithMessageString:messageString
81                                                                         acceptButton:AILocalizedString(@"Yes",nil)
82                                                                           denyButton:AILocalizedString(@"No",nil)
83                                                                         responseInfo:responseInfo];
86 + (void)showFingerprintPromptWithMessageString:(NSString *)messageString 
87                                                                   acceptButton:(NSString *)acceptButton
88                                                                         denyButton:(NSString *)denyButton
89                                                                   responseInfo:(NSDictionary *)responseInfo
91         ESTextAndButtonsWindowController        *windowController;
93         AIAccount       *account = [responseInfo objectForKey:@"AIAccount"];
95         NSImage         *serviceImage = nil;
96         
97         if (account) {
98                 serviceImage = [AIServiceIcons serviceIconForObject:account
99                                                                                                            type:AIServiceIconLarge
100                                                                                                   direction:AIIconNormal];
101         }
102         
103         windowController = [ESTextAndButtonsWindowController showTextAndButtonsWindowWithTitle:AILocalizedString(@"OTR Fingerprint Verification",nil)
104                                                                                                                                                          defaultButton:acceptButton
105                                                                                                                                                    alternateButton:denyButton
106                                                                                                                                                            otherButton:AILocalizedString(@"Help", nil)
107                                                                                                                                                                   onWindow:nil
108                                                                                                                                                  withMessageHeader:nil
109                                                                                                                                                                 andMessage:[AIHTMLDecoder decodeHTML:messageString]
110                                                                                                                                                                          image:serviceImage
111                                                                                                                                                                         target:self
112                                                                                                                                                                   userInfo:responseInfo];       
116 * @brief Window was closed, either by a button being clicked or the user closing it
117  */
118 + (BOOL)textAndButtonsWindowDidEnd:(NSWindow *)window returnCode:(AITextAndButtonsReturnCode)returnCode userInfo:(id)userInfo
120         BOOL    shouldCloseWindow = YES;
121         
122         if (userInfo && [userInfo objectForKey:@"Fingerprint"]) {
123                 BOOL    fingerprintAccepted;
124                 
125                 if (returnCode == AITextAndButtonsOtherReturn) {
126                         NSString                        *who = [userInfo objectForKey:@"who"];
127                         
128                         NSString *message = [NSString stringWithFormat:AILocalizedString(@"A fingerprint is a unique identifier "
129                                                                                                                                                          "that you should use to verify the identity of %@.\n\nTo verify the fingerprint, contact %@ via some "
130                                                                                                                                                          "other authenticated channel such as the telephone or GPG-signed email. "
131                                                                                                                                                          "Each of you should tell your fingerprint to the other.", nil),
132                                 who,
133                                 who];
134                         
135                         [ESTextAndButtonsWindowController showTextAndButtonsWindowWithTitle:nil
136                                                                                                                                   defaultButton:nil
137                                                                                                                                 alternateButton:nil
138                                                                                                                                         otherButton:nil
139                                                                                                                                            onWindow:window
140                                                                                                                           withMessageHeader:AILocalizedString(@"Fingerprint Help", nil)
141                                                                                                                                          andMessage:[[[NSAttributedString alloc] initWithString:message] autorelease]
142                                                                                                                                                  target:self
143                                                                                                                                            userInfo:nil];       
144                         
145                         //Don't close the original window if the help button is pressed
146                         shouldCloseWindow = NO;
147                         
148                 } else {
149                         fingerprintAccepted = ((returnCode == AITextAndButtonsDefaultReturn) ? YES : NO);
150                         
151                         [self unknownFingerprintResponseInfo:userInfo
152                                                                          wasAccepted:fingerprintAccepted];
153                 }
154         }
155         
156         return shouldCloseWindow;
159 + (void)unknownFingerprintResponseInfo:(NSDictionary *)responseInfo wasAccepted:(BOOL)fingerprintAccepted
161         AIAccount       *account = [responseInfo objectForKey:@"AIAccount"];
162         NSString        *who = [responseInfo objectForKey:@"who"];
163         
164         ConnContext *context = otrl_context_find(otrg_get_userstate(),
165                                                                                          [who UTF8String], [[account internalObjectID] UTF8String],
166                                                                                          [[[account service] serviceCodeUniqueID] UTF8String],
167                                                                                          0, NULL, NULL, NULL);
168     Fingerprint *fprint;
169     BOOL oldtrust;
170         
171     if (context == NULL) {
172                 AILog(@"Warning: ESOTRUnknownFingerprintController: NULL context for %@",responseInfo);
173                 return;
174         }
175         
176         fprint = context->active_fingerprint;
178     if (fprint == NULL) {
179                 AILog(@"Warning: ESOTRUnknownFingerprintController: NULL fprint for %@",responseInfo);
180                 return;
181         }
182         
183     oldtrust = (fprint->trust && fprint->trust[0]);
184         
185     /* See if anything's changed */
186     if (fingerprintAccepted != oldtrust) {
187                 otrl_context_set_trust(fprint, fingerprintAccepted ? "verified" : "");
188                 //Write the new info to disk, redraw the UI
189                 otrg_plugin_write_fingerprints();
190                 otrg_ui_update_keylist();
191     }   
194 @end