]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/CommandInterpreter.php
add 'dm' as a synonym for 'd' command
[quix0rs-gnu-social.git] / classes / CommandInterpreter.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/classes/Command.php');
23
24 class CommandInterpreter {
25         
26         function handle_command($user, $text) {
27                 # XXX: localise
28
29                 $text = preg_replace('/\s+/', ' ', trim($text));
30                 list($cmd, $arg) = explode(' ', $text, 2);
31
32                 # We try to support all the same commands as Twitter, see
33                 # http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
34                 # There are a few compatibility commands from earlier versions of 
35                 # Laconica
36                 
37                 switch(strtolower($cmd)) {
38                  case 'help':
39                         if ($arg) {
40                                 return NULL;
41                         }
42                         return new HelpCommand($user);
43                  case 'on':
44                         if ($arg) {
45                                 list($other, $extra) = explode(' ', $arg, 2);
46                                 if ($extra) {
47                                         return NULL;
48                                 } else {
49                                         return new OnCommand($user, $other);
50                                 }
51                         } else {
52                                 return new OnCommand($user);
53                         }
54                  case 'off':
55                         if ($arg) {
56                                 list($other, $extra) = explode(' ', $arg, 2);
57                                 if ($extra) {
58                                         return NULL;
59                                 } else {
60                                         return new OffCommand($user, $other);
61                                 }
62                         } else {
63                                 return new OffCommand($user);
64                         }
65                  case 'stop':
66                  case 'quit':
67                         if ($arg) {
68                                 return NULL;
69                         } else {
70                                 return new OffCommand($user);
71                         }
72                  case 'follow':
73                  case 'sub':
74                         if (!$arg) {
75                                 return NULL;
76                         }
77                         list($other, $extra) = explode(' ', $arg, 2);
78                         if ($extra) {
79                                 return NULL;
80                         } else {
81                                 return new SubCommand($user, $other);
82                         }
83                  case 'leave':
84                  case 'unsub':
85                         if (!$arg) {
86                                 return NULL;
87                         }
88                         list($other, $extra) = explode(' ', $arg, 2);
89                         if ($extra) {
90                                 return NULL;
91                         } else {
92                                 return new UnsubCommand($user, $other);
93                         }
94                  case 'get':
95                  case 'last':
96                         if (!$arg) {
97                                 return NULL;
98                         }
99                         list($other, $extra) = explode(' ', $arg, 2);
100                         if ($extra) {
101                                 return NULL;
102                         } else {
103                                 return new GetCommand($user, $other);
104                         }
105                  case 'd':
106                  case 'dm':
107                         if (!$arg) {
108                                 return NULL;
109                         }
110                         list($other, $extra) = explode(' ', $arg, 2);
111                         if (!$extra) {
112                                 return NULL;
113                         } else {
114                                 return new MessageCommand($user, $other, $extra);
115                         }
116                  case 'whois':
117                         if (!$arg) {
118                                 return NULL;
119                         }
120                         list($other, $extra) = explode(' ', $arg, 2);
121                         if ($extra) {
122                                 return NULL;
123                         } else {
124                                 return new WhoisCommand($user, $other);
125                         }
126                  case 'fav':
127                         if (!$arg) {
128                                 return NULL;
129                         }
130                         list($other, $extra) = explode(' ', $arg, 2);
131                         if ($extra) {
132                                 return NULL;
133                         } else {
134                                 return new FavCommand($user, $other);
135                         }
136                  case 'nudge':
137                         if (!$arg) {
138                                 return NULL;
139                         }
140                         list($other, $extra) = explode(' ', $arg, 2);
141                         if ($extra) {
142                                 return NULL;
143                         } else {
144                                 return new NudgeCommand($user, $other);
145                         }
146                  case 'stats':
147                         if ($arg) {
148                                 return NULL;
149                         }
150                         return new StatsCommand($user);
151                  case 'invite':
152                         if (!$arg) {
153                                 return NULL;
154                         }
155                         list($other, $extra) = explode(' ', $arg, 2);
156                         if ($extra) {
157                                 return NULL;
158                         } else {
159                                 return new InviteCommand($user, $other);
160                         }
161                  case 'track':
162                         if (!$arg) {
163                                 return NULL;
164                         }
165                         list($word, $extra) = explode(' ', $arg, 2);
166                         if ($extra) {
167                                 return NULL;
168                         } else if ($word == 'off') {
169                                 return new TrackOffCommand($user);
170                         } else {
171                                 return new TrackCommand($user, $word);
172                         }
173                  case 'untrack':
174                         if (!$arg) {
175                                 return NULL;
176                         }
177                         list($word, $extra) = explode(' ', $arg, 2);
178                         if ($extra) {
179                                 return NULL;
180                         } else if ($word == 'all') {
181                                 return new TrackOffCommand($user);
182                         } else {
183                                 return new UntrackCommand($user, $word);
184                         }
185                  case 'tracks':
186                  case 'tracking':
187                         if ($arg) {
188                                 return NULL;
189                         }
190                         return new TrackingCommand($user);
191                  default:
192                         return false;
193                 }
194         }
195 }
196