📄 gw_stuff.c
字号:
/* process additional item(s) */ newstring = expand_addr (item, defdom); rcpt_list_insert (list, newstring); nfields++; free (newstring); } /* while (...) */ /*------------------------------------------Conclusions */ free (s); return (nfields);} /* sub_tkize_field () *//*========================================================*/char *mkfromfield (char *srcgsm, char *host, char *defdom){ char *fromfield; /*--------------------------------------Initializations */ fromfield = (char *) malloc ((BUFFSIZE + 1) * sizeof (char)); if (! fromfield) { syslog ((FACILITY | LOG_ERR), "can't malloc()."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't malloc()"); } fromfield[0] = '\0'; /*-------------------------------------------Processing */ sprintf (fromfield, "%s%s@%s.%s", GWUSER, srcgsm, host, defdom); /*------------------------------------------Conclusions */ return (fromfield);} /* mkfromfield () *//*========================================================*/void reset_mail_struct (struct email_msg *m){ if (m->from) { free (m->from); m->from = NULL; } if (!empty_rcpt_list (m->to)) free_rcpt_list (&(m->to)); if (!empty_rcpt_list (m->cc)) free_rcpt_list (&(m->cc)); if (!empty_rcpt_list (m->bcc)) free_rcpt_list (&(m->bcc)); if (m->reply_to) { free (m->reply_to); m->reply_to = NULL; } if (m->subject) { free (m->subject); m->subject = NULL; } if (m->body) { free (m->body); m->body = NULL; }} /* reset_mail_struct () *//*========================================================*/int parse_smail (struct inbox_line *ibl, struct email_msg *m, char *host, char *domain){ char *s; char *s_saved; char *ptr; char *s_end; int end_found; char field_header; char end_marker; /*--------------------------------------Initializations */ /* struct email fields */ m->from = NULL; rcpt_list_init (&(m->to)); rcpt_list_init (&(m->cc)); rcpt_list_init (&(m->bcc)); m->reply_to = NULL; m->subject = NULL; m->body = NULL; /* copy sms string to scratch space - we can't modify it */ s = (char *) malloc ((MAXMSGLEN + 1) * sizeof (char)); if (! s) { syslog ((FACILITY | LOG_ERR), "can't malloc()."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't malloc()"); } s[0] = '\0'; strcpy (s, ibl->text); s_saved = s;#ifdef INCL_DEBUG_CODE fprintf (stderr, "\ns as received:\n[%s]\n", s);#endif /*--------------------------------------Main Processing */ /* First field have to be To: */ if ((toupper (s[0]) == 'T') && (s[1] == ':')) { /* if space between : and address, remove it */ if (s[2] == ' ') { ptr = (s + 2); shiftleft (ptr, 1); } end_found = FALSE; ptr = s; while (!end_found) { if ((ptr = strchr (ptr, ' ')) == NULL) { /* To: was the only field */ syslog ((FACILITY | LOG_ERR), "can't parse, nothing more than To: field."); free (s_saved); return (-1); } if ((ptr[-1] == ',') && (ptr[0] != '\0')) { /* we have a multipart to: field */ ptr++; } else { end_found = TRUE; s_end = ptr; } } /* while (!end_found) */ if (ptr[0] != '\0') { ptr++; } s_end[0] = '\0'; /* sub-tokenize it and save it */#ifdef INCL_DEBUG_CODE fprintf (stderr, "to field: [%s]\n", s);#endif sub_tkize_field (&(m->to), s, domain); /* reset s to begin of next field */ s = ptr; /* now loop to process the rest */ while (s[0]) { field_header = toupper (s[0]); if ((strchr (FIELD_HEADERS, field_header) != NULL) && (s[1] == ':')) { /* we have a valid header */ switch (field_header) { case 'T' : { end_marker = ' '; break; } case 'C' : { end_marker = ' '; break; } case 'R' : { if (m->reply_to) { free (m->reply_to); } end_marker = ' '; break; } case 'B' : { end_marker = ' '; break; } case 'F' : { if (m->from) { free (m->from); } end_marker = ' '; break; } case 'S' : { if (m->subject) { free (m->subject); } end_marker = '#'; break; } } /* switch (field_header) */ /* if space between : and address, remove it */ if (s[2] == ' ') { ptr = (s + 2); shiftleft (ptr, 1); } end_found = FALSE; ptr = s; while (!end_found) { if ((ptr = strchr (ptr, end_marker)) == NULL) { /* we reached end-of-string */ end_found = TRUE; ptr = s + strlen (s); s_end = ptr; } else { if ((ptr[-1] == ',') && (ptr[0] != '\0')) { /* multipart field */ ptr++; } else { end_found = TRUE; s_end = ptr; } } /* if (end_marker not found) */ } /* while (!end_found) */ if (ptr[0] != '\0') { ptr++; } s_end[0] = '\0'; /* assigns the right field */ switch (field_header) { case 'T' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif sub_tkize_field (&(m->to), s, domain); break; } case 'C' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif sub_tkize_field (&(m->cc), s, domain); break; } case 'R' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif clean_field (s); m->reply_to = expand_addr (s, domain); break; } case 'B' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif sub_tkize_field (&(m->bcc), s, domain); break; } case 'F' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif clean_field (s); m->from = expand_addr (s, domain); break; } case 'S' : {#ifdef INCL_DEBUG_CODE fprintf (stderr, "field: [%s]\n", s);#endif /* remove field header */ s += 2; /* if ':' is followed by ' ', remove it as well */ if ((s[0]) && (s[0] == ' ')) { s++; } m->subject = (char *) malloc ((strlen (s) + 1) * sizeof (char)); if (! m->subject) { syslog ((FACILITY | LOG_ERR), "can't malloc()."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't malloc()"); } m->subject[0] = '\0'; strcpy (m->subject, s); break; } } /* switch (field_header) */ s = ptr; } else { /* the rest is considered BODY */ if (s[0]) {#ifdef INCL_DEBUG_CODE fprintf (stderr, "body: [%s]\n", s);#endif m->body = (char *) malloc ((strlen (s) + 1) * sizeof (char)); if (! m->body) { syslog ((FACILITY | LOG_ERR), "can't malloc()."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't malloc()"); } m->body[0] = '\0'; strcpy (m->body, s); ptr = s + strlen (s); s = ptr; } /* if (s[0]) */ } /* if (valid header found) */ } /* while (s[0]) */ } else { /* can't even find To: field */ syslog ((FACILITY | LOG_ERR), "can't parse, can't find To: field."); free (s_saved); return (-1); } /* if ('T' followed by ':') */ /*--------------------------Make sure we have a subject */ if (! m->subject) { m->subject = (char *) malloc ((MINIBUFF + 1) * sizeof (char)); if (! m->subject) { syslog ((FACILITY | LOG_ERR), "can't malloc()."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't malloc()"); } m->subject[0] = '\0'; sprintf (m->subject, "An SMS for you from GSM number %s.", ibl->fromgsm); } /*-------------------------------------------------Exit */ free (s_saved); return 0; /* success */} /* parse_smail () *//*========================================================*/int slurp_n_catch (int fd, int resptime, char *catch)/* slurps n_lines lines from the input stream, and return a boolvalue telling whether catch was found in any of those lines. */{ int nread, retval, previous, found; fd_set inputs; struct timeval timeout; char *buffer; /*--------------------------------------Initializations */ nread = previous = 0; found = FALSE; FD_ZERO (&inputs); FD_SET (fd, &inputs); timeout.tv_sec = 10; timeout.tv_usec = 0; /* wait for data to arrive on the line */ retval = select (FD_SETSIZE, &inputs, NULL, NULL, &timeout); switch (retval) { case 0: syslog ((FACILITY | LOG_WARNING), "timeout while waiting for server reply."); break; case -1: syslog ((FACILITY | LOG_ERR), "call to select() failed."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: call to select() failed"); break; default: if (FD_ISSET (fd, &inputs)) { /* first wait for all data to be ready */ ioctl (fd, FIONREAD, &nread); while (nread != previous) { sleep (resptime); previous = nread; ioctl (fd, FIONREAD, &nread); } /* while (nread != previous) */ /* we know what's the data size - alloc space for it */ buffer = (char *) malloc ((nread + 1) * sizeof (char)); if (!buffer) { syslog ((FACILITY | LOG_ERR), "can't allocate buffer space."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: can't allocate buffer space"); } /* now we can finally read this data */ nread = read (fd, buffer, nread); switch (nread) { case 0: /* EOF */ buffer[nread] = '\0'; syslog ((FACILITY | LOG_WARNING), "no data from server waiting for [%s].", catch); break; case -1: syslog ((FACILITY | LOG_ERR), "error while reading answer from server."); unlink (MBOX_LOCKF); syserr ("sms2mailgw: error while reading answer from server"); break; default: buffer[nread] = '\0';#ifdef INCL_DEBUG_CODE fprintf (stderr, "pid<%d> Got : [%s] (%d char)\n", getpid (), buffer, nread);#endif /* here we could pre-process it (remove Ctrl-Z) */ /* look for catch */ found = (strstr (buffer, catch) != NULL); break; } /* switch (nread) */ } /* if (FD_ISSET... */ free (buffer); break; } /* switch (retval) */ /*------------------------------------------------------*/#ifdef INCL_DEBUG_CODE fprintf (stderr, "catch found: [%s]\n", found ? "yes" : "no");#endif /*----------------------------------Conclusion and exit */ return (found);} /* slurp_n_catch () *//*========================================================*/int send_mail (struct email_msg *m, struct inbox_line *ibl, char *mailhost, char *localhost, char *defdom){ struct servent *sent; struct in_addr server_ip; struct sockaddr_in sockaddr; struct rcpt_item *cursor; int sockfd; int addrlen; char *cmdline; char *nd;#ifdef INCL_DEBUG_CODE fprintf (stderr, "===========================================\n"); if (m->from) fprintf (stderr, "From: %s\n", m->from); if (!empty_rcpt_list (m->to)) { fprintf (stderr, "To: recepients\n"); print_rcpt_list (m->to); } if (!empty_rcpt_list (m->cc)) { fprintf (stderr, "CC: recepients\n"); print_rcpt_list (m->cc); } if (!empty_rcpt_list (m->bcc)) { fprintf (stderr, "BCC: recepients\n"); print_rcpt_list (m->bcc); } if (m->reply_to)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -