forked from kiwanami/emacs-skype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skype.el
1930 lines (1722 loc) · 68.8 KB
/
skype.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; skype.el --- skype UI for emacs users..
;; Copyright (C) 2009, 2010 SAKURAI Masashi
;; Revision 1.3
;; Author: SAKURAI Masashi <m.sakurai@kiwanami.net>
;; Keywords: skype, chat
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Improving Skype GUI.
;; This is under development...
;;
;; To use this program, locate this file to load-path directory,
;; and add the following code to your .emacs.
;; ------------------------------
;; (require 'skype)
;; (setq skype--my-user-handle "your skype account")
;; ------------------------------
;; If you have anything.el, bind `skype--anything-command' to key,
;; like (global-set-key (kbd "M-9") 'skype--anything-command).
;;; History:
;; Revision 1.3 2010/09/15
;; Bug fixed: skype--split. (thanks to @naota344)
;;
;; Revision 1.2 2010/01/08
;; Bug fixed and refactored.
;;
;; Revision 1.1 2009/03/01
;; Automatically detect icon path.
;;
;; Revision 1.0 2009/02/21
;; First release.
;;
;;; Development memo
;; TODO
;;
;; main buffer
;; missed
;; contacts
;; menu
;; change my profile
;;
;; total incoming buffer
;; replay
;;
;; contacts buffer
;; call, chat, remove
;; change display name
;; last login time
;; show profile
;;
;; additional
;; custom user icon
;; [naming rule]
;;
;; skype--com...
;; skype--cache...
;; skype--timer...
;;
;; skype--my-status...
;; skype--emoticon...
;; skype--user...
;; skype--chat...
;; skype--chatmsg...
;;
;; skype--chat-mode...
;; skype--message-mode...
;;
;; [major-mode: chat]
;;
;; buffer-local:
;; * skype-chat-handle
;; * skype-last-updated-time
;; * skype-chatmsg-table
;; * skype-send-history
;; * skype-mode-line-chat-info
;; * skype-auto-read-state
;;
;; [major-mode: chatmsg]
;;
;; buffer-local:
;; * skype-chat-handle
;; * skype-chat-buffer
;; * skype-send-history
;; * skype-history-pos
;; * skype-writing-text
;; * skype-commit-function
;; * skype-mode-line-prompt
;;
;; [major-mode: member]
;;
;; buffer-local:
;; * skype-chat-handle
;; * skype-chat-buffer
;; * skype-member-getter-function
;; * skype-mode-line-prompt
;;
;;; Code:
(require 'dbus)
(eval-when-compile (require 'cl))
(defvar skype--my-user-handle "(your skype account name)"
"Your user account name.")
(defvar skype--libpath (file-name-directory (locate-library "skype"))
"skype.el directory. [automatically detected by locate-library]")
(defvar skype--icon-path (concat skype--libpath "/icons")
"Directory for the skype icons. [automatically detected]")
(defvar skype--emoticon-path (concat skype--libpath "/emoticons")
"Directory for the skype emoticons. [automatically detected]")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; private fundamental functions
(defun skype--string-trim (txt)
"Remove white space characters at head and tail
from the given string."
(let ((ret txt))
(setq ret (if (string-match "^\\s-*" ret)
(substring ret (match-end 0))
ret))
(or
(loop for i downfrom (1- (length ret)) downto 0 do
(if (/= 32 (char-syntax (aref ret i)))
(return (substring ret 0 (1+ i)))))
"")))
(defun skype--split (txt)
(mapcar 'skype--string-trim (split-string txt ", ")))
(defmacro skype--collect (arg &rest forms)
(declare (indent 1))
(let ((rvar (gensym "--skype--")))
`(let (,rvar)
(dolist (it ,arg)
(let ((val (progn ,@forms)))
(if val (push val ,rvar))))
(nreverse ,rvar))))
(defun skype--join (arg sep)
(mapconcat (lambda(i) i) arg sep))
(defun skype--icon (text img)
(put-text-property 0 (length text) 'display img text)
text)
(defun skype--message (text)
(unless (active-minibuffer-window)
(message text)))
;; cache control
(defun skype--cache-init-db (builder expire-sec)
"Return a cache database object. The database object has 4
methods: get, expire, each and clear. If a value indicated by
key is not found or expired, a new object is created by the
BUILDER function."
(lexical-let*
((db (make-hash-table :test 'equal)) ; key -> (object . time)
(builder builder) ; lexical bind
(expire-sec expire-sec) ; lexical bind
(dispatch
(lambda (cmd args)
(let ((cftime (float-time (current-time))))
(cond
((eq cmd 'get) ; (get key)
(let* ((key (car args))
(obj (gethash key db)))
(when (or (null obj)
(< (+ expire-sec (cdr obj)) cftime))
(setq obj (cons (funcall builder key) cftime))
(puthash key obj db))
(car obj)))
((eq cmd 'expire) ; (expire key)
(let ((key (car args)))
(remhash key db)))
((eq cmd 'clear) ; (clear)
(clrhash db))
((eq cmd 'each) ; (each lambda(key obj time) )
(lexical-let ((func (car args)))
(maphash (lambda(k v)
(funcall func k (car v) (cdr v)))
db)))
(t (error "Unknown command [%s]" cmd)))))))
dispatch))
(defsubst skype--cache-get (key db)
(funcall db 'get (list key)))
(defsubst skype--cache-expire (key db)
(funcall db 'expire (list key)))
(defsubst skype--cache-clear (db)
(funcall db 'clear nil))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skype Low-level API
;;; (without gui)
(defvar skype--com-debug-mode nil
"Debug for skype communication port.")
(defun skype--com (message)
"This function provides the primitive communicate with the Skype API.
If the API returns an error output, the function throws a skype-error signal.
If `skype--com-debug-mode' is non-nil, this function logs I/O into the debug buffer."
(let ((ret
(dbus-call-method
:session
"com.Skype.API" ; service name
"/com/Skype" ; path
"com.Skype.API" ; interface name
"Invoke" ; method name
message)))
(when skype--com-debug-mode
(let* ((buf-name "*skype-com debug*")
(buf (get-buffer buf-name)))
(save-excursion
(unless buf
(setq buf (get-buffer-create buf-name))
(buffer-disable-undo buf))
(set-buffer buf)
(goto-char (point-max))
(insert ">> " message "\n")
(insert "<< " ret "\n"))))
(if (= (or (string-match "^ERROR [0-9]+" ret) -1) 0)
(signal 'skype-error (list ret))
ret)))
(defun skype--com-get (object-name)
"Return the value that can be get like following
Skype API command:
<- GET (object-name)
-> (object-name) (value)"
(let ((ret (skype--com (concat "GET " object-name))))
(skype--string-trim (substring ret (length object-name)))))
(defun skype--com-get-object-attr (id type attr)
"Return the value that can be get like following Skype API command:\n
-> GET (type) (id) (attr)\n
<- (type) (id) (attr) (value)\n."
(skype--com-get (concat type " " id " " attr)))
;; emoticons
(defvar skype--emoticon-map (make-hash-table :test 'equal)
"[internal use] Hash-table object for emoticons [code -> emoticon object].")
(defvar skype--emoticon-list nil
"[internal use] Emoticon list for selection. (text . code)")
(defvar skype--emoticon-table
'(("smile" ":)" ":=)" ":-)")
("sadsmile" ":(" ":=(" ":-(")
("bigsmile" ":D" ":=D" ":-D" ":d" ":=d" ":-d")
("cool" "8)" "8=)" "8-)" "B)" "B=)" "B-)" "(cool)")
("wink" ":o" ":=o" ":-o" ":O" ":=O" ":-O")
("crying" ";(" ";-(" ";=(")
("sweating" "(sweat)" "(:|")
("speechless" ":|" ":=|" ":-|")
("kiss" ":*" ":=*" ":-*")
("tongueout" ":P" ":=P" ":-P" ":p" ":=p" ":-p")
("blush" "(blush)" ":$" ":-$" ":=$" ":\">")
("wondering" ":^)")
("sleepy" "|-)" "I-)" "I=)" "(snooze)")
("dull" "|(" "|-(" "|=(")
("inlove" "(inlove)")
("evilgrin" "]:)" ">:)" "(grin)")
("talking" "(talk)")
("yawn" "(yawn)" "|-()")
("puke" "(puke)" ":&" ":-&" ":=&")
("doh" "(doh)")
("angry" ":@" ":-@" ":=@" "x(" "x-(" "x=(" "X(" "X-(" "X=(")
("itwasntme" "(wasntme)")
("party" "(party)")
("worried" ":S" ":-S" ":=S" ":s" ":-s" ":=s")
("mmm" "(mm)")
("nerd" "8-|" "B-|8" "|B" "|8=|" "B=|" "(nerd)")
("lipssealed" ":x" ":-x" ":X" ":-X" ":#" ":-#" ":=x" ":=X" ":=#")
("hi" "(hi)" "(wave)")
("call" "(call)")
("devil" "(devil)")
("angel" "(angel)")
("envy" "(envy)")
("wait" "(wait)")
("bear" "(bear)" "(hug)")
("makeup" "(makeup)" "(kate)")
("giggle" "(giggle)" "(chuckle)")
("clapping" "(clap)")
("thinking" "(think)" ":?" ":-?" ":=?")
("bow" "(bow)")
("rofl" "(rofl)")
("whew" "(whew)")
("happy" "(happy)")
("smirk" "(smirk)")
("nod" "(nod)")
("shake" "(shake)")
("punch" "(punch)")
("emo" "(emo)")
("yes" "(y)" "(Y)" "(ok)")
("no" "(n)" "(N)")
("handshake" "(handshake)")
("skype" "(skype)" "(ss)")
("heart" "(h)" "(H)" "(l)" "(L)")
("brokenheart" "(u)" "(U)")
("mail" "(e)" "(m)")
("flower" "(f)" "(F)")
("rain" "(rain)" "(london)" "(st)")
("sun" "(sun)")
("time" "(o)" "(O)" "(time)")
("music" "(music)")
("movie" "(~)" "(film)" "(movie)")
("phone" "(mp)" "(ph)")
("coffee" "(coffee)")
("pizza" "(pizza)" "(pi)")
("cash" "(cash)" "(mo)" "($)")
("muscle" "(muscle)" "(flex)")
("cake" "(^)" "(cake)")
("beer" "(beer)")
("drink" "(d)" "(D)")
("dance" "(dance)" "\\o/" "\\:D/" "\\:d/")
("ninja" "(ninja)")
("star" "(*)")
("mooning" "(mooning)")
("middlefinger" "(finger)")
("bandit" "(bandit)")
("drunk" "(drunk)")
("smoke" "(smoking)" "(smoke)" "(ci)")
("toivo" "(toivo)")
("rock" "(rock)")
("headbang" "(headbang)" "(banghead)")
("bug" "(bug)")
("fubar" "(fubar)")
("poolparty" "(poolparty)")
("swear" "(swear)")
("tmi" "(tmi)"))
"Emoticon translation table. (filename text...)")
(defun skype--emoticons-init ()
(when (file-directory-p skype--emoticon-path)
(lexical-let
((icon-table (make-hash-table :test 'equal))
(emoticon-map (make-hash-table :test 'equal))
(emoticon-list nil))
(dolist (path (directory-files (expand-file-name skype--emoticon-path)))
(let ((filename (file-name-nondirectory path)))
(if (string-match "-\\([a-z]+\\)\\.png$" filename)
(puthash (match-string 1 filename)
(create-image
(concat
(file-name-as-directory skype--emoticon-path) path)
'png nil ':ascent 90)
icon-table))))
(dolist (i skype--emoticon-table)
(let* ((name (car i))
(img (gethash name icon-table))
(codes (cdr i)))
(dolist (code codes)
(puthash code img emoticon-map))
(let ((icon " "))
(put-text-property 0 1 'display img icon)
(push (cons (concat (substring name 0)
" " icon " "
(skype--join codes " "))
(car codes))
emoticon-list))))
(setq skype--emoticon-list emoticon-list)
(setq skype--emoticon-map emoticon-map))))
(defun skype--emoticons-replace (arg)
"Replace emoticon texts by corresponding icons."
(let ((buf (get-buffer-create " *skype work*"))
(text (substring arg 0)) name pos)
(buffer-disable-undo buf)
(save-excursion
(set-buffer buf)
(erase-buffer)
(insert text)
(maphash
(lambda (code img)
(goto-char (point-min))
(while (setq pos (search-forward code nil t))
(put-text-property (- pos (length code)) pos
'display img)))
skype--emoticon-map)
(buffer-string))))
;;; User status
(defvar skype--status-codes (make-hash-table :test 'equal)
"[internal use] Hash-table object for status codes [code -> status object].")
(defstruct skype-status name code icon)
(defun skype--get-my-status ()
"Return the current user status code."
(skype--com-get "USERSTATUS"))
(defun skype--set-my-status (status-code)
"Set a user status.
Valid status codes are UNKNOWN, ONLINE, OFFLINE, SKYPEME,
AWAY, NA, DND, INVISIBLE and LOGGEDOUT."
(skype--com (concat "SET USERSTATUS " status-code)))
(defun skype--status-init ()
(clrhash skype--status-codes)
(dolist (i
'(("Unknown" "UNKNOWN" "StatusPending")
("Online" "ONLINE" "StatusOnline")
("Offline" "OFFLINE" "StatusOffline")
("Skype me" "SKYPEME" "StatusSkypeMe")
("Away" "AWAY" "StatusAway")
("Not available" "NA" "StatusNotAvailable")
("Do not disturb" "DND" "StatusDoNotDisturb")
("Invisible" "INVISIBLE" "StatusInvisible")
("Logged out" "LOGGEDOUT" "StatusOffline")))
(let ((name (car i))
(code (cadr i))
(file-header (caddr i)))
(puthash code (make-skype-status
:name name :code code
:icon (append
(create-image
(format "%s/%s_16x16.png"
skype--icon-path file-header) 'png)
'(:ascent 90)))
skype--status-codes))))
(defun skype--status-get-icon (code)
(let ((obj (gethash code skype--status-codes))
(text (substring code 0)))
(when obj
(skype--icon text (skype-status-icon obj)))
text))
;;; mood text
(defun skype--set-my-mood-text (text)
"Change the your mood text."
(skype--com (concat "SET PROFILE MOOD_TEXT "
(encode-coding-string text 'utf-8-dos))))
;;; chat message type
(defvar skype--chatmsg-body-map (make-hash-table :test 'equal)
"[internal use] translation map for message type.")
(defun skype--init-chatmsg ()
(dolist
(i
'(("SETTOPIC" "" "The chat topic was changed: -> ")
("SAID" "said" "")
("ADDEDMEMBERS" "" "Added members. ")
("SAWMEMBERS" "saw members" "")
("CREATEDCHATWITH" "" "Created a new chat. ")
("LEFT" "left" "A member was left. ")
("POSTEDCONTACTS" "" "Posted contacts")
("GAP_IN_CHAT" "gap in chat" "")
("SETROLE" "" "Set role")
("KICKED" "" "Kicked")
("KICKBANNED" "" "Kick banned")
("SETOPTIONS" "" "Set options")
("SETPICTURE" "" "Set picture")
("SETGUIDELINES" "" "Set guidelines")
("JOINEDASAPPLICANT" "" "Joined as applicant")
("EMOTED" "emoted" "---------oOOo--------\n")
("UNKNOWN" "UNKNOWN" "")
))
(puthash (car i) (cdr i) skype--chatmsg-body-map)))
;;; Users
(defun skype--get-group-users (group-handle)
"Return a list of user-handles those belong to the given group."
(skype--split (skype--com-get (concat "GROUP " group-handle " USERS"))))
(defun skype--user-get-attr (user-handle attr)
"Return a property value.
The argument USER-HANDLE can be `skype-user' object."
(skype--com-get-object-attr user-handle "USER" attr))
(defstruct skype-user
handle fullname mood-text display-name status
t-name t-viewtext)
(defvar skype--user-object-cache
(skype--cache-init-db
(lambda(key) (skype--user-create-object key)) 300)
"[internal use] user object cache.")
(defun skype--user-create-object (user-handle &optional last-obj)
"Build the user object from the given user-handle."
(if last-obj
(progn
(setf (skype-user-mood-text last-obj)
(skype--user-get-attr user-handle "MOOD_TEXT"))
(setf (skype-user-status last-obj)
(skype--user-get-attr user-handle "ONLINESTATUS"))
(setf (skype-user-t-viewtext last-obj) nil)
last-obj)
(make-skype-user
:handle user-handle
:fullname (skype--user-get-attr user-handle "FULLNAME")
:mood-text (skype--user-get-attr user-handle "MOOD_TEXT")
:display-name (skype--user-get-attr user-handle "DISPLAYNAME")
:status (skype--user-get-attr user-handle "ONLINESTATUS"))))
(defsubst skype--user-handle-to-object (handle)
(if (skype-user-p handle)
handle
(skype--cache-get handle skype--user-object-cache)))
(defun skype--user-get-name (user)
"Return the appropriate user name from the given user object."
(labels ((sn (str) (if (or (null str) (= (length str) 0)) nil str)))
(let* ((obj (skype--user-handle-to-object user))
(name (skype-user-t-name obj)))
(if name name ; return
(setq name
(or (sn (skype-user-display-name obj))
(sn (skype-user-fullname obj))
(sn (skype-user-handle obj))))
(setf (skype-user-t-name obj) name)
name))))
(defun skype--user-get-viewtext (user)
"Return the user name, status icon and mood-text."
(let* ((obj (skype--user-handle-to-object user))
(viewtext (skype-user-t-viewtext obj)))
(if viewtext viewtext ; return
(let* ((name (skype--user-get-name obj))
(status-obj (gethash (skype-user-status obj) skype--status-codes))
(status-name (concat "[" (substring (skype-status-name status-obj) 0) "]"))
(mood-text (skype-user-mood-text obj)))
(setq viewtext
(concat name " "
(skype--icon status-name (skype-status-icon status-obj))
(if (> (length mood-text) 0)
(concat " < " mood-text))))
(setf (skype-user-t-viewtext obj) viewtext)
viewtext))))
(defun skype--get-all-contact-users (&optional group-type)
"Return a list of user handles those belong to the ALL_FRIENDS group."
(let ((c-group-type (or group-type "ALL_FRIENDS"))
(groups (skype--get-all-groups)))
(if (listp groups)
(lexical-let
((friend-group
(some (lambda (gid)
(and (string=
c-group-type
(skype--com-get-object-attr gid "GROUP" "TYPE"))
gid))
groups)))
(if friend-group
(skype--get-group-users friend-group))))))
;;; User actions
(defun skype--start-chat (user-handle)
"Start a skype chat and return the chat-handle.
The argument USER-HANDLE can be `skype-user' object."
(when (skype-user-p user-handle)
(setq user-handle (skype-user-handle user-handle)))
(let ((ret (skype--com (concat "CHAT CREATE " user-handle))))
(if (string-match "^CHAT \\(.+\\) STATUS DIALOG" ret)
(match-string-no-properties 1 ret))))
(defun skype--start-call (user-handle)
"Start a skype call and return the call-handle.
The argument USER-HANDLE can be `skype-user' object."
(when (skype-user-p user-handle)
(setq user-handle (skype-user-handle user-handle)))
(let ((ret (skype--com (concat "CALL " user-handle))))
(if (string-match "^CALL \\([^ ]+\\) " ret)
(match-string-no-properties 1 ret))))
;;; Groups
(defun skype--get-all-groups ()
"Return a list of all group handles."
(let ((ret (skype--com "SEARCH GROUPS ALL")))
(skype--split (substring ret 7))))
;;; Chats
(defvar skype--chat-object-cache
(skype--cache-init-db
(lambda (key) (skype--chat-create-object key))
(* 15 60))
"[internal use] chat object cache.")
(defstruct skype-chat chat-handle title time)
(defun skype--chat-handle-to-object (handle)
(if (skype-chat-p handle)
handle
(let ((obj (skype--cache-get handle skype--chat-object-cache)))
obj)))
(defun skype--chat-create-object (chat-handle)
"Build a chat object from the chat that is indicated by the given chat-handle."
(make-skype-chat
:chat-handle chat-handle
:title (skype--chat-get-attr chat-handle "FRIENDLYNAME")
:time (skype--chat-get-last-timestamp chat-handle)))
(defun skype--chat-get-last-timestamp (chat-handle)
"Return last chat message time as float seconds."
(skype--convert-from-skype-time
(skype--chat-get-attr chat-handle "ACTIVITY_TIMESTAMP")))
(defun skype--chat-get-attr (chat-handle attr)
"Return a property value."
(skype--com-get-object-attr chat-handle "CHAT" attr))
(defun skype--chat-get-buffername (chat-handle)
(let ((obj (skype--chat-handle-to-object chat-handle)))
(concat "SkypeChat:[" (skype-chat-title obj) "]")))
(defun skype--chat-get-recent-objects ()
"Return a list of recent chat objects."
(skype--chat-get-objects "RECENTCHATS"))
(defun skype--chat-get-bookmarked-objects ()
"Return a list of bookmarked chat objects."
(skype--chat-get-objects "BOOKMARKEDCHATS"))
(defun skype--chat-get-missed-objects ()
"Return a list of missed chat objects."
(skype--chat-get-objects "MISSEDCHATS"))
(defun skype--chat-get-active-objects ()
"Return a list of active chat objects."
(skype--chat-get-objects "ACTIVECHATS"))
(defun skype--chat-get-objects (search-item)
"Return a list of chat objects those are indicated by the given keyword.
This function is used by `skype--chat-get-recent-objects', `skype--chat-get-bookmarked-objects',
`skype--chat-get-missed-objects' and `skype--chat-get-active-objects'."
(let ((ret (skype--com (concat "SEARCH " search-item))))
(if (string-match "^CHATS #" ret)
(mapcar 'skype--chat-handle-to-object
(skype--split (substring ret 5)))
nil)))
;;; Chat actions
(defun skype--chat-send-message (chat-handle text)
"Send a chat message.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(skype--com
(concat "CHATMESSAGE " chat-handle " "
(encode-coding-string
(if skype--chat-send-message-function
(funcall skype--chat-send-message-function
(skype--chat-handle-to-object chat-handle) text)
text)
'utf-8-dos))))
(defvar skype--chat-send-message-function nil
"Abnormal hook for sending a message with two argument,
the skype-chat object and the sending text message. The returned
text is sent to the skype.") ; TODO test
(defun skype--chat-set-topic (chat-handle topic)
"Set a chat title.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(skype--com (concat "ALTER CHAT " chat-handle " SETTOPIC "
(encode-coding-string topic 'utf-8-dos))))
(defun skype--chat-add-member (chat-handle new-member)
"Add a member into to a chat.
The argument CHAT-HANDLE can be `skype-chat' object.
The argument NEW-MEMBER is user-handle or `skype-user' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(when (skype-user-p new-member)
(setq new-member (skype-user-handle new-member)))
(skype--com (concat "ALTER CHAT " chat-handle " ADDMEMBERS " new-member)))
(defun skype--chat-leave (chat-handle)
"Leave the chat.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(skype--com (concat "ALTER CHAT " chat-handle " LEAVE")))
(defun skype--chat-missed-p (chat-handle)
"Return non-nil, if missed-flag for the chat is true."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(some (lambda (c) (string= chat-handle (skype-chat-chat-handle c)))
(skype--chat-get-missed-objects)))
(defun skype--chat-clear-missed-flag (chat-handle)
"Set a seen flag for all recent chat messages."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(dolist (i (skype--chat-get-recent-chatmsg-handles chat-handle))
(skype--chatmsg-clear-missed-flag i)))
(defun skype--chat-bookmarked-p (chat-handle)
"Return non-nil, if the chat is bookmarked."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(string= "TRUE" (skype--chat-get-attr chat-handle "BOOKMARKED")))
(defun skype--chat-bookmark (chat-handle)
"Add a chat to the list of bookmarked chats.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(skype--com (concat "ALTER CHAT " chat-handle " BOOKMARK")))
(defun skype--chat-unbookmark (chat-handle)
"Remove a chat from the list of bookmarked chats.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(skype--com (concat "ALTER CHAT " chat-handle " UNBOOKMARK")))
(defun skype--chat-toggle-bookmark (chat-handle)
"Toggle the bookmark state of the chat.
If the chat changes to bookmarked, return t.
If unbookmarked, return nil.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(if (skype--chat-bookmarked-p chat-handle)
(progn
(skype--chat-unbookmark chat-handle)
nil)
(skype--chat-bookmark chat-handle)
t))
(defun skype--chat-get-members (chat-handle)
"Return handles of chat members who participates in the chat."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(split-string (skype--chat-get-attr chat-handle "MEMBERS") " "))
(defun skype--chat-members-get-buffername (chat-handle)
(let ((obj (skype--chat-handle-to-object chat-handle)))
(format "Skype Chat members:[%s]"
(skype-chat-title obj))))
;;; ChatMessages
;; t-state: 'update 'new 'unchange
(defstruct skype-chatmsg handle from-handle time status type body t-state)
(defun skype--chatmsg-create-object (handle last-object)
"Build a chatmsg object from the given chatmsg-handle and
register the object to the local variable `skype-chatmsg-table'."
(let*
((last-ftime (float-time skype-last-updated-time))
(edited-ftime (string-to-number (skype--chatmsg-get-attr handle "EDITED_TIMESTAMP")))
(last-status (and last-object (skype-chatmsg-status last-object)))
new-object)
(cond
(last-object ; 1 cached object
(cond
((memq last-status '(received sending)) ; 1-1 state change
(setf (skype-chatmsg-status last-object)
(skype--chatmsg-get-status handle))
(setf (skype-chatmsg-t-state last-object)
(if (eq last-status (skype-chatmsg-status last-object))
'unchange 'update)))
((< last-ftime edited-ftime) ; 1-2 edited
(setf (skype-chatmsg-body last-object)
(skype--chatmsg-get-body handle (skype-chatmsg-type last-object)))
(setf (skype-chatmsg-t-state last-object) 'update))
(t ; 1-3 unchange
(setf (skype-chatmsg-t-state last-object) 'unchange)))
(setq new-object last-object))
(t ; 2 new object
(let ((type (skype--chatmsg-get-attr handle "TYPE")))
(setq new-object
(make-skype-chatmsg
:handle handle
:from-handle (skype--chatmsg-get-attr handle "FROM_HANDLE")
:time (skype--convert-from-skype-time
(skype--chatmsg-get-attr handle "TIMESTAMP"))
:status (skype--chatmsg-get-status handle)
:type type
:body (skype--chatmsg-get-body handle type)
:t-state 'new)))
(puthash handle new-object skype-chatmsg-table)))
new-object))
(defun skype--chatmsg-handle-to-object (handle)
"Return a chatmsg object. This function uses the buffer local
hash-table `skype-chatmsg-table' and time-stamp
`skype-last-updated-time'."
(skype--chatmsg-create-object handle (skype--chatmsg-get-cache-object handle)))
(defsubst skype--chatmsg-get-cache-object (handle)
(gethash handle skype-chatmsg-table))
(defsubst skype--chatmsg-get-attr (chat-handle attr)
"Return a property value."
(skype--com-get-object-attr chat-handle "CHATMESSAGE" attr))
(defsubst skype--convert-from-skype-time (str)
"Return a float time value from a string time value
of the skype API."
(seconds-to-time (string-to-number str)))
(defun skype--strtime (time)
(if (equal (cdddr (decode-time time))
(cdddr (decode-time (current-time))))
(format-time-string "%H:%M:%S" time)
(format-time-string "%Y/%m/%d %H:%M:%S" time)))
(defun skype--chatmsg-get-body (message-handle type)
(concat
(cadr (gethash type skype--chatmsg-body-map))
(skype--chatmsg-get-attr message-handle "BODY")))
(defun skype--chatmsg-get-status (message-handle)
(let ((str (skype--chatmsg-get-attr message-handle "STATUS")))
(intern (downcase str))))
(defun skype--chat-get-chatmsgs (chat-handle)
"(Not work because skype API...) Return a list of chat message
handles those belong to the chat that is indicated by the given
chat-handle. The argument CHAT-HANDLE can be `skype-chat'
object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(let ((line (skype--chat-get-attr chat-handle "CHATMESSAGES")))
(if line (skype--split line))))
(defun skype--chat-get-recent-chatmsg-handles (chat-handle)
"Return a list of recent chat message handles those belong to
the chat that is indicated by the given chat-handle.
The argument CHAT-HANDLE can be `skype-chat' object."
(when (skype-chat-p chat-handle)
(setq chat-handle (skype-chat-chat-handle chat-handle)))
(let ((line (skype--chat-get-attr chat-handle "RECENTCHATMESSAGES")))
(if line (skype--split line))))
(defun skype--chatmsg-mine-p (message-handle)
"Return non-nil if the given message is sent by you."
(let ((user-handle
(if (skype-chatmsg-p message-handle)
(skype-chatmsg-from-handle message-handle)
(skype--chatmsg-get-attr message-handle "FROM_HANDLE"))))
(string= user-handle skype--my-user-handle)))
(defun skype--chatmsg-clear-missed-flag (message-handle)
"Set a seen flag for the chat message."
(skype--com (concat "SET CHATMESSAGE " message-handle " SEEN")))
(defun skype--chatmsg-get-buffername (chat-obj)
(format "SkypeMessage:[%s]" (skype-chat-title chat-obj)))
(defun skype--chatmsg-edit-message (chatmsg-handle text)
"Edit a chat message."
(skype--com (concat "SET CHATMESSAGE " chatmsg-handle " BODY "
(encode-coding-string text 'utf-8-dos))))
;; initialize
(defun skype--init ()
"Initialize the Skype API connection."
(interactive)
(if (not (dbus-ping :session "com.Skype.API"))
nil
(skype--com "NAME emacs23-dbus")
(skype--com "PROTOCOL 5")
(skype--status-init)
(skype--emoticons-init)
(skype--init-chatmsg)
(skype--cache-clear skype--chat-object-cache)
(skype--cache-clear skype--user-object-cache)
t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skype High-level API
;;; commands and gui
(defvar skype--default-auto-read-state nil
"If this variable is non-nil, the function
`skype--update-chat-buffer' makes the state of incoming chat
messages read state automatically." )
(defun skype--open-chat-buffer (chat-handle)
"Create and switch to the skype chat buffer.
This function create some buffer-local variables
`skype-chat-handle' and `skype-last-updated-time'."
(let* ((chat-obj
(if (skype-chat-p chat-handle) chat-handle
(skype--chat-handle-to-object chat-handle)))
(buf (get-buffer-create (skype--chat-get-buffername chat-obj))))
(save-excursion
(set-buffer buf)
(unless (eq major-mode 'skype--chat-mode)
(skype--chat-mode)
(make-local-variable 'skype-chat-handle)
(make-local-variable 'skype-last-updated-time)
(make-local-variable 'skype-chatmsg-table)
(make-local-variable 'skype-auto-read-state)
(setq skype-chat-handle (skype-chat-chat-handle chat-obj)
skype-last-updated-time nil
skype-auto-read-state skype--default-auto-read-state
skype-chatmsg-table (make-hash-table :test 'equal))
(buffer-disable-undo buf)
(skype--chat-mode-update-mode-line))
(skype--timer-start-update)
(skype--update-chat-buffer buf))
buf))
(defun skype--chat-buffer-p (&optional buffer)
(unless buffer
(setq buffer (current-buffer)))
(eq 'skype--chat-mode
(buffer-local-value 'major-mode buffer)))
(defun skype--update-chat-buffer (&optional buffer)
"Getting recent chat messages, update the current buffer.
If the current buffer does not have the local-variable
`skype-chat-handle', do nothing. Return number of updated chat
messages."
(let ((update-num 0))
(save-current-buffer
(when buffer (set-buffer buffer))
(setq buffer-read-only nil)
(unwind-protect
(when (skype--chat-buffer-p)
(let* ((chat-obj (skype--chat-handle-to-object skype-chat-handle))
(msg-handles (skype--chat-get-recent-chatmsg-handles chat-obj))
(last-ftime (and skype-last-updated-time
(float-time skype-last-updated-time)))
(cftime (current-time)) (last-user "")
(last-point (point)) (last-eobp (eobp)))
;; update chat buffer
(unless skype-last-updated-time
(erase-buffer))
(dolist (msg (mapcar 'skype--chatmsg-handle-to-object msg-handles))
(when (or (null skype-last-updated-time)
(not (eq (skype-chatmsg-t-state msg) 'unchange)))
(skype--update-chat-buffer-one
msg (string= last-user (skype-chatmsg-from-handle msg)))
(incf update-num))
(setq last-user (skype-chatmsg-from-handle msg)))
;; change chatmsg status
(when skype-auto-read-state
(maphash (lambda(key val)
(if (eq 'received (skype-chatmsg-status val))
(skype--chatmsg-clear-missed-flag key)))
skype-chatmsg-table))
;; point move
(if last-eobp (goto-char (point-max))
(goto-char last-point)
(if (< 0 update-num) ; not smart
(beginning-of-line)))
(setq skype-last-updated-time cftime)))
(setq buffer-read-only t)) ; <- finally
(skype--chat-mode-update-mode-line))
update-num))
(defun skype--update-chat-buffer-one (chatmsg-object &optional same-user)
"Search the chat message region and replace the new text."
(let ( (pos (point-min))
(hmsg (skype-chatmsg-handle chatmsg-object))
begin end )
(if (eq 'new (skype-chatmsg-t-state chatmsg-object))
;; new
(goto-char (point-max))