⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gw_stuff.c

📁 手机短消息的服务器端和客户端的源代码 是应用于LINUX/UNIX开发环境的
💻 C
📖 第 1 页 / 共 3 页
字号:
    fprintf (stderr, "Reply-To: %s\n", m->reply_to);  if (m->subject)    fprintf (stderr, "Subject: %s\n", m->subject);  if (m->body)    fprintf (stderr, "%s\n", m->body);  fprintf (stderr, "===========================================\n");#endif      /*--------------------------------------Initializations */  cmdline = (char *) malloc ((BUFFSIZE + 1) * sizeof (char));  if (!cmdline) {        syslog ((FACILITY | LOG_ERR), "can't malloc().");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't malloc()");  }  cmdline[0] = '\0';  /* first resolve server name */  server_ip.s_addr = resolve (mailhost);    /* get the port number we should connect to */  if ((sent = getservbyname ("smtp", "tcp")) == NULL) {    syslog ((FACILITY | LOG_ERR), "can't get service port info.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't get service port info");  }#ifdef INCL_DEBUG_CODE  fprintf (stderr, "found port <%d> for service smtp\n", ntohs (sent->s_port));#endif      /* create the socket */  if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {    syslog ((FACILITY | LOG_ERR), "can't create socket.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't create socket");  }    /* build the server socket address parameters */  sockaddr.sin_family = AF_INET;  sockaddr.sin_port = sent->s_port;  sockaddr.sin_addr.s_addr = server_ip.s_addr;  addrlen = sizeof (sockaddr);    /* now connect to the server */  if (connect (sockfd, (struct sockaddr *)&sockaddr, addrlen) == -1) {    syslog ((FACILITY | LOG_ERR), "can't connect to server.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't connect to server");  }      /*......................................Start of dialog */  /* slurp server announce and catch prompt */  if (!slurp_n_catch (sockfd, 1, "220")) {    syslog ((FACILITY | LOG_ERR), "can't get server announce.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't get server announce");  }      /* set sender ID - get ok */  sprintf (cmdline, "HELO %s.%s\n", localhost, defdom);  tellsock (sockfd, cmdline);  if (!slurp_n_catch (sockfd, 1, "250")) {    syslog ((FACILITY | LOG_ERR), "failed on HELO command.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: failed on HELO command");  }      /* send from: field */  sprintf (cmdline, "MAIL FROM: %s\n", m->from);  tellsock (sockfd, cmdline);  if (!slurp_n_catch (sockfd, 1, "250")) {    syslog ((FACILITY | LOG_ERR), "failed on MAIL command.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: failed on MAIL command");  }    /*. . . . . . . . . . . . . Loop through all recepients */  /* To: field */  if (!empty_rcpt_list (m->to)) {    cursor = m->to.head;    sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);    tellsock (sockfd, cmdline);    if (!slurp_n_catch (sockfd, 1, "250")) {      syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");      unlink (MBOX_LOCKF);      syserr ("sms2mailgw: failed on RCPT command");    }    while ((cursor = cursor->next) != NULL) {      sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);      tellsock (sockfd, cmdline);      if (!slurp_n_catch (sockfd, 1, "250")) {	syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");	unlink (MBOX_LOCKF);	syserr ("sms2mailgw: failed on RCPT command");      }    }                                      /* while (...) */  }                             /* if (not empty to-list) */    /* CC: field */  if (!empty_rcpt_list (m->cc)) {    cursor = m->cc.head;    sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);    tellsock (sockfd, cmdline);    if (!slurp_n_catch (sockfd, 1, "250")) {      syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");      unlink (MBOX_LOCKF);      syserr ("sms2mailgw: failed on RCPT command");    }    while ((cursor = cursor->next) != NULL) {      sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);      tellsock (sockfd, cmdline);      if (!slurp_n_catch (sockfd, 1, "250")) {	syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");	unlink (MBOX_LOCKF);	syserr ("sms2mailgw: failed on RCPT command");      }    }                                      /* while (...) */  }                             /* if (not empty cc-list) */  /* BCC: field */  if (!empty_rcpt_list (m->bcc)) {    cursor = m->bcc.head;    sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);    tellsock (sockfd, cmdline);    if (!slurp_n_catch (sockfd, 1, "250")) {      syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");      unlink (MBOX_LOCKF);      syserr ("sms2mailgw: failed on RCPT command");    }    while ((cursor = cursor->next) != NULL) {      sprintf (cmdline, "RCPT TO: %s\n", cursor->rcpt);      tellsock (sockfd, cmdline);      if (!slurp_n_catch (sockfd, 1, "250")) {	syslog ((FACILITY | LOG_ERR), "failed on RCPT command.");	unlink (MBOX_LOCKF);	syserr ("sms2mailgw: failed on RCPT command");      }    }                                      /* while (...) */  }                            /* if (not empty bcc-list) */  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . */  /* now send mail data */  sprintf (cmdline, "DATA\n");  tellsock (sockfd, cmdline);  if (!slurp_n_catch (sockfd, 1, "354")) {    syslog ((FACILITY | LOG_ERR), "failed on DATA command.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: failed on DATA command");  }    /* From: field as "comment" */  if (m->from) {    sprintf (cmdline, "From: %s\n", m->from);    tellsock (sockfd, cmdline);  }  /* To: field as "comment" */  if (!empty_rcpt_list (m->to)) {    cursor = m->to.head;    sprintf (cmdline, "To: %s", cursor->rcpt);    while ((cursor = cursor->next) != NULL) {      strcat (cmdline, ", ");      strcat (cmdline, cursor->rcpt);    }                                      /* while (...) */    strcat (cmdline, "\n");    tellsock (sockfd, cmdline);  }                             /* if (not empty to-list) */  /* CC: field as "comment" */  if (!empty_rcpt_list (m->cc)) {    cursor = m->cc.head;    sprintf (cmdline, "Cc: %s", cursor->rcpt);    while ((cursor = cursor->next) != NULL) {      strcat (cmdline, ", ");      strcat (cmdline, cursor->rcpt);    }                                      /* while (...) */    strcat (cmdline, "\n");    tellsock (sockfd, cmdline);  }                             /* if (not empty to-list) */  /* BCC: field as "comment" */  if (!empty_rcpt_list (m->bcc)) {    cursor = m->bcc.head;    sprintf (cmdline, "Bcc: %s", cursor->rcpt);    while ((cursor = cursor->next) != NULL) {      strcat (cmdline, ", ");      strcat (cmdline, cursor->rcpt);    }                                      /* while (...) */    strcat (cmdline, "\n");    tellsock (sockfd, cmdline);  }                             /* if (not empty to-list) */  /* Reply-to: field */  if (m->reply_to) {    sprintf (cmdline, "Reply-to: %s\n", m->reply_to);    tellsock (sockfd, cmdline);  }  /* Subject: field */  if (m->subject) {    sprintf (cmdline, "Subject: %s\n", m->subject);    tellsock (sockfd, cmdline);  }  /* Message Body */  if (m->body) {    sprintf (cmdline, "%s\n", m->body);    tellsock (sockfd, cmdline);  }    /* add some system info */  sprintf (cmdline, "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");  tellsock (sockfd, cmdline);  sprintf (cmdline, "This mail has been relayed to you by SMSLink's sms2mailgw\n");  tellsock (sockfd, cmdline);  sprintf (cmdline, "ver. %s (%s) running on %s.%s.\n", SMS_GW_VERSION,           SMS_GW_DATE, localhost, defdom);  tellsock (sockfd, cmdline);  nd = nicedate (ibl->date);  sprintf (cmdline, "The original message was received on %s at %s\n",           nd, ibl->time);  tellsock (sockfd, cmdline);  sprintf (cmdline, "from the GSM number [%s] through device [%s].\n", ibl->fromgsm,           ibl->device);  tellsock (sockfd, cmdline);  sprintf (cmdline, "The message ID was [%d].\n", ibl->msgid);  tellsock (sockfd, cmdline);    /* now close the DATA section: one dot only */  sprintf (cmdline, ".\n");  tellsock (sockfd, cmdline);  if (!slurp_n_catch (sockfd, 1, "250")) {    syslog ((FACILITY | LOG_ERR), "failed on DATA transfer.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: failed on DATA transfer");  }    /* send the QUIT command */  sprintf (cmdline, "QUIT\n");  tellsock (sockfd, cmdline);  if (!slurp_n_catch (sockfd, 1, "221")) {    syslog ((FACILITY | LOG_ERR), "failed on QUIT command.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: failed on QUIT command");  }    /*------------------------------------------Conclusions */  /* close socket */  if (close (sockfd) == -1) {    syslog ((FACILITY | LOG_ERR), "can't close socket.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't close socket");  }    /* free what's need to be */  free (nd);    /* leave */  return (0);}                                         /* send_mail () *//*========================================================*/int mailbox_run (char *localhost, char *defaultdomain){  struct email_msg email;  struct inbox_line tkline;            /* tokenized line */  char *line;                                /* raw line */  char *buffline;                       /* transfer line */  FILE *inbox;  FILE *newbox;  int anymailfound = FALSE;  int filecreatedyet = FALSE;  int msg_count = 0;  int lineismail;  int lcursor;  int nline = 0;  char *cmd;  char *newboxname;    /*---------------------------------Initialize variables */  line = (char *) malloc ((BIGBUFF + 1) * sizeof (char));  if (! line) {    syslog ((FACILITY | LOG_ERR), "can't malloc().");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't malloc()");  }  line[0] = '\0';  buffline = (char *) malloc ((BIGBUFF + 1) * sizeof (char));  if (! buffline) {    syslog ((FACILITY | LOG_ERR), "can't malloc().");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't malloc()");  }  buffline[0] = '\0';  cmd = (char *) malloc ((BUFFSIZE + 1) * sizeof (char));  if (! cmd) {    syslog ((FACILITY | LOG_ERR), "can't malloc().");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't malloc()");  }  cmd[0] = '\0';  newboxname = (char *) malloc ((PATH_MAX + 1) * sizeof (char));  if (! newboxname) {    syslog ((FACILITY | LOG_ERR), "can't malloc().");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't malloc()");  }  newboxname[0] = '\0';  sprintf (newboxname, "%s.tmp.%d", MBOX_FILE, getpid());  /*--------------------------------------------Open file */  if ((inbox = fopen (MBOX_FILE, "r")) == NULL) {    syslog ((FACILITY | LOG_ERR), "can't fopen() inbox file.");    unlink (MBOX_LOCKF);    syserr ("sms2mailgw: can't fopen() inbox file");  }  /*----------------------Read file and process each line */  while (fgets (line, BIGBUFF, inbox) != NULL) {    nline++;    /* tokenize line to fill a struct inbox_line */    if (tkize_ibox_line (line, &tkline) == -1) {      syslog ((FACILITY | LOG_ERR), "inbox corruption - can't parse line %d.", nline);      unlink (MBOX_LOCKF);      syserr ("sms2mailgw: inbox corruption - can't parse line");    }        /* is it a mail message ? */    if ((toupper (tkline.text[0]) == 'T') && (tkline.text[1] == ':')) {      if (parse_smail (&tkline, &email, localhost, defaultdomain) == -1) {        /* reject line after all */	lineismail = FALSE;	/* log it */        syslog ((FACILITY | LOG_NOTICE), "inbox line #%d don't comply with protocol.",	       nline);      }      else {        lineismail = TRUE;        anymailfound = TRUE;	if (!email.from) {	  email.from = mkfromfield (tkline.fromgsm, localhost, defaultdomain);	}        send_mail (&email, &tkline, MAILHOST, localhost, defaultdomain);	reset_mail_struct (&email);        msg_count++;      }    }    else {      lineismail = FALSE;    }                              /* if (it is an email) */    if (anymailfound) {      if (!filecreatedyet) {        /* open new inbox file */	if ((newbox = fopen (newboxname, "w")) == NULL) {	  syslog ((FACILITY | LOG_ERR), "can't fopen() temp inbox file.");          unlink (MBOX_LOCKF);	  syserr ("sms2mailgw: can't fopen() temp inbox file");	}        filecreatedyet = TRUE;	/* transfer up to current line - current not included */        lcursor = 1;        /* "rewind" input file */        if (fseek (inbox, (long) 0, SEEK_SET) == -1) {	  syslog ((FACILITY | LOG_ERR), "can't fseek() on inbox file.");          unlink (MBOX_LOCKF);	  syserr ("sms2mailgw: can't fseek() on inbox file");        }        while (lcursor < nline) {          if (fgets (buffline, BIGBUFF, inbox) == NULL) {	    syslog ((FACILITY | LOG_ERR), "can't read from inbox file.");            unlink (MBOX_LOCKF);	    syserr ("sms2mailgw: can't read from inbox file");          }          if (fputs (buffline, newbox) == EOF) {	    syslog ((FACILITY | LOG_ERR), "can't write to new inbox file.");            unlink (MBOX_LOCKF);	    syserr ("sms2mailgw: can't write to new inbox file");          }          lcursor++;        }                      /* while (lcursor < nline) */        /* re-read line #nline to skip it */        if (fgets (buffline, BIGBUFF, inbox) == NULL) {	  syslog ((FACILITY | LOG_ERR), "can't read from inbox file.");          unlink (MBOX_LOCKF);	  syserr ("sms2mailgw: can't read from inbox file");        }      }                            /* if (msg_count == 1) */      if (! lineismail) {        /* transfer current line to new file */        if (fputs (line, newbox) == EOF) {	  syslog ((FACILITY | LOG_ERR), "can't write to new inbox file.");          unlink (MBOX_LOCKF);	  syserr ("sms2mailgw: can't write to new inbox file");        }      }                              /* if (! lineismail) */    }                                /* if (anymailfound) */  }                                    /* while (fgets... */  /*------------------------------Close file and conclude */  fclose (inbox);  if (anymailfound) {    fclose (newbox);    /* now mv newbox inbox */    sprintf (cmd, "mv %s %s", newboxname, MBOX_FILE);    if (system (cmd) != 0) {      syslog ((FACILITY | LOG_ERR), "can't move new inbox file - system() failed.");      unlink (MBOX_LOCKF);      syserr ("sms2mailgw: can't move new inbox file - system() failed");    }  }    /* free what's needs to be */  free (line);  free (buffline);  free (cmd);  free (newboxname);    return (msg_count);}                                       /* mailbox_run () *//*========================================================== * EOF : gw_stuff.c *===================*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -