core cleanup: xmlnode replacement in sipe-ews.c
[siplcs.git] / src / adium / ESPurpleSIPEAccount.m
blob3f9d086b14ded661f9c03d97e7877b0869984bf1
1 //
2 //  ESSIPEAccount.m
3 //  SIPEAdiumPlugin
4 //
5 //  Created by Matt Meissner on 10/30/09.
6 //  Copyright 2009 Matt Meissner. All rights reserved.
7 //
9 #import <Adium/AIStatus.h>
10 #import <Adium/AIHTMLDecoder.h>
12 #import "ESPurpleSIPEAccount.h"
14 #include "sipe.h"
15 #include "sipe-utils.h"
17 // taken from sipe.c
18 /* Status identifiers (see also: sipe_status_types()) */
19 #define SIPE_STATUS_ID_UNKNOWN     purple_primitive_get_id_from_type(PURPLE_STATUS_UNSET)     /* Unset (primitive) */
20 #define SIPE_STATUS_ID_OFFLINE     purple_primitive_get_id_from_type(PURPLE_STATUS_OFFLINE)   /* Offline (primitive) */
21 #define SIPE_STATUS_ID_AVAILABLE   purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE) /* Online */
22 /*      PURPLE_STATUS_UNAVAILABLE: */
23 #define SIPE_STATUS_ID_BUSY        "busy"                                                     /* Busy */
24 #define SIPE_STATUS_ID_DND         "do-not-disturb"                                           /* Do Not Disturb */
25 #define SIPE_STATUS_ID_ONPHONE     "on-the-phone"                                             /* On The Phone */
26 #define SIPE_STATUS_ID_INVISIBLE   purple_primitive_get_id_from_type(PURPLE_STATUS_INVISIBLE) /* Appear Offline */
27 /*      PURPLE_STATUS_AWAY: */
28 #define SIPE_STATUS_ID_IDLE        "idle"                                                     /* Idle/Inactive */
29 #define SIPE_STATUS_ID_BRB         "be-right-back"                                            /* Be Right Back */
30 #define SIPE_STATUS_ID_AWAY        purple_primitive_get_id_from_type(PURPLE_STATUS_AWAY)      /* Away (primitive) */
31 #define SIPE_STATUS_ID_LUNCH       "out-to-lunch"                                             /* Out To Lunch */
32 /* ???  PURPLE_STATUS_EXTENDED_AWAY */
33 /* ???  PURPLE_STATUS_MOBILE */
34 /* ???  PURPLE_STATUS_TUNE */
37 @implementation ESPurpleSIPEAccount
38 - (const char*)protocolPlugin
40         return "prpl-sipe";
43 - (void)configurePurpleAccount
45         [super configurePurpleAccount]; 
47         NSLog(@"AccountName: %s\n", self.purpleAccountName ? self.purpleAccountName : "NULL");
48         NSLog(@"Account: %x\n", account);
49         NSLog(@"Port: %d\n", self.port);
50         NSLog(@"Server: %@\n", self.host);
52         purple_account_set_username(account, self.purpleAccountName);
54         if (self.host && [self.host length]) {
55                 if (self.port) {
56                         // TODO: figure out a better size for this!
57                         char tmp[512];
58                         sprintf(tmp, "%s:%d", [self.host UTF8String], self.port);
59                         purple_account_set_string(account, "server", tmp);
60                 } else {
61                         // super-class already set this
62                 }
63         }
65         // TODO: hook up the GUI to actually set this
66         purple_account_set_string(account, "transport", "tls");
69 - (const char *)purpleStatusIDForStatus:(AIStatus *)statusState
70                                                           arguments:(NSMutableDictionary *)arguments
72     const char    *statusID = NULL;
73         
74     switch (statusState.statusType) {
75         case AIAvailableStatusType:
76             statusID = purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE);
77             break;
78         case AIAwayStatusType:
79             statusID = purple_primitive_get_id_from_type(PURPLE_STATUS_AWAY);
80             break;
81                         
82         case AIInvisibleStatusType:
83             statusID = purple_primitive_get_id_from_type(PURPLE_STATUS_INVISIBLE);
84             break;
85                         
86         case AIOfflineStatusType:
87             statusID = purple_primitive_get_id_from_type(PURPLE_STATUS_OFFLINE);
88             break;
89     }    
90     
91     return statusID;
95 #pragma mark File transfer
97 - (BOOL)canSendFolders
99         return NO;
102 - (void)beginSendOfFileTransfer:(ESFileTransfer *)fileTransfer
104         [super _beginSendOfFileTransfer:fileTransfer];
107 - (void)acceptFileTransferRequest:(ESFileTransfer *)fileTransfer
109     [super acceptFileTransferRequest:fileTransfer];    
112 - (void)rejectFileReceiveRequest:(ESFileTransfer *)fileTransfer
114     [super rejectFileReceiveRequest:fileTransfer];    
117 - (void)cancelFileTransfer:(ESFileTransfer *)fileTransfer
119         [super cancelFileTransfer:fileTransfer];
122 #pragma mark Status Messages
124 - (NSString *)statusNameForPurpleBuddy:(PurpleBuddy *)buddy
126     NSString        *statusName = nil;
127     PurplePresence  *presence = purple_buddy_get_presence(buddy);
128     PurpleStatus        *status = purple_presence_get_active_status(presence);
129     const char      *purpleStatusID = purple_status_get_id(status);
130     
131     if (!purpleStatusID) return nil;
132         
133     if (!strcmp(purpleStatusID, jabber_buddy_state_get_status_id(JABBER_BUDDY_STATE_CHAT))) {
134         statusName = STATUS_NAME_FREE_FOR_CHAT;
135                 
136     } else if (!strcmp(purpleStatusID, jabber_buddy_state_get_status_id(JABBER_BUDDY_STATE_XA))) {
137         statusName = STATUS_NAME_EXTENDED_AWAY;
138                 
139     } else if (!strcmp(purpleStatusID, jabber_buddy_state_get_status_id(JABBER_BUDDY_STATE_DND))) {
140         statusName = STATUS_NAME_DND;
141                 
142     }   
143     
144     return statusName;
149  * @brief Status message for a contact
150  */
151 - (NSAttributedString *)statusMessageForPurpleBuddy:(PurpleBuddy *)buddy
153         PurplePresence                          *presence = purple_buddy_get_presence(buddy);
154         PurpleStatus                            *status = (presence ? purple_presence_get_active_status(presence) : NULL);
155         const char                                      *message = (status ? purple_status_get_attr_string(status, "message") : NULL);
156         char                                            *sipemessage = NULL;
157         NSString                                        *statusMessage = nil;
158         struct sipe_account_data        *sip;
159         struct sipe_buddy                       *sbuddy;
161         if (!message) {
163                 sip = (struct sipe_account_data *) buddy->account->gc->proto_data;
164                 if (sip)  //happens on pidgin exit
165                 {
166                         GList *keys = g_hash_table_get_keys(sip->buddies);
167                         sbuddy = g_hash_table_lookup(sip->buddies, buddy->name);
168                         if (sbuddy) {
169                                 if (!is_empty(sbuddy->activity) && !is_empty(sbuddy->annotation))
170                                 {
171                                         sipemessage = g_strdup_printf("%s. %s", sbuddy->activity, sbuddy->annotation);
172                                 }
173                                 else if (!is_empty(sbuddy->activity))
174                                 {
175                                         sipemessage = g_strdup(sbuddy->activity);
176                                 }
177                                 else if (!is_empty(sbuddy->annotation))
178                                 {
179                                         sipemessage = g_strdup(sbuddy->annotation);
180                                 }
181                         }
182                 }
183         }
184         
185         // Get the plugin's status message for this buddy if they don't have a status message
186         if (!message && !sipemessage) {
187                 PurplePluginProtocolInfo  *prpl_info = self.protocolInfo;
188                 
189                 if (prpl_info && prpl_info->status_text) {
190                         char *status_text = (prpl_info->status_text)(buddy);
191                         
192                         // Don't display "Offline" as a status message.
193                         if (status_text && strcmp(status_text, _("Offline")) != 0) {
194                                 statusMessage = [NSString stringWithUTF8String:status_text];                            
195                         }
196                         
197                         g_free(status_text);
198                 }
199         } else if (sipemessage) {
200                 statusMessage = [NSString stringWithUTF8String:sipemessage];
201                 g_free(sipemessage);
202         } else {
203                 statusMessage = [NSString stringWithUTF8String:message];
204         }
206         return statusMessage ? [AIHTMLDecoder decodeHTML:statusMessage] : nil;
207         }
209 @end