8a7e3206abad43a323247f7add0503614b5a8b24
[modest] / src / modest-account-mgr-helpers.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <modest-account-mgr-helpers.h>
31 #include <modest-account-mgr-priv.h>
32 #include <tny-simple-list.h>
33 #include <modest-runtime.h>
34 #include <string.h>
35
36 gboolean
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
38                                         gboolean enabled)
39 {
40         return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
41 }
42
43
44 gboolean
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
46 {
47         return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
48 }
49
50 #if 0 /* Not needed, but works. */
51 static gint
52 compare_option_strings_for_name (const gchar* a, const gchar* b)
53 {
54         /* printf("  debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
55         const gchar* sep = strchr(a, '=');
56         if (!sep)
57                 return -1;
58                 
59         gint len = sep - a;
60         if(len <= 0)
61                 return -1;
62                 
63         /* Get the part of the string before the =.
64          * Note that this allocation is inefficient just so we can do a strcmp. */
65         gchar* name = g_malloc (len+1);
66         memcpy(name, a, len);
67         name[len] = 0; /* Null-termination. */
68         
69         /* printf("    debug: name=%s\n", name); */
70
71         gint result = strcmp (name, b);
72         
73         g_free (name);
74         
75         return result;
76 }
77            
78 gchar*
79 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
80 {
81         if (!options_list)
82                 return NULL;
83         
84         gchar *result = NULL;
85         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
86         if(option) {
87                 /* Get the value part of the key=value pair: */
88                 const gchar* pair = (const gchar*)option->data;
89                 
90                 const gchar* sep = strchr(pair, '=');
91                 if (sep) {
92                         gint len = sep - pair;
93                         if(len > 0) {
94                                 result = g_strdup(sep+1);
95                                 
96                                 /* Avoid returning an empty string instead of NULL. */
97                                 if(result && strlen(result) == 0) {
98                                         g_free(result);
99                                         result = NULL;
100                                 }
101                         }
102                 }
103         }
104                 
105         return result;
106 }
107
108 gboolean
109 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
110 {
111         if (!options_list)
112                 return FALSE;
113         
114         gboolean result = FALSE;
115         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
116         if(option) {
117                 return TRUE;
118         }
119                 
120         return result;
121 }
122 #endif
123
124
125 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self, 
126         const gchar* account_name,
127         const gchar* connection_name, const gchar* server_account_name)
128 {
129         modest_account_mgr_remove_connection_specific_smtp (self, account_name, connection_name);
130         
131         GSList *list = modest_account_mgr_get_list (self, account_name, 
132                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
133                                                     MODEST_CONF_VALUE_STRING, TRUE);
134                 
135         /* The server account is in the item after the connection name: */
136         GSList *list_connection = g_slist_append (list, (gpointer)connection_name);
137         list_connection = g_slist_append (list_connection, (gpointer)server_account_name);
138         
139         /* Reset the changed list: */
140         modest_account_mgr_set_list (self, account_name, 
141                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
142                                                     MODEST_CONF_VALUE_STRING, TRUE);
143                                 
144         /* TODO: Should we free the items too, or just the list? */
145         g_slist_free (list);
146         
147         return TRUE;
148 }
149
150 /**
151  * modest_account_mgr_remove_connection_specific_smtp
152  * @self: a ModestAccountMgr instance
153  * @name: the account name
154  * @connection_name: A libconic IAP connection name
155  * 
156  * Disassacoiate a server account to use with the specific connection for this account.
157  *
158  * Returns: TRUE if it worked, FALSE otherwise
159  */                              
160 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self, 
161         const gchar* account_name, const gchar* connection_name)
162 {
163         GSList *list = modest_account_mgr_get_list (self, account_name, 
164                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
165                                                     MODEST_CONF_VALUE_STRING, TRUE);
166         if (!list)
167                 return FALSE;
168                 
169         /* The server account is in the item after the connection name: */
170         GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
171         if (list_connection) {
172                 /* remove both items: */
173                 GSList *temp = g_slist_delete_link(list_connection, list_connection);
174                 temp = g_slist_delete_link(temp, g_slist_next(temp));
175         }
176         
177         /* Reset the changed list: */
178         modest_account_mgr_set_list (self, account_name, 
179                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
180                                                     MODEST_CONF_VALUE_STRING, TRUE);
181                                 
182         /* TODO: Should we free the items too, or just the list? */
183         g_slist_free (list);
184         
185         return TRUE;
186 }
187
188 /**
189  * modest_account_mgr_get_connection_specific_smtp
190  * @self: a ModestAccountMgr instance
191  * @name: the account name
192  * @connection_name: A libconic IAP connection name
193  * 
194  * Retrieve a server account to use with this specific connection for this account.
195  *
196  * Returns: a server account name to use for this connection, or NULL if none is specified.
197  */                      
198 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
199                                          const gchar* connection_name)
200 {
201         gchar *result = NULL;
202         
203         GSList *list = modest_account_mgr_get_list (self, account_name, 
204                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
205                                                     MODEST_CONF_VALUE_STRING, TRUE);
206         if (!list)
207                 return NULL;
208                 
209         /* The server account is in the item after the connection name: */
210         GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
211         if (list_connection) {
212                 GSList * list_server_account = g_slist_next(list_connection);
213                 if (list_server_account)
214                         result = g_strdup ((gchar*)(list_server_account->data));
215         }
216                                 
217         /* TODO: Should we free the items too, or just the list? */
218         g_slist_free (list);
219         
220         return result;
221 }
222                                          
223                                          
224
225 static ModestProtocol
226 get_secure_auth_for_conf_string(const gchar* value)
227 {
228         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
229         if (value) {
230                 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
231                         result = MODEST_PROTOCOL_AUTH_NONE;
232                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
233                         result = MODEST_PROTOCOL_AUTH_PASSWORD;
234                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
235                         result = MODEST_PROTOCOL_AUTH_CRAMMD5;
236         }
237         
238         return result;
239 }
240
241 ModestProtocol
242 modest_server_account_get_secure_auth (ModestAccountMgr *self, 
243         const gchar* account_name)
244 {
245         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
246         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
247                 TRUE /* server account */);
248         if (value) {
249                 result = get_secure_auth_for_conf_string (value);
250                         
251                 g_free (value);
252         }
253         
254         return result;
255 }
256
257
258 void
259 modest_server_account_set_secure_auth (ModestAccountMgr *self, 
260         const gchar* account_name, ModestProtocol secure_auth)
261 {
262         /* Get the conf string for the enum value: */
263         const gchar* str_value = NULL;
264         if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
265                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
266         else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
267                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
268         else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
269                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
270         
271         /* Set it in the configuration: */
272         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
273 }
274
275 static ModestProtocol
276 get_security_for_conf_string(const gchar* value)
277 {
278         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
279         if (value) {
280                 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
281                         result = MODEST_PROTOCOL_SECURITY_NONE;
282                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
283                         result = MODEST_PROTOCOL_SECURITY_TLS;
284                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
285                         result = MODEST_PROTOCOL_SECURITY_SSL;
286         }
287         
288         return result;
289 }
290
291 ModestProtocol
292 modest_server_account_get_security (ModestAccountMgr *self, 
293         const gchar* account_name)
294 {
295         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
296         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
297                 TRUE /* server account */);
298         if (value) {
299                 result = get_security_for_conf_string (value);
300                         
301                 g_free (value);
302         }
303         
304         return result;
305 }
306
307 void
308 modest_server_account_set_security (ModestAccountMgr *self, 
309         const gchar* account_name, ModestProtocol security)
310 {
311         /* Get the conf string for the enum value: */
312         const gchar* str_value = NULL;
313         if (security == MODEST_PROTOCOL_SECURITY_NONE)
314                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
315         else if (security == MODEST_PROTOCOL_SECURITY_TLS)
316                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
317         else if (security == MODEST_PROTOCOL_SECURITY_SSL)
318                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
319         
320         /* Set it in the configuration: */
321         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
322 }
323                      
324 #if 0                     
325 gchar*
326 modest_account_mgr_get_server_account_option (ModestAccountMgr *self, 
327         const gchar* account_name, const gchar* option_name)
328 {
329         GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
330                                                      MODEST_CONF_VALUE_STRING, TRUE);
331         if (!option_list)
332                 return NULL;
333                 
334         gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
335         
336         /* TODO: Should we free the items too, or just the list? */
337         g_slist_free (option_list);
338                 
339         return result;
340 }
341 #endif
342
343 static ModestServerAccountData*
344 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
345 {
346         ModestServerAccountData *data;
347         gchar *proto;
348         
349         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);      
350         data = g_slice_new0 (ModestServerAccountData);
351         
352         data->account_name = g_strdup (name);
353         data->hostname     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
354         data->username     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);  
355         proto              = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
356         data->proto        = modest_protocol_info_get_protocol (proto);
357         g_free (proto);
358
359         data->port         = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
360         
361         gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
362         data->secure_auth  = get_secure_auth_for_conf_string(secure_auth_str);
363         g_free (secure_auth_str);
364                 
365         gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
366         data->security     = get_security_for_conf_string(security_str);
367         g_free (security_str);
368         
369         data->last_updated = modest_account_mgr_get_int    (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
370         
371         data->password     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
372         data->uri          = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
373         data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
374                                                      MODEST_CONF_VALUE_STRING, TRUE);
375                                                    
376         
377         return data;
378 }
379
380
381 static void
382 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
383                                              ModestServerAccountData* data)
384 {
385         g_return_if_fail (self);
386
387         if (!data)
388                 return; /* not an error */
389
390         g_free (data->account_name);
391         data->account_name = NULL;
392         
393         g_free (data->hostname);
394         data->hostname = NULL;
395         
396         g_free (data->username);
397         data->username = NULL;
398
399         g_free (data->password);
400         data->password = NULL;
401         
402         if (data->options) {
403                 GSList *tmp = data->options;
404                 while (tmp) {
405                         g_free (tmp->data);
406                         tmp = g_slist_next (tmp);
407                 }
408                 g_slist_free (data->options);
409         }
410
411         g_slice_free (ModestServerAccountData, data);
412 }
413
414 /** You must use modest_account_mgr_free_account_data() on the result.
415  */
416 ModestAccountData*
417 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
418 {
419         ModestAccountData *data;
420         gchar *server_account;
421         gchar *default_account;
422         
423         g_return_val_if_fail (self, NULL);
424         g_return_val_if_fail (name, NULL);
425         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);      
426         data = g_slice_new0 (ModestAccountData);
427         
428         data->account_name = g_strdup (name);
429
430         data->display_name = modest_account_mgr_get_string (self, name,
431                                                             MODEST_ACCOUNT_DISPLAY_NAME,
432                                                             FALSE);
433         data->fullname     = modest_account_mgr_get_string (self, name,
434                                                               MODEST_ACCOUNT_FULLNAME,
435                                                                FALSE);
436         data->email        = modest_account_mgr_get_string (self, name,
437                                                             MODEST_ACCOUNT_EMAIL,
438                                                             FALSE);
439         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
440
441         default_account    = modest_account_mgr_get_default_account (self);
442         data->is_default   = (default_account && strcmp (default_account, name) == 0);
443         g_free (default_account);
444
445         /* store */
446         server_account     = modest_account_mgr_get_string (self, name,
447                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
448                                                             FALSE);
449         if (server_account) {
450                 data->store_account =
451                         modest_account_mgr_get_server_account_data (self, server_account);
452                 g_free (server_account);
453         }
454
455         /* transport */
456         server_account = modest_account_mgr_get_string (self, name,
457                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
458                                                         FALSE);
459         if (server_account) {
460                 data->transport_account =
461                         modest_account_mgr_get_server_account_data (self, server_account);
462                 g_free (server_account);
463         }
464
465         return data;
466 }
467
468
469 void
470 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
471 {
472         g_return_if_fail (self);
473
474         if (!data) /* not an error */ 
475                 return;
476
477         g_free (data->account_name);
478         g_free (data->display_name);
479         g_free (data->fullname);
480         g_free (data->email);
481
482         modest_account_mgr_free_server_account_data (self, data->store_account);
483         modest_account_mgr_free_server_account_data (self, data->transport_account);
484         
485         g_slice_free (ModestAccountData, data);
486 }
487
488
489 gchar*
490 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
491 {
492         gchar *account; 
493         ModestConf *conf;
494         GError *err = NULL;
495         
496         g_return_val_if_fail (self, NULL);
497
498         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
499         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
500         
501         if (err) {
502                 g_printerr ("modest: failed to get '%s': %s\n",
503                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
504                 g_error_free (err);
505                 g_free (account);
506                 return  NULL;
507         }
508         
509         /* it's not really an error if there is no default account */
510         if (!account) 
511                 return NULL;
512
513         /* sanity check */
514         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
515                 g_printerr ("modest: default account does not exist\n");
516                 g_free (account);
517                 return NULL;
518         }
519
520         return account;
521 }
522
523
524 gboolean
525 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
526 {
527         ModestConf *conf;
528         
529         g_return_val_if_fail (self,    FALSE);
530         g_return_val_if_fail (account, FALSE);
531         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
532                               FALSE);
533         
534         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
535                 
536         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
537                                        account, NULL);
538
539 }
540
541 gchar*
542 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
543 {
544         gchar *fullname, *email, *from;
545         
546         g_return_val_if_fail (self, NULL);
547         g_return_val_if_fail (name, NULL);
548
549         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
550                                                        FALSE);
551         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
552                                                        FALSE);
553         from = g_strdup_printf ("%s <%s>",
554                                 fullname ? fullname : "",
555                                 email    ? email    : "");
556         g_free (fullname);
557         g_free (email);
558
559         return from;
560 }
561
562 /* Add a number to the end of the text, or increment a number that is already there.
563  */
564 static gchar*
565 util_increment_name (const gchar* text)
566 {
567         /* Get the end character,
568          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
569          */
570         const gchar* end = NULL;
571         if (!g_utf8_validate (text, -1, &end))
572                 return NULL;
573   
574         if (!end)
575                 return NULL;
576                 
577         --end; /* Go to before the null-termination. */
578                 
579         /* Look at each UTF-8 characer, starting at the end: */
580         const gchar* p = end;
581         const gchar* alpha_end = NULL;
582         while (p)
583         {       
584                 /* Stop when we reach the first character that is not a numeric digit: */
585                 const gunichar ch = g_utf8_get_char (p);
586                 if (!g_unichar_isdigit (ch)) {
587                         alpha_end = p;
588                         break;
589                 }
590                 
591                 p = g_utf8_prev_char (p);       
592         }
593         
594         if(!alpha_end) {
595                 /* The text must consist completely of numeric digits. */
596                 alpha_end = text;
597         }
598         else
599                 ++alpha_end;
600         
601         /* Intepret and increment the number, if any: */
602         gint num = atol (alpha_end);
603         ++num;
604         
605         /* Get the name part: */
606         gint name_len = alpha_end - text;
607         gchar *name_without_number = g_malloc(name_len + 1);
608         memcpy (name_without_number, text, name_len);
609         name_without_number[name_len] = 0;\
610         
611     /* Concatenate the text part and the new number: */ 
612         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
613         g_free (name_without_number);
614         
615         return result;  
616 }
617
618 gchar*
619 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
620         gboolean server_account)
621 {
622         gchar *account_name = g_strdup (starting_name);
623
624         while (modest_account_mgr_account_exists (self, 
625                 account_name, server_account /*  server_account */)) {
626                         
627                 gchar * account_name2 = util_increment_name (account_name);
628                 g_free (account_name);
629                 account_name = account_name2;
630         }
631         
632         return account_name;
633 }
634
635 gchar*
636 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
637 {
638         gchar *account_name = g_strdup (starting_name);
639
640         while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
641                         
642                 gchar * account_name2 = util_increment_name (account_name);
643                 g_free (account_name);
644                 account_name = account_name2;
645         }
646         
647         return account_name;
648 }