Implemented RTS/CTS handshaking (-C/--rtscts vs. -x/--xonxoff)
[mdsms.git] / mdsms.c
diff --git a/mdsms.c b/mdsms.c
index 2b7edd1..ccd9d5c 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -273,8 +273,10 @@ Usage: %s [-c|--config <cfgfile>] [-d|--device <device>]\n\
  <command name>\tProgram to run on receive, message will be on stdin\n\
 \t\tFollowing substitutes are recognized:\n\
 \t\t%%p - source phone number\n\
-\t\t%%T - timestamp from SMSC as number of seconds from 1970\n\
-\t\t%%t - ctime(3) style timestamp (e.g. \"Wed Jun 30 21:49:08 1993\")\n\
+\t\t%%T - timestamp from SMSC as # of seconds from 1970 (-1 if invalid)\n\
+\t\t%%t - ctime(3) style timestamp (e.g. \"Wed Jun 30 21:49:08 1993\"),\n\
+\t\t     empty string if invalid\n\
+\t\t%%s - originating SMSC number, if available (else empty)\n\
 --logo-send:\n\
  <GSMnet id>\t* Oper. logo: Enter custom network code MccMnc, e.g. 23002\n\
 \t\t* Oper. logo: Specify \"%s\" to read network code from NOL file\n\
@@ -314,7 +316,8 @@ static const struct option longopts[]={
 {"file"    ,0,0,'f'},
 {"verbose" ,0,0,'v'},
 {"help"    ,0,0,'h'},
-{"version" ,0,0,'V'}};
+{"version" ,0,0,'V'},
+{NULL      ,0,0,0  }};
 
 static void processargs(int argp,char **args,const char *from);
 
@@ -661,7 +664,7 @@ char *s;
                body=glueargstack(&bodylen," ");
                for (s=body;(s=strchr(s,'%'));s++)
                        switch (*++s) {
-                               case 'p': case 'T': case 't': break;
+                               case 'p': case 'T': case 't': case 's': break;
                                default:
                                        error(_("Unknown formatsymbol '%c' (use \"%%%%%c\" to fix it) at pos %d in: %s"),
                                                *s,*s,s-body,body);
@@ -1088,7 +1091,7 @@ unsigned char bin[2+(MAXNUMLEN+1)/2];
        textconv(pdusmsc,bin,bin[0]+1);
 }
 
-static inline unsigned char charconv(char c,size_t offs)
+static inline unsigned char charconv_send(char c,size_t offs)
 {
        switch (c) {
                case '@': return(0);
@@ -1105,6 +1108,17 @@ static inline unsigned char charconv(char c,size_t offs)
 #endif
 }
 
+static inline unsigned char charconv_recv(char c,size_t offs)
+{ /* FIXME: unify with charconv_send() */
+       switch (c) {
+               case 0: return('@');
+               case 2: return('$');
+               default:
+                       return(c);
+               }
+/* strict checking not done, see charconv_send */
+}
+
 /* Logo format shamelessly stolen from GNokii-0.3.0: http://www.gnokii.org/
  * Beware - Nokia Smart Messaging specification 1.0.0 and 2.0.0 is incompatible
  * with Nokia current product line implementation
@@ -1243,19 +1257,19 @@ long size;
 
 #define WORD(n) (((unsigned char)buf[(n)])|(((unsigned char)buf[(n)+1])<<8))
 
-       if (!(f=fopen(logoname,"rb")))
-               error(_("^!Cannot open ring file \"%s\" for r/o"),logoname);
-       if ((size=getfilesize(f,logoname))==-1)
+       if (!(f=fopen(ringname,"rb")))
+               error(_("^!Cannot open ring file \"%s\" for r/o"),ringname);
+       if ((size=getfilesize(f,ringname))==-1)
                error(_("!File size determination is essential to continue operation"));
        if (size<0x103)
                error(_("!File \"%s\" size %ld too small (must >=0x103)! Is it .000 file?"),
-                       logoname,size);
+                       ringname,size);
        if (fseek(f,0x100,SEEK_SET))
-               error(_("^Seeking error on \"%s\", ignoring"),logoname);
+               error(_("^Seeking error on \"%s\", ignoring"),ringname);
        size-=0x100;
        if (size<=BIN1_PAYLOAD) {
                if ((got=fread(bin1+7,1,size,f))!=size)
-                       error(_("^Read error on \"%s\", wanted %ld, got %d"),logoname,size,got);
+                       error(_("^Read error on \"%s\", wanted %ld, got %d"),ringname,size,got);
                error(_("\nSending ring tone \"%s\" as single SMS (size %ld, max %d)"),
                        ringname,size,BIN1_PAYLOAD);
                nokiaprep(bin1,7+size);
@@ -1276,12 +1290,12 @@ long size;
                        binn[11]=fragn;
                        want=MIN(size,BINN_PAYLOAD);
                        if ((got=fread(binn+12,1,want,f))!=want)
-                               error(_("^Read error on \"%s\", wanted %d, got %d"),logoname,want,got);
+                               error(_("^Read error on \"%s\", wanted %d, got %d"),ringname,want,got);
                        nokiaprep(binn,12+want);
                        size-=want;
                        }
                }
-       chkfclose(f,logoname);
+       chkfclose(f,ringname);
 #undef WORD
 }
 
@@ -1311,7 +1325,7 @@ size_t offs=0;
        while (bodylen || inb) {
                if (!inb) {
                        assert(bodylen>0); assert(!!*body);
-                       inreg=charconv(*bodyr++,offs++);
+                       inreg=charconv_send(*bodyr++,offs++);
                        bodylen--;
                        inb=7;
                        }
@@ -1457,13 +1471,9 @@ const char *sf,*ss;
        return(NULL);
 }
 
-static char *receive_number;
+static char *receive_number,*receive_smsc;
 static time_t receive_time;
 
-/* +CMT: "+420602431329",,"99/10/25,03:21:03-00" */
-static int receive_headerparse(char *buf)
-{
-char *s,*s1,*err;
 struct tm tm;
 static const struct {
        off_t strpos;
@@ -1480,9 +1490,36 @@ static const struct {
                TP_ENT(18,tm_sec ,0,59,N_("second")),
                /* Time zone ignored */
                };
-int i,val;
+#define GETTIME(i) (*(int *)(((char *)&tm)+timeparse[(i)].tmpos))
+
+static void maketime(const char *string)
+{
+int val;
+int i;
 
-#define DIGIT2(s) (((s)[0]-'0')*10+((s)[1]-'0'))
+       for (i=0;i<NELEM(timeparse);i++) {
+               val=GETTIME(i);
+               if (val<timeparse[i].min || val>timeparse[i].max) {
+                       error(_("Weird value of %s, is %d but expected %d..%d, setting to %d"),
+                               _(timeparse[i].name),val,timeparse[i].min,timeparse[i].max,timeparse[i].min);
+                       GETTIME(i)=timeparse[i].min;
+                       }
+               }
+       if (tm.tm_year<70) tm.tm_year+=100;
+       tm.tm_mon--;
+       d7("mktime(y%dm%dd%dh%dm%ds%d)\n",
+               tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
+       if ((receive_time=mktime(&tm))==-1)
+               error(_("^mktime(3) failed for %s"),string);
+}
+
+/* +CMT: "+420602431329",,"99/10/25,03:21:03-00" */
+static int receive_headerparse(char *buf)
+{
+char *s,*s1,*err;
+int i;
+
+#define DIGIT2ASC(s) (((s)[0]-'0')*10+((s)[1]-'0'))
 
        for (s=buf;*s==' ';s++);
        if (*s++!='"') {
@@ -1494,6 +1531,7 @@ int i,val;
                error(_("Only one '\"' found in CMT header: %s"),buf);
                return(0);
                }
+       free(receive_smsc); receive_smsc=NULL;
        free(receive_number);
        chk(receive_number=malloc(s-s1+1));
        memcpy(receive_number,s1,s-s1); receive_number[s-s1]='\0';
@@ -1504,28 +1542,16 @@ int i,val;
                return(0);
                }
        memset(&tm,0,sizeof(tm)); /* may be redundant */
-       for (i=0;i<NELEM(timeparse);i++) {
-               val=DIGIT2(s+timeparse[i].strpos);
-               if (val<timeparse[i].min || val>timeparse[i].max) {
-                       error(_("Weird value of %s, is %d but expected %d..%d, setting to %d"),
-                               _(timeparse[i].name),val,timeparse[i].min,timeparse[i].max,timeparse[i].min);
-                       val=timeparse[i].min;
-                       }
-               *(int *)(((char *)&tm)+timeparse[i].tmpos)=val;
-               }
+       for (i=0;i<NELEM(timeparse);i++)
+               GETTIME(i)=DIGIT2ASC(s+timeparse[i].strpos);
        if (tm.tm_year<70) tm.tm_year+=100;
        tm.tm_mon--;
-       d7("mktime(y%dm%dd%dh%dm%ds%d)\n",
-               tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
-       if ((receive_time=mktime(&tm))==-1) {
-               error(_("^mktime(3) failed for %s"),s+2);
-               return(0);
-               }
+       maketime(s+2);
        return(1);
-#undef DIGIT2
+#undef DIGIT2ASC
 }
 
-static void receive_accept(char *bodyline)
+static void receive_text(char *bodyline)
 {
 char *buf,*s,*s1,*s2,*s3;
 pid_t pid;
@@ -1533,7 +1559,7 @@ char tbuf[32];
 int i;
 FILE *f;
 
-       d2("receive_accept: %s\n",bodyline);
+       d2("receive_text: %s\n",bodyline);
 #if RECEIVE_TEST
        pid=0;
 #else
@@ -1550,7 +1576,7 @@ FILE *f;
                s1=s;
                do {
                        s1=strchr(s1+(s1!=s),'%');
-                       } while (s1 && s1[1]!='p' && s1[1]!='T' && s1[1]!='t');
+                       } while (s1 && s1[1]!='p' && s1[1]!='T' && s1[1]!='t' && s1[1]!='s');
                if (!s1) {
                        pushargstack_one(s,0);
                        break;
@@ -1568,6 +1594,7 @@ FILE *f;
                                pushargstack_one(tbuf,0);
                                break;
                        case 't':
+                               if (receive_time==-1) break;
                                if (!(s2=ctime(&receive_time))) {
                                        error(_("Failing ctime(3), ignoring substitution"));
                                        break;
@@ -1575,6 +1602,9 @@ FILE *f;
                                if ((s3=strchr(s2,'\n'))) *s3='\0';
                                pushargstack_one(s2,0);
                                break;
+                       case 's':
+                               if (receive_smsc) pushargstack_one(receive_smsc,0);
+                               break;
                        default: assert(0);
                        }
                }
@@ -1593,6 +1623,173 @@ err:
        exit(EXIT_SUCCESS); /* cleanup() has been disabled */
 }
 
+static inline unsigned char fromhex(char c)
+{
+       c&=0xDF;
+       return(c<'A'?c-'0':(c-'A')+0xA);
+}
+
+static int teldecode(char *text,unsigned char *bin,size_t digits)
+{
+unsigned char b;
+int r=0,i;
+
+       for (i=0;i<digits;text++,i++) {
+               if (!(i&1)) b=*bin;
+               else b=(*bin++)>>4;
+               b&=0x0F;
+               if (b<=0x09)
+                       *text=b+'0';
+               else {
+                       *text='?';
+                       r++;
+                       }
+               }
+       *text='\0';
+       return(r);
+}
+
+static void sctsparse(unsigned char *bin,const char *pduline,int offs)
+{
+#define DIGIT2BIN(v) (((v)&0x0F)*10+(((v)>>4)&0x0F))
+int i;
+
+       receive_time=-1;
+       memset(&tm,0,sizeof(tm)); /* may be redundant */
+       for (i=0;i<NELEM(timeparse);offs++,i++) {
+               if ((*bin&0x0F)>0x09 || (*bin&0xF0)>0x90) {
+                       error(_("Invalid value of \"%s\" at offset %d in: %s"),
+                               timeparse[i].name,offs,pduline);
+                       return;
+                       }
+               GETTIME(i)=DIGIT2BIN(*bin);
+               bin++;
+               }
+       maketime(pduline);
+
+#undef DIGIT2BIN
+}
+
+static void receive_pdu(char *pduline)
+{
+unsigned char pdu[140+0x100],*pdup,*pdue,oalen,inreg;
+char text[160+1],*textp,*s;
+size_t pdulinel=strlen(pduline),want;
+size_t udl,udlb;
+int inb,outb,xb;
+
+       d2("receive_pdu: %s\n",pduline);
+       if (pdulinel>2*sizeof(pdu))
+               { error(_("PDU too long (%d/2) to be valid: %s"),pdulinel,pduline); return; }
+       if (pdulinel&1)
+               { error(_("PDU length odd (%d): %s"),pdulinel,pduline); return; }
+       if (pdulinel<2*13)
+               { error(_("PDU length %d too small (min. 2*%d): %s"),pdulinel,13,pduline); return; }
+       for (pdup=pdu;*pduline;pduline+=2) {
+               if (!isxdigit(pduline[0]) || !(isxdigit(pduline[1])))
+               { error(_("Invalid hex byte: %c%c on byte %d in: %s"),
+                       pduline[0],pduline[1],pdup-pdu,pduline); return; }
+               *pdup++=(fromhex(pduline[0])<<4)|fromhex(pduline[1]);
+               }
+       pdue=pdup;
+       free(receive_smsc);
+       if (*pdu<=1) {
+               receive_smsc=NULL;
+               }
+       else {
+               if (*pdu>10)
+                       { error(_("SMSC length too large (%d, max. %d): %s"),*pdu,10,pduline); return; }
+               chk(receive_smsc=malloc(1+2*(*pdu)+1));
+               s=receive_smsc;
+               if (pdu[1]==ADDR_INT) *s++='+';
+               else {
+                       if (pdu[1]!=ADDR_NAT)
+                               error(_("Unknown address type 0x%02X of %s, ignoring in PDU: %s"),pdu[1],_("SMSC"),pduline); return;
+                       }
+               if (teldecode(s,pdu+2,2*(*pdu-1)-((pdu[1+(*pdu)]&0xF0)==0xF0)))
+                       error(_("Some digits unrecognized in %s \"%s\", ignoring in PDU: %s"),_("SMSC"),receive_smsc,pduline);
+               }
+       pdup=pdu+1+(*pdu);
+       if (*pdup&0x03) /* PDU type */
+               error(_("Unrecognized PDU type 0x%02X at offset %d, dropping: %s"),*pdup,pdup-pdu,pduline);
+       pdup++;
+       free(receive_number);
+       if ((oalen=*pdup++)>10) /* OA len */
+               { error(_("Originating number too large (%d, max. %d): %s"),oalen,10,pduline); return; }
+       if (pdup+(want=1+(oalen+1)/2+10)>pdue)
+               { error(_("PDU length too short (want %d, is %d): %s"),(pdup-pdu)+want,pdue-pdu,pduline); return; }
+       chk(receive_number=malloc(1+2*(*pdup)+1));
+       s=receive_number;
+       if (*pdup==ADDR_INT) *s++='+';
+       else {
+               if (*pdup!=ADDR_NAT)
+                       error(_("Unknown address type 0x%02X of %s, ignoring in PDU: %s"),*pdup,_("originating number"),pduline); return;
+               }
+       pdup++;
+       if (teldecode(s,pdup,oalen))
+               error(_("Some digits unrecognized in %s \"%s\", ignoring in PDU at offset %d: %s"),
+                       _("originating number"),receive_number,pdup-pdu,pduline);
+       pdup+=(oalen+1)/2;
+       if (*pdup) /* PID */
+               error(_("PID number %02X unsupported, ignoring: %s"),*pdup,pduline);
+       pdup++;
+       if (*pdup) { /* DCS */
+               if ((*pdup&0xF4)==0xF4)
+                       { error(_("DCS 0x%02X indicates 8-bit data, unsupported, dropping: %s"),*pdup,pduline); return; }
+               error(_("DCS 0x%02X unsupported, will attempt decoding: %s"),*pdup,pduline);
+               }
+       pdup++;
+       sctsparse(pdup,pduline,pdup-pdu);
+       pdup+=7;
+       /* UDL */
+       udl=*pdup++;
+       if (pdue-pdup>140) {
+               error(_("PDU data (%d) exceed maximum length of %d bytes, cut: %s"),
+                       pdue-pdup,140,pduline);
+               pdue=pdup+140;
+               }
+       udlb=(udl*7+7)/8;
+       if (pdup+udlb>pdue) {
+size_t udl1,udlb1;
+
+               udlb1=pdue-pdup;
+               udl1=(udlb1*8)/7;
+               error(_("PDU data length (%d/7->%d/8) longer than data (%d), cut to %d/7->%d/8: %s"),
+                       udl,udlb,pdue-pdup,udl1,udlb1,pduline);
+               udl=udl1; udlb=udlb1;
+               }
+       else
+               error(_("Trailing garbage ignored in PDU data (UDL %d/7->%d/8, got %d) in: %s"),
+                       udl,udlb,pdue-pdup,pduline);
+       textp=text;
+       inb=outb=0;
+       inreg=0; /* GCC happiness */
+       while (udl) {
+               if (!inb) {
+                       inreg=*pdup++;
+                       inb=8;
+                       }
+               if (!outb) {
+                       assert(textp<text+160);
+                       *textp=0x00;
+                       outb=7;
+                       }
+               xb=MIN(inb,outb);
+#if 0
+               d4("inb=%d,outb=%d,xb=%d\n",inb,outb,xb);
+#endif
+               *textp|=((inreg>>(unsigned)(7-inb))&((1<<xb)-1))<<(unsigned)(8-outb);
+               inb-=xb; outb-=xb;
+               if (!outb) {
+                       *textp=charconv_recv(*textp,textp-text);
+                       textp++;
+                       udl--;
+                       }
+               }
+       *textp=0;
+       receive_text(text);
+}
+
 static struct {
        char **sp;
        long *ip;
@@ -1608,10 +1805,11 @@ static struct {
 int main(int argc,char **argv)
 {
 char *s;
-int i;
+int i,cmgf=-1 /* GCC happiness */;
 unsigned fatal=0;
 speed_t portbaud=0 /* GCC happiness */;
 enum modenum argsmode;
+unsigned parts=0;
 
        if ((s=strrchr((pname=*argv),'/'))) pname=s+1;
 
@@ -1797,18 +1995,32 @@ retryall:
                devcmd("",NULL,"\r\nAT\033\032"); /* ESCAPE, CTRL-Z */
                devcmd(NULL,NULL,"\r\nAT");
                smscset();
-               switch (mode) {
-                       case MODE_SEND:
-retrysendcmgf:
+               if (mode==MODE_SEND || mode==MODE_RECEIVE) {
+                       cmgf=-1;
+                       do {
                                if (!devcmd(NULL,NULL,"!\r\nAT+CMGF=0")) {
                                        if (!devcmd(NULL,NULL,"!\r\nAT+CMGF=1"))
-                                               { retrying(); goto retrysendcmgf; }
+                                               { retrying(); continue; }
                /* CMGF=1 */
+                                       if (verbose>=1)
+                                               error(_(".Using AT+CMGF=1 (text mode).."));
+                                       cmgf=1;
+                                       }
+                               else {
+               /* CMGF=0 */
+                                       if (verbose>=1)
+                                               error(_(".Using AT+CMGF=0 (PDU mode).."));
+                                       cmgf=0;
+                                       }
+                               } while (cmgf==-1);
+                       }
+               switch (mode) {
+                       case MODE_SEND:
+                               if (cmgf) {
                                        devcmd("\n> ",NULL,"\r\nAT+CMGS=\"%s\"",phone);
                                        s=devcmd(NULL,"\n+CMGS:","!~%s\032",body);
                                        }
                                else {
-               /* CMGF=0 */
                                        devcmd("\n> ",NULL,"\r\nAT+CMGS=%d",(strlen(pdusmsc)+strlen(pdudata))/2);
                                        s=devcmd(NULL,"\n+CMGS:","!~%s%s\032",pdusmsc,pdudata);
                                        }
@@ -1819,18 +2031,18 @@ struct hexdata *hd;
 
                                restore="\r\nAT+CSMP=17,,0,0";
                                devcmd(NULL,NULL,"\r\nAT+CSMP=81,,0,245");
-                               devcmd("\n> ",NULL,"\r\nAT+CMGS=\"%s\"",phone);
                                while ((hd=hexdata)) {
+                                       devcmd("\n> ",NULL,"\r\nAT+CMGS=\"%s\"",phone);
                                        if (!(s=devcmd(NULL,"\n+CMGS:","!~%s\032",hd->data))) break;
-                                       hexdata=hd->next;
+                                       if ((hexdata=hd->next)) pushargstack_one(s,0);
                                        free(hd);
+                                       parts++;
                                        }
                                } break;
                        case MODE_RECEIVE:
-                               devcmd(NULL,NULL,"\r\nAT+CMGF=1");
                                restore="\r\nAT+CNMI=,0";
-                               devcmd(NULL,NULL,"\r\nAT+CSDH=0");
                                devcmd(NULL,NULL,"\r\nAT+CNMI=,2");
+                               devcmd(NULL,NULL,"\r\nAT+CSDH=0");
                                unlockdevice(0);
                                /* Never bail-out when we got up to this point */
                                if (maxretryn!=-1 && verbose>=1)
@@ -1853,11 +2065,14 @@ struct hexdata *hd;
                                        d1("Reading a message for us...\n");
                                        if (!(s=devcmd("\r","@+CMT:"," ")))
                                                goto retryall;
-                                       if (!(i=receive_headerparse(s)))
+                                       if (cmgf && !(i=receive_headerparse(s)))
                                                error(_("Receive-header parsing failed on: %s"),s);
                                        if (!(s=devcmd("\r","@\n"," ")))
                                                goto retryall;
-                                       if (i) receive_accept(s);
+                                       if (cmgf) {
+                                               if (i) receive_text(s);
+                                               }
+                                       else receive_pdu(s);
                                        if (!devcmd("\n",NULL," ")) /* eat last '\n' */
                                                goto retryall;
                                        } while (datawait(1));
@@ -1867,10 +2082,13 @@ struct hexdata *hd;
                        }
                if (!s) { retrying(); goto retryall; }
 
-       while (isspace(*s)) s++;
-       if (verbose>=1) error(_("\nMessage successfuly sent with MR (Message Reference): %s"),s);
+       pushargstack_one(s,0); s=NULL;
+       if (verbose>=1) while ((s=nextargstack())) {
+               while (isspace(*s)) s++;
+               error(_("\nMessage successfuly sent with MR (Message Reference): %s"),s);
+               }
        devcmd(NULL,NULL,"\r\nAT");
 
-       logmsg(_("SMS sent (after %d retries), message reference: %s"),retrycnt,s);
+       logmsg(_("SMS sent (after %d retries), %d part(s)"),retrycnt,parts);
        return(EXIT_SUCCESS);
 }