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

📄 mms_util.c

📁 手机端彩信的编解码、以及接收和发送。非常有用。
💻 C
📖 第 1 页 / 共 4 页
字号:
	       mime_entity_set_body(m, s);
	       octstr_destroy(s);
	  }
	  http_header_remove_all(headers, "Content-Transfer-Encoding"); /* Remove it in all cases (?).*/
	  mime_replace_headers(m, headers);

     done:
	  /* XXX may be we should deal with other transfer encodings here as well... */
	  
	  octstr_destroy(ctype);	  
	  octstr_destroy(te);
	  http_destroy_headers(headers);
     }
}

int _mms_gw_isprint(int c)
{
     return isprint(c) || isspace(c);
}


/* Change content coding for mime entities that need it. */
void base64_mimeparts(MIMEEntity *m, int all)
{
     int i, n;

     if ((n = mime_entity_num_parts(m)) > 0)
	  for (i = 0; i<n; i++) {
	       MIMEEntity *x = mime_entity_get_part(m, i);
	       base64_mimeparts(x, all);
	       mime_entity_replace_part(m, i, x);
	       mime_entity_destroy(x);
	  }
     else { /* A non-multipart message .*/
	  List *headers = mime_entity_headers(m);
	  Octstr *ctype = http_header_value(headers, octstr_imm("Content-Type"));
	  Octstr *te = http_header_value(headers, octstr_imm("Content-Transfer-Encoding"));
	  Octstr *body = mime_entity_body(m);
	  if (ctype && 
	      (te == NULL || octstr_str_case_compare(te, "binary") == 0) &&  
	      body && 
	      (all || octstr_check_range(body, 0, octstr_len(body), _mms_gw_isprint) == 0) &&
	      !DRM_CONTENT_TYPE(ctype)) { /* don't touch drm content object. */
	       octstr_binary_to_base64(body);	      
	       
	       http_header_remove_all(headers, "Content-Transfer-Encoding");
	       http_header_add(headers, "Content-Transfer-Encoding", "base64");
	       mime_entity_set_body(m, body);
	       mime_replace_headers(m, headers);
	  }
	  octstr_destroy(ctype);	  
	  octstr_destroy(te);
	  octstr_destroy(body);
	  http_destroy_headers(headers);
     }
}

static void addmmscname(Octstr *s, Octstr *myhostname)
{
     int j;
     int len = octstr_len(s);
     
     if (octstr_search_char(s, '@', 0) >= 0)
	  return; /* Nothing to do. */
     
     j = octstr_case_search(s, octstr_imm("/TYPE=PLMN"), 0);
     if (j > 0 && j - 1 +  sizeof "/TYPE=PLMN" == len)  /* A proper number. */	
	  octstr_format_append(s, "@%S", myhostname);
     
     
}

static int send2email(Octstr *to, Octstr *from, Octstr *subject, 
		      Octstr *msgid,
		      MIMEEntity *m, int append_hostname, Octstr **error, 
		      char *sendmail_cmd, Octstr *myhostname)
{
     Octstr *s;
     FILE *f;
     int ret = MMS_SEND_OK, i, n;
     Octstr *cmd = octstr_create(""); 
     List *headers = mime_entity_headers(m); /* we don't want the mime version header removed. */

     if (append_hostname) { /* Add our hostname to all phone numbers. */
	  List *l = http_create_empty_headers();
	  Octstr *xfrom = http_header_value(headers, octstr_imm("From"));
	  List *lto = http_header_find_all(headers, "To");
	  List *lcc = http_header_find_all(headers, "Cc");
	  
	  if (xfrom) {
	       addmmscname(xfrom, myhostname);
	       http_header_add(l, "From", octstr_get_cstr(xfrom));
	       octstr_destroy(xfrom);
	  }
	  http_header_remove_all(headers, "From");
	
	  for (i = 0, n = gwlist_len(lto); i < n; i++) {	 
	       Octstr *name, *value;	 
	       
	       http_header_get(lto, i, &name, &value);	 
	       
	       if (!value || !name ||	 
		   octstr_case_compare(name, octstr_imm("To")) != 0)	 
		    goto loop;	 
	       
	       addmmscname(value, myhostname);	 
	       http_header_add(l, "To", octstr_get_cstr(value));	 
	  loop:	 
	       octstr_destroy(value);	 
	       octstr_destroy(name);	 
	  }	 
 	 
	  http_destroy_headers(lto);	 
	  http_header_remove_all(headers, "To");	 
	  
	  for (i = 0, n = gwlist_len(lcc); i < n; i++) {	 
	       Octstr *name, *value;	 
	       
	       http_header_get(lcc, i, &name, &value);	 
	       
	       if (!value || !name ||	 
		   octstr_case_compare(name, octstr_imm("Cc")) != 0)	 
		    goto loop2;	 
	       
	       addmmscname(value, myhostname);	 
	       http_header_add(l, "Cc", octstr_get_cstr(value));	 
	  loop2:	 
	       octstr_destroy(value);	 
	       octstr_destroy(name);	 
	  }	 
	  
	  http_destroy_headers(lcc);	 
	  http_header_remove_all(headers, "Cc");	 
   
	  http_append_headers(headers, l); /* combine old with new. */
	  http_destroy_headers(l);
     }
	  
     /* Pack headers, get string rep of mime entity. */
     http_header_pack(headers); 
     mime_replace_headers(m, headers);
     s = mime_entity_to_octstr(m);

     /* 
      *	Make the command: Transpose % formatting characters:
      * f - from address
      * t - recipient
      * s - subject
      * m - message id 
      */
     
     i = 0;
     for (;;) {
	  Octstr *tmp;
	  while (sendmail_cmd[i]) {
	       char c = sendmail_cmd[i];
	       if (c == '%' && sendmail_cmd[i + 1])
		    break;
	       octstr_append_char(cmd, c);
	       i++;
	  }
	  if (!sendmail_cmd[i])
	       break;
	  
	  switch(sendmail_cmd[i+1]) {
	  case 't':
	       tmp = octstr_duplicate(to);
	       escape_shell_chars(tmp);
	       octstr_append(cmd, tmp);
	       octstr_destroy(tmp);
	       break;
	  case 'f':
	       if (append_hostname) {
		    Octstr *xfrom = octstr_duplicate(from);
		    addmmscname(xfrom, myhostname);
		    escape_shell_chars(xfrom);

		    octstr_append(cmd, xfrom);
		    octstr_destroy(xfrom);
	       } else {
		    tmp = octstr_duplicate(from);
		    escape_shell_chars(tmp);		    
		    octstr_append(cmd, tmp);
		    octstr_destroy(tmp);
	       }
	       break;
	  case 's':
	       tmp = octstr_duplicate(subject);
	       escape_shell_chars(tmp);
	       octstr_append(cmd, subject);
	       octstr_destroy(tmp);
	       break;
	  case 'm':
	       tmp = octstr_duplicate(msgid);
	       escape_shell_chars(tmp);
	       octstr_append(cmd, msgid);
	       octstr_destroy(tmp);
	       break;
	  case '%':
	       octstr_format_append(cmd, "%%");
	       break;
	  default:
	       octstr_format_append(cmd, "%%%c", sendmail_cmd[i+1]);
	       break;
	  }
	  i += 2;
     }

 
     debug("mms.sendtoemail", 0, "preparing to execute %s to send to email: ", octstr_get_cstr(cmd));
     
     if ((f = popen(octstr_get_cstr(cmd), "w")) == NULL) {
	  *error = octstr_format("popen failed for %S: %d: %s",
				  cmd, errno, strerror(errno));
	  ret = MMS_SEND_ERROR_TRANSIENT;
	  goto done;
     }
     
     if (octstr_print(f, s) < 0) {
	  *error = octstr_format("send email failed in octstr_print %d: %s",
				  errno, strerror(errno));
	  pclose(f);
	  ret = MMS_SEND_ERROR_TRANSIENT;	  
	  goto done;
     }

     if ((ret = pclose(f)) != 0) {
	  *error = octstr_format("Send email command returned non-zero %d: errno=%s",
				  ret, strerror(errno));
	  ret = MMS_SEND_ERROR_TRANSIENT;	  
     } else 
	  ret = MMS_SEND_QUEUED;

 done:
     http_destroy_headers(headers);
     octstr_destroy(cmd);
     octstr_destroy(s);
     return ret;
}


int mm_send_to_email(Octstr *to, Octstr *from, Octstr *subject, 
		      Octstr *msgid,
		      MIMEEntity *m, int append_hostname, Octstr **error, 
		      char *sendmail_cmd, Octstr *myhostname)
{
     return send2email(to,from,subject,msgid,m,append_hostname,error,sendmail_cmd,myhostname);
}

/* Send this message to email recipient. */
int mms_sendtoemail(Octstr *from, Octstr *to, 
		    Octstr *subject, Octstr *msgid,
		    MmsMsg *msg, int dlr, 
		    Octstr **error, char *sendmail_cmd,		    
		    Octstr *myhostname, 
		    int trans_msg,
		    int trans_smil, char *txt, char *html,
		    int mm4, 
		    char *transid)
{
     
     MIMEEntity *m = NULL;
     List *headers = NULL;
     List *newhdrs = http_create_empty_headers();
     int ret = 0, mtype;
     
     gw_assert(msg);     
     mtype = mms_messagetype(msg);

     if (!to || 
	 octstr_search_char(to, '@', 0) < 0) {
	  *error = octstr_format("Invalid email address %S!", to);
	  return MMS_SEND_ERROR_FATAL;
     }
   
     if (!trans_msg)
	  m = mms_tomime(msg,0);
     else if ((ret = mms_format_special(msg, trans_smil, txt, html, &m)) < 0 ||
	      m == NULL) {
	  warning(0, "MMS: send2email failed to format message (msg=%s,ret=%d)",
		  m ? "OK" : "Not transformed",ret);
	  return -ret;
     }      
     
     base64_mimeparts(m,0); /* make sure parts are base64 formatted. */

     headers = mime_entity_headers(m);
     
     /* Before we send it, we insert some email friendly headers if they are missing. */
     if (!mm4) {
	  http_header_add(newhdrs, "Subject", subject ? octstr_get_cstr(subject) : "MMS Message");
	  http_header_remove_all(headers, "From");
	  http_header_add(newhdrs, "From", octstr_get_cstr(from));
	  http_header_remove_all(headers, "To");
	  http_header_add(newhdrs, "To", octstr_get_cstr(to));
	  http_header_add(newhdrs, "Message-ID", msgid ? octstr_get_cstr(msgid) : "");
	  http_header_add(newhdrs, "MIME-Version", "1.0");
     } else {
	  char *x, tmp[32];	  
	  Octstr *xsender = octstr_format("system-user@%S", myhostname);
	  http_header_add(newhdrs, "X-Mms-Message-ID", msgid ? octstr_get_cstr(msgid) : "");
	  
	  sprintf(tmp, "%d.%d.%d", 
		  MAJOR_VERSION(MMS_3GPP_VERSION),
		  MINOR1_VERSION(MMS_3GPP_VERSION),
		  MINOR2_VERSION(MMS_3GPP_VERSION));
	  http_header_add(newhdrs, "X-Mms-3GPP-MMS-Version", tmp);
	  http_header_add(newhdrs, "X-Mms-Originator-System",
			  octstr_get_cstr(xsender));
	  http_header_remove_all(headers, "X-Mms-Message-Type");
	  if (mtype == MMS_MSGTYPE_SEND_REQ ||
	      mtype == MMS_MSGTYPE_RETRIEVE_CONF)
	       x = "MM4_forward.REQ";
	  else if (mtype == MMS_MSGTYPE_DELIVERY_IND) {
	       Octstr *s = http_header_value(headers, octstr_imm("X-Mms-Status"));
	       x  = "MM4_delivery_report.REQ";
	       
	       /* rename status header. */
	       http_header_remove_all(headers, "X-Mms-Status");
	       http_header_add(newhdrs, "X-Mms-MM-Status-Code", 
			       s ? octstr_get_cstr(s) : "Unrecognised");
	       if (!s)
		    warning(0, "MMS Delivery report with missing Status!");
	       octstr_destroy(s);
	  } else if (mtype == MMS_MSGTYPE_READ_REC_IND)
	       x = "MM4_read_reply_report.REQ";
	  else {
	       *error = octstr_format("Invalid message type %s on MM4 outgoing interface!", 
				      mms_message_type_to_cstr(mtype));
	       x = "";
	       ret =  MMS_SEND_ERROR_FATAL;
	       goto done;
	  }
	  http_header_add(newhdrs, "X-Mms-Message-Type", x);
	  /* Add a few more MM4 headers. */
	  http_header_add(newhdrs, "X-Mms-Ack-Request", dlr ? "Yes" : "No");
	  http_header_add(newhdrs, "Sender", octstr_get_cstr(from));
	  http_header_add(newhdrs, "X-Mms-Transaction-ID", transid);

	  octstr_destroy(xsender);
     }
     
     http_header_combine(headers, newhdrs); 
     mime_replace_headers(m, headers);
     
 done:
     http_destroy_headers(headers);
     http_destroy_headers(newhdrs);

     if (ret == 0)
	  ret = send2email(to, 
			   from, subject, msgid, m, mm4 == 0, error, sendmail_cmd, myhostname);
     mime_entity_destroy(m);

     return ret;
}

void mms_log2(char *logmsg, Octstr *from, Octstr *to, 
	      int msize, Octstr *msgid,
	      Octstr *acct,
	      Octstr *viaproxy,
	      char *interface, Octstr *ua, Octstr *mmboxloc)
{
     List *l;
     if (to) {
	  l = gwlist_create();
	  gwlist_append(l, to);
     } else
	  l = NULL;
     
     mms_log(logmsg, from,l,msize,msgid,acct,viaproxy,interface,ua,mmboxloc);
     
     if (l) 
	  gwlist_destroy(l, NULL);
}

void mms_log(char *logmsg, Octstr *from, List *to, 
	     int msize, Octstr *msgid,
	     Octstr *acct,
	     Octstr *viaproxy,
	     char *interface, Octstr *ua, Octstr *mmboxloc)
{
     Octstr *xto = octstr_create("");
     int i, n = to ? gwlist_len(to) : 0;

     for (i = 0; i < n; i++)
	  octstr_format_append(xto, 
			       "%s%S",
			       (i == 0) ? "" : ", ",
			       gwlist_get(to,i));
       
     alog("%s MMS [INT:%s] [ACT:%s] [MMSC:%s] [from:%s] [to:%s] [msgid:%s] [size=%d] [UA:%s] [MMBox:%s]", 
	  logmsg, interface, 
	  acct ? octstr_get_cstr(acct) : "",
	  viaproxy ? octstr_get_cstr(viaproxy) : "",
	  from ? octstr_get_cstr(from) : "",
	  octstr_get_cstr(xto),
	  msgid ? octstr_get_cstr(msgid) : "",
	  msize,
	  ua ? octstr_get_cstr(ua) : "", 
	  mmboxloc ? octstr_get_cstr(mmboxloc) : "");

     octstr_destroy(xto);
}


⌨️ 快捷键说明

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