/***************************************************/
int main(int argc, char **argv)
{
- char mailAddress[BUFFMAXLN]; /* mail address */
- char limitDate[WORDMAXLN]; /* limit date */
- char device[WORDMAXLN]; /* device name */
+ char mailAddress[BUFFMAXLN]=""; /* mail address */
+ char limitDate[WORDMAXLN]=""; /* limit date */
+ char device[WORDMAXLN]=""; /* device name */
+ char deviceList[BUFFMAXLN]=""; /* device name list */
+ char mailAddressPrev[BUFFMAXLN]=""; /* mail address of previous record */
+ char limitDatePrev[WORDMAXLN]=""; /* limit date of previous record */
int count=0; /* count for sending mails */
char* progName; /* the name of this program in argv[0] */
InitConf();
if(!InitMngDb()) return 0;
- /* get mail address for users near expiration date and send mail */
- while(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
- if(SendMail(mailAddress, limitDate, device)) count++;
+ /* get first mail address near expiration date */
+ if(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
+
+ /* if get, save the first record */
+ strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
+ strncpy(limitDatePrev, limitDate, WORDMAXLN);
+ strncpy(deviceList, device, WORDMAXLN);
+
+ /* get next mail address near expiration date */
+ while(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
+
+ /* if mailAddress or limitDate are not same as previous */
+ if( strcmp(mailAddress, mailAddressPrev)!=0
+ || strcmp(limitDate, limitDatePrev)!=0 ){
+
+ /* send mail to the address and clear deviceList */
+ if(!isNull(mailAddressPrev)){
+ if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
+ }
+ deviceList[0]='\0';
+ }
+
+ /* save the mail Address and limit date */
+ strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
+ strncpy(limitDatePrev, limitDate, WORDMAXLN);
+
+ /* add the device name to the deviceList
+ if list is too long, it is terminated by ", ......" */
+ if(isNull(deviceList)){
+ strncpy(deviceList, device, WORDMAXLN);
+ }
+ else if(strstr(deviceList, ", ......")!=NULL) ;
+ else if( strlen(deviceList) > BUFFMAXLN*0.8 ){
+ strncat(deviceList, ", ......", BUFFMAXLN);
+ }
+ else{
+ strncat(deviceList, ", ", BUFFMAXLN);
+ strncat(deviceList, device, BUFFMAXLN);
+ }
+ }
+
+ /* post process at the end of get loop */
+ if( !isNull(mailAddressPrev) ){
+ if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
+ }
}
/* finalize */