/* ------------------------------------------------------------------ * get_mail_attribute_values () */ static CFMutableDictionaryRef get_mail_attribute_values ( const char *in_mail_attribute, struct od_user_opts *in_out_opts ) { CFMutableDictionaryRef cf_mut_dict_ref = NULL; unsigned long ul_size = strlen( in_mail_attribute ); CFDataRef cf_data_ref = CFDataCreate( NULL, (const UInt8 *)in_mail_attribute, ul_size ); if ( !cf_data_ref ) return( NULL ); CFPropertyListRef cf_plist_ref = CFPropertyListCreateFromXMLData( kCFAllocatorDefault, cf_data_ref, kCFPropertyListImmutable, NULL ); if ( cf_plist_ref ) { if ( CFDictionaryGetTypeID() == CFGetTypeID( cf_plist_ref ) ) { cf_mut_dict_ref = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFRetain(cf_mut_dict_ref); CFDictionaryAddValue( cf_mut_dict_ref, CFSTR(kXMLKeyAttrVersion), CFSTR(kXMLValueVersion2) ); CFDictionaryRef cf_dict_ref = (CFDictionaryRef)cf_plist_ref; get_acct_state( cf_dict_ref, cf_mut_dict_ref, in_out_opts ); get_alt_loc( cf_dict_ref, cf_mut_dict_ref ); get_mail_quota( cf_dict_ref, cf_mut_dict_ref ); } } CFRelease( cf_data_ref ); return(cf_mut_dict_ref); } /* get_mail_attribute_values */
/* ------------------------------------------------------------------ * get_attributes_local () */ static bool get_attributes_local ( struct od_user_opts *in_out_opts, const char *in_user_guid ) { bool b_out = FALSE; /* look in local file first */ CFDictionaryRef cf_dict_data = (CFDictionaryRef)read_user_settings( user_settings_path, kCFPropertyListImmutable ); if ( !cf_dict_data ) return( FALSE ); CFStringRef cf_str = CFStringCreateWithCString( NULL, in_user_guid, kCFStringEncodingUTF8 ); if ( cf_str ) { if ( CFDictionaryContainsKey( cf_dict_data, cf_str ) ) { CFDictionaryRef cf_dict_user = (CFDictionaryRef)CFDictionaryGetValue( cf_dict_data, cf_str ); if ( cf_dict_user && (CFGetTypeID( cf_dict_user ) == CFDictionaryGetTypeID()) ) { get_acct_state( cf_dict_user, NULL, in_out_opts ); get_alt_loc( cf_dict_user, NULL ); get_mail_quota( cf_dict_user, NULL ); b_out = TRUE; } } CFRelease( cf_str ); } CFRelease( cf_dict_data ); return( b_out ); } /* get_attributes_local */
static void mail_func_send(t_connection * c, const char * str) { int i; char *dest; char const *p,*myname; t_account * recv; t_mailbox * mailbox; if (c==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return; } if (str==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string"); return; } for(i=0;str[i]==' ';i++); /* skip any spaces */ if (str[i]=='\0') { /* the %mail send command has no receiver */ message_send_text(c,message_type_error,c,"You must specify the receiver"); message_send_text(c,message_type_error,c,"Syntax: /mail send <receiver> <message>"); return; } p=str+i; /* set ip at the start of receiver string */ for(i=1;p[i]!=' ' && p[i]!='\0';i++); /* skip the receiver string */ if (p[i]=='\0') { /* it seems user forgot to write any message */ message_send_text(c,message_type_error,c,"Your message is empty!"); message_send_text(c,message_type_error,c,"Syntax: /mail send <receiver> <message>"); return; } dest=xmalloc(i+1); memmove(dest,p,i); dest[i]='\0'; /* copy receiver in his separate string */ if ((recv=accountlist_find_account(dest))==NULL) { /* is dest a valid account on this server ? */ message_send_text(c,message_type_error,c,"Receiver UNKNOWN!"); xfree(dest); return; } xfree(dest); /* free dest here, the sooner the better */ if ((mailbox=mailbox_open(recv, mbox_mode_write))==NULL) { message_send_text(c,message_type_error,c,"There was an error completing your request!"); return; } if (get_mail_quota(recv)<=mailbox_count(mailbox)) { /* check quota */ message_send_text(c,message_type_error,c,"Receiver has reached his mail quota. Your message will NOT be sent."); mailbox_close(mailbox); return; } myname=conn_get_username(c); /* who am i ? */ if (mailbox_deliver(mailbox,myname,p+i+1)<0) message_send_text(c,message_type_error,c,"There was an error completing your request!"); else message_send_text(c,message_type_info,c,"Your mail has been sent successfully."); mailbox_close(mailbox); }
static void mail_func_send(t_connection * c, std::istream& istr) { if (!c) { ERROR0("got NULL connection"); return; } std::string dest; istr >> dest; if (dest.empty()) { message_send_text(c, message_type_error, c, "You must specify the receiver"); message_send_text(c, message_type_error, c, "Syntax: /mail send <receiver> <message>"); return; } std::string message; std::getline(istr, message); std::string::size_type pos(message.find_first_not_of(" \t")); if (pos == std::string::npos) { message_send_text(c, message_type_error, c, "Your message is empty!"); message_send_text(c, message_type_error, c, "Syntax: /mail send <receiver> <message>"); return; } t_account * recv = accountlist_find_account(dest.c_str()); if (!recv) { message_send_text(c, message_type_error, c, "Receiver UNKNOWN!"); return; } Mailbox mbox(account_get_uid(recv)); if (get_mail_quota(recv) <= mbox.size()) { message_send_text(c, message_type_error, c, "Receiver has reached his mail quota. Your message will NOT be sent."); return; } try { mbox.deliver(conn_get_username(c), message.substr(pos)); message_send_text(c, message_type_info, c, "Your mail has been sent successfully."); } catch (const Mailbox::DeliverError&) { message_send_text(c, message_type_error, c, "There was an error completing your request!"); } }
static void mail_func_read(t_connection * c, const char * str) { t_account * user; t_mailbox * mailbox; const char *p; char tmp[256]; int i; if (c==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return; } if (str==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string"); return; } for(i=0;str[i]==' ';i++); p=str+i; if ((user=conn_get_account(c))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL account"); return; } mailbox=mailbox_open(user, mbox_mode_read); if (*p=='\0') { /* user wants to see the mail summary */ struct maillist_struct *maill, *mp; unsigned int idx; if (!mailbox_count(mailbox)) { message_send_text(c,message_type_info,c,"You have no mail."); mailbox_close(mailbox); return; } if ((maill=mailbox_get_list(mailbox))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL maillist"); mailbox_close(mailbox); return; } sprintf(tmp,"You have %d messages. Your mail qouta is set to %d.",mailbox_count(mailbox),get_mail_quota(user)); message_send_text(c,message_type_info,c,tmp); message_send_text(c,message_type_info,c,"ID Sender Date"); message_send_text(c,message_type_info,c,"-------------------------------------"); for(mp=maill,idx=1;mp!=NULL;mp=mp->next,idx++) { sprintf(tmp,"%02u %-14s %s",idx,mp->sender,ctime(&mp->timestamp)); clean_str(tmp); /* ctime() appends an newline that we get cleaned */ message_send_text(c,message_type_info,c,tmp); } message_send_text(c,message_type_info,c,"Use /mail read <ID> to read the content of any message"); mailbox_unget_list(maill); } else { /* user wants to read a message */ int idx; t_mail * mail; for(i=0;p[i]>='0' && p[i]<='9' && p[i]!='\0';i++); if (p[i]!='\0' && p[i]!=' ') { message_send_text(c,message_type_error,c,"Invalid index. Please use /mail read <index> where <index> is a number."); mailbox_close(mailbox); return; } idx=atoi(p); if (idx<1 || idx>mailbox_count(mailbox)) { message_send_text(c,message_type_error,c,"That index is out of range."); mailbox_close(mailbox); return; } if ((mail=mailbox_read(mailbox,idx))==NULL) { message_send_text(c,message_type_error,c,"There was an error completing your request."); mailbox_close(mailbox); return; } sprintf(tmp,"Message #%d from %s on %s:",idx,mail->sender,clean_str(ctime(&mail->timestamp))); message_send_text(c,message_type_info,c,tmp); message_send_text(c,message_type_info,c,mail->message); mailbox_unread(mail); } mailbox_close(mailbox); }
static void mail_func_read(t_connection * c, std::istream& istr) { if (!c) { ERROR0("got NULL connection"); return; } std::string token; istr >> token; t_account * user = conn_get_account(c); Mailbox mbox(account_get_uid(user)); if (token.empty()) { /* user wants to see the mail summary */ if (mbox.empty()) { message_send_text(c,message_type_info,c,"You have no mail."); return; } MailList mlist; mbox.readAll(mlist); std::ostringstream ostr; ostr << "You have " << mbox.size() << " messages. Your mail quote is set to " << get_mail_quota(user) << '.'; message_send_text(c, message_type_info, c, ostr.str().c_str()); message_send_text(c, message_type_info, c, "ID Sender Date"); message_send_text(c, message_type_info, c, "-------------------------------------"); for(MailList::const_iterator it(mlist.begin()); it != mlist.end(); ++it) { ostr.str(""); ostr << std::setfill('0') << std::setw(2) << std::right << (it - mlist.begin()) << " " << std::setfill(' ') << std::setw(14) << std::left << it->sender() << ' '; char buff[128]; std::strftime(buff, sizeof(buff), "%a %b %d %H:%M:%S %Y", std::localtime(&it->timestamp())); ostr << buff; message_send_text(c, message_type_info, c, ostr.str().c_str()); } message_send_text(c,message_type_info,c,"Use /mail read <ID> to read the content of any message"); } else { /* user wants to read a message */ if (std::find_if(token.begin(), token.end(), NonNumericChar) != token.end()) { message_send_text(c,message_type_error,c,"Invalid index. Please use /mail read <index> where <index> is a number."); return; } try { unsigned idx = std::atoi(token.c_str()); Mail mail(mbox.read(idx)); std::ostringstream ostr; ostr << "Message #" << idx << " from " << mail.sender() << " on "; char buff[128]; std::strftime(buff, sizeof(buff), "%a %b %d %H:%M:%S %Y", std::localtime(&mail.timestamp())); ostr << buff << ':'; message_send_text(c, message_type_info, c, ostr.str().c_str()); message_send_text(c, message_type_info, c, mail.message().c_str()); } catch (const Mailbox::ReadError&) { message_send_text(c,message_type_error,c,"There was an error completing your request."); } } }