Beispiel #1
0
static int sendNotify(MmsEnvelope *e)
{
     Octstr *to;
     MmsMsg *msg, *smsg = NULL;
     MmsEnvelopeTo *xto = gwlist_get(e->to, 0);
     Octstr *err = NULL;
     time_t tnow = time(NULL);
     int j, k, len;
     Octstr *phonenum = NULL, *rcpt_ip = NULL, *msgId, *from, *fromproxy;
     int mtype, msize;
     int res = MMS_SEND_OK, dlr;
     time_t expiryt;
     char *prov_notify_event = NULL;
     char *rtype = NULL;

          
     if (e->lastaccess != 0) { /* This message has been fetched at least once, no more signals. */	  
	  e->sendt = e->expiryt + 3600*24*30*12;
	  info(0, "MM1: Message [ID: %s] fetched/touched at least once. Skipping",
	       e->xqfname);
	  return settings->qfs->mms_queue_update(e);
     }

     if (!xto) {
	  error(0, "mobilesender: Queue entry %s with no recipients!", 
		e->xqfname);
	  return 0;
     }

     msg = settings->qfs->mms_queue_getdata(e);
     to = octstr_duplicate(xto->rcpt);
     expiryt = e->expiryt;
     msgId = e->msgId ? octstr_duplicate(e->msgId) : NULL;
     from = octstr_duplicate(e->from);
     fromproxy = e->fromproxy ? octstr_duplicate(e->fromproxy) : NULL;
     msize = e->msize;
     dlr = e->dlr;

     if (e->expiryt != 0 &&  /* Handle message expiry. */
	 e->expiryt < tnow) {
	  err = octstr_format("MMSC error: Message expired while sending to %S!", to);
	  res = MMS_SEND_ERROR_FATAL;
	  prov_notify_event = "failedfetch";
	  rtype = "Expired";	  
	  goto done;
     } else if (e->attempts >= settings->maxsendattempts) {
	  err = octstr_format("MMSC error: Failed to deliver to %S after %ld attempts!", 
			      to, e->attempts);
	  res = MMS_SEND_ERROR_FATAL;

	  prov_notify_event = "failedfetch";
	  rtype = "Expired";	  
	  goto done;
     }
     
     j = octstr_case_search(to, octstr_imm("/TYPE=PLMN"), 0);
     k = octstr_case_search(to, octstr_imm("/TYPE=IPv"), 0);
     len = octstr_len(to);
     
     if (j > 0 && j - 1 +  sizeof "/TYPE=PLMN" == len) { /* A proper number. */
	  phonenum = octstr_copy(to, 0, j);
#if 0
	  normalize_number(octstr_get_cstr(settings->unified_prefix), &phonenum);
#else
	  mms_normalize_phonenum(&phonenum, 
				 octstr_get_cstr(settings->unified_prefix), 
				 settings->strip_prefixes);	  
#endif
     }     else if (k > 0 && k + sizeof "/TYPE=IPv" == len) 
	  rcpt_ip = octstr_copy(to, 0, k);
     else {
	  /* We only handle phone numbers here. */
	  err = octstr_format("Unexpected recipient %s in MT queue!", octstr_get_cstr(to));	  
	  res = MMS_SEND_ERROR_FATAL;	  
	  goto done;
     }
     
     mtype = mms_messagetype(msg);     
     
     /* For phone, getting here means the message can be delivered. So: 
      * - Check whether the recipient is provisioned, if not, wait (script called will queue creation req)
      * - If the recipient can't take MMS, then send SMS.
      */
     
     /* We handle two types of requests: send and delivery/read notifications. 
      * other types of messages cannot possibly be in this queue!
      */
     
     if (mtype == MMS_MSGTYPE_SEND_REQ ||
	 mtype == MMS_MSGTYPE_RETRIEVE_CONF) {
	  Octstr *url, *transid;
	  
	  if (phonenum) {
	       int send_ind = mms_ind_send(settings->prov_getstatus, phonenum);
	       
	       if (send_ind < 0 && 
		   settings->notify_unprovisioned)
		    send_ind = 0;

	       if (send_ind < 0) { /* That is, recipient is not (yet) provisioned. */
		    res = MMS_SEND_ERROR_TRANSIENT;
		    err = octstr_format("%S is not provisioned for MMS reception, delivery deferred!", 
					phonenum);
		    
		    /* Do not increase delivery attempts counter. */
		    e->lasttry = tnow;		    
		    e->sendt = e->lasttry + settings->send_back_off * (1 + e->attempts);
		    
		    if (settings->qfs->mms_queue_update(e) == 1) 
			 e = NULL; /* Queue entry gone. */	
		    else 	  
			 settings->qfs->mms_queue_free_env(e);		    		    
		    goto done;
	       } else if (send_ind == 0) { /*  provisioned but does not support */		    
		    Octstr *s = octstr_format(octstr_get_cstr(settings->mms_notify_txt),
					      from);
		    if (settings->notify_unprovisioned && s && octstr_len(s) > 0) { /* Only send if the string was set. */
			 List *pheaders;					    
			 Octstr *sto = octstr_duplicate(phonenum);
			 
			 octstr_url_encode(s);
			 octstr_url_encode(sto);
			 
			 url = octstr_format("%S&text=%S&to=%S",settings->sendsms_url,s, sto);
			 pheaders = http_create_empty_headers();
			 http_header_add(pheaders, "Connection", "close");
			 http_header_add(pheaders, "User-Agent", MM_NAME "/" VERSION);      
			 
			 http_start_request(httpcaller, HTTP_METHOD_GET, url, 
					    pheaders, NULL, 0, &edummy, NULL);			 			 
			 http_destroy_headers(pheaders);
			 octstr_destroy(url);
			 octstr_destroy(sto);		   		 
		    } else if (s)
			 octstr_destroy(s);	
		    res = MMS_SEND_OK;
		    err = octstr_imm("No MMS Ind support, sent SMS instead");
		    
		    xto->process = 0; /* No more processing. */
		    if (settings->qfs->mms_queue_update(e) == 1) 
			 e = NULL; 
		    else 	  
			 settings->qfs->mms_queue_free_env(e);		    
		    goto done;

	       }	       
	  }
	  
	  /* To get here means we can send Ind. */
	  url = mms_makefetchurl(e->xqfname, e->token, MMS_LOC_MQUEUE,
				 phonenum ? phonenum : to,
				 settings);
	  info(0, "Preparing to notify client to fetch message at URL: %s",
	       octstr_get_cstr(url));
	  transid = mms_maketransid(e->xqfname, settings->host_alias);	  
	  
	  smsg = mms_notification(msg, e->msize, url, transid, 
				  e->expiryt ? e->expiryt :
				  tnow + settings->default_msgexpiry,
				  settings->optimize_notification_size);
	  octstr_destroy(transid);
	  octstr_destroy(url);
     } else if (mtype == MMS_MSGTYPE_DELIVERY_IND ||
		mtype == MMS_MSGTYPE_READ_ORIG_IND) 
	  smsg = msg;
     else {
	  error(0, "Unexpected message type %s for %s found in MT queue!", 
		mms_message_type_to_cstr(mtype), octstr_get_cstr(to));
	  res = MMS_SEND_ERROR_FATAL;
	  goto done;
     }
     
     if (smsg)
	  start_push(phonenum ? phonenum : rcpt_ip, 
		     phonenum ? 1 : 0, 
		     e, smsg); /* Send the message. 
				* Don't touch 'e' after this point!
				* It may be freed by receive thread. 
				*/
     
     if (smsg != msg && smsg)
	  mms_destroy(smsg);
    
 done:
     if (err != NULL && 
	 res != MMS_SEND_ERROR_TRANSIENT) { /* If there was a report request and this is a legit error
					     *  queue it. 
					     */
	  
	  if (dlr) {	       
	       MmsMsg *m = mms_deliveryreport(msgId, to, tnow, 
					      rtype ? octstr_imm(rtype) : 
					      octstr_imm("Indeterminate"));
	       
	       List *l = gwlist_create();
	       Octstr *res;
	       gwlist_append(l, from);
	       
	       /* Add to queue, switch via proxy to be from proxy. */
	       res = settings->qfs->mms_queue_add(to ? to : settings->system_user, l,  err, 
						  NULL, fromproxy,  
						  tnow, tnow+settings->default_msgexpiry, m, NULL, 
						  NULL, NULL,
						  NULL, NULL,
						  NULL,
						  0,
						  octstr_get_cstr(settings->mm1_queuedir), 
						  "MM2",
						  settings->host_alias);
	       gwlist_destroy(l, NULL);
	       mms_destroy(m);
	       octstr_destroy(res);
	  }
	  
     }
     
     /* Write to log */
     info(0, "%s Mobile Queue MMS Send Notify: From=%s, to=%s, msgsize=%d, reason=%s", 
	  SEND_ERROR_STR(res),
	  octstr_get_cstr(from), octstr_get_cstr(to), msize,
	  err ? octstr_get_cstr(err) : "");
     
     
     if (res == MMS_SEND_ERROR_FATAL) {
	  xto->process = 0;  /* No more attempts to deliver, delete this. */	       
	  if (settings->qfs->mms_queue_update(e) == 1) 
	       e = NULL; /* Queue entry gone. */	
	  else 	  
	       settings->qfs->mms_queue_free_env(e);
     }    /* Else queue will be updated/freed elsewhere. */
     
     
     if (prov_notify_event)
	  notify_prov_server(octstr_get_cstr(settings->prov_notify), 
			     to ? octstr_get_cstr(to) : "unknown", 
			     prov_notify_event, 
			     rtype ? rtype : "",
			     e ? e->msgId : NULL, NULL, NULL);

     if (msg) mms_destroy(msg);       

     octstr_destroy(phonenum);
     
     octstr_destroy(rcpt_ip);
     octstr_destroy(to);
     octstr_destroy(msgId);
     octstr_destroy(fromproxy);
     octstr_destroy(from);
     octstr_destroy(err);

     return 1;
}
Beispiel #2
0
static int sendMsg(MmsEnvelope *e)
{
     MmsMsg *msg = NULL;
     int i, n;     
     Octstr *otransid = e->hdrs ? http_header_value(e->hdrs, octstr_imm("X-Mbuni-TransactionID")) : NULL;

     if ((msg = qfs->mms_queue_getdata(e)) == NULL)  {
	  mms_error(0,  "MM7", NULL, "MMSBox queue error: Failed to load message for queue id [%s]!", e->xqfname);
	  goto done2;
     }
     
     for (i = 0, n = gwlist_len(e->to); i<n; i++) {
	  int res = MMS_SEND_OK;
	  MmsEnvelopeTo *to = gwlist_get(e->to, i);
	  Octstr *err = NULL;
	  time_t tnow = time(NULL);
	  MmscGrp *mmc = NULL;
	  Octstr *new_msgid = NULL;
	  List *errl = NULL;
	  int is_email = 0;

	  if (!to || !to->process)
	       continue;
	  
	  if (e->expiryt != 0 &&  /* Handle message expiry. */
	      e->expiryt < tnow) {
               err = octstr_format("MMSC error: Message expired while sending to %S!", to->rcpt);
               res = MMS_SEND_ERROR_FATAL;
	       
               goto done;
          } else if (e->attempts >= maxsendattempts) {
               err = octstr_format("MMSBox error: Failed to deliver to "
				   "%S after %ld attempts. (max attempts allowed is %ld)!", 
                                   to->rcpt, e->attempts, 
                                   maxsendattempts);
               res = MMS_SEND_ERROR_FATAL;
               goto done;
          }

	  is_email = (octstr_search_char(to->rcpt, '@', 0) > 0);
	  
	  if ((mmc = get_handler_mmc(e->viaproxy, to->rcpt, e->from)) == NULL && !is_email) {
               err = octstr_format("MMSBox error: Failed to deliver to "
				   "%S. Don't know how to route!", 
                                   to->rcpt);
               res = MMS_SEND_ERROR_TRANSIENT;
	       goto done;
	  }
	  
	  if (is_email) {
               int j = octstr_case_search(e->from, octstr_imm("/TYPE=PLMN"), 0);
               int k = octstr_case_search(e->from, octstr_imm("/TYPE=IPv"), 0);
               int len = octstr_len(e->from);
               Octstr *pfrom;
	       
               
               if (j > 0 && j - 1 +  sizeof "/TYPE=PLMN" == len) 
                    pfrom = octstr_copy(e->from, 0, j);
               else if (k > 0 && k + sizeof "/TYPE=IPv" == len) 
                    pfrom = octstr_copy(e->from, 0, k);
               else
                    pfrom = octstr_duplicate(e->from);

               if (octstr_search_char(e->from, '@', 0) < 0)
                    octstr_format_append(pfrom,"@%S", myhostname);
	       
	       res = mms_sendtoemail(pfrom, to->rcpt, 
				     e->subject ? e->subject : octstr_imm(""),
				     e->msgId, msg, 0, &err, octstr_get_cstr(sendmail_cmd),
				     myhostname, 0, 0, 
				     "", 
				     "", 0, 
				     e->xqfname, 
				     e->hdrs);
	       octstr_destroy(pfrom);
	  } else {
	       res = mms_sendtommsc(mmc, e, 
				    to->rcpt, 
				    otransid,
				    msg, 
				    &new_msgid,
				    &errl);
	       if (errl)
		    err = http_header_value(errl, octstr_imm("X-Mbuni-Error"));
	  }
	  if (mmc) {
	       if (res == MMS_SEND_OK)
		    mmc->mt_pdus++;
	       else
		    mmc->mt_errors++;	  
	       mmc->last_pdu = time(NULL);
	       return_mmsc_conn(mmc); /* important. */
	  }
     done:
	  if (res == MMS_SEND_OK || res == MMS_SEND_QUEUED) {
	       to->process = 0;
	       
	       if (e->msgtype == MMS_MSGTYPE_SEND_REQ ||
		   e->msgtype == MMS_MSGTYPE_RETRIEVE_CONF) /* queue dlr as needed. */
		    queue_dlr(mmc, e->from, to->rcpt, new_msgid, octstr_imm("Forwarded"), "MM7-Out", errl);
	  } else if (res == MMS_SEND_ERROR_FATAL && mmc) {
	       if (e->msgtype == MMS_MSGTYPE_SEND_REQ ||
		   e->msgtype == MMS_MSGTYPE_RETRIEVE_CONF) /* queue dlr as needed. */		    
		    queue_dlr(mmc, e->from, to->rcpt, e->msgId, 			
			      (e->expiryt != 0 && e->expiryt < tnow) ? 
			 octstr_imm("Expired") : octstr_imm("Rejected"), 
			      "MM7-Out", errl);
	  }
	  if (res == MMS_SEND_ERROR_FATAL)
	       to->process = 0; /* No more attempts. */        


	  /* handle CDR */
	  if (res == MMS_SEND_OK || res == MMS_SEND_QUEUED || res == MMS_SEND_ERROR_FATAL) {
	       Octstr *mclass = mms_get_header_value(msg, octstr_imm("X-Mms-Message-Class"));
	       Octstr *prio = mms_get_header_value(msg, octstr_imm("X-Mms-Priority"));
	       Octstr *mstatus  = mms_get_header_value(msg, octstr_imm("X-Mms-Status"));
	       
	       /* Do CDR */
	       cdrfs->logcdr(e->created, 
			     octstr_get_cstr(e->from),
			     octstr_get_cstr(to->rcpt),
			     octstr_get_cstr(e->msgId),
			     mmc ? octstr_get_cstr(mmc->id) : NULL, /* Should we touch mmc here? XXX */ 
			     e->src_interface, 
			     "MM7",
			     e->msize, 
			     (char *)mms_message_type_to_cstr(e->msgtype),
			     
			     prio ? octstr_get_cstr(prio) : NULL,
			     mclass ? octstr_get_cstr(mclass) : NULL,
			     res == MMS_SEND_ERROR_FATAL ? "dropped" : (mstatus ? octstr_get_cstr(mstatus) : "sent"),
			     e->dlr,
			     0);
	       
	       octstr_destroy(mclass);
	       octstr_destroy(prio);
	       octstr_destroy(mstatus);	       
	  }

	  if (err == NULL)
	       mms_info(0, "MM7", NULL, "%s MMSBox Outgoing Queue MMS Send: From %s, to %s, msgsize=%ld: msgid=[%s]", 
			SEND_ERROR_STR(res),
			octstr_get_cstr(e->from), octstr_get_cstr(to->rcpt), e->msize,
			new_msgid ? octstr_get_cstr(new_msgid) : "N/A");
	  else 
	       mms_error_ex("MT", 0, 
			    "MM7", NULL, 
			    "%s MMSBox Outgoing Queue MMS Send: From %s, to %s, msgsize=%ld: %s", 
			    SEND_ERROR_STR(res),
			    octstr_get_cstr(e->from), octstr_get_cstr(to->rcpt), e->msize, octstr_get_cstr(err));
	  
	  octstr_destroy(new_msgid);
	  octstr_destroy(err);
	  http_destroy_headers(errl);
	  
	  e->lasttry = tnow;
          if (qfs->mms_queue_update(e) == 1) { 
               e = NULL;
               break; /* Queue entry gone. */             
          }
     }

done2:
     mms_destroy(msg);     
     octstr_destroy(otransid);
     
     if (e) { /* Update the queue if it is still valid (e.g. recipients not handled) 
	       * XXX can this happen here??... 
	       */
	  e->lasttry = time(NULL);
	  e->attempts++;  /* Update count of number of delivery attempts. */   
	  e->sendt = e->lasttry + mmsbox_send_back_off * e->attempts;
	  
	  if (qfs->mms_queue_update(e) != 1)
	       qfs->mms_queue_free_env(e);
     }

     
     return 1; /* always delete queue entry. */
}